-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathlock_test.go
337 lines (264 loc) · 7.45 KB
/
lock_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package lock
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/volatiletech/authboss/v3"
"github.com/volatiletech/authboss/v3/mocks"
)
func TestInit(t *testing.T) {
t.Parallel()
ab := authboss.New()
l := &Lock{}
if err := l.Init(ab); err != nil {
t.Fatal(err)
}
}
type testHarness struct {
lock *Lock
ab *authboss.Authboss
bodyReader *mocks.BodyReader
mailer *mocks.Emailer
redirector *mocks.Redirector
renderer *mocks.Renderer
responder *mocks.Responder
session *mocks.ClientStateRW
storer *mocks.ServerStorer
}
func testSetup() *testHarness {
harness := &testHarness{}
harness.ab = authboss.New()
harness.bodyReader = &mocks.BodyReader{}
harness.mailer = &mocks.Emailer{}
harness.redirector = &mocks.Redirector{}
harness.renderer = &mocks.Renderer{}
harness.responder = &mocks.Responder{}
harness.session = mocks.NewClientRW()
harness.storer = mocks.NewServerStorer()
harness.ab.Paths.LockNotOK = "/lock/not/ok"
harness.ab.Modules.LockAfter = 3
harness.ab.Modules.LockDuration = time.Hour
harness.ab.Modules.LockWindow = time.Minute
harness.ab.Config.Core.BodyReader = harness.bodyReader
harness.ab.Config.Core.Logger = mocks.Logger{}
harness.ab.Config.Core.Mailer = harness.mailer
harness.ab.Config.Core.Redirector = harness.redirector
harness.ab.Config.Core.MailRenderer = harness.renderer
harness.ab.Config.Core.Responder = harness.responder
harness.ab.Config.Storage.SessionState = harness.session
harness.ab.Config.Storage.Server = harness.storer
harness.lock = &Lock{harness.ab}
return harness
}
func TestBeforeAuthAllow(t *testing.T) {
t.Parallel()
harness := testSetup()
user := &mocks.User{
Email: "[email protected]",
Locked: time.Time{},
}
harness.storer.Users["[email protected]"] = user
r := mocks.Request("GET")
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
w := httptest.NewRecorder()
handled, err := harness.lock.BeforeAuth(w, r, false)
if err != nil {
t.Error(err)
}
if handled {
t.Error("it shouldn't have been handled")
}
}
func TestBeforeAuthDisallow(t *testing.T) {
t.Parallel()
harness := testSetup()
user := &mocks.User{
Email: "[email protected]",
Locked: time.Now().UTC().Add(time.Hour),
}
harness.storer.Users["[email protected]"] = user
r := mocks.Request("GET")
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
w := httptest.NewRecorder()
handled, err := harness.lock.BeforeAuth(w, r, false)
if err != nil {
t.Error(err)
}
if !handled {
t.Error("it should have been handled")
}
if w.Code != http.StatusTemporaryRedirect {
t.Error("code was wrong:", w.Code)
}
opts := harness.redirector.Options
if opts.RedirectPath != harness.ab.Paths.LockNotOK {
t.Error("redir path was wrong:", opts.RedirectPath)
}
if len(opts.Failure) == 0 {
t.Error("expected a failure message")
}
}
func TestAfterAuthSuccess(t *testing.T) {
t.Parallel()
harness := testSetup()
last := time.Now().UTC().Add(-time.Hour)
user := &mocks.User{
Email: "[email protected]",
AttemptCount: 45,
LastAttempt: last,
}
harness.storer.Users["[email protected]"] = user
r := mocks.Request("GET")
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
w := httptest.NewRecorder()
handled, err := harness.lock.AfterAuthSuccess(w, r, false)
if err != nil {
t.Error(err)
}
if handled {
t.Error("it should never be handled")
}
user = harness.storer.Users["[email protected]"]
if 0 != user.GetAttemptCount() {
t.Error("attempt count wrong:", user.GetAttemptCount())
}
if !last.Before(user.GetLastAttempt()) {
t.Errorf("last attempt should be more recent, old: %v new: %v", last, user.GetLastAttempt())
}
}
func TestAfterAuthFailure(t *testing.T) {
t.Parallel()
harness := testSetup()
user := &mocks.User{
Email: "[email protected]",
}
harness.storer.Users["[email protected]"] = user
if IsLocked(harness.storer.Users["[email protected]"]) {
t.Error("should not be locked")
}
r := mocks.Request("GET")
w := httptest.NewRecorder()
var handled bool
var err error
for i := 1; i <= 3; i++ {
if IsLocked(harness.storer.Users["[email protected]"]) {
t.Error("should not be locked")
}
r := r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
handled, err = harness.lock.AfterAuthFail(w, r, false)
if err != nil {
t.Fatal(err)
}
if i < 3 {
if handled {
t.Errorf("%d) should not be handled until lock occurs", i)
}
user := harness.storer.Users["[email protected]"]
if user.GetAttemptCount() != i {
t.Errorf("attempt count wrong, want: %d, got: %d", i, user.GetAttemptCount())
}
if IsLocked(user) {
t.Error("should not be locked")
}
}
}
if !handled {
t.Error("should have been handled at the end")
}
if !IsLocked(harness.storer.Users["[email protected]"]) {
t.Error("should be locked at the end")
}
if w.Code != http.StatusTemporaryRedirect {
t.Error("code was wrong:", w.Code)
}
opts := harness.redirector.Options
if opts.RedirectPath != harness.ab.Paths.LockNotOK {
t.Error("redir path was wrong:", opts.RedirectPath)
}
if len(opts.Failure) == 0 {
t.Error("expected a failure message")
}
}
func TestLock(t *testing.T) {
t.Parallel()
harness := testSetup()
user := &mocks.User{
Email: "[email protected]",
}
harness.storer.Users["[email protected]"] = user
if IsLocked(harness.storer.Users["[email protected]"]) {
t.Error("should not be locked")
}
if err := harness.lock.Lock(context.Background(), "[email protected]"); err != nil {
t.Error(err)
}
if !IsLocked(harness.storer.Users["[email protected]"]) {
t.Error("should be locked")
}
}
func TestUnlock(t *testing.T) {
t.Parallel()
harness := testSetup()
user := &mocks.User{
Email: "[email protected]",
Locked: time.Now().UTC().Add(time.Hour),
}
harness.storer.Users["[email protected]"] = user
if !IsLocked(harness.storer.Users["[email protected]"]) {
t.Error("should be locked")
}
if err := harness.lock.Unlock(context.Background(), "[email protected]"); err != nil {
t.Error(err)
}
if IsLocked(harness.storer.Users["[email protected]"]) {
t.Error("should no longer be locked")
}
}
func TestMiddlewareAllow(t *testing.T) {
t.Parallel()
ab := authboss.New()
called := false
server := Middleware(ab)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
}))
user := &mocks.User{
Locked: time.Now().UTC().Add(-time.Hour),
}
r := mocks.Request("GET")
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
w := httptest.NewRecorder()
server.ServeHTTP(w, r)
if !called {
t.Error("The user should have been allowed through")
}
}
func TestMiddlewareDisallow(t *testing.T) {
t.Parallel()
ab := authboss.New()
redirector := &mocks.Redirector{}
ab.Config.Paths.LockNotOK = "/lock/not/ok"
ab.Config.Core.Logger = mocks.Logger{}
ab.Config.Core.Redirector = redirector
called := false
server := Middleware(ab)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
}))
user := &mocks.User{
Locked: time.Now().UTC().Add(time.Hour),
}
r := mocks.Request("GET")
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
w := httptest.NewRecorder()
server.ServeHTTP(w, r)
if called {
t.Error("The user should not have been allowed through")
}
if redirector.Options.Code != http.StatusTemporaryRedirect {
t.Error("expected a redirect, but got:", redirector.Options.Code)
}
if p := redirector.Options.RedirectPath; p != "/lock/not/ok" {
t.Error("redirect path wrong:", p)
}
}