auth
auth/profile_test.go
Make all tests that deal with the store interfaces go through the Context. This is mainly important so that pre- and post- save/retrieval/deletion/whatever transforms can be done without doing them in every single implementation of the store. Change the Endpoint URI property to be a string, not a *url.URL. This makes testing easier, JSON responses cleaner, and is all around just a better strategy. Just because we turn it into a URL every now and then doesn't mean that's how we need to store it. Add JSON tags to the Client type and Endpoint type. Create normalizeURI and normalizeURIString methods to... well, normalize the Endpoint URIs. This makes it so that we can compare them, and forgive some arbitrary user behaviour (like slashes, etc.) Add a NormalizedURI property to the Endpoint type. This is where we store the NormalizedURI, which is what we'll be using when we want to check if an endpoint is valid or not. For the sake of tests and predictability, however, we always want to redirect to the URI, not the NormalizedURI. Add checks to the Client creation API endpoint to give better errors. Now leaving out the Type won't be considered an invalid type, it will be considered a missing parameter. An empty name will be reported as a missing parameter, a name with too few characters will be reported as an insufficient name, and a name with too many characters will be reported as an overflow name. We gather as many of these errors as apply before returning. Check if an Endpoint URI is absolute before adding it as an endpoint, or return an invalid value error if it is not. Always return the errors array when creating a client. We could succeed in creating one or more things and still have errors. We should return anything that's created _as well as_ any errors encountered. Add unit testing for our CreateClientHandler. Fix our oauth2 tests so that if there's an error in the body, it's in the test logs. This should help debugging significantly. Fix our oauth2 tests so that the Profile only requires 1 iteration for its password hashing. This means each time we want to validate a session, it doesn't add a full second to our test runs. This is a big speed improvement for our tests. Add test helper methods for comparing API errors, API responses, and filling in server-generated information in a response that it's impossible to have an expectation around (e.g., IDs) so that we can use our comparison helpers to check if a response is as we expect it. Fix a typo in our Context helpers that was reporting no sessionStore being set _only_ when a sessionStore was set. So yes, the opposite of what we wanted. Oops. This was discovered by passing all our tests through the context. methods instead of operating on the stores themselves.
1.1 --- a/profile_test.go Wed Jan 14 00:23:30 2015 -0500 1.2 +++ b/profile_test.go Sun Jan 18 01:02:14 2015 -0500 1.3 @@ -99,15 +99,16 @@ 1.4 LastSeen: time.Now(), 1.5 } 1.6 for _, store := range profileStores { 1.7 - err := store.saveProfile(profile) 1.8 + context := Context{profiles: store} 1.9 + err := context.SaveProfile(profile) 1.10 if err != nil { 1.11 t.Errorf("Error saving profile to %T: %s", store, err) 1.12 } 1.13 - err = store.saveProfile(profile) 1.14 + err = context.SaveProfile(profile) 1.15 if err != ErrProfileAlreadyExists { 1.16 t.Errorf("Expected ErrProfileAlreadyExists from %T, got %+v", store, err) 1.17 } 1.18 - retrieved, err := store.getProfileByID(profile.ID) 1.19 + retrieved, err := context.GetProfileByID(profile.ID) 1.20 if err != nil { 1.21 t.Errorf("Error retrieving profile from %T: %s", store, err) 1.22 } 1.23 @@ -115,15 +116,15 @@ 1.24 if !match { 1.25 t.Errorf("Expected `%v` in the `%s` field of profile retrieved from %T, got `%v`", expectation, field, store, result) 1.26 } 1.27 - err = store.deleteProfile(profile.ID) 1.28 + err = context.DeleteProfile(profile.ID) 1.29 if err != nil { 1.30 t.Errorf("Error removing profile from %T: %s", store, err) 1.31 } 1.32 - retrieved, err = store.getProfileByID(profile.ID) 1.33 + retrieved, err = context.GetProfileByID(profile.ID) 1.34 if err != ErrProfileNotFound { 1.35 t.Errorf("Expected ErrProfileNotFound from %T, got %+v and %+v", store, retrieved, err) 1.36 } 1.37 - err = store.deleteProfile(profile.ID) 1.38 + err = context.DeleteProfile(profile.ID) 1.39 if err != ErrProfileNotFound { 1.40 t.Errorf("Expected ErrProfileNotFound from %T, got %+v", store, err) 1.41 } 1.42 @@ -213,15 +214,16 @@ 1.43 t.Errorf("Expected field `%s` to be `%v`, got `%v`", field, expected, got) 1.44 } 1.45 for _, store := range profileStores { 1.46 - err := store.saveProfile(profile) 1.47 + context := Context{profiles: store} 1.48 + err := context.SaveProfile(profile) 1.49 if err != nil { 1.50 t.Errorf("Error saving profile in %T: %s", store, err) 1.51 } 1.52 - err = store.updateProfile(profile.ID, change) 1.53 + err = context.UpdateProfile(profile.ID, change) 1.54 if err != nil { 1.55 t.Errorf("Error updating profile in %T: %s", store, err) 1.56 } 1.57 - retrieved, err := store.getProfileByID(profile.ID) 1.58 + retrieved, err := context.GetProfileByID(profile.ID) 1.59 if err != nil { 1.60 t.Errorf("Error getting profile from %T: %s", store, err) 1.61 } 1.62 @@ -229,11 +231,11 @@ 1.63 if !match { 1.64 t.Errorf("Expected field `%s` to be `%v`, got `%v` from %T", field, expected, got, store) 1.65 } 1.66 - err = store.deleteProfile(profile.ID) 1.67 + err = context.DeleteProfile(profile.ID) 1.68 if err != nil { 1.69 t.Errorf("Error deleting profile from %T: %s", store, err) 1.70 } 1.71 - err = store.updateProfile(profile.ID, change) 1.72 + err = context.UpdateProfile(profile.ID, change) 1.73 if err != ErrProfileNotFound { 1.74 t.Errorf("Expected ErrProfileNotFound, got %v from %T", err, store) 1.75 } 1.76 @@ -256,25 +258,26 @@ 1.77 Compromised: &truth, 1.78 } 1.79 for _, store := range profileStores { 1.80 - err := store.saveProfile(profile1) 1.81 + context := Context{profiles: store} 1.82 + err := context.SaveProfile(profile1) 1.83 if err != nil { 1.84 t.Errorf("Error saving profile in %T: %s", store, err) 1.85 } 1.86 - err = store.saveProfile(profile2) 1.87 + err = context.SaveProfile(profile2) 1.88 if err != nil { 1.89 t.Errorf("Error saving profile in %T: %s", store, err) 1.90 } 1.91 - err = store.saveProfile(profile3) 1.92 + err = context.SaveProfile(profile3) 1.93 if err != nil { 1.94 t.Errorf("Error saving profile in %T: %s", store, err) 1.95 } 1.96 - err = store.updateProfiles([]uuid.ID{profile1.ID, profile3.ID}, change) 1.97 + err = context.UpdateProfiles([]uuid.ID{profile1.ID, profile3.ID}, change) 1.98 if err != nil { 1.99 t.Errorf("Error updating profile in %T: %s", store, err) 1.100 } 1.101 profile1.Compromised = truth 1.102 profile3.Compromised = truth 1.103 - retrieved, err := store.getProfileByID(profile1.ID) 1.104 + retrieved, err := context.GetProfileByID(profile1.ID) 1.105 if err != nil { 1.106 t.Errorf("Error getting profile from %T: %s", store, err) 1.107 } 1.108 @@ -282,7 +285,7 @@ 1.109 if !match { 1.110 t.Errorf("Expected field `%s` to be `%v`, got `%v` from %T", field, expected, got, store) 1.111 } 1.112 - retrieved, err = store.getProfileByID(profile2.ID) 1.113 + retrieved, err = context.GetProfileByID(profile2.ID) 1.114 if err != nil { 1.115 t.Errorf("Error getting profile from %T: %s", store, err) 1.116 } 1.117 @@ -290,7 +293,7 @@ 1.118 if !match { 1.119 t.Errorf("Expected field `%s` to be `%v`, got `%v` from %T", field, expected, got, store) 1.120 } 1.121 - retrieved, err = store.getProfileByID(profile3.ID) 1.122 + retrieved, err = context.GetProfileByID(profile3.ID) 1.123 if err != nil { 1.124 t.Errorf("Error getting profile from %T: %s", store, err) 1.125 } 1.126 @@ -311,15 +314,16 @@ 1.127 LastUsed: time.Now().Add(-1 * time.Minute), 1.128 } 1.129 for _, store := range profileStores { 1.130 - err := store.addLogin(login) 1.131 + context := Context{profiles: store} 1.132 + err := context.AddLogin(login) 1.133 if err != nil { 1.134 t.Errorf("Error adding login to %T: %s", store, err) 1.135 } 1.136 - err = store.addLogin(login) 1.137 + err = context.AddLogin(login) 1.138 if err != ErrLoginAlreadyExists { 1.139 t.Errorf("Expected ErrLoginAlreadyExists from %T, got %+v", store, err) 1.140 } 1.141 - retrieved, err := store.listLogins(login.ProfileID, 10, 0) 1.142 + retrieved, err := context.ListLogins(login.ProfileID, 10, 0) 1.143 if err != nil { 1.144 t.Errorf("Error retrieving logins from %T: %s", store, err) 1.145 } 1.146 @@ -331,12 +335,12 @@ 1.147 t.Errorf("Expected `%v` in the `%s` field of login retrieved from %T, got `%v`", expectation, field, store, result) 1.148 } 1.149 lastUsed := time.Now() 1.150 - err = store.recordLoginUse(login.Value, lastUsed) 1.151 + err = context.RecordLoginUse(login.Value, lastUsed) 1.152 if err != nil { 1.153 t.Errorf("Error recording use of login to %T: %s", store, err) 1.154 } 1.155 login.LastUsed = lastUsed 1.156 - retrieved, err = store.listLogins(login.ProfileID, 10, 0) 1.157 + retrieved, err = context.ListLogins(login.ProfileID, 10, 0) 1.158 if err != nil { 1.159 t.Errorf("Error retrieving logins from %T: %s", store, err) 1.160 } 1.161 @@ -347,15 +351,15 @@ 1.162 if !match { 1.163 t.Errorf("Expected `%v` in the `%s` field of login retrieved from %T, got `%v`", expectation, field, store, result) 1.164 } 1.165 - err = store.removeLogin(login.Value, login.ProfileID) 1.166 + err = context.RemoveLogin(login.Value, login.ProfileID) 1.167 if err != nil { 1.168 t.Errorf("Error removing login from %T: %s", store, err) 1.169 } 1.170 - retrieved, err = store.listLogins(login.ProfileID, 10, 0) 1.171 + retrieved, err = context.ListLogins(login.ProfileID, 10, 0) 1.172 if len(retrieved) != 0 { 1.173 t.Errorf("Expected 0 login results from %T, got %d: %+v", store, len(retrieved), retrieved) 1.174 } 1.175 - err = store.removeLogin(login.Value, login.ProfileID) 1.176 + err = context.RemoveLogin(login.Value, login.ProfileID) 1.177 if err != ErrLoginNotFound { 1.178 t.Errorf("Expected ErrLoginNotFound from %T, got %+v", store, err) 1.179 } 1.180 @@ -386,15 +390,16 @@ 1.181 LastUsed: time.Now().Add(-1 * time.Minute), 1.182 } 1.183 for _, store := range profileStores { 1.184 - err := store.saveProfile(profile) 1.185 + context := Context{profiles: store} 1.186 + err := context.SaveProfile(profile) 1.187 if err != nil { 1.188 t.Errorf("Error saving profile in %T: %s", store, err) 1.189 } 1.190 - err = store.addLogin(login) 1.191 + err = context.AddLogin(login) 1.192 if err != nil { 1.193 t.Errorf("Error storing login in %T: %s", store, err) 1.194 } 1.195 - retrieved, err := store.getProfileByLogin(login.Value) 1.196 + retrieved, err := context.GetProfileByLogin(login.Value) 1.197 if err != nil { 1.198 t.Errorf("Error retrieving profile by login from %T: %s", store, err) 1.199 }