-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
user_test.go
163 lines (156 loc) · 3.79 KB
/
user_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package sessions
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)
// TestUser is a test class for users.
type TestUser struct {
ID string
Item string
}
// Return the user ID.
func (u *TestUser) GetID() interface{} {
return u.ID
}
// Test login.
func TestUserLogin(t *testing.T) {
defer reset()
var saved int
Persistence = ExtendablePersistenceLayer{
LoadSessionFunc: func(id string) (*Session, error) {
if id != sessionID {
return nil, fmt.Errorf("Requested wrong session: %s", id)
}
return &Session{
created: time.Now().Add(-2 * time.Minute),
lastAccess: time.Now().Add(-2 * time.Minute),
}, nil
},
SaveSessionFunc: func(id string, session *Session) error {
saved++
return nil
},
}
req := httptest.NewRequest("", "/", nil)
req.AddCookie(&http.Cookie{Name: SessionCookie, Value: sessionID})
res := httptest.NewRecorder()
session, err := Start(res, req, false)
if err != nil {
t.Error(err)
}
if session == nil {
t.Error("Expected session, received nil")
return
}
user := &TestUser{}
if err := session.LogIn(user, true, res); err != nil {
t.Error(err)
}
if saved != 3 { // 1 from log in, 2 from switch ID.
t.Errorf("New session was not saved (%d)", saved)
}
if session.User() != User(user) {
t.Error("User was not logged in")
return
}
}
// Test logout.
func TestUserLogout(t *testing.T) {
defer reset()
user := &TestUser{ID: "userid"}
Persistence = ExtendablePersistenceLayer{
LoadSessionFunc: func(id string) (*Session, error) {
return &Session{
user: user,
created: time.Now().Add(-2 * time.Minute),
lastAccess: time.Now().Add(-2 * time.Minute),
}, nil
},
UserSessionsFunc: func(userID interface{}) ([]string, error) {
return []string{sessionID, "1", "2", "3"}, nil
},
}
req := httptest.NewRequest("", "/", nil)
req.AddCookie(&http.Cookie{Name: SessionCookie, Value: sessionID})
res := httptest.NewRecorder()
session, err := Start(res, req, false)
if err != nil {
t.Error(err)
}
if session == nil {
t.Error("Expected session, received nil")
return
}
if err := session.LogOut(); err != nil {
// We don't need this but let's cover it anyway.
t.Error(err)
return
}
if err := LogOut(user.ID); err != nil {
t.Error(err)
return
}
for id, session := range sessions.sessions {
if session.user != nil {
t.Errorf("User still logged into session %s", id)
}
}
}
// Testing a refresh of all sessions of a given user.
func TestUserRefresh(t *testing.T) {
defer reset()
user := &TestUser{ID: "userid", Item: "item"}
Persistence = ExtendablePersistenceLayer{
LoadSessionFunc: func(id string) (*Session, error) {
return &Session{
user: user,
created: time.Now().Add(-2 * time.Minute),
lastAccess: time.Now().Add(-2 * time.Minute),
}, nil
},
UserSessionsFunc: func(userID interface{}) ([]string, error) {
return []string{sessionID, "1", "2", "3"}, nil
},
}
req := httptest.NewRequest("", "/", nil)
req.AddCookie(&http.Cookie{Name: SessionCookie, Value: sessionID})
res := httptest.NewRecorder()
session, err := Start(res, req, false)
if err != nil {
t.Error(err)
}
if session == nil {
t.Error("Expected session, received nil")
return
}
modifiedUser := session.User().(*TestUser)
modifiedUser.Item = "newitem"
if e := RefreshUser(modifiedUser); e != nil {
t.Error(e)
return
}
PurgeSessions()
sessionIDs, err := Persistence.UserSessions("userid")
if err != nil {
t.Error(err)
return
}
if len(sessionIDs) != 4 {
t.Errorf("Invalid number of sessions: %d", len(sessionIDs))
return
}
for _, sessionID := range sessionIDs {
session, err := sessions.Get(sessionID)
if err != nil {
t.Error(err)
return
}
item := session.user.(*TestUser).Item
if item != "newitem" {
t.Errorf("Found a user with the wrong item: %s", item)
}
}
}