-
Notifications
You must be signed in to change notification settings - Fork 0
/
confirm_test.go
504 lines (405 loc) · 11.9 KB
/
confirm_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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package confirmcode
import (
"bytes"
"context"
"crypto/sha512"
"encoding/base64"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/volatiletech/authboss/v3"
"github.com/volatiletech/authboss/v3/mocks"
)
type mockUser struct {
Email string
Confirmed bool
ConfirmCode string
ConfirmExpiration time.Time
ConfirmLastAttempt time.Time
}
var (
_ authboss.User = &mockUser{}
_ User = &mockUser{}
)
func (m mockUser) GetPID() string { return m.Email }
func (m mockUser) GetEmail() string { return m.Email }
func (m mockUser) GetConfirmed() bool { return m.Confirmed }
func (m mockUser) GetConfirmCode() string { return m.ConfirmCode }
func (m mockUser) GetConfirmExpiration() time.Time { return m.ConfirmExpiration }
func (m mockUser) GetConfirmLastAttempt() time.Time { return m.ConfirmLastAttempt }
func (m *mockUser) PutPID(v string) { m.Email = v }
func (m *mockUser) PutEmail(v string) { m.Email = v }
func (m *mockUser) PutConfirmed(v bool) { m.Confirmed = v }
func (m *mockUser) PutConfirmCode(v string) { m.ConfirmCode = v }
func (m *mockUser) PutConfirmExpiration(v time.Time) { m.ConfirmExpiration = v }
func (m *mockUser) PutConfirmLastAttempt(v time.Time) { m.ConfirmLastAttempt = v }
func TestInit(t *testing.T) {
t.Parallel()
ab := authboss.New()
router := &mocks.Router{}
renderer := &mocks.Renderer{}
errHandler := &mocks.ErrorHandler{}
ab.Config.Core.Router = router
ab.Config.Core.MailRenderer = renderer
ab.Config.Core.ErrorHandler = errHandler
_, err := Setup(ab, Defaults())
if err != nil {
t.Fatal(err)
}
if err := renderer.HasLoadedViews(EmailConfirmHTML, EmailConfirmTxt); err != nil {
t.Error(err)
}
if err := router.HasGets("/confirm"); err != nil {
t.Error(err)
}
}
type testHarness struct {
confirm *Confirm
ab *authboss.Authboss
bodyReader *mocks.BodyReader
mailer *mocks.Emailer
redirector *mocks.Redirector
renderer *mocks.Renderer
responder *mocks.Responder
session *mocks.ClientStateRW
storer *mockServerStorer
}
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 = newMockServerStorer()
harness.ab.Modules.MailNoGoroutine = true
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.confirm = &Confirm{Authboss: harness.ab, config: Defaults()}
return harness
}
func TestPreventAuthAllow(t *testing.T) {
t.Parallel()
harness := testSetup()
user := &mockUser{
Confirmed: true,
}
r := mocks.Request("GET")
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
w := httptest.NewRecorder()
handled, err := harness.confirm.PreventAuth(w, r, false)
if err != nil {
t.Error(err)
}
if handled {
t.Error("it should not have been handled")
}
}
func TestPreventDisallow(t *testing.T) {
t.Parallel()
harness := testSetup()
user := &mockUser{
Confirmed: false,
}
r := mocks.Request("GET")
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
w := httptest.NewRecorder()
handled, err := harness.confirm.PreventAuth(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("redirect did not occur")
}
if p := harness.redirector.Options.RedirectPath; p != "/confirm" {
t.Error("redirect path was wrong:", p)
}
}
func TestStartConfirmationWeb(t *testing.T) {
t.Parallel()
harness := testSetup()
user := &mockUser{Email: "[email protected]"}
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.confirm.StartConfirmationWeb(w, r, false)
if err != nil {
t.Error(err)
}
if !handled {
t.Error("it should always be handled")
}
if w.Code != http.StatusTemporaryRedirect {
t.Error("redirect did not occur")
}
if p := harness.redirector.Options.RedirectPath; p != "/confirm" {
t.Error("redirect path was wrong:", p)
}
if to := harness.mailer.Email.To[0]; to != "[email protected]" {
t.Error("mailer sent e-mail to wrong person:", to)
}
}
func TestGetSuccess(t *testing.T) {
t.Parallel()
harness := testSetup()
user := &mockUser{Email: "[email protected]", Confirmed: false}
r := mocks.Request("GET")
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
w := httptest.NewRecorder()
if err := harness.confirm.Get(w, r); err != nil {
t.Error(err)
}
if w.Code != http.StatusOK {
t.Error("expected ok, got:", w.Code)
}
if p := harness.responder.Page; p != PageConfirm {
t.Error("page was wrong:", p)
}
}
func TestPostValidationFailure(t *testing.T) {
t.Parallel()
harness := testSetup()
harness.bodyReader.Return = mocks.Values{
Errors: []error{errors.New("fail")},
}
r := mocks.Request("POST")
w := httptest.NewRecorder()
if err := harness.confirm.Post(w, r); err != nil {
t.Error(err)
}
if w.Code != http.StatusTemporaryRedirect {
t.Error("expected a redirect, got:", w.Code)
}
if p := harness.redirector.Options.RedirectPath; p != "/confirm" {
t.Error("redir path was wrong:", p)
}
if reason := harness.redirector.Options.Failure; reason != "confirm token is invalid" {
t.Error("reason for failure was wrong:", reason)
}
}
func TestPostUserNotFoundFailure(t *testing.T) {
t.Parallel()
harness := testSetup()
harness.bodyReader.Return = mocks.Values{
Token: "123-123",
}
r := mocks.Request("POST")
w := httptest.NewRecorder()
if err := harness.confirm.Post(w, r); err != nil {
t.Error(err)
}
if w.Code != http.StatusTemporaryRedirect {
t.Error("expected a redirect, got:", w.Code)
}
if p := harness.redirector.Options.RedirectPath; p != "/confirm" {
t.Error("redir path was wrong:", p)
}
if reason := harness.redirector.Options.Failure; reason != "confirm token is invalid" {
t.Error("reason for failure was wrong:", reason)
}
}
func TestPostSuccess(t *testing.T) {
t.Parallel()
code, hash, err := GenerateConfirmCreds(8)
if err != nil {
t.Fatal(err)
}
user := &mockUser{
Email: "[email protected]",
Confirmed: false,
ConfirmExpiration: time.Now().UTC().Add(time.Hour),
ConfirmLastAttempt: time.Now().UTC().Add(-time.Hour),
ConfirmCode: hash,
}
storer := &mockServerStorer{
Users: map[string]*mockUser{user.Email: user},
}
harness := testSetup()
harness.ab.Storage.Server = storer
harness.bodyReader.Return = mocks.Values{
Token: addDashes(code, 3),
}
r := mocks.Request("POST")
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
w := httptest.NewRecorder()
if err := harness.confirm.Post(w, r); err != nil {
t.Error(err)
}
if w.Code != http.StatusTemporaryRedirect {
t.Error("expected a redirect, got:", w.Code)
}
if p := harness.redirector.Options.RedirectPath; p != "/" {
t.Error("redir path was wrong:", p)
}
if reason := harness.redirector.Options.Success; reason != "You have successfully confirmed your account." {
t.Error("reason for success was wrong:", reason)
}
user = storer.Users[user.Email]
if user.ConfirmCode != "" {
t.Error("confirm code not cleared")
}
if !user.Confirmed {
t.Error("user was not confirmed")
}
if !user.ConfirmExpiration.IsZero() {
t.Error("confirm expiration not cleared")
}
if !user.ConfirmLastAttempt.IsZero() {
t.Error("confirm last attempt not cleared")
}
}
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 := &mockUser{
Confirmed: true,
}
r := mocks.Request("POST")
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.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 := &mockUser{
Confirmed: false,
}
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 != "/auth/confirm" {
t.Error("redirect path wrong:", p)
}
}
func TestAddDashes(t *testing.T) {
t.Parallel()
tests := []struct {
In string
Want string
Dashes int
}{
{In: "123123", Dashes: 0, Want: "123123"},
{In: "123123", Dashes: 1, Want: "1-2-3-1-2-3"},
{In: "123123", Dashes: 2, Want: "12-31-23"},
{In: "123123", Dashes: 3, Want: "123-123"},
{In: "123123", Dashes: 4, Want: "1231-23"},
{In: "123123", Dashes: 5, Want: "12312-3"},
{In: "123123", Dashes: 6, Want: "123123"},
}
for i, test := range tests {
got := addDashes(test.In, test.Dashes)
if got != test.Want {
t.Errorf("%d) want: %s, got: %s", i, test.Want, got)
}
}
}
func TestGenerateConfirmCreds(t *testing.T) {
t.Parallel()
code, hash, err := GenerateConfirmCreds(6)
if err != nil {
t.Error(err)
}
if code == hash {
t.Error("the code and hash should be different")
}
if len(hash) != base64.StdEncoding.EncodedLen(sha512.Size) {
t.Errorf("hash length was wrong (%d): %s", len(hash), hash)
}
if len(code) != 6 {
t.Errorf("code length was wrong (%d): %s", len(code), code)
}
codeHash, err := base64.StdEncoding.DecodeString(hash)
if err != nil {
t.Error(err)
}
check := sha512.Sum512([]byte(code))
if 0 != bytes.Compare(codeHash, check[:]) {
t.Error("expected code hashes to match")
}
}
var (
_ authboss.ServerStorer = &mockServerStorer{}
_ Storer = &mockServerStorer{}
)
type mockServerStorer struct {
Users map[string]*mockUser
}
// NewServerStorer constructor
func newMockServerStorer() *mockServerStorer {
return &mockServerStorer{
Users: make(map[string]*mockUser),
}
}
// New constructs a blank user to later be created
func (s *mockServerStorer) New(context.Context) authboss.User {
return &mockUser{}
}
// Create a user
func (s *mockServerStorer) Create(ctx context.Context, user authboss.User) error {
u := user.(*mockUser)
if _, ok := s.Users[u.Email]; ok {
return authboss.ErrUserFound
}
s.Users[u.Email] = u
return nil
}
// Load a user
func (s *mockServerStorer) Load(ctx context.Context, key string) (authboss.User, error) {
user, ok := s.Users[key]
if ok {
return user, nil
}
return nil, authboss.ErrUserNotFound
}
func (s *mockServerStorer) Save(ctx context.Context, user authboss.User) error {
u := user.(*mockUser)
if _, ok := s.Users[u.Email]; !ok {
return authboss.ErrUserNotFound
}
s.Users[u.Email] = u
return nil
}
func (s *mockServerStorer) LoadByConfirmCode(ctx context.Context, code string) (User, error) {
for _, v := range s.Users {
if v.ConfirmCode == code {
return v, nil
}
}
return nil, authboss.ErrUserNotFound
}