This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
test-reset-password.js
344 lines (293 loc) · 10.1 KB
/
test-reset-password.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
'use strict';
var assert = require('assert');
var cheerio = require('cheerio');
var request = require('supertest');
var uuid = require('uuid');
var helpers = require('../helpers');
var SpaRootFixture = require('../fixtures/spa-root-fixture');
function requestResetPage(app, sptoken) {
var config = app.get('stormpathConfig');
return request(app)
.get(config.web.changePassword.uri + (sptoken ? ('?sptoken=' + sptoken) : ''));
}
function assertResetFormExists(res) {
var $ = cheerio.load(res.text);
// Assert that the form was rendered.
assert.equal($('input[name="password"]').length, 1);
assert.equal($('input[name="passwordAgain"]').length, 1);
}
function assertPasswordMismatchError(res) {
var $ = cheerio.load(res.text);
// Assert that the error was shown.
assert($('.alert-danger').html().match(/Passwords do not match/));
}
function assertPasswordPolicyError(res) {
var $ = cheerio.load(res.text);
// Assert that the error was shown.
assert($('.alert-danger').html().match(/Account password minimum length not satisfied/));
}
function assertPasswordNotConfirmedError(res) {
var $ = cheerio.load(res.text);
// Assert that the error was shown.
assert($('.alert-danger').html().match(/Password is required/));
}
describe('resetPassword', function () {
var defaultExpressApp;
var passwordResetToken;
var stormpathAccount;
var stormpathApplication;
var stormpathClient;
before(function (done) {
stormpathClient = helpers.createClient();
helpers.createApplication(stormpathClient, function (err, app) {
if (err) {
return done(err);
}
stormpathApplication = app;
app.createAccount(helpers.newUser(), function (err, account) {
if (err) {
return done(err);
}
stormpathAccount = account;
app.sendPasswordResetEmail({ email: account.email }, function (err, tokenResource) {
if (err) {
return done(err);
}
passwordResetToken = tokenResource.href.match(/\/([^\/]+)$/)[1];
defaultExpressApp = helpers.createStormpathExpressApp({
application: stormpathApplication
});
defaultExpressApp.on('stormpath.ready', done);
});
});
});
});
after(function (done) {
helpers.destroyApplication(stormpathApplication, done);
});
afterEach(function (done) {
helpers.setPasswordResetStatus(stormpathApplication, 'ENABLED', function (err) {
done(err);
});
});
it('should disable password reset functionality if the directory has it disabled', function (done) {
helpers.setPasswordResetStatus(stormpathApplication, 'DISABLED', function (err) {
if (err) {
return done(err);
}
var app = helpers.createStormpathExpressApp({
application: {
href: stormpathApplication.href
}
});
app.on('stormpath.ready', function () {
requestResetPage(app)
.expect(404)
.end(done);
});
});
});
it('should redirect to the /forgot view is no sptoken is given', function (done) {
var config = defaultExpressApp.get('stormpathConfig');
requestResetPage(defaultExpressApp)
.expect(302)
.expect('Location', config.web.forgotPassword.uri)
.end(done);
});
it('should redirect to the /forgot view is the sptoken is invalid, with an invlaid token status', function (done) {
var config = defaultExpressApp.get('stormpathConfig');
requestResetPage(defaultExpressApp, 'invalid token')
.expect(302)
.expect('Location', config.web.forgotPassword.uri + '?status=invalid_sptoken')
.end(done);
});
describe('with an invalid token', function () {
describe('as HTML', function () {
it('should redirect me to /forgot?status=invalid_sptoken', function (done) {
var config = defaultExpressApp.get('stormpathConfig');
requestResetPage(defaultExpressApp, 'invalid token')
.expect(302)
.expect('Location', config.web.forgotPassword.uri + '?status=invalid_sptoken')
.end(done);
});
});
describe('as JSON', function () {
it('should respond with an error object', function (done) {
var config = defaultExpressApp.get('stormpathConfig');
request(defaultExpressApp)
.get(config.web.changePassword.uri + '?sptoken=foo')
.set('Accept', 'application/json')
.expect(404, {
message: 'This password reset request does not exist. Please request a new password reset.',
status: 404
}, done);
});
});
});
describe('with a valid token', function () {
var passwordResetToken;
before(function (done) {
stormpathApplication.sendPasswordResetEmail({ email: stormpathAccount.email }, function (err, tokenResource) {
if (err) {
return done(err);
}
passwordResetToken = tokenResource.href.match(/\/([^\/]+)$/)[1];
done();
});
});
it('should render the password reset form if the token is valid', function (done) {
requestResetPage(defaultExpressApp, passwordResetToken)
.expect(200)
.end(function (err, res) {
assertResetFormExists(res);
done();
});
});
it('should allow me to verify the token with the JSON API', function (done) {
var config = defaultExpressApp.get('stormpathConfig');
request(defaultExpressApp)
.get(config.web.changePassword.uri + '?sptoken=' + passwordResetToken)
.set('Accept', 'application/json')
.expect(200, '', done);
});
});
it('should error if the passwords do not match', function (done) {
var config = defaultExpressApp.get('stormpathConfig');
request(defaultExpressApp)
.post(config.web.changePassword.uri)
.type('form')
.send({
password: 'a',
passwordAgain: 'b',
sptoken: passwordResetToken
})
.expect(200)
.end(function (err, res) {
assertPasswordMismatchError(res);
done();
});
});
it('should error if the password is too short (does not meet policy requirements)', function (done) {
var config = defaultExpressApp.get('stormpathConfig');
request(defaultExpressApp)
.post(config.web.changePassword.uri)
.type('form')
.send({
password: 'a',
passwordAgain: 'a',
sptoken: passwordResetToken
})
.expect(200)
.end(function (err, res) {
assertPasswordPolicyError(res);
done();
});
});
it('should error if the the password is not entered twice', function (done) {
var config = defaultExpressApp.get('stormpathConfig');
request(defaultExpressApp)
.post(config.web.changePassword.uri)
.type('form')
.send({
password: 'a',
sptoken: passwordResetToken
})
.expect(200)
.end(function (err, res) {
assertPasswordNotConfirmedError(res);
done();
});
});
it('should allow me to change the password, with a valid token, and send me to the login page', function (done) {
var config = defaultExpressApp.get('stormpathConfig');
var newPassword = uuid() + uuid().toUpperCase();
request(defaultExpressApp)
.post(config.web.changePassword.uri)
.type('form')
.send({
password: newPassword,
passwordAgain: newPassword,
sptoken: passwordResetToken
})
.expect(302)
.expect('Location', config.web.login.uri + '?status=reset')
.end(function () {
// Assert that the password can be used to login.
stormpathApplication.authenticateAccount({
username: stormpathAccount.username,
password: newPassword
}, function (err) {
assert.ifError(err);
done();
});
});
});
it('should allow me to change the password, with a valid token, via the JSON api', function (done) {
// Need to get another token because we consumed it in the last test
stormpathApplication.sendPasswordResetEmail({ email: stormpathAccount.email }, function (err, tokenResource) {
if (err) {
return done(err);
}
var passwordResetToken = tokenResource.href.match(/\/([^\/]+)$/)[1];
var config = defaultExpressApp.get('stormpathConfig');
var newPassword = uuid() + uuid().toUpperCase();
request(defaultExpressApp)
.post(config.web.changePassword.uri)
.set('Accept', 'application/json')
.send({
password: newPassword,
passwordAgain: newPassword,
sptoken: passwordResetToken
})
.expect(200)
.end(function () {
// Assert that the password can be used to login.
stormpathApplication.authenticateAccount({
username: stormpathAccount.username,
password: newPassword
}, function (err) {
assert.ifError(err);
done();
});
});
});
});
it('should send me to the request-new-link page if i use and already consumed token', function (done) {
// The token was consumed by the previous test, above.
var config = defaultExpressApp.get('stormpathConfig');
request(defaultExpressApp)
.post(config.web.changePassword.uri)
.type('form')
.send({ sptoken: passwordResetToken })
.expect(302)
.expect('Location', config.web.forgotPassword.uri + '?status=invalid_sptoken')
.end(done);
});
describe('if configured with a SPA root', function () {
var spaRootFixture;
before(function (done) {
spaRootFixture = new SpaRootFixture({
application: {
href: stormpathApplication.href
},
web: {
changePassword: {
enabled: true
}
}
});
spaRootFixture.before(done);
});
after(function (done) {
spaRootFixture.after(done);
});
it('should return the SPA root', function (done) {
var app = spaRootFixture.expressApp;
app.on('stormpath.ready', function () {
requestResetPage(app, passwordResetToken)
.expect(200)
.end(spaRootFixture.assertResponse(done));
});
});
});
});