This repository has been archived by the owner on May 10, 2019. It is now read-only.
/
verify-in-different-browser-test.js
executable file
·368 lines (342 loc) · 9.94 KB
/
verify-in-different-browser-test.js
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
#!/usr/bin/env node
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
require('./lib/test_env.js');
const
assert = require('assert'),
vows = require('vows'),
start_stop = require('./lib/start-stop.js'),
wsapi = require('./lib/wsapi.js'),
primary = require('./lib/primary.js');
var suite = vows.describe('verify-in-different-browser');
// start up a pristine server
start_stop.addStartupBatches(suite);
// This test ensures that when email verification of a secondary address
// occurs in a browsing context other than the one that initiated it,
// the user must re-provide their password.
// first we'll need to authenticate a user with an assertion from a
// primary IdP
const TEST_DOMAIN = 'example.domain',
TEST_EMAIL = 'testuser@' + TEST_DOMAIN,
TEST_ORIGIN = 'http://127.0.0.1:10002',
TEST_PASS = 'fakepass',
SECONDARY_EMAIL = 'secondary@notexample.domain',
SECOND_SECONDARY_EMAIL = 'secondsecondary@notexample.domain',
THIRD_SECONDARY_EMAIL = 'thirdsecondary@notexample.domain',
FOURTH_SECONDARY_EMAIL = 'fourthsecondary@notexample.domain';
var primaryUser = new primary({
email: TEST_EMAIL,
domain: TEST_DOMAIN
});
suite.addBatch({
"set things up": {
topic: function() {
primaryUser.setup(this.callback);
},
"works": function() {
// nothing to do here
}
}
});
// first we'll create an account without a password by using
// a primary address.
suite.addBatch({
"generating an assertion": {
topic: function() {
return primaryUser.getAssertion(TEST_ORIGIN, this.callback);
},
"succeeds": function(err, r) {
assert.isString(r);
},
"and logging in with the assertion": {
topic: function(err, assertion) {
wsapi.post('/wsapi/auth_with_assertion', {
assertion: assertion,
ephemeral: true
}).call(this);
},
"succeeds": function(err, r) {
var resp = JSON.parse(r.body);
assert.isObject(resp);
assert.isTrue(resp.success);
}
}
}
});
var token;
// let's add a secondary email to this account
suite.addBatch({
"add a new email address to our account": {
topic: wsapi.post('/wsapi/stage_email', {
email: SECONDARY_EMAIL,
pass: TEST_PASS,
site:'https://fakesite.com'
}),
"succeeds": function(err, r) {
assert.strictEqual(r.code, 200);
},
"and get a token": {
topic: function() {
start_stop.waitForToken(this.callback);
},
"successfully": function (err, t) {
assert.isNull(err);
this._token = t;
assert.strictEqual(typeof t, 'string');
},
"then clearing cookies and completing": {
topic: function() {
wsapi.clearCookies();
wsapi.post('/wsapi/complete_email_confirmation', {
token: this._token
}).call(this);
},
"fails without a password": function(err, r) {
assert.strictEqual(r.code, 401);
},
"but succeeds": {
topic: function() {
wsapi.post('/wsapi/complete_email_confirmation', {
token: this._token,
pass: TEST_PASS
}).call(this);
},
"with one": function(err, r) {
assert.strictEqual(r.code, 200);
}
}
}
}
}
});
// after adding a secondary and setting password, we're password auth'd
suite.addBatch({
"auth_level": {
topic: wsapi.get('/wsapi/session_context'),
"is 'password' after authenticating with password" : function(err, r) {
assert.strictEqual(JSON.parse(r.body).auth_level, 'password');
}
}
});
// we can authenticate with our password
suite.addBatch({
"authenticating with our newly set password" : {
topic: wsapi.post('/wsapi/authenticate_user', {
email: TEST_EMAIL,
pass: TEST_PASS,
ephemeral: false
}),
"works": function(err, r) {
assert.strictEqual(r.code, 200);
}
}
});
// let's add another secondary email, again by confirming the address on
// "a different browser". This time, the server will have to authenticate
// us by pulling our password out of our user record rather than out of
// the stage table.
suite.addBatch({
"add a new email address to our account": {
topic: wsapi.post('/wsapi/stage_email', {
email: SECOND_SECONDARY_EMAIL,
site:'https://fakesite.com'
}),
"succeeds": function(err, r) {
assert.strictEqual(r.code, 200);
},
"and get a token": {
topic: function() {
start_stop.waitForToken(this.callback);
},
"successfully": function (err, t) {
assert.isNull(err);
this._token = t;
assert.strictEqual(typeof t, 'string');
},
"then clearing cookies and completing": {
topic: function() {
wsapi.clearCookies();
wsapi.post('/wsapi/complete_email_confirmation', {
token: this._token
}).call(this);
},
"fails without a password": function(err, r) {
assert.strictEqual(r.code, 401);
},
"but succeeds": {
topic: function() {
wsapi.post('/wsapi/complete_email_confirmation', {
token: this._token,
pass: TEST_PASS
}).call(this);
},
"with one": function(err, r) {
assert.strictEqual(r.code, 200);
}
}
}
}
}
});
// we're password auth'd
suite.addBatch({
"auth_level": {
topic: wsapi.get('/wsapi/session_context'),
"is 'password' after authenticating with password" : function(err, r) {
assert.strictEqual(JSON.parse(r.body).auth_level, 'password');
}
}
});
// we can still authenticate with our password
suite.addBatch({
"authenticating with our newly set password" : {
topic: wsapi.post('/wsapi/authenticate_user', {
email: TEST_EMAIL,
pass: TEST_PASS,
ephemeral: false
}),
"works": function(err, r) {
assert.strictEqual(r.code, 200);
}
}
});
// now we've tested proper restrictions on the add email, flow, how about
// new account creation?
// creating a new account and verifying in "a different browser" requires password
suite.addBatch({
"staging a new account": {
topic: wsapi.post('/wsapi/stage_user', {
email: THIRD_SECONDARY_EMAIL,
pass: TEST_PASS,
site: 'http://fakesite.com:1235'
}),
"succeeds": function(err, r) {
assert.strictEqual(r.code, 200);
assert.strictEqual(JSON.parse(r.body).success, true);
},
"yields a token": {
topic: function() {
start_stop.waitForToken(this.callback);
},
"successfully": function (err, t) {
assert.isNull(err);
this._token = t;
assert.strictEqual(typeof t, 'string');
},
"then clearing cookies and completing": {
topic: function() {
wsapi.clearCookies();
wsapi.post('/wsapi/complete_user_creation', {
token: this._token
}).call(this);
},
"fails without a password": function(err, r) {
assert.strictEqual(r.code, 401);
},
"but succeeds": {
topic: function() {
wsapi.post('/wsapi/complete_user_creation', {
token: this._token,
pass: TEST_PASS
}).call(this);
},
"with one": function(err, r) {
assert.strictEqual(r.code, 200);
}
}
}
}
}
});
// creating a new account and verifying in "the same browser" requires no password
suite.addBatch({
"staging a new account": {
topic: wsapi.post('/wsapi/stage_user', {
email: FOURTH_SECONDARY_EMAIL,
pass: TEST_PASS,
site: 'http://fakesite.com:1235'
}),
"succeeds": function(err, r) {
assert.strictEqual(r.code, 200);
assert.strictEqual(JSON.parse(r.body).success, true);
},
"yields a token": {
topic: function() {
start_stop.waitForToken(this.callback);
},
"successfully": function (err, t) {
assert.isNull(err);
this._token = t;
assert.strictEqual(typeof t, 'string');
},
"and completion with only a token": {
topic: function() {
wsapi.post('/wsapi/complete_user_creation', {
token: this._token
}).call(this);
},
"succeeds": function(err, r) {
assert.strictEqual(r.code, 200);
}
}
}
}
});
suite.addBatch({
"authentication with first email": {
topic: wsapi.post('/wsapi/authenticate_user', {
email: TEST_EMAIL,
pass: TEST_PASS,
ephemeral: false
}),
"works": function(err, r) {
assert.strictEqual(r.code, 200);
},
},
"authentication with second email": {
topic: wsapi.post('/wsapi/authenticate_user', {
email: SECONDARY_EMAIL,
pass: TEST_PASS,
ephemeral: false
}),
"works": function(err, r) {
assert.strictEqual(r.code, 200);
}
},
"authentication with third email": {
topic: wsapi.post('/wsapi/authenticate_user', {
email: SECOND_SECONDARY_EMAIL,
pass: TEST_PASS,
ephemeral: false
}),
"works": function(err, r) {
assert.strictEqual(r.code, 200);
}
},
"authentication with fourth email": {
topic: wsapi.post('/wsapi/authenticate_user', {
email: THIRD_SECONDARY_EMAIL,
pass: TEST_PASS,
ephemeral: false
}),
"works": function(err, r) {
assert.strictEqual(r.code, 200);
}
},
"authentication with fifth email": {
topic: wsapi.post('/wsapi/authenticate_user', {
email: FOURTH_SECONDARY_EMAIL,
pass: TEST_PASS,
ephemeral: false
}),
"works": function(err, r) {
assert.strictEqual(r.code, 200);
}
}
});
// shut the server down and cleanup
start_stop.addShutdownBatches(suite);
// run or export the suite.
if (process.argv[1] === __filename) suite.run();
else suite.export(module);