auth
auth/grant_test.go
Update Context with new CheckEndpoint and CountEndpoints. Update the Context.CheckEndpoint method to pass through the new "strict" parameter that controls how URLs are checked for validation. Add a Context.CountEndpoints method to safely access the number of endpoints in stored for the client in the associent ClientStore. Finally, remove the error return from Context.Render, as we'd only ever want to log that, anyways. So we may as well just log it from the central function.
1 package auth
3 import (
4 "testing"
5 "time"
7 "code.secondbit.org/uuid"
8 )
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
15 }
16 if !grant1.Created.Equal(grant2.Created) {
17 return false, "created", grant1.Created, grant2.Created
18 }
19 if grant1.ExpiresIn != grant2.ExpiresIn {
20 return false, "expires in", grant1.ExpiresIn, grant2.ExpiresIn
21 }
22 if !grant1.ClientID.Equal(grant2.ClientID) {
23 return false, "client ID", grant1.ClientID, grant2.ClientID
24 }
25 if grant1.Scope != grant2.Scope {
26 return false, "scope", grant1.Scope, grant2.Scope
27 }
28 if grant1.RedirectURI != grant2.RedirectURI {
29 return false, "redirect URI", grant1.RedirectURI, grant2.RedirectURI
30 }
31 if grant1.State != grant2.State {
32 return false, "state", grant1.State, grant2.State
33 }
34 return true, "", nil, nil
35 }
37 func TestGrantStoreSuccess(t *testing.T) {
38 t.Parallel()
39 grant := Grant{
40 Code: "code",
41 Created: time.Now(),
42 ExpiresIn: 180,
43 ClientID: uuid.NewID(),
44 Scope: "scope",
45 RedirectURI: "redirectURI",
46 State: "state",
47 }
48 for _, store := range grantStores {
49 err := store.SaveGrant(grant)
50 if err != nil {
51 t.Errorf("Error saving grant to %T: %s", store, err)
52 }
53 err = store.SaveGrant(grant)
54 if err != ErrGrantAlreadyExists {
55 t.Errorf("Expected ErrGrantAlreadyExists from %T, got %+v", store, err)
56 }
57 retrieved, err := store.GetGrant(grant.Code)
58 if err != nil {
59 t.Errorf("Error retrieving grant from %T: %s", store, err)
60 }
61 match, field, expectation, result := compareGrants(grant, retrieved)
62 if !match {
63 t.Errorf("Expected `%v` in the `%s` field of grant retrieved from %T, got `%v`", expectation, field, store, result)
64 }
65 err = store.DeleteGrant(grant.Code)
66 if err != nil {
67 t.Errorf("Error removing grant from %T: %s", store, err)
68 }
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)
72 }
73 err = store.DeleteGrant(grant.Code)
74 if err != ErrGrantNotFound {
75 t.Errorf("Expected ErrGrantNotFound from %T, got %+v", store, err)
76 }
77 }
78 }