auth

Paddy 2015-04-25 Parent:3e8964a914ef

164:cf1aef6eb81f Go to Latest

auth/scope_test.go

Clean up after Client deletion, finish cleaning up after Profile deletion. 6f473576c6ae started cleaning up after Profiles when they're deleted, but didn't clean up the Clients created by that Profile. This fixes that, and also fixes a BUG note about cleaning up after a Client when it's deleted. Extend the authorizationCodeStore to have a deleteAuthorizationCodesByClientID method that will delete the AuthorizationCodes that have been granted by the Client specified by the passed ID. We also implemented this in memstore and postgres, so tests continue to pass. Extend the clientStore to have a deleteClientsByOwner method that will delete the Clients that were created by the Profile specified by the passed ID. We also implemented this in memstore and postgres, so tests continue to pass. Extend the clientStore to have a removeEndpointsByClientID method that will delete the Endpoints that belong(ed) to a the Client specified by the passed ID. We also implemented this in memstore and postgres, so tests continue to pass. Extend the tokenStore to have a revokeTokensByClientID method that will revoke all the Tokens that were granted to the Client specified by the passed ID. We also implemented this in memstore and postgres, so tests continue to pass. When listing Clients by their owner, allow setting the num argument (which controls how many to return) to 0 or lower, and using that to signal "return all Clients belonging to this owner", instead of paging. This is useful when deleting the Clients belonging to a Profile as part of the cleanup after deleting the Profile. Create a cleanUpAfterClientDeletion helper function that will delete the Endpoints and AuthorizationCodes belonging to a Client, and revoke the Tokens belonging to a Client, as part of cleaning up after a Client has been deleted. Add a check in the handler for listing Clients owned by a Profile to disallow the num argument to be lower than 1, because the API should be forced to page. Call our cleanUpAfterClientDeletion once the Client has been deleted in the appropriate handler. Fill out our Context with new methods to wrap all the new methods we're adding to our *Stores. In cleanUpAfterProfileDeletion, obtain a list of clients belonging to the owner, use our new DeleteClientsByOwner method to remove all of them, and then use the list to run our new cleanUpAfterClientDeletion function to clear away the final remnants of a Profile when it's deleted.

History
1 package auth
3 import "os"
4 import "testing"
6 func init() {
7 if os.Getenv("PG_TEST_DB") != "" {
8 p, err := NewPostgres(os.Getenv("PG_TEST_DB"))
9 if err != nil {
10 panic(err)
11 }
12 scopeStores = append(scopeStores, &p)
13 }
14 }
16 var scopeStores = []scopeStore{NewMemstore()}
18 func compareScopes(scope1, scope2 Scope) (success bool, field string, val1, val2 interface{}) {
19 if scope1.ID != scope2.ID {
20 return false, "ID", scope1.ID, scope2.ID
21 }
22 if scope1.Name != scope2.Name {
23 return false, "Name", scope1.Name, scope2.Name
24 }
25 if scope1.Description != scope2.Description {
26 return false, "Description", scope1.Description, scope2.Description
27 }
28 return true, "", nil, nil
29 }
31 func TestScopeInScopeStore(t *testing.T) {
32 scope := Scope{
33 ID: "testscope",
34 Name: "Test Scope",
35 Description: "Access to testing data.",
36 }
37 scope2 := Scope{
38 ID: "testscope2",
39 Name: "Test Scope 2",
40 Description: "Access to minions.",
41 }
42 scope3 := Scope{
43 ID: "testscope3",
44 Name: "Test Scope 3",
45 Description: "Access to bananas.",
46 }
47 updatedName := "Updated Scope"
48 updatedDescription := "An updated scope."
49 update := ScopeChange{
50 Name: &updatedName,
51 }
52 update2 := ScopeChange{
53 Description: &updatedDescription,
54 }
55 update3 := ScopeChange{
56 Name: &updatedName,
57 Description: &updatedDescription,
58 }
59 for _, store := range scopeStores {
60 context := Context{scopes: store}
61 retrieved, err := context.GetScopes([]string{scope.ID})
62 if len(retrieved) != 0 {
63 t.Logf("%+v", retrieved)
64 t.Errorf("Expected %d results, got %d from %T", 0, len(retrieved), store)
65 }
66 err = context.CreateScopes([]Scope{scope})
67 if err != nil {
68 t.Errorf("Error saving scope to %T: %s", store, err)
69 }
70 err = context.CreateScopes([]Scope{scope})
71 if err != ErrScopeAlreadyExists {
72 t.Errorf("Expected ErrScopeAlreadyExists, got %s instead for %T", err, store)
73 }
74 retrieved, err = context.GetScopes([]string{scope.ID})
75 if err != nil {
76 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
77 }
78 if len(retrieved) != 1 {
79 t.Logf("%+v", retrieved)
80 t.Errorf("Expected %d results, got %d from %T", 1, len(retrieved), store)
81 }
82 success, field, val1, val2 := compareScopes(scope, retrieved[0])
83 if !success {
84 t.Errorf("Expected %s to be %+v, got %+v from %T", field, val1, val2, store)
85 }
86 err = context.CreateScopes([]Scope{scope2, scope3})
87 if err != nil {
88 t.Errorf("Unexpected error trying to create scope2 and scope3 in %T: %+v", store, err)
89 }
90 retrieved, err = context.GetScopes([]string{scope.ID, scope2.ID, scope3.ID})
91 if err != nil {
92 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
93 }
94 if len(retrieved) != 3 {
95 t.Logf("%+v", retrieved)
96 t.Errorf("Expected %d results, got %d from %T", 3, len(retrieved), store)
97 }
98 for pos, s := range []Scope{scope, scope2, scope3} {
99 success, field, val1, val2 = compareScopes(s, retrieved[pos])
100 if !success {
101 t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store)
102 }
103 }
104 err = context.UpdateScope(scope.ID, update)
105 if err != nil {
106 t.Errorf("Unexpected error updating scope in %T: %+v", store, err)
107 }
108 scope.ApplyChange(update)
109 err = context.UpdateScope(scope2.ID, update2)
110 if err != nil {
111 t.Errorf("Unexpected error updating scope in %T: %+v", store, err)
112 }
113 scope2.ApplyChange(update2)
114 err = context.UpdateScope(scope3.ID, update3)
115 if err != nil {
116 t.Errorf("Unexpected error updating scope in %T: %+v", store, err)
117 }
118 scope3.ApplyChange(update3)
119 retrieved, err = context.ListScopes()
120 if err != nil {
121 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
122 }
123 if len(retrieved) != 3 {
124 t.Logf("%+v", retrieved)
125 t.Errorf("Expected %d results, got %d from %T", 3, len(retrieved), store)
126 }
127 for pos, s := range []Scope{scope, scope2, scope3} {
128 success, field, val1, val2 = compareScopes(s, retrieved[pos])
129 if !success {
130 t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store)
131 }
132 }
133 err = context.RemoveScopes([]string{scope.ID, scope2.ID, scope3.ID})
134 if err != nil {
135 t.Errorf("Unexpected error removing scopes from %T: %+v", store, err)
136 }
137 retrieved, err = context.ListScopes()
138 if err != nil {
139 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
140 }
141 if len(retrieved) != 0 {
142 t.Logf("%+v", retrieved)
143 t.Errorf("Expected %d results, got %d from %T", 0, len(retrieved), store)
144 }
145 err = context.RemoveScopes([]string{scope.ID})
146 if err == nil {
147 t.Errorf("No error returned removing non-existent scopes from %T", store)
148 }
149 if err != ErrScopeNotFound {
150 t.Errorf("Unexpected error removing non-existent scopes from %T: %+v", store, err)
151 }
152 err = context.UpdateScope(scope.ID, update)
153 if err != ErrScopeNotFound {
154 t.Errorf("Unexpected error updating non-existent scopes from %T: %+v", store, err)
155 }
156 }
157 }