auth
auth/session_test.go
Add our BUG notices. Rather than keeping the list of things to implement or test on sticky notes attached to my monitor, let's give them BUG designations within the code. Now `godoc . bugs` will list them out for us. Isn't that nice?
1 package auth
3 import (
4 "testing"
5 "time"
7 "code.secondbit.org/uuid.hg"
8 )
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
15 }
16 if session1.IP != session2.IP {
17 return false, "IP", session1.IP, session2.IP
18 }
19 if session1.UserAgent != session2.UserAgent {
20 return false, "UserAgent", session1.UserAgent, session2.UserAgent
21 }
22 if !session1.ProfileID.Equal(session2.ProfileID) {
23 return false, "ProfileID", session1.ProfileID, session2.ProfileID
24 }
25 if !session1.Created.Equal(session2.Created) {
26 return false, "Created", session1.Created, session2.Created
27 }
28 if session1.Login != session2.Login {
29 return false, "Login", session1.Login, session2.Login
30 }
31 if session1.Active != session2.Active {
32 return false, "Active", session1.Active, session2.Active
33 }
34 return true, "", nil, nil
35 }
37 func TestSessionStoreSuccess(t *testing.T) {
38 t.Parallel()
39 session := Session{
40 ID: uuid.NewID().String() + uuid.NewID().String(),
41 IP: "127.0.0.1",
42 UserAgent: "TestRunner",
43 ProfileID: uuid.NewID(),
44 Created: time.Now(),
45 Login: "test@example.com",
46 Active: true,
47 }
48 for _, store := range sessionStores {
49 context := Context{sessions: store}
50 err := context.CreateSession(session)
51 if err != nil {
52 t.Errorf("Error saving session to %T: %s", store, err)
53 }
54 err = context.CreateSession(session)
55 if err != ErrSessionAlreadyExists {
56 t.Errorf("Expected ErrSessionAlreadyExists from %T, got %s", store, err)
57 }
58 retrieved, err := context.GetSession(session.ID)
59 if err != nil {
60 t.Errorf("Error retrieving session from %T: %s", store, err)
61 }
62 success, field, expectation, result := compareSessions(session, retrieved)
63 if !success {
64 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
65 }
66 retrievedList, err := context.ListSessions(session.ProfileID, time.Time{}, 10)
67 if err != nil {
68 t.Errorf("Error retrieving sessions by profile from %T: %s", store, err)
69 }
70 if len(retrievedList) != 1 {
71 t.Errorf("Expected 1 session retrieved by profile from %T, got %d", store, len(retrievedList))
72 }
73 success, field, expectation, result = compareSessions(session, retrievedList[0])
74 if !success {
75 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
76 }
77 err = context.RemoveSession(session.ID)
78 if err != nil {
79 t.Errorf("Error removing session from %T: %s", store, err)
80 }
81 retrieved, err = context.GetSession(session.ID)
82 if err != ErrSessionNotFound {
83 t.Errorf("Expected ErrSessionNotFound from %T, got %s", store, err)
84 }
85 retrievedList, err = context.ListSessions(session.ProfileID, time.Time{}, 10)
86 if err != nil {
87 t.Errorf("Error retrieving sessions by profile from %T: %s", store, err)
88 }
89 if len(retrievedList) != 0 {
90 t.Errorf("Expected 0 sessions retrieved by profile from %T, got %d", store, len(retrievedList))
91 }
92 err = context.RemoveSession(session.ID)
93 if err != ErrSessionNotFound {
94 t.Errorf("Expected ErrSessionNotFound from %T, got %s", store, err)
95 }
96 }
97 }
99 // BUG(paddy): We need to test the CreateSessionHandler.
100 // BUG(paddy): We need to test the credentialsValidate function.