Bugfixes and tests for getting grants.
Add tests for the grant confirmation part of the request to get a grant code.
When the request is denied, the redirect should have an access_denied error.
When the request is approved, the redirect should contain a code.
Fix numerous bugs in which the redirect URL didn't contain the parameters we
thought it would. Basically, anything that still used req.URL.Query().Set()
instead of copying the query, modifying it, and setting req.URL.RawQuery.
Fix a bug in which the redirectURL wasn't being properly set. Basically, when we
moved the redirectURI processing to the top of the file (c29c7df35905 for those
who forgot), we didn't update the reference to it lower in the file, where
redirectURI was being updated and we expected that to be reflected in the
processing.
The HTTP handler for getting grant codes is now completely tested except for
returning internal errors, which requires a new test harness be built to provoke
internal errors on demand. At this point, however, I'd like to continue on
implementing endpoints.
7 "code.secondbit.org/uuid"
10 var tokenStores = []tokenStore{NewMemstore()}
12 func compareTokens(token1, token2 Token) (success bool, field string, val1, val2 interface{}) {
13 if token1.AccessToken != token2.AccessToken {
14 return false, "access token", token1.AccessToken, token2.AccessToken
16 if token1.RefreshToken != token2.RefreshToken {
17 return false, "refresh token", token1.RefreshToken, token2.RefreshToken
19 if !token1.Created.Equal(token2.Created) {
20 return false, "created", token1.Created, token2.Created
22 if token1.ExpiresIn != token2.ExpiresIn {
23 return false, "expires in", token1.ExpiresIn, token2.ExpiresIn
25 if token1.TokenType != token2.TokenType {
26 return false, "token type", token1.TokenType, token2.TokenType
28 if token1.Scope != token2.Scope {
29 return false, "scope", token1.Scope, token2.Scope
31 if !token1.ProfileID.Equal(token2.ProfileID) {
32 return false, "profile ID", token1.ProfileID, token2.ProfileID
34 return true, "", nil, nil
37 func TestTokenStoreSuccess(t *testing.T) {
40 AccessToken: "access",
41 RefreshToken: "refresh",
46 ProfileID: uuid.NewID(),
48 for _, store := range tokenStores {
49 err := store.saveToken(token)
51 t.Errorf("Error saving token to %T: %s", store, err)
53 err = store.saveToken(token)
54 if err != ErrTokenAlreadyExists {
55 t.Errorf("Expected ErrTokenAlreadyExists from %T, got %s", store, err)
57 retrievedAccess, err := store.getToken(token.AccessToken, false)
59 t.Errorf("Error retrieving token from %T: %s", store, err)
61 success, field, expectation, result := compareTokens(token, retrievedAccess)
63 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
65 retrievedRefresh, err := store.getToken(token.RefreshToken, true)
67 t.Errorf("Error retrieving refresh token from %T: %s", store, err)
69 success, field, expectation, result = compareTokens(token, retrievedRefresh)
71 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
73 retrievedProfile, err := store.getTokensByProfileID(token.ProfileID, 25, 0)
75 t.Errorf("Error retrieving token by profile from %T: %s", store, err)
77 if len(retrievedProfile) != 1 {
78 t.Errorf("Expected 1 token retrieved by profile ID from %T, got %+v", store, retrievedProfile)
80 success, field, expectation, result = compareTokens(token, retrievedProfile[0])
82 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
84 err = store.removeToken(token.AccessToken)
86 t.Errorf("Error removing token from %T: %s", store, err)
88 _, err = store.getToken(token.AccessToken, false)
89 if err != ErrTokenNotFound {
90 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err)
92 _, err = store.getToken(token.RefreshToken, true)
93 if err != ErrTokenNotFound {
94 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err)
96 retrievedProfile, err = store.getTokensByProfileID(token.ProfileID, 25, 0)
98 t.Errorf("Error retrieving token by profile from %T: %s", store, err)
100 if len(retrievedProfile) != 0 {
101 t.Errorf("Expected list of 0 tokens from %T, got %+v", store, retrievedProfile)
103 err = store.removeToken(token.AccessToken)
104 if err != ErrTokenNotFound {
105 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err)