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.
7 "code.secondbit.org/uuid.hg"
10 var sessionStores = []sessionStore{NewMemstore()}
12 func compareSessions(session1, session2 Session) (success bool, field string, val1, val2 interface{}) {
13 if session1.ID != session2.ID {
14 return false, "ID", session1.ID, session2.ID
16 if session1.IP != session2.IP {
17 return false, "IP", session1.IP, session2.IP
19 if session1.UserAgent != session2.UserAgent {
20 return false, "UserAgent", session1.UserAgent, session2.UserAgent
22 if !session1.ProfileID.Equal(session2.ProfileID) {
23 return false, "ProfileID", session1.ProfileID, session2.ProfileID
25 if !session1.Created.Equal(session2.Created) {
26 return false, "Created", session1.Created, session2.Created
28 if session1.Login != session2.Login {
29 return false, "Login", session1.Login, session2.Login
31 if session1.Active != session2.Active {
32 return false, "Active", session1.Active, session2.Active
34 return true, "", nil, nil
37 func TestSessionStoreSuccess(t *testing.T) {
40 ID: uuid.NewID().String() + uuid.NewID().String(),
42 UserAgent: "TestRunner",
43 ProfileID: uuid.NewID(),
45 Login: "test@example.com",
48 for _, store := range sessionStores {
49 context := Context{sessions: store}
50 err := context.CreateSession(session)
52 t.Errorf("Error saving session to %T: %s", store, err)
54 err = context.CreateSession(session)
55 if err != ErrSessionAlreadyExists {
56 t.Errorf("Expected ErrSessionAlreadyExists from %T, got %s", store, err)
58 retrieved, err := context.GetSession(session.ID)
60 t.Errorf("Error retrieving session from %T: %s", store, err)
62 success, field, expectation, result := compareSessions(session, retrieved)
64 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
66 retrievedList, err := context.ListSessions(session.ProfileID, time.Time{}, 10)
68 t.Errorf("Error retrieving sessions by profile from %T: %s", store, err)
70 if len(retrievedList) != 1 {
71 t.Errorf("Expected 1 session retrieved by profile from %T, got %d", store, len(retrievedList))
73 success, field, expectation, result = compareSessions(session, retrievedList[0])
75 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
77 err = context.RemoveSession(session.ID)
79 t.Errorf("Error removing session from %T: %s", store, err)
81 retrieved, err = context.GetSession(session.ID)
82 if err != ErrSessionNotFound {
83 t.Errorf("Expected ErrSessionNotFound from %T, got %s", store, err)
85 retrievedList, err = context.ListSessions(session.ProfileID, time.Time{}, 10)
87 t.Errorf("Error retrieving sessions by profile from %T: %s", store, err)
89 if len(retrievedList) != 0 {
90 t.Errorf("Expected 0 sessions retrieved by profile from %T, got %d", store, len(retrievedList))
92 err = context.RemoveSession(session.ID)
93 if err != ErrSessionNotFound {
94 t.Errorf("Expected ErrSessionNotFound from %T, got %s", store, err)