forked from mozilla/persona
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstalled-mysql-test.js
executable file
·409 lines (372 loc) · 10.9 KB
/
stalled-mysql-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
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
#!/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');
if (process.env['NODE_ENV'] != 'test_mysql') process.exit(0);
const assert = require('assert'),
vows = require('vows'),
start_stop = require('./lib/start-stop.js'),
wsapi = require('./lib/wsapi.js'),
temp = require('temp'),
fs = require('fs'),
jwcrypto = require('jwcrypto'),
path = require('path');
var suite = vows.describe('forgotten-email');
require("jwcrypto/lib/algs/ds");
require("jwcrypto/lib/algs/rs");
// disable vows (often flakey?) async error behavior
suite.options.error = false;
// let's reduce the amount of time allowed for queries, so that
// we get a faster failure and tests run quicker
process.env['MAX_QUERY_TIME_MS'] = 250;
// and let's instruct children to pretend as if the driver is
// stalled if a file exists
var stallFile = temp.path({suffix: '.stall'});
process.env['STALL_MYSQL_WHEN_PRESENT'] = stallFile;
start_stop.addStartupBatches(suite);
// ever time a new token is sent out, let's update the global
// var 'token'
var token = undefined;
function addStallDriverBatch(stall) {
suite.addBatch({
"changing driver state": {
topic: function() {
if (stall) fs.writeFileSync(stallFile, "");
else fs.unlinkSync(stallFile);
// After changing the file which indicates to child
// processes whether the driver should simulate a stalled
// state or not, we need to wait for them to detect the
// change. because we use `fs.watchFile()` on a short poll,
// this should be nearly instantaneous. 300ms is a magic number
// which is hoped to allow plenty of time even on a loaded
// machine
setTimeout(this.callback, 300);
},
"completes": function(err, r) { }
}
});
}
// first stall mysql
addStallDriverBatch(true);
// call session context once to populate CSRF stuff in the
// wsapi client lib
suite.addBatch({
"get context": {
topic: wsapi.get('/wsapi/session_context'),
"works" : function(err, r) {
assert.isNull(err);
}
}
});
// now try all apis that can be excercised without further setup
suite.addBatch({
"ping": {
topic: wsapi.get('/wsapi/ping', {}),
"fails with 500 when db is stalled": function(err, r) {
assert.strictEqual(r.code, 500);
}
},
"address_info": {
topic: wsapi.get('/wsapi/address_info', {
email: '[email protected]'
}),
"works": function(err, r) {
// address info with a primary address doesn't need db access.
assert.strictEqual(r.code, 200);
}
},
"address_info": {
topic: wsapi.get('/wsapi/address_info', {
email: '[email protected]'
}),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"have_email": {
topic: wsapi.get('/wsapi/have_email', {
email: '[email protected]'
}),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"authenticate_user": {
topic: wsapi.post('/wsapi/authenticate_user', {
email: '[email protected]',
pass: 'oogabooga',
ephemeral: false
}),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"complete_email_confirmation": {
topic: wsapi.post('/wsapi/complete_email_confirmation', {
token: 'bogusbogusbogusbogusbogusbogusbogusbogusbogusbog'
}),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"complete_user_creation": {
topic: wsapi.post('/wsapi/complete_user_creation', {
token: 'bogusbogusbogusbogusbogusbogusbogusbogusbogusbog',
pass: 'alsobogus'
}),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"email_for_token": {
topic: wsapi.get('/wsapi/email_for_token', {
token: 'bogusbogusbogusbogusbogusbogusbogusbogusbogusbog'
}),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"stage_user": {
topic: wsapi.post('/wsapi/stage_user', {
email: '[email protected]',
pass: 'a_password',
site: 'https://whatev.er'
}),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
}
});
// now unstall the driver, we'll create an account and sign in in
// order to test the behavior of the remaining APIs when the database
// is stalled
addStallDriverBatch(false);
var token = undefined;
suite.addBatch({
"ping": {
topic: wsapi.get('/wsapi/ping', {}),
"works when database is unstalled": function(err, r) {
// address info with a primary address doesn't need db access.
assert.strictEqual(r.code, 200);
}
}
});
suite.addBatch({
"account staging": {
topic: wsapi.post('/wsapi/stage_user', {
email: "[email protected]",
pass: 'a_password',
site: 'http://fakesite.com'
}),
"works": function(err, r) {
assert.equal(r.code, 200);
}
}
});
suite.addBatch({
"a token": {
topic: function() {
start_stop.waitForToken(this.callback);
},
"is obtained": function (t) {
assert.strictEqual(typeof t, 'string');
},
"setting password": {
topic: function(token) {
wsapi.post('/wsapi/complete_user_creation', {
token: token
}).call(this);
},
"works just fine": function(err, r) {
assert.equal(r.code, 200);
}
}
}
});
// re-stall mysql
addStallDriverBatch(true);
// test remaining wsapis
suite.addBatch({
"ping": {
topic: wsapi.get('/wsapi/ping', { }),
"fails": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"account_cancel": {
topic: wsapi.post('/wsapi/account_cancel', { }),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"cert_key": {
topic: wsapi.post('/wsapi/cert_key', {
email: "[email protected]",
pubkey: JSON.stringify("bogusbogusbogusbogusbogusbogusbogusbogusbogusbogusbogus"),
ephemeral: false
}),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"email_addition_status": {
topic: wsapi.get('/wsapi/email_addition_status', {
email: "[email protected]"
}),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"list_emails": {
topic: wsapi.get('/wsapi/list_emails', {}),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"remove_email": {
topic: wsapi.post('/wsapi/remove_email', {
email: "[email protected]"
}),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"session_context": {
topic: wsapi.get('/wsapi/session_context', { }),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"stage_email": {
topic: wsapi.post('/wsapi/stage_email', {
email: "[email protected]",
pass: 'a_password',
site: "https://foo.com"
}),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"update_password": {
topic: wsapi.post('/wsapi/update_password', {
oldpass: "oldpassword",
newpass: "newpassword"
}),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"user_creation_status": {
topic: wsapi.get('/wsapi/user_creation_status', {
email: "[email protected]"
}),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
}
});
// now let's test apis that require an assertion, and only after verifying
// that, hit the database
const TEST_DOMAIN = 'example.domain',
TEST_EMAIL = 'testuser@' + TEST_DOMAIN,
TEST_ORIGIN = 'http://127.0.0.1:10002',
TEST_FIRST_ACCT = '[email protected]';
var g_keypair, g_cert, g_assertion;
suite.addBatch({
"generating a keypair": {
topic: function() {
jwcrypto.generateKeypair({algorithm: "DS", keysize:256}, this.callback);
},
"succeeds": function(err, r) {
assert.isObject(r);
assert.isObject(r.publicKey);
assert.isObject(r.secretKey);
g_keypair = r;
}
}
});
var g_privKey = jwcrypto.loadSecretKey(
require('fs').readFileSync(
path.join(__dirname, '..', 'example', 'primary', 'sample.privatekey')));
suite.addBatch({
"generting a certificate": {
topic: function() {
var domain = process.env['SHIMMED_DOMAIN'];
var expiration = new Date();
expiration.setTime(new Date().valueOf() + 60 * 60 * 1000);
jwcrypto.cert.sign({publicKey: g_keypair.publicKey, principal: {email: TEST_EMAIL}},
{expiresAt: expiration, issuedAt: new Date(), issuer: TEST_DOMAIN},
null, g_privKey, this.callback);
},
"works swimmingly": function(err, cert) {
g_cert = cert;
assert.isString(cert);
assert.lengthOf(cert.split('.'), 3);
}
}
});
suite.addBatch({
"generating an assertion": {
topic: function() {
var self = this;
var expirationDate = new Date(new Date().getTime() + (2 * 60 * 1000));
jwcrypto.assertion.sign({}, {audience: TEST_ORIGIN, expiresAt: expirationDate},
g_keypair.secretKey, function(err, assertion) {
self.callback(err,
err ? undefined : jwcrypto.cert.bundle([g_cert], assertion));
});
},
"succeeds": function(err, r) {
assert.isNull(err);
assert.isString(r);
g_assertion = r;
}
}
});
// finally! we have our assertion in g_assertion
suite.addBatch({
"add_email_with_assertion": {
topic: function() {
wsapi.post('/wsapi/add_email_with_assertion', {
assertion: g_assertion
}).call(this);
},
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"auth_with_assertion": {
topic: function() {
wsapi.post('/wsapi/auth_with_assertion', {
assertion: g_assertion,
ephemeral: true
}).call(this);
},
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
},
"create_account_with_assertion": {
topic: function() {
wsapi.post('/wsapi/create_account_with_assertion', {
assertion: g_assertion
}).call(this);
},
"fails with 404": function(err, r) {
assert.strictEqual(r.code, 404);
}
},
"logout": { // logout needs the database too
topic: wsapi.post('/wsapi/logout', { }),
"fails with 503": function(err, r) {
assert.strictEqual(r.code, 503);
}
}
});
// finally, unblock mysql so we can shut down
addStallDriverBatch(false);
start_stop.addShutdownBatches(suite);
// run or export the suite.
if (process.argv[1] === __filename) suite.run();
else suite.export(module);