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.
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())