auth

Paddy 2015-01-18 Parent:c03b5eb3179e Child:23c1a07c8a61

116:e000b1c24fc0 Go to Latest

auth/session_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.

History
paddy@77 1 package auth
paddy@77 2
paddy@77 3 import (
paddy@77 4 "testing"
paddy@77 5 "time"
paddy@77 6
paddy@107 7 "code.secondbit.org/uuid.hg"
paddy@77 8 )
paddy@77 9
paddy@77 10 var sessionStores = []sessionStore{NewMemstore()}
paddy@77 11
paddy@77 12 func compareSessions(session1, session2 Session) (success bool, field string, val1, val2 interface{}) {
paddy@77 13 if session1.ID != session2.ID {
paddy@77 14 return false, "ID", session1.ID, session2.ID
paddy@77 15 }
paddy@77 16 if session1.IP != session2.IP {
paddy@77 17 return false, "IP", session1.IP, session2.IP
paddy@77 18 }
paddy@77 19 if session1.UserAgent != session2.UserAgent {
paddy@77 20 return false, "UserAgent", session1.UserAgent, session2.UserAgent
paddy@77 21 }
paddy@77 22 if !session1.ProfileID.Equal(session2.ProfileID) {
paddy@77 23 return false, "ProfileID", session1.ProfileID, session2.ProfileID
paddy@77 24 }
paddy@77 25 if !session1.Created.Equal(session2.Created) {
paddy@77 26 return false, "Created", session1.Created, session2.Created
paddy@77 27 }
paddy@77 28 if session1.Login != session2.Login {
paddy@77 29 return false, "Login", session1.Login, session2.Login
paddy@77 30 }
paddy@77 31 if session1.Active != session2.Active {
paddy@77 32 return false, "Active", session1.Active, session2.Active
paddy@77 33 }
paddy@77 34 return true, "", nil, nil
paddy@77 35 }
paddy@77 36
paddy@77 37 func TestSessionStoreSuccess(t *testing.T) {
paddy@77 38 t.Parallel()
paddy@77 39 session := Session{
paddy@77 40 ID: uuid.NewID().String() + uuid.NewID().String(),
paddy@77 41 IP: "127.0.0.1",
paddy@77 42 UserAgent: "TestRunner",
paddy@77 43 ProfileID: uuid.NewID(),
paddy@77 44 Created: time.Now(),
paddy@77 45 Login: "test@example.com",
paddy@77 46 Active: true,
paddy@77 47 }
paddy@77 48 for _, store := range sessionStores {
paddy@116 49 context := Context{sessions: store}
paddy@116 50 err := context.CreateSession(session)
paddy@77 51 if err != nil {
paddy@77 52 t.Errorf("Error saving session to %T: %s", store, err)
paddy@77 53 }
paddy@116 54 err = context.CreateSession(session)
paddy@77 55 if err != ErrSessionAlreadyExists {
paddy@77 56 t.Errorf("Expected ErrSessionAlreadyExists from %T, got %s", store, err)
paddy@77 57 }
paddy@116 58 retrieved, err := context.GetSession(session.ID)
paddy@77 59 if err != nil {
paddy@77 60 t.Errorf("Error retrieving session from %T: %s", store, err)
paddy@77 61 }
paddy@77 62 success, field, expectation, result := compareSessions(session, retrieved)
paddy@77 63 if !success {
paddy@77 64 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
paddy@77 65 }
paddy@116 66 retrievedList, err := context.ListSessions(session.ProfileID, time.Time{}, 10)
paddy@77 67 if err != nil {
paddy@77 68 t.Errorf("Error retrieving sessions by profile from %T: %s", store, err)
paddy@77 69 }
paddy@77 70 if len(retrievedList) != 1 {
paddy@77 71 t.Errorf("Expected 1 session retrieved by profile from %T, got %d", store, len(retrievedList))
paddy@77 72 }
paddy@77 73 success, field, expectation, result = compareSessions(session, retrievedList[0])
paddy@77 74 if !success {
paddy@77 75 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
paddy@77 76 }
paddy@116 77 err = context.RemoveSession(session.ID)
paddy@77 78 if err != nil {
paddy@77 79 t.Errorf("Error removing session from %T: %s", store, err)
paddy@77 80 }
paddy@116 81 retrieved, err = context.GetSession(session.ID)
paddy@77 82 if err != ErrSessionNotFound {
paddy@77 83 t.Errorf("Expected ErrSessionNotFound from %T, got %s", store, err)
paddy@77 84 }
paddy@116 85 retrievedList, err = context.ListSessions(session.ProfileID, time.Time{}, 10)
paddy@77 86 if err != nil {
paddy@77 87 t.Errorf("Error retrieving sessions by profile from %T: %s", store, err)
paddy@77 88 }
paddy@77 89 if len(retrievedList) != 0 {
paddy@77 90 t.Errorf("Expected 0 sessions retrieved by profile from %T, got %d", store, len(retrievedList))
paddy@77 91 }
paddy@116 92 err = context.RemoveSession(session.ID)
paddy@77 93 if err != ErrSessionNotFound {
paddy@77 94 t.Errorf("Expected ErrSessionNotFound from %T, got %s", store, err)
paddy@77 95 }
paddy@77 96 }
paddy@77 97 }