auth

Paddy 2014-09-07 Parent:5bf0a5fd1d01 Child:bd274615ce72

34:14599a5c7819 Go to Latest

auth/grant_test.go

Further grant testing. Use the %T format to return the name of the GrantStore that failed the test, rather than just returning the position of the store. Test that storing a grant twice yields an ErrGrantAlreadyExists. Test that retrieving a grant that doesn't exist yields an ErrGrantNotFound. Compare returned grants against expectations.

History
1 package auth
3 import (
4 "testing"
5 "time"
7 "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 grant := Grant{
39 Code: "code",
40 Created: time.Now(),
41 ExpiresIn: 180,
42 ClientID: uuid.NewID(),
43 Scope: "scope",
44 RedirectURI: "redirectURI",
45 State: "state",
46 }
47 for _, store := range grantStores {
48 err := store.SaveGrant(grant)
49 if err != nil {
50 t.Errorf("Error saving grant to %T: %s", store, err)
51 }
52 err = store.SaveGrant(grant)
53 if err != ErrGrantAlreadyExists {
54 t.Errorf("Expected ErrGrantAlreadyExists from %T, got %+v", store, err)
55 }
56 retrieved, err := store.GetGrant(grant.Code)
57 if err != nil {
58 t.Errorf("Error retrieving grant from %T: %s", store, err)
59 }
60 match, field, expectation, result := compareGrants(grant, retrieved)
61 if !match {
62 t.Errorf("Expected `%v` in the `%s` field of grant retrieved from %T, got `%v`", expectation, field, store, result)
63 }
64 err = store.DeleteGrant(grant.Code)
65 if err != nil {
66 t.Errorf("Error removing grant from %T: %s", store, err)
67 }
68 retrieved, err = store.GetGrant(grant.Code)
69 if err != ErrGrantNotFound {
70 t.Errorf("Expected ErrGrantNotFound from %T, got %+v and %+v", store, retrieved, err)
71 }
72 err = store.DeleteGrant(grant.Code)
73 if err != ErrGrantNotFound {
74 t.Errorf("Expected ErrGrantNotFound from %T, got %+v", store, err)
75 }
76 }
77 }