auth
auth/grant_test.go
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.
| paddy@29 | 1 package auth |
| paddy@29 | 2 |
| paddy@29 | 3 import ( |
| paddy@29 | 4 "testing" |
| paddy@29 | 5 "time" |
| paddy@29 | 6 |
| paddy@45 | 7 "code.secondbit.org/uuid" |
| paddy@29 | 8 ) |
| paddy@29 | 9 |
| paddy@57 | 10 var grantStores = []grantStore{NewMemstore()} |
| paddy@29 | 11 |
| paddy@34 | 12 func compareGrants(grant1, grant2 Grant) (success bool, field string, grant1val, grant2val interface{}) { |
| paddy@34 | 13 if grant1.Code != grant2.Code { |
| paddy@34 | 14 return false, "code", grant1.Code, grant2.Code |
| paddy@34 | 15 } |
| paddy@34 | 16 if !grant1.Created.Equal(grant2.Created) { |
| paddy@34 | 17 return false, "created", grant1.Created, grant2.Created |
| paddy@34 | 18 } |
| paddy@34 | 19 if grant1.ExpiresIn != grant2.ExpiresIn { |
| paddy@34 | 20 return false, "expires in", grant1.ExpiresIn, grant2.ExpiresIn |
| paddy@34 | 21 } |
| paddy@34 | 22 if !grant1.ClientID.Equal(grant2.ClientID) { |
| paddy@34 | 23 return false, "client ID", grant1.ClientID, grant2.ClientID |
| paddy@34 | 24 } |
| paddy@34 | 25 if grant1.Scope != grant2.Scope { |
| paddy@34 | 26 return false, "scope", grant1.Scope, grant2.Scope |
| paddy@34 | 27 } |
| paddy@34 | 28 if grant1.RedirectURI != grant2.RedirectURI { |
| paddy@34 | 29 return false, "redirect URI", grant1.RedirectURI, grant2.RedirectURI |
| paddy@34 | 30 } |
| paddy@34 | 31 if grant1.State != grant2.State { |
| paddy@34 | 32 return false, "state", grant1.State, grant2.State |
| paddy@34 | 33 } |
| paddy@34 | 34 return true, "", nil, nil |
| paddy@34 | 35 } |
| paddy@34 | 36 |
| paddy@29 | 37 func TestGrantStoreSuccess(t *testing.T) { |
| paddy@36 | 38 t.Parallel() |
| paddy@29 | 39 grant := Grant{ |
| paddy@29 | 40 Code: "code", |
| paddy@29 | 41 Created: time.Now(), |
| paddy@29 | 42 ExpiresIn: 180, |
| paddy@29 | 43 ClientID: uuid.NewID(), |
| paddy@29 | 44 Scope: "scope", |
| paddy@29 | 45 RedirectURI: "redirectURI", |
| paddy@29 | 46 State: "state", |
| paddy@29 | 47 } |
| paddy@34 | 48 for _, store := range grantStores { |
| paddy@57 | 49 err := store.saveGrant(grant) |
| paddy@29 | 50 if err != nil { |
| paddy@34 | 51 t.Errorf("Error saving grant to %T: %s", store, err) |
| paddy@34 | 52 } |
| paddy@57 | 53 err = store.saveGrant(grant) |
| paddy@34 | 54 if err != ErrGrantAlreadyExists { |
| paddy@34 | 55 t.Errorf("Expected ErrGrantAlreadyExists from %T, got %+v", store, err) |
| paddy@29 | 56 } |
| paddy@57 | 57 retrieved, err := store.getGrant(grant.Code) |
| paddy@29 | 58 if err != nil { |
| paddy@34 | 59 t.Errorf("Error retrieving grant from %T: %s", store, err) |
| paddy@29 | 60 } |
| paddy@34 | 61 match, field, expectation, result := compareGrants(grant, retrieved) |
| paddy@34 | 62 if !match { |
| paddy@34 | 63 t.Errorf("Expected `%v` in the `%s` field of grant retrieved from %T, got `%v`", expectation, field, store, result) |
| paddy@34 | 64 } |
| paddy@57 | 65 err = store.deleteGrant(grant.Code) |
| paddy@29 | 66 if err != nil { |
| paddy@34 | 67 t.Errorf("Error removing grant from %T: %s", store, err) |
| paddy@29 | 68 } |
| paddy@57 | 69 retrieved, err = store.getGrant(grant.Code) |
| paddy@29 | 70 if err != ErrGrantNotFound { |
| paddy@34 | 71 t.Errorf("Expected ErrGrantNotFound from %T, got %+v and %+v", store, retrieved, err) |
| paddy@34 | 72 } |
| paddy@57 | 73 err = store.deleteGrant(grant.Code) |
| paddy@34 | 74 if err != ErrGrantNotFound { |
| paddy@34 | 75 t.Errorf("Expected ErrGrantNotFound from %T, got %+v", store, err) |
| paddy@29 | 76 } |
| paddy@29 | 77 } |
| paddy@29 | 78 } |