auth

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

148:06fb735031bb Go to Latest

auth/scope_test.go

Do a first, naive pass at storing profiles in Postgres. This is untested against an actual database. It's a best-guess attempt at SQL. It _should_ work. I think. Start storing things in Postgres, starting with Profiles and Logins. This necessitates the addition of a Deleted property to the Profile type, because I'm not deleting those in case of accidental deletion. Logins, though, we'll delete. This also necessitates updating the profileStore interface to no longer have a deleteProfile method, because we're tracking that through updates now. Then we need to update our profileStore tests, because they no longer clean up after themselves. Which, come to think of it, may cause some problems later.

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 }