-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstore_session_test.go
154 lines (126 loc) · 4.03 KB
/
store_session_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
package passwordless
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestSessionStoreToken(t *testing.T) {
now := time.Now()
cs := NewCookieStore([]byte{}, []byte{}, []byte{})
valid, err := cs.verifyToken("", "1337", "userid")
assert.Error(t, err)
assert.False(t, valid)
tok, err := cs.newToken("1337", "userid", now.Add(time.Hour))
assert.NoError(t, err)
valid, err = cs.verifyToken(tok, "1337", "userid")
assert.NoError(t, err)
assert.True(t, valid)
valid, err = cs.verifyToken(tok, "1338", "userid")
assert.NoError(t, err)
assert.False(t, valid)
valid, err = cs.verifyToken(tok, "1337", "userie")
assert.NoError(t, err)
assert.False(t, valid)
valid, err = cs.verifyToken(tok+" ", "1337", "userid")
assert.Error(t, err)
assert.False(t, valid)
// Check token expiry
tok, err = cs.newToken("1337", "userid", now.Add(-time.Hour))
assert.NoError(t, err, "negative TTL should not fail")
valid, err = cs.verifyToken(tok, "1337", "userid")
assert.Error(t, err, "expired should produce error")
assert.False(t, valid, "expired should not validate")
}
func TestSessionStoreExists(t *testing.T) {
cs := NewCookieStore([]byte(""), []byte(""), []byte("testtesttesttest"))
// Fail when attempting to Store with bad context
err := cs.Store(nil, "", "", time.Hour)
assert.Equal(t, err, ErrNoResponseWriter)
// Fail when attempting to Verify without valid cookie
req, err := http.NewRequest("", "", nil)
v, tm, err := cs.Exists(SetContext(nil, nil, req), "uid")
assert.Error(t, err)
assert.False(t, v)
assert.Equal(t, time.Time{}, tm)
// Write token to cookie
rec := NewResponseRecorder()
ctx := SetContext(nil, rec, nil)
err = cs.Store(ctx, "token", "uid", time.Hour)
assert.NoError(t, err)
assert.NotNil(t, rec.Header().Get("Set-Cookie"))
// Read response
resp := rec.Response()
req, err = http.NewRequest("", "", nil)
assert.NoError(t, err)
for _, c := range resp.Cookies() {
req.AddCookie(c)
}
// Check Exists
v, tm, err = cs.Exists(SetContext(nil, nil, req), "uid")
assert.NoError(t, err)
assert.True(t, v)
assert.NotEqual(t, time.Time{}, tm)
// Check Exists fails for wrong uid
v, tm, err = cs.Exists(SetContext(nil, nil, req), "anotheruid")
assert.Equal(t, err, ErrWrongTokenUID)
assert.False(t, v)
assert.Equal(t, time.Time{}, tm)
// Test bad cookie fails verification
req, err = http.NewRequest("", "", nil)
req.AddCookie(&http.Cookie{Name: "passwordless", Value: "invalid!"})
v, tm, err = cs.Exists(SetContext(nil, nil, req), "uid")
assert.Error(t, err)
assert.False(t, v)
assert.Equal(t, time.Time{}, tm)
}
func TestSessionStoreVerify(t *testing.T) {
cs := NewCookieStore([]byte(""), []byte(""), []byte("testtesttesttest"))
// Write token to cookie
rec := NewResponseRecorder()
ctx := SetContext(nil, rec, nil)
err := cs.Store(ctx, "token", "uid", time.Hour)
assert.NoError(t, err)
assert.NotNil(t, rec.Header().Get("Set-Cookie"))
// Read response
resp := rec.Response()
req, err := http.NewRequest("", "", nil)
assert.NoError(t, err)
for _, c := range resp.Cookies() {
req.AddCookie(c)
}
// Verify bad token fails
v, err := cs.Verify(SetContext(nil, nil, req), "badtoken", "uid")
assert.NoError(t, err)
assert.False(t, v)
// Verify good token succeeds
v, err = cs.Verify(SetContext(nil, nil, req), "token", "uid")
assert.NoError(t, err)
assert.True(t, v)
}
func TestSessionStoreDelete(t *testing.T) {
cs := NewCookieStore([]byte(""), []byte(""), []byte(""))
err := cs.Delete(nil, "")
assert.Equal(t, ErrNoResponseWriter, err)
rec := NewResponseRecorder()
err = cs.Delete(SetContext(nil, rec, nil), "")
assert.Nil(t, err)
assert.NotEmpty(t, rec.Header().Get("Set-Cookie"))
}
type ResponseRecorder struct {
*httptest.ResponseRecorder
}
func NewResponseRecorder() *ResponseRecorder {
return &ResponseRecorder{
ResponseRecorder: httptest.NewRecorder(),
}
}
func (r ResponseRecorder) Response() *http.Response {
return &http.Response{
StatusCode: r.Code,
Header: r.HeaderMap,
Body: ioutil.NopCloser(r.Body),
}
}