auth
auth/token_test.go
Fix token tests. Use the %T format to return the name of the TokenStore that failed tests, instead of just returning the position. Compare the fields of the tokens retrieved against the expected fields.
| paddy@28 | 1 package auth |
| paddy@28 | 2 |
| paddy@28 | 3 import ( |
| paddy@28 | 4 "testing" |
| paddy@28 | 5 "time" |
| paddy@28 | 6 |
| paddy@28 | 7 "secondbit.org/uuid" |
| paddy@28 | 8 ) |
| paddy@28 | 9 |
| paddy@28 | 10 var tokenStores = []TokenStore{NewMemstore()} |
| paddy@28 | 11 |
| paddy@35 | 12 func compareTokens(token1, token2 Token) (success bool, field string, val1, val2 interface{}) { |
| paddy@35 | 13 if token1.AccessToken != token2.AccessToken { |
| paddy@35 | 14 return false, "access token", token1.AccessToken, token2.AccessToken |
| paddy@35 | 15 } |
| paddy@35 | 16 if token1.RefreshToken != token2.RefreshToken { |
| paddy@35 | 17 return false, "refresh token", token1.RefreshToken, token2.RefreshToken |
| paddy@35 | 18 } |
| paddy@35 | 19 if !token1.Created.Equal(token2.Created) { |
| paddy@35 | 20 return false, "created", token1.Created, token2.Created |
| paddy@35 | 21 } |
| paddy@35 | 22 if token1.ExpiresIn != token2.ExpiresIn { |
| paddy@35 | 23 return false, "expires in", token1.ExpiresIn, token2.ExpiresIn |
| paddy@35 | 24 } |
| paddy@35 | 25 if token1.TokenType != token2.TokenType { |
| paddy@35 | 26 return false, "token type", token1.TokenType, token2.TokenType |
| paddy@35 | 27 } |
| paddy@35 | 28 if token1.Scope != token2.Scope { |
| paddy@35 | 29 return false, "scope", token1.Scope, token2.Scope |
| paddy@35 | 30 } |
| paddy@35 | 31 if !token1.ProfileID.Equal(token2.ProfileID) { |
| paddy@35 | 32 return false, "profile ID", token1.ProfileID, token2.ProfileID |
| paddy@35 | 33 } |
| paddy@35 | 34 return true, "", nil, nil |
| paddy@35 | 35 } |
| paddy@35 | 36 |
| paddy@28 | 37 func TestTokenStoreSuccess(t *testing.T) { |
| paddy@28 | 38 token := Token{ |
| paddy@28 | 39 AccessToken: "access", |
| paddy@28 | 40 RefreshToken: "refresh", |
| paddy@28 | 41 Created: time.Now(), |
| paddy@28 | 42 ExpiresIn: 3600, |
| paddy@28 | 43 TokenType: "bearer", |
| paddy@28 | 44 Scope: "scope", |
| paddy@28 | 45 ProfileID: uuid.NewID(), |
| paddy@28 | 46 } |
| paddy@35 | 47 for _, store := range tokenStores { |
| paddy@28 | 48 err := store.SaveToken(token) |
| paddy@28 | 49 if err != nil { |
| paddy@35 | 50 t.Errorf("Error saving token to %T: %s", err) |
| paddy@28 | 51 } |
| paddy@28 | 52 retrievedAccess, err := store.GetToken(token.AccessToken, false) |
| paddy@28 | 53 if err != nil { |
| paddy@35 | 54 t.Errorf("Error retrieving token from %T: %s", store, err) |
| paddy@28 | 55 } |
| paddy@35 | 56 success, field, expectation, result := compareTokens(token, retrievedAccess) |
| paddy@35 | 57 if !success { |
| paddy@35 | 58 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store) |
| paddy@35 | 59 } |
| paddy@28 | 60 retrievedRefresh, err := store.GetToken(token.RefreshToken, true) |
| paddy@28 | 61 if err != nil { |
| paddy@35 | 62 t.Errorf("Error retrieving refresh token from %T: %s", store, err) |
| paddy@28 | 63 } |
| paddy@35 | 64 success, field, expectation, result = compareTokens(token, retrievedRefresh) |
| paddy@35 | 65 if !success { |
| paddy@35 | 66 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store) |
| paddy@35 | 67 } |
| paddy@28 | 68 retrievedProfile, err := store.GetTokensByProfileID(token.ProfileID, 25, 0) |
| paddy@28 | 69 if err != nil { |
| paddy@35 | 70 t.Errorf("Error retrieving token by profile from %T: %s", store, err) |
| paddy@28 | 71 } |
| paddy@28 | 72 if len(retrievedProfile) != 1 { |
| paddy@35 | 73 t.Errorf("Expected 1 token retrieved by profile ID from %T, got %+v", store, retrievedProfile) |
| paddy@28 | 74 } |
| paddy@35 | 75 success, field, expectation, result = compareTokens(token, retrievedProfile[0]) |
| paddy@35 | 76 if !success { |
| paddy@35 | 77 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store) |
| paddy@35 | 78 } |
| paddy@28 | 79 err = store.RemoveToken(token.AccessToken) |
| paddy@28 | 80 if err != nil { |
| paddy@35 | 81 t.Errorf("Error removing token from %T: %s", store, err) |
| paddy@28 | 82 } |
| paddy@28 | 83 _, err = store.GetToken(token.AccessToken, false) |
| paddy@28 | 84 if err != ErrTokenNotFound { |
| paddy@35 | 85 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err) |
| paddy@28 | 86 } |
| paddy@28 | 87 _, err = store.GetToken(token.RefreshToken, true) |
| paddy@28 | 88 if err != ErrTokenNotFound { |
| paddy@35 | 89 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err) |
| paddy@28 | 90 } |
| paddy@28 | 91 retrievedProfile, err = store.GetTokensByProfileID(token.ProfileID, 25, 0) |
| paddy@28 | 92 if err != nil { |
| paddy@35 | 93 t.Errorf("Error retrieving token by profile from %T: %s", store, err) |
| paddy@28 | 94 } |
| paddy@28 | 95 if len(retrievedProfile) != 0 { |
| paddy@35 | 96 t.Errorf("Expected list of 0 tokens from %T, got %+v", store, retrievedProfile) |
| paddy@28 | 97 } |
| paddy@28 | 98 } |
| paddy@28 | 99 } |