auth

Paddy 2015-01-24 Parent:23c1a07c8a61 Child:163ce22fa4c9

129:4f5d13d2f7c7 Go to Latest

auth/session_test.go

Test our getClientAuth helper, switch to table-based tests. Our getClientAuth helper was being tested implicitly when we tested our verifyClient helper, but let's test them separately. While we're at it, let's use table based tests instead of copy and paste. I noticed a lot of copy/paste errors while I was updating this, and the less test code we have and the easier we make it to test new edge cases, the better of we are.

History
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.