auth

Paddy 2015-03-22 Parent:e090a69e711f Child:de5e09680f6b

151:77db7c65216c Go to Latest

auth/scope_test.go

Implement postgres clientStore. Stop requiring the client ID be passed to clientStore.addEndpoints and context.AddEndpoints. The Endpoints themselves contain the client ID. When using the authd server, set the log flags to include the file path and line number. Add an ErrEndpointAlreadyExists error, to return when creating an endpoint and its ID already exists in the database. Add a Deleted property to Clients and remove the clientStore.deleteClient and context.DeleteClient methods. We're not going to actually remove that data, and we want to be able to restore it, so include it in the ClientChange type and call it using UpdateClient. Create a ClientChange.Empty helper method that will return whether the ClientChange has any changes to perform. Return ErrClientNotFound from clientStore.getClient if the Client's Deleted property is set to true. This also requires us to ignore ErrClientNotFound errors when calling memstore.listClientsByOwner, as they should just be skipped instead of returning an error. Add the postgres type methods needed to implement clientStore. Include postgres as a clientStore if the testing.Short() flag is not set. Generate a new ID for the Client on every run in the tests, now that we can't actually remove it from the database/memstore in code. We really just need a *Store.Reset() function that erases all the data and starts over again, to give the tests a clean execution environment (and so they can clean up after themselves). Add the CREATE TABLE statements for the Clients table and the Endpoints table to sql/postgres_init.sql.

History
1 package auth
3 import "testing"
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
10 }
11 if scope1.Name != scope2.Name {
12 return false, "Name", scope1.Name, scope2.Name
13 }
14 if scope1.Description != scope2.Description {
15 return false, "Description", scope1.Description, scope2.Description
16 }
17 return true, "", nil, nil
18 }
20 func TestScopeInScopeStore(t *testing.T) {
21 scope := Scope{
22 ID: "testscope",
23 Name: "Test Scope",
24 Description: "Access to testing data.",
25 }
26 scope2 := Scope{
27 ID: "testscope2",
28 Name: "Test Scope 2",
29 Description: "Access to minions.",
30 }
31 scope3 := Scope{
32 ID: "testscope3",
33 Name: "Test Scope 3",
34 Description: "Access to bananas.",
35 }
36 updatedName := "Updated Scope"
37 updatedDescription := "An updated scope."
38 update := ScopeChange{
39 ID: scope.ID,
40 Name: &updatedName,
41 }
42 update2 := ScopeChange{
43 ID: scope2.ID,
44 Description: &updatedDescription,
45 }
46 update3 := ScopeChange{
47 ID: scope3.ID,
48 Name: &updatedName,
49 Description: &updatedDescription,
50 }
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)
57 }
58 if e, ok := err.(ErrScopeNotFound); !ok {
59 t.Errorf("Expected ErrScopeNotFound, got %+v instead for %T", err, store)
60 } else {
61 if e.Pos != 0 {
62 t.Errorf("Expected the error to be in position %d, got position %d from %T", 0, e.Pos, store)
63 }
64 if e.ID != scope.ID {
65 t.Errorf("Expected the error to be with scope %s, got %s from %T", scope.ID, e.ID, store)
66 }
67 }
68 err = context.CreateScopes([]Scope{scope})
69 if err != nil {
70 t.Errorf("Error saving scope to %T: %s", store, err)
71 }
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)
75 } else {
76 if e.Pos != 0 {
77 t.Errorf("Expected the error to be in position %d, got position %d from %T", 0, e.Pos, store)
78 }
79 if e.ID != scope.ID {
80 t.Errorf("Expected the error to be for ID %s, got %s from %T", scope.ID, e.ID, store)
81 }
82 }
83 retrieved, err = context.GetScopes([]string{scope.ID})
84 if err != nil {
85 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
86 }
87 if len(retrieved) != 1 {
88 t.Logf("%+v", retrieved)
89 t.Errorf("Expected %d results, got %d from %T", 1, len(retrieved), store)
90 }
91 success, field, val1, val2 := compareScopes(scope, retrieved[0])
92 if !success {
93 t.Errorf("Expected %s to be %+v, got %+v from %T", field, val1, val2, store)
94 }
95 err = context.CreateScopes([]Scope{scope2, scope3})
96 if err != nil {
97 t.Errorf("Unexpected error trying to create scope2 and scope3 in %T: %+v", store, err)
98 }
99 retrieved, err = context.GetScopes([]string{scope.ID, scope2.ID, scope3.ID})
100 if err != nil {
101 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
102 }
103 if len(retrieved) != 3 {
104 t.Logf("%+v", retrieved)
105 t.Errorf("Expected %d results, got %d from %T", 3, len(retrieved), store)
106 }
107 for pos, s := range []Scope{scope, scope2, scope3} {
108 success, field, val1, val2 = compareScopes(s, retrieved[pos])
109 if !success {
110 t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store)
111 }
112 }
113 updated, err := context.UpdateScopes([]ScopeChange{update, update2, update3})
114 if err != nil {
115 t.Errorf("Unexpected error updating scopes in %T: %+v", store, err)
116 }
117 if len(updated) != 3 {
118 t.Logf("%+v", updated)
119 t.Errorf("Expected %d results, got %d from %T", 3, len(updated), store)
120 }
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])
126 if !success {
127 t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store)
128 }
129 }
130 retrieved, err = context.ListScopes()
131 if err != nil {
132 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
133 }
134 if len(retrieved) != 3 {
135 t.Logf("%+v", retrieved)
136 t.Errorf("Expected %d results, got %d from %T", 3, len(retrieved), store)
137 }
138 for pos, s := range []Scope{scope, scope2, scope3} {
139 success, field, val1, val2 = compareScopes(s, retrieved[pos])
140 if !success {
141 t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store)
142 }
143 }
144 err = context.RemoveScopes([]string{scope.ID, scope2.ID, scope3.ID})
145 if err != nil {
146 t.Errorf("Unexpected error removing scopes from %T: %+v", store, err)
147 }
148 retrieved, err = context.ListScopes()
149 if err != nil {
150 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
151 }
152 if len(retrieved) != 0 {
153 t.Logf("%+v", retrieved)
154 t.Errorf("Expected %d results, got %d from %T", 0, len(retrieved), store)
155 }
156 err = context.RemoveScopes([]string{scope.ID})
157 if err == nil {
158 t.Errorf("No error returned removing non-existent scopes from %T", store)
159 }
160 if e, ok := err.(ErrScopeNotFound); !ok {
161 t.Errorf("Unexpected error removing non-existent scopes from %T: %+v", store, err)
162 } else {
163 if e.Pos != 0 {
164 t.Errorf("Expected error to be for scope ID in pos %d, but got %d from %T", 0, e.Pos, store)
165 }
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)
168 }
169 }
170 updated, err = context.UpdateScopes([]ScopeChange{update})
171 if err == nil {
172 t.Errorf("No error returned updating non-existent scopes from %T", store)
173 }
174 if e, ok := err.(ErrScopeNotFound); !ok {
175 t.Errorf("Unexpected error updating non-existent scopes from %T: %+v", store, err)
176 } else {
177 if e.Pos != 0 {
178 t.Errorf("Expected error to be for scope ID in pos %d, but got %d from %T", 0, e.Pos, store)
179 }
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)
182 }
183 }
184 }
185 }
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},
191 }
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())
195 }
196 }
197 }