Test our GetClientHandler function, add isAuthError helper.
Add a helper that identifies whether the error passed to it is an authentication
error or is some other type of error. This is useful fo checking whether or not
an internal error occurred while authenticating users.
Update all instances where we call our authentication helper to make them use
the new error helper. All tests continue to pass.
Add a new test case for retrieving a client as an unauthenticated user. This
clears the client's secret from the response before sending it.
Update the GetClientHandler function to return the secret when the owner of the
client used Basic Auth in the request.
Add a new test case for retrieving a client as an authenticated user, both the
owner and a non-owner user. This makes sure the secret is divulged only in the
appropriate cases.
5 var scopeStores = []scopeStore{NewMemstore()}
7 func compareScopes(scope1, scope2 Scope) (success bool, field string, val1, val2 interface{}) {
8 if scope1.ID != scope2.ID {
9 return false, "ID", scope1.ID, scope2.ID
11 if scope1.Name != scope2.Name {
12 return false, "Name", scope1.Name, scope2.Name
14 if scope1.Description != scope2.Description {
15 return false, "Description", scope1.Description, scope2.Description
17 return true, "", nil, nil
20 func TestScopeInScopeStore(t *testing.T) {
24 Description: "Access to testing data.",
29 Description: "Access to minions.",
34 Description: "Access to bananas.",
36 updatedName := "Updated Scope"
37 updatedDescription := "An updated scope."
38 update := ScopeChange{
42 update2 := ScopeChange{
44 Description: &updatedDescription,
46 update3 := ScopeChange{
49 Description: &updatedDescription,
51 for _, store := range scopeStores {
52 context := Context{scopes: store}
53 retrieved, err := context.GetScopes([]string{scope.ID})
54 if len(retrieved) != 0 {
55 t.Logf("%+v", retrieved)
56 t.Errorf("Expected %d results, got %d from %T", 0, len(retrieved), store)
58 if e, ok := err.(ErrScopeNotFound); !ok {
59 t.Errorf("Expected ErrScopeNotFound, got %+v instead for %T", err, store)
62 t.Errorf("Expected the error to be in position %d, got position %d from %T", 0, e.Pos, store)
65 t.Errorf("Expected the error to be with scope %s, got %s from %T", scope.ID, e.ID, store)
68 err = context.CreateScopes([]Scope{scope})
70 t.Errorf("Error saving scope to %T: %s", store, err)
72 err = context.CreateScopes([]Scope{scope})
73 if e, ok := err.(ErrScopeAlreadyExists); !ok {
74 t.Errorf("Expected ErrScopeAlreadyExists, got %s instead for %T", err, store)
77 t.Errorf("Expected the error to be in position %d, got position %d from %T", 0, e.Pos, store)
80 t.Errorf("Expected the error to be for ID %s, got %s from %T", scope.ID, e.ID, store)
83 retrieved, err = context.GetScopes([]string{scope.ID})
85 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
87 if len(retrieved) != 1 {
88 t.Logf("%+v", retrieved)
89 t.Errorf("Expected %d results, got %d from %T", 1, len(retrieved), store)
91 success, field, val1, val2 := compareScopes(scope, retrieved[0])
93 t.Errorf("Expected %s to be %+v, got %+v from %T", field, val1, val2, store)
95 err = context.CreateScopes([]Scope{scope2, scope3})
97 t.Errorf("Unexpected error trying to create scope2 and scope3 in %T: %+v", store, err)
99 retrieved, err = context.GetScopes([]string{scope.ID, scope2.ID, scope3.ID})
101 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
103 if len(retrieved) != 3 {
104 t.Logf("%+v", retrieved)
105 t.Errorf("Expected %d results, got %d from %T", 3, len(retrieved), store)
107 for pos, s := range []Scope{scope, scope2, scope3} {
108 success, field, val1, val2 = compareScopes(s, retrieved[pos])
110 t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store)
113 updated, err := context.UpdateScopes([]ScopeChange{update, update2, update3})
115 t.Errorf("Unexpected error updating scopes in %T: %+v", store, err)
117 if len(updated) != 3 {
118 t.Logf("%+v", updated)
119 t.Errorf("Expected %d results, got %d from %T", 3, len(updated), store)
121 scope.ApplyChange(update)
122 scope2.ApplyChange(update2)
123 scope3.ApplyChange(update3)
124 for pos, s := range []Scope{scope, scope2, scope3} {
125 success, field, val1, val2 = compareScopes(s, updated[pos])
127 t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store)
130 retrieved, err = context.ListScopes()
132 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
134 if len(retrieved) != 3 {
135 t.Logf("%+v", retrieved)
136 t.Errorf("Expected %d results, got %d from %T", 3, len(retrieved), store)
138 for pos, s := range []Scope{scope, scope2, scope3} {
139 success, field, val1, val2 = compareScopes(s, retrieved[pos])
141 t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store)
144 err = context.RemoveScopes([]string{scope.ID, scope2.ID, scope3.ID})
146 t.Errorf("Unexpected error removing scopes from %T: %+v", store, err)
148 retrieved, err = context.ListScopes()
150 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
152 if len(retrieved) != 0 {
153 t.Logf("%+v", retrieved)
154 t.Errorf("Expected %d results, got %d from %T", 0, len(retrieved), store)
156 err = context.RemoveScopes([]string{scope.ID})
158 t.Errorf("No error returned removing non-existent scopes from %T", store)
160 if e, ok := err.(ErrScopeNotFound); !ok {
161 t.Errorf("Unexpected error removing non-existent scopes from %T: %+v", store, err)
164 t.Errorf("Expected error to be for scope ID in pos %d, but got %d from %T", 0, e.Pos, store)
166 if e.ID != scope.ID {
167 t.Errorf("Expected error to be for scope ID %s, but got %s from %T", scope.ID, e.ID, store)
170 updated, err = context.UpdateScopes([]ScopeChange{update})
172 t.Errorf("No error returned updating non-existent scopes from %T", store)
174 if e, ok := err.(ErrScopeNotFound); !ok {
175 t.Errorf("Unexpected error updating non-existent scopes from %T: %+v", store, err)
178 t.Errorf("Expected error to be for scope ID in pos %d, but got %d from %T", 0, e.Pos, store)
180 if e.ID != scope.ID {
181 t.Errorf("Expected error to be for scope ID %s, but got %s from %T", scope.ID, e.ID, store)
187 func TestScopeErrors(t *testing.T) {
188 errors := map[string]error{
189 "scope test couldn't be found": ErrScopeNotFound{ID: "test", Pos: 0},
190 "scope test already exists": ErrScopeAlreadyExists{ID: "test", Pos: 0},
192 for expectation, e := range errors {
193 if e.Error() != expectation {
194 t.Errorf("Expected %+v to produce '%s', produced '%s'", e, expectation, e.Error())