auth
auth/token_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/token_test.go Wed Jan 14 00:23:30 2015 -0500 1.2 +++ b/token_test.go Sun Jan 18 01:02:14 2015 -0500 1.3 @@ -55,15 +55,16 @@ 1.4 ProfileID: uuid.NewID(), 1.5 } 1.6 for _, store := range tokenStores { 1.7 - err := store.saveToken(token) 1.8 + context := Context{tokens: store} 1.9 + err := context.SaveToken(token) 1.10 if err != nil { 1.11 t.Errorf("Error saving token to %T: %s", store, err) 1.12 } 1.13 - err = store.saveToken(token) 1.14 + err = context.SaveToken(token) 1.15 if err != ErrTokenAlreadyExists { 1.16 t.Errorf("Expected ErrTokenAlreadyExists from %T, got %s", store, err) 1.17 } 1.18 - retrievedAccess, err := store.getToken(token.AccessToken, false) 1.19 + retrievedAccess, err := context.GetToken(token.AccessToken, false) 1.20 if err != nil { 1.21 t.Errorf("Error retrieving token from %T: %s", store, err) 1.22 } 1.23 @@ -71,7 +72,7 @@ 1.24 if !success { 1.25 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store) 1.26 } 1.27 - retrievedRefresh, err := store.getToken(token.RefreshToken, true) 1.28 + retrievedRefresh, err := context.GetToken(token.RefreshToken, true) 1.29 if err != nil { 1.30 t.Errorf("Error retrieving refresh token from %T: %s", store, err) 1.31 } 1.32 @@ -79,7 +80,7 @@ 1.33 if !success { 1.34 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store) 1.35 } 1.36 - retrievedProfile, err := store.getTokensByProfileID(token.ProfileID, 25, 0) 1.37 + retrievedProfile, err := context.GetTokensByProfileID(token.ProfileID, 25, 0) 1.38 if err != nil { 1.39 t.Errorf("Error retrieving token by profile from %T: %s", store, err) 1.40 } 1.41 @@ -90,11 +91,11 @@ 1.42 if !success { 1.43 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store) 1.44 } 1.45 - err = store.revokeToken(token.AccessToken, false) 1.46 + err = context.RevokeToken(token.AccessToken, false) 1.47 if err != nil { 1.48 t.Errorf("Error revoking token in %T: %s", store, err) 1.49 } 1.50 - retrievedRevoked, err := store.getToken(token.AccessToken, false) 1.51 + retrievedRevoked, err := context.GetToken(token.AccessToken, false) 1.52 if err != nil { 1.53 t.Errorf("Error retrieving token from %T: %s", store, err) 1.54 } 1.55 @@ -104,34 +105,34 @@ 1.56 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store) 1.57 } 1.58 // TODO(paddy): test revoking by refresh token. 1.59 - err = store.removeToken(token.AccessToken) 1.60 + err = context.RemoveToken(token.AccessToken) 1.61 if err != nil { 1.62 t.Errorf("Error removing token from %T: %s", store, err) 1.63 } 1.64 - _, err = store.getToken(token.AccessToken, false) 1.65 + _, err = context.GetToken(token.AccessToken, false) 1.66 if err != ErrTokenNotFound { 1.67 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err) 1.68 } 1.69 - _, err = store.getToken(token.RefreshToken, true) 1.70 + _, err = context.GetToken(token.RefreshToken, true) 1.71 if err != ErrTokenNotFound { 1.72 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err) 1.73 } 1.74 - retrievedProfile, err = store.getTokensByProfileID(token.ProfileID, 25, 0) 1.75 + retrievedProfile, err = context.GetTokensByProfileID(token.ProfileID, 25, 0) 1.76 if err != nil { 1.77 t.Errorf("Error retrieving token by profile from %T: %s", store, err) 1.78 } 1.79 if len(retrievedProfile) != 0 { 1.80 t.Errorf("Expected list of 0 tokens from %T, got %+v", store, retrievedProfile) 1.81 } 1.82 - err = store.removeToken(token.AccessToken) 1.83 + err = context.RemoveToken(token.AccessToken) 1.84 if err != ErrTokenNotFound { 1.85 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err) 1.86 } 1.87 - err = store.revokeToken(token.AccessToken, false) 1.88 + err = context.RevokeToken(token.AccessToken, false) 1.89 if err != ErrTokenNotFound { 1.90 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err) 1.91 } 1.92 - err = store.revokeToken(token.RefreshToken, true) 1.93 + err = context.RevokeToken(token.RefreshToken, true) 1.94 if err != ErrTokenNotFound { 1.95 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err) 1.96 }