forked from volatiletech/authboss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register_test.go
316 lines (254 loc) · 7.48 KB
/
register_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
package register
import (
"net/http"
"net/http/httptest"
"testing"
"golang.org/x/crypto/bcrypt"
"github.com/pkg/errors"
"github.com/volatiletech/authboss"
"github.com/volatiletech/authboss/internal/mocks"
)
func TestRegisterInit(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.ViewRenderer = renderer
ab.Config.Core.ErrorHandler = errHandler
ab.Config.Storage.Server = &mocks.ServerStorer{}
reg := &Register{}
if err := reg.Init(ab); err != nil {
t.Fatal(err)
}
if err := renderer.HasLoadedViews(PageRegister); err != nil {
t.Error(err)
}
if err := router.HasGets("/register"); err != nil {
t.Error(err)
}
if err := router.HasPosts("/register"); err != nil {
t.Error(err)
}
}
func TestRegisterGet(t *testing.T) {
t.Parallel()
ab := authboss.New()
responder := &mocks.Responder{}
ab.Config.Core.Responder = responder
a := &Register{ab}
if err := a.Get(nil, nil); err != nil {
t.Error(err)
}
if responder.Page != PageRegister {
t.Error("wanted login page, got:", responder.Page)
}
if responder.Status != http.StatusOK {
t.Error("wanted ok status, got:", responder.Status)
}
}
type testHarness struct {
reg *Register
ab *authboss.Authboss
bodyReader *mocks.BodyReader
responder *mocks.Responder
redirector *mocks.Redirector
session *mocks.ClientStateRW
storer *mocks.ServerStorer
}
func testSetup() *testHarness {
harness := &testHarness{}
harness.ab = authboss.New()
harness.bodyReader = &mocks.BodyReader{}
harness.redirector = &mocks.Redirector{}
harness.responder = &mocks.Responder{}
harness.session = mocks.NewClientRW()
harness.storer = mocks.NewServerStorer()
harness.ab.Paths.RegisterOK = "/ok"
harness.ab.Config.Core.BodyReader = harness.bodyReader
harness.ab.Config.Core.Logger = mocks.Logger{}
harness.ab.Config.Core.Responder = harness.responder
harness.ab.Config.Core.Redirector = harness.redirector
harness.ab.Config.Storage.SessionState = harness.session
harness.ab.Config.Storage.Server = harness.storer
harness.reg = &Register{harness.ab}
return harness
}
func TestRegisterPostSuccess(t *testing.T) {
t.Parallel()
setupMore := func(harness *testHarness) *testHarness {
harness.ab.Modules.RegisterPreserveFields = []string{"email", "another"}
harness.bodyReader.Return = mocks.ArbValues{
Values: map[string]string{
"email": "[email protected]",
"password": "hello world",
"another": "value",
},
}
return harness
}
t.Run("normal", func(t *testing.T) {
t.Parallel()
h := setupMore(testSetup())
r := mocks.Request("POST")
resp := httptest.NewRecorder()
w := h.ab.NewResponse(resp)
if err := h.reg.Post(w, r); err != nil {
t.Error(err)
}
user, ok := h.storer.Users["[email protected]"]
if !ok {
t.Error("user was not persisted in the DB")
}
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte("hello world")); err != nil {
t.Error("password was not properly encrypted:", err)
}
if user.Arbitrary["another"] != "value" {
t.Error("arbitrary values not saved")
}
if h.session.ClientValues[authboss.SessionKey] != "[email protected]" {
t.Error("user should have been logged in:", h.session.ClientValues)
}
if resp.Code != http.StatusTemporaryRedirect {
t.Error("code was wrong:", resp.Code)
}
if h.redirector.Options.RedirectPath != "/ok" {
t.Error("redirect path was wrong:", h.redirector.Options.RedirectPath)
}
})
t.Run("handledAfter", func(t *testing.T) {
t.Parallel()
h := setupMore(testSetup())
var afterCalled bool
h.ab.Events.After(authboss.EventRegister, func(w http.ResponseWriter, r *http.Request, handled bool) (bool, error) {
w.WriteHeader(http.StatusTeapot)
afterCalled = true
return true, nil
})
r := mocks.Request("POST")
resp := httptest.NewRecorder()
w := h.ab.NewResponse(resp)
if err := h.reg.Post(w, r); err != nil {
t.Error(err)
}
user, ok := h.storer.Users["[email protected]"]
if !ok {
t.Error("user was not persisted in the DB")
}
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte("hello world")); err != nil {
t.Error("password was not properly encrypted:", err)
}
if val, ok := h.session.ClientValues[authboss.SessionKey]; ok {
t.Error("user should not have been logged in:", val)
}
if resp.Code != http.StatusTeapot {
t.Error("code was wrong:", resp.Code)
}
if !afterCalled {
t.Error("the after handler should have been called")
}
})
}
func TestRegisterPostValidationFailure(t *testing.T) {
t.Parallel()
h := testSetup()
// Ensure the below is sorted, the sort normally happens in Init() that we don't call
h.ab.Modules.RegisterPreserveFields = []string{"another", "email"}
h.bodyReader.Return = mocks.ArbValues{
Values: map[string]string{
"email": "[email protected]",
"password": "hello world",
"another": "value",
},
Errors: []error{
errors.New("bad password"),
},
}
r := mocks.Request("POST")
resp := httptest.NewRecorder()
w := h.ab.NewResponse(resp)
if err := h.reg.Post(w, r); err != nil {
t.Error(err)
}
if h.responder.Status != http.StatusOK {
t.Error("wrong status:", h.responder.Status)
}
if h.responder.Page != PageRegister {
t.Error("rendered wrong page:", h.responder.Page)
}
errList := h.responder.Data[authboss.DataValidation].(map[string][]string)
if e := errList[""][0]; e != "bad password" {
t.Error("validation error wrong:", e)
}
intfD, ok := h.responder.Data[authboss.DataPreserve]
if !ok {
t.Fatal("there was no preserved data")
}
d := intfD.(map[string]string)
if d["email"] != "[email protected]" {
t.Error("e-mail was not preserved:", d)
} else if d["another"] != "value" {
t.Error("another value was not preserved", d)
} else if _, ok = d["password"]; ok {
t.Error("password was preserved", d)
}
}
func TestRegisterPostUserExists(t *testing.T) {
t.Parallel()
h := testSetup()
// Ensure the below is sorted, the sort normally happens in Init() that we don't call
h.ab.Modules.RegisterPreserveFields = []string{"another", "email"}
h.storer.Users["[email protected]"] = &mocks.User{}
h.bodyReader.Return = mocks.ArbValues{
Values: map[string]string{
"email": "[email protected]",
"password": "hello world",
"another": "value",
},
}
r := mocks.Request("POST")
resp := httptest.NewRecorder()
w := h.ab.NewResponse(resp)
if err := h.reg.Post(w, r); err != nil {
t.Error(err)
}
if h.responder.Status != http.StatusOK {
t.Error("wrong status:", h.responder.Status)
}
if h.responder.Page != PageRegister {
t.Error("rendered wrong page:", h.responder.Page)
}
errList := h.responder.Data[authboss.DataValidation].(map[string][]string)
if e := errList[""][0]; e != "user already exists" {
t.Error("validation error wrong:", e)
}
intfD, ok := h.responder.Data[authboss.DataPreserve]
if !ok {
t.Fatal("there was no preserved data")
}
d := intfD.(map[string]string)
if d["email"] != "[email protected]" {
t.Error("e-mail was not preserved:", d)
} else if d["another"] != "value" {
t.Error("another value was not preserved", d)
} else if _, ok = d["password"]; ok {
t.Error("password was preserved", d)
}
}
func TestHasString(t *testing.T) {
t.Parallel()
strs := []string{"b", "c", "d", "e"}
if !hasString(strs, "b") {
t.Error("should have a")
}
if !hasString(strs, "e") {
t.Error("should have d")
}
if hasString(strs, "a") {
t.Error("should not have a")
}
if hasString(strs, "f") {
t.Error("should not have f")
}
}