|
|
|
|
@ -2,6 +2,7 @@
|
|
|
|
|
package conformance |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"context" |
|
|
|
|
"reflect" |
|
|
|
|
"sort" |
|
|
|
|
"testing" |
|
|
|
|
@ -80,6 +81,7 @@ func mustBeErrAlreadyExists(t *testing.T, kind string, err error) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testAuthRequestCRUD(t *testing.T, s storage.Storage) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
codeChallenge := storage.PKCE{ |
|
|
|
|
CodeChallenge: "code_challenge_test", |
|
|
|
|
CodeChallengeMethod: "plain", |
|
|
|
|
@ -111,12 +113,12 @@ func testAuthRequestCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
|
|
|
|
|
identity := storage.Claims{Email: "foobar"} |
|
|
|
|
|
|
|
|
|
if err := s.CreateAuthRequest(a1); err != nil { |
|
|
|
|
if err := s.CreateAuthRequest(ctx, a1); err != nil { |
|
|
|
|
t.Fatalf("failed creating auth request: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Attempt to create same AuthRequest twice.
|
|
|
|
|
err := s.CreateAuthRequest(a1) |
|
|
|
|
err := s.CreateAuthRequest(ctx, a1) |
|
|
|
|
mustBeErrAlreadyExists(t, "auth request", err) |
|
|
|
|
|
|
|
|
|
a2 := storage.AuthRequest{ |
|
|
|
|
@ -142,7 +144,7 @@ func testAuthRequestCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
HMACKey: []byte("hmac_key"), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := s.CreateAuthRequest(a2); err != nil { |
|
|
|
|
if err := s.CreateAuthRequest(ctx, a2); err != nil { |
|
|
|
|
t.Fatalf("failed creating auth request: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -179,6 +181,7 @@ func testAuthRequestCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testAuthCodeCRUD(t *testing.T, s storage.Storage) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
a1 := storage.AuthCode{ |
|
|
|
|
ID: storage.NewID(), |
|
|
|
|
ClientID: "client1", |
|
|
|
|
@ -201,7 +204,7 @@ func testAuthCodeCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := s.CreateAuthCode(a1); err != nil { |
|
|
|
|
if err := s.CreateAuthCode(ctx, a1); err != nil { |
|
|
|
|
t.Fatalf("failed creating auth code: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -224,10 +227,10 @@ func testAuthCodeCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Attempt to create same AuthCode twice.
|
|
|
|
|
err := s.CreateAuthCode(a1) |
|
|
|
|
err := s.CreateAuthCode(ctx, a1) |
|
|
|
|
mustBeErrAlreadyExists(t, "auth code", err) |
|
|
|
|
|
|
|
|
|
if err := s.CreateAuthCode(a2); err != nil { |
|
|
|
|
if err := s.CreateAuthCode(ctx, a2); err != nil { |
|
|
|
|
t.Fatalf("failed creating auth code: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -256,6 +259,7 @@ func testAuthCodeCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testClientCRUD(t *testing.T, s storage.Storage) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
id1 := storage.NewID() |
|
|
|
|
c1 := storage.Client{ |
|
|
|
|
ID: id1, |
|
|
|
|
@ -267,12 +271,12 @@ func testClientCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
err := s.DeleteClient(id1) |
|
|
|
|
mustBeErrNotFound(t, "client", err) |
|
|
|
|
|
|
|
|
|
if err := s.CreateClient(c1); err != nil { |
|
|
|
|
if err := s.CreateClient(ctx, c1); err != nil { |
|
|
|
|
t.Fatalf("create client: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Attempt to create same Client twice.
|
|
|
|
|
err = s.CreateClient(c1) |
|
|
|
|
err = s.CreateClient(ctx, c1) |
|
|
|
|
mustBeErrAlreadyExists(t, "client", err) |
|
|
|
|
|
|
|
|
|
id2 := storage.NewID() |
|
|
|
|
@ -284,7 +288,7 @@ func testClientCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
LogoURL: "https://goo.gl/JIyzIC", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := s.CreateClient(c2); err != nil { |
|
|
|
|
if err := s.CreateClient(ctx, c2); err != nil { |
|
|
|
|
t.Fatalf("create client: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -325,6 +329,7 @@ func testClientCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testRefreshTokenCRUD(t *testing.T, s storage.Storage) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
id := storage.NewID() |
|
|
|
|
refresh := storage.RefreshToken{ |
|
|
|
|
ID: id, |
|
|
|
|
@ -345,12 +350,12 @@ func testRefreshTokenCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
}, |
|
|
|
|
ConnectorData: []byte(`{"some":"data"}`), |
|
|
|
|
} |
|
|
|
|
if err := s.CreateRefresh(refresh); err != nil { |
|
|
|
|
if err := s.CreateRefresh(ctx, refresh); err != nil { |
|
|
|
|
t.Fatalf("create refresh token: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Attempt to create same Refresh Token twice.
|
|
|
|
|
err := s.CreateRefresh(refresh) |
|
|
|
|
err := s.CreateRefresh(ctx, refresh) |
|
|
|
|
mustBeErrAlreadyExists(t, "refresh token", err) |
|
|
|
|
|
|
|
|
|
getAndCompare := func(id string, want storage.RefreshToken) { |
|
|
|
|
@ -401,7 +406,7 @@ func testRefreshTokenCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
ConnectorData: []byte(`{"some":"data"}`), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := s.CreateRefresh(refresh2); err != nil { |
|
|
|
|
if err := s.CreateRefresh(ctx, refresh2); err != nil { |
|
|
|
|
t.Fatalf("create second refresh token: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -443,6 +448,7 @@ func (n byEmail) Less(i, j int) bool { return n[i].Email < n[j].Email }
|
|
|
|
|
func (n byEmail) Swap(i, j int) { n[i], n[j] = n[j], n[i] } |
|
|
|
|
|
|
|
|
|
func testPasswordCRUD(t *testing.T, s storage.Storage) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
// Use bcrypt.MinCost to keep the tests short.
|
|
|
|
|
passwordHash1, err := bcrypt.GenerateFromPassword([]byte("secret"), bcrypt.MinCost) |
|
|
|
|
if err != nil { |
|
|
|
|
@ -455,12 +461,12 @@ func testPasswordCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
Username: "jane", |
|
|
|
|
UserID: "foobar", |
|
|
|
|
} |
|
|
|
|
if err := s.CreatePassword(password1); err != nil { |
|
|
|
|
if err := s.CreatePassword(ctx, password1); err != nil { |
|
|
|
|
t.Fatalf("create password token: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Attempt to create same Password twice.
|
|
|
|
|
err = s.CreatePassword(password1) |
|
|
|
|
err = s.CreatePassword(ctx, password1) |
|
|
|
|
mustBeErrAlreadyExists(t, "password", err) |
|
|
|
|
|
|
|
|
|
passwordHash2, err := bcrypt.GenerateFromPassword([]byte("password"), bcrypt.MinCost) |
|
|
|
|
@ -474,7 +480,7 @@ func testPasswordCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
Username: "john", |
|
|
|
|
UserID: "barfoo", |
|
|
|
|
} |
|
|
|
|
if err := s.CreatePassword(password2); err != nil { |
|
|
|
|
if err := s.CreatePassword(ctx, password2); err != nil { |
|
|
|
|
t.Fatalf("create password token: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -533,6 +539,7 @@ func testPasswordCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testOfflineSessionCRUD(t *testing.T, s storage.Storage) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
userID1 := storage.NewID() |
|
|
|
|
session1 := storage.OfflineSessions{ |
|
|
|
|
UserID: userID1, |
|
|
|
|
@ -543,12 +550,12 @@ func testOfflineSessionCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
|
|
|
|
|
// Creating an OfflineSession with an empty Refresh list to ensure that
|
|
|
|
|
// an empty map is translated as expected by the storage.
|
|
|
|
|
if err := s.CreateOfflineSessions(session1); err != nil { |
|
|
|
|
if err := s.CreateOfflineSessions(ctx, session1); err != nil { |
|
|
|
|
t.Fatalf("create offline session with UserID = %s: %v", session1.UserID, err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Attempt to create same OfflineSession twice.
|
|
|
|
|
err := s.CreateOfflineSessions(session1) |
|
|
|
|
err := s.CreateOfflineSessions(ctx, session1) |
|
|
|
|
mustBeErrAlreadyExists(t, "offline session", err) |
|
|
|
|
|
|
|
|
|
userID2 := storage.NewID() |
|
|
|
|
@ -559,7 +566,7 @@ func testOfflineSessionCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
ConnectorData: []byte(`{"some":"data"}`), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := s.CreateOfflineSessions(session2); err != nil { |
|
|
|
|
if err := s.CreateOfflineSessions(ctx, session2); err != nil { |
|
|
|
|
t.Fatalf("create offline session with UserID = %s: %v", session2.UserID, err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -607,6 +614,7 @@ func testOfflineSessionCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testConnectorCRUD(t *testing.T, s storage.Storage) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
id1 := storage.NewID() |
|
|
|
|
config1 := []byte(`{"issuer": "https://accounts.google.com"}`) |
|
|
|
|
c1 := storage.Connector{ |
|
|
|
|
@ -616,12 +624,12 @@ func testConnectorCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
Config: config1, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := s.CreateConnector(c1); err != nil { |
|
|
|
|
if err := s.CreateConnector(ctx, c1); err != nil { |
|
|
|
|
t.Fatalf("create connector with ID = %s: %v", c1.ID, err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Attempt to create same Connector twice.
|
|
|
|
|
err := s.CreateConnector(c1) |
|
|
|
|
err := s.CreateConnector(ctx, c1) |
|
|
|
|
mustBeErrAlreadyExists(t, "connector", err) |
|
|
|
|
|
|
|
|
|
id2 := storage.NewID() |
|
|
|
|
@ -633,7 +641,7 @@ func testConnectorCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
Config: config2, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := s.CreateConnector(c2); err != nil { |
|
|
|
|
if err := s.CreateConnector(ctx, c2); err != nil { |
|
|
|
|
t.Fatalf("create connector with ID = %s: %v", c2.ID, err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -744,6 +752,7 @@ func testKeysCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testGC(t *testing.T, s storage.Storage) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
est, err := time.LoadLocation("America/New_York") |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatal(err) |
|
|
|
|
@ -772,7 +781,7 @@ func testGC(t *testing.T, s storage.Storage) {
|
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := s.CreateAuthCode(c); err != nil { |
|
|
|
|
if err := s.CreateAuthCode(ctx, c); err != nil { |
|
|
|
|
t.Fatalf("failed creating auth code: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -823,7 +832,7 @@ func testGC(t *testing.T, s storage.Storage) {
|
|
|
|
|
HMACKey: []byte("hmac_key"), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := s.CreateAuthRequest(a); err != nil { |
|
|
|
|
if err := s.CreateAuthRequest(ctx, a); err != nil { |
|
|
|
|
t.Fatalf("failed creating auth request: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -860,7 +869,7 @@ func testGC(t *testing.T, s storage.Storage) {
|
|
|
|
|
Expiry: expiry, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := s.CreateDeviceRequest(d); err != nil { |
|
|
|
|
if err := s.CreateDeviceRequest(ctx, d); err != nil { |
|
|
|
|
t.Fatalf("failed creating device request: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -900,7 +909,7 @@ func testGC(t *testing.T, s storage.Storage) {
|
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := s.CreateDeviceToken(dt); err != nil { |
|
|
|
|
if err := s.CreateDeviceToken(ctx, dt); err != nil { |
|
|
|
|
t.Fatalf("failed creating device token: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -931,6 +940,7 @@ func testGC(t *testing.T, s storage.Storage) {
|
|
|
|
|
// testTimezones tests that backends either fully support timezones or
|
|
|
|
|
// do the correct standardization.
|
|
|
|
|
func testTimezones(t *testing.T, s storage.Storage) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
est, err := time.LoadLocation("America/New_York") |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatal(err) |
|
|
|
|
@ -956,7 +966,7 @@ func testTimezones(t *testing.T, s storage.Storage) {
|
|
|
|
|
Groups: []string{"a", "b"}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
if err := s.CreateAuthCode(c); err != nil { |
|
|
|
|
if err := s.CreateAuthCode(ctx, c); err != nil { |
|
|
|
|
t.Fatalf("failed creating auth code: %v", err) |
|
|
|
|
} |
|
|
|
|
got, err := s.GetAuthCode(c.ID) |
|
|
|
|
@ -975,6 +985,7 @@ func testTimezones(t *testing.T, s storage.Storage) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testDeviceRequestCRUD(t *testing.T, s storage.Storage) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
d1 := storage.DeviceRequest{ |
|
|
|
|
UserCode: storage.NewUserCode(), |
|
|
|
|
DeviceCode: storage.NewID(), |
|
|
|
|
@ -984,12 +995,12 @@ func testDeviceRequestCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
Expiry: neverExpire.Round(time.Second), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := s.CreateDeviceRequest(d1); err != nil { |
|
|
|
|
if err := s.CreateDeviceRequest(ctx, d1); err != nil { |
|
|
|
|
t.Fatalf("failed creating device request: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Attempt to create same DeviceRequest twice.
|
|
|
|
|
err := s.CreateDeviceRequest(d1) |
|
|
|
|
err := s.CreateDeviceRequest(ctx, d1) |
|
|
|
|
mustBeErrAlreadyExists(t, "device request", err) |
|
|
|
|
|
|
|
|
|
got, err := s.GetDeviceRequest(d1.UserCode) |
|
|
|
|
@ -1004,6 +1015,7 @@ func testDeviceRequestCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testDeviceTokenCRUD(t *testing.T, s storage.Storage) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
codeChallenge := storage.PKCE{ |
|
|
|
|
CodeChallenge: "code_challenge_test", |
|
|
|
|
CodeChallengeMethod: "plain", |
|
|
|
|
@ -1020,12 +1032,12 @@ func testDeviceTokenCRUD(t *testing.T, s storage.Storage) {
|
|
|
|
|
PKCE: codeChallenge, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := s.CreateDeviceToken(d1); err != nil { |
|
|
|
|
if err := s.CreateDeviceToken(ctx, d1); err != nil { |
|
|
|
|
t.Fatalf("failed creating device token: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Attempt to create same Device Token twice.
|
|
|
|
|
err := s.CreateDeviceToken(d1) |
|
|
|
|
err := s.CreateDeviceToken(ctx, d1) |
|
|
|
|
mustBeErrAlreadyExists(t, "device token", err) |
|
|
|
|
|
|
|
|
|
// Update the device token, simulate a redemption
|
|
|
|
|
|