Test our Postgres profileStore implementation.
Update all our test cases to use time.Now().Round(time.Millisecond), because Go
uses nanosecond precision on time values, but Postgres silently truncates that
to millisecond precision. This caused our tests to report false failures that
were just silent precision loss, not actual failures.
Set up our authd server to use the Postgres store for profiles and automatically
create a test scope when starting up.
Log errors when creating Clients through the API, instead of just swallowing
them and sending back cryptic act of god errors.
Add a NewPostgres helper that returns a postgres profileStore from a connection
string (passed through pq transparently).
Add an Empty() bool helper to ProfileChange and BulkProfileChange types, so we
can determine if there are any changes we need to act on easily.
Log errors when creating Pofiles through the API, instead of just swalloing them
and sending back cryptic act of god errors.
Remove the ` quotes around field and table names, which are not supported in
Postgres. This required adding a few functions/methods to pan.
Detect situations where a profile was expected and not found, and return
ErrProfileNotFound.
Detect pq errors thrown when the profiles_pkey constraint is violated, and
transform them to the ErrProfileAlreadyExists error.
Detect empty ProfileChange and BulkProfileChange variables and abort the
updateProfile and updateProfiles methods early, before invalid SQL is generated.
Detect pq errors thrown when the logins_pkey constraint is violated, and
transform them to the ErrLoginAlreadyExists error.
Detect when removing a Login and no rows were affected, and return an
ErrLoginNotFound.
Create an sql dir with a postgres_init script that will initialize the schema of
the tables expected in the database.
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())