-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackendRedisSession_test.go
133 lines (116 loc) · 4.25 KB
/
backendRedisSession_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
package auth
import (
"testing"
"time"
"github.com/EndFirstCorp/onedb/redis"
)
func TestNewBackendRedisSession(t *testing.T) {
}
func TestRedisCreateSession(t *testing.T) {
// expired session error
m := redis.NewMock(nil, nil, nil, nil)
r := backendRedisSession{db: m, prefix: "test"}
_, err := r.CreateSession("1", "[email protected]", map[string]interface{}{"info": "values"}, "hash", "csrfToken", time.Now(), time.Now())
if err == nil || len(m.QueriesRun()) != 0 {
t.Error("expected error")
}
// success
session, err := r.CreateSession("1", "[email protected]", map[string]interface{}{"info": "values"}, "hash", "csrfToken", time.Now(), time.Now().AddDate(1, 0, 0))
m.VerifyNextCommand(t, "SetWithExpire", session, 2592000)
if session.SessionHash != "hash" {
t.Error("expected valid session")
}
}
func TestRedisCreateRememberMe(t *testing.T) {
// expired rememberMe
m := redis.NewMock(nil, nil, nil, nil)
r := backendRedisSession{db: m, prefix: "test"}
_, err := r.CreateRememberMe("1", "[email protected]", "selector", "token", time.Now(), time.Now())
if err == nil || len(m.QueriesRun()) != 0 {
t.Error("expected error")
}
// success
rememberMe, err := r.CreateRememberMe("1", "[email protected]", "selector", "token", time.Now(), time.Now().AddDate(1, 0, 0))
m.VerifyNextCommand(t, "SetWithExpire", rememberMe, 2592000)
if rememberMe.Selector != "selector" || rememberMe.TokenHash != "token" {
t.Error("expected valid rememberMe")
}
}
func TestRedisGetSession(t *testing.T) {
data := []LoginSession{{Email: "[email protected]", SessionHash: "hash"}}
m := redis.NewMock(nil, nil, data, nil)
r := backendRedisSession{db: m, prefix: "test"}
s, err := r.GetSession("hash")
if err != nil || s.Email != "[email protected]" || s.SessionHash != "hash" {
t.Error("expected error", err, s)
}
}
func TestRedisUpdateSession(t *testing.T) {
// success
data := []LoginSession{{Email: "[email protected]", SessionHash: "hash", ExpireTimeUTC: time.Now().AddDate(1, 0, 0)}}
m := redis.NewMock(nil, nil, data, nil)
r := backendRedisSession{db: m, prefix: "test"}
err := r.UpdateSession("hash", futureTime, futureTime)
if err != nil {
t.Error("expected success", err)
}
// error. No data
m = redis.NewMock(nil, nil, nil, nil)
r = backendRedisSession{db: m, prefix: "test"}
err = r.UpdateSession("hash", futureTime, futureTime)
if err == nil {
t.Error("expected success")
}
}
func TestRedisDeleteSession(t *testing.T) {
// success
data := LoginSession{Email: "[email protected]", SessionHash: "hash", ExpireTimeUTC: time.Now().AddDate(1, 0, 0)}
m := redis.NewMock(nil, nil, data, nil)
r := backendRedisSession{db: m, prefix: "test"}
if err := r.DeleteSession("hash"); err != nil {
t.Error("expected success")
}
}
func TestRedisGetRememberMe(t *testing.T) {
// success
data := []rememberMeSession{{Selector: "selector"}}
m := redis.NewMock(nil, nil, data, nil)
r := backendRedisSession{db: m, prefix: "test"}
rememberMe, err := r.GetRememberMe("selector")
if err != nil || rememberMe.Selector != "selector" {
t.Error("expected to find rememberMe", err, rememberMe)
}
}
func TestRedisUpdateRememberMe(t *testing.T) {
// success
data := []rememberMeSession{{Selector: "selector", ExpireTimeUTC: time.Now().AddDate(1, 0, 0)}}
m := redis.NewMock(nil, nil, data, nil)
r := backendRedisSession{db: m, prefix: "test"}
err := r.UpdateRememberMe("selector", futureTime)
if err != nil {
t.Error("expected success", err)
}
// nothing to renew
m = redis.NewMock(nil, nil, nil, nil)
r = backendRedisSession{db: m, prefix: "test"}
err = r.UpdateRememberMe("selector", time.Now())
if err == nil {
t.Error("expected error", err)
}
// expired
data = []rememberMeSession{{Selector: "selector", ExpireTimeUTC: time.Now().AddDate(0, 0, -1)}}
m = redis.NewMock(nil, nil, data, nil)
r = backendRedisSession{db: m, prefix: "test"}
err = r.UpdateRememberMe("selector", time.Now())
if err == nil || err.Error() != "Unable to save expired rememberMe" {
t.Error("expected error", err)
}
}
func TestRedisDeleteRememberMe(t *testing.T) {
data := rememberMeSession{Selector: "selector", ExpireTimeUTC: time.Now().AddDate(1, 0, 0)}
m := redis.NewMock(nil, nil, data, nil)
r := backendRedisSession{db: m, prefix: "test"}
if err := r.DeleteRememberMe("selector"); err != nil {
t.Error("expected success")
}
}