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 grantStores = []grantStore{NewMemstore()}
12 func compareGrants(grant1, grant2 Grant) (success bool, field string, grant1val, grant2val interface{}) {
13 if grant1.Code != grant2.Code {
14 return false, "code", grant1.Code, grant2.Code
16 if !grant1.Created.Equal(grant2.Created) {
17 return false, "created", grant1.Created, grant2.Created
19 if grant1.ExpiresIn != grant2.ExpiresIn {
20 return false, "expires in", grant1.ExpiresIn, grant2.ExpiresIn
22 if !grant1.ClientID.Equal(grant2.ClientID) {
23 return false, "client ID", grant1.ClientID, grant2.ClientID
25 if grant1.Scope != grant2.Scope {
26 return false, "scope", grant1.Scope, grant2.Scope
28 if grant1.RedirectURI != grant2.RedirectURI {
29 return false, "redirect URI", grant1.RedirectURI, grant2.RedirectURI
31 if grant1.State != grant2.State {
32 return false, "state", grant1.State, grant2.State
34 return true, "", nil, nil
37 func TestGrantStoreSuccess(t *testing.T) {
43 ClientID: uuid.NewID(),
45 RedirectURI: "redirectURI",
48 for _, store := range grantStores {
49 err := store.saveGrant(grant)
51 t.Errorf("Error saving grant to %T: %s", store, err)
53 err = store.saveGrant(grant)
54 if err != ErrGrantAlreadyExists {
55 t.Errorf("Expected ErrGrantAlreadyExists from %T, got %+v", store, err)
57 retrieved, err := store.getGrant(grant.Code)
59 t.Errorf("Error retrieving grant from %T: %s", store, err)
61 match, field, expectation, result := compareGrants(grant, retrieved)
63 t.Errorf("Expected `%v` in the `%s` field of grant retrieved from %T, got `%v`", expectation, field, store, result)
65 err = store.deleteGrant(grant.Code)
67 t.Errorf("Error removing grant from %T: %s", store, err)
69 retrieved, err = store.getGrant(grant.Code)
70 if err != ErrGrantNotFound {
71 t.Errorf("Expected ErrGrantNotFound from %T, got %+v and %+v", store, retrieved, err)
73 err = store.deleteGrant(grant.Code)
74 if err != ErrGrantNotFound {
75 t.Errorf("Expected ErrGrantNotFound from %T, got %+v", store, err)