1 |
|
2 |
|
3 |
|
4 |
|
5 | var request = require('supertest'), should = require('should'), express = require('express'), bodyParser = require('body-parser'),
|
6 | app = express(), _ = require('lodash'), async = require('async'), smtp = require('smtp-tester'),
|
7 | r = request(app), mail, fs = require('fs'),
|
8 | activator = require('../lib/activator'), templates = __dirname+'/resources',
|
9 | mailer = require('nodemailer'), jwt = require('jsonwebtoken'), SIGNKEY = "1234567890abcdefghijklmn",
|
10 | USERS = {
|
11 | "1": {id:"1",childObject:{id:"1"},email:"me@you.com",password:"1234",activated:false}
|
12 | }, lang,
|
13 | users,
|
14 | quote = function (regex) {
|
15 |
|
16 | var ret = regex.replace(/([()[{*+.$^\\/\\|?])/g, '\\$1');
|
17 |
|
18 | return(ret);
|
19 | },
|
20 | bodyMatcher = function (body,matcher) {
|
21 |
|
22 | var ret = body.replace(/[\r\n]+/g,'').match(new RegExp(quote(matcher.replace(/[\r\n]+/g,'')).replace(/<%=[^%]+%>/g,'.*')));
|
23 |
|
24 | return(ret);
|
25 | },
|
26 | changeResetTime = function (token,diff) {
|
27 | var original = jwt.decode(token), code;
|
28 | original.iat = original.iat - 100*60;
|
29 | code = jwt.sign(original,SIGNKEY,{algorithm:"HS256"});
|
30 | return code;
|
31 | },
|
32 | userModel = {
|
33 | _find: function (login,cb) {
|
34 | var found = null;
|
35 | if (!login) {
|
36 | cb("nologin");
|
37 | } else if (users[login]) {
|
38 | cb(null,_.cloneDeep(users[login]));
|
39 | } else {
|
40 | _.each(users,function (val) {
|
41 | if (val && val.email === login) {
|
42 | found = val;
|
43 | return(false);
|
44 | }
|
45 | });
|
46 | cb(null,_.cloneDeep(found));
|
47 | }
|
48 | },
|
49 | find: function() {
|
50 | this._find.apply(this,arguments);
|
51 | },
|
52 | activate: function (id,cb) {
|
53 | if (id && users[id]) {
|
54 | users[id].activated = true;
|
55 | cb(null);
|
56 | } else {
|
57 | cb(404);
|
58 | }
|
59 | },
|
60 | setPassword: function (id,password,cb) {
|
61 | if (id && users[id]) {
|
62 | users[id].password = password;
|
63 | cb(null);
|
64 | } else {
|
65 | cb(404);
|
66 | }
|
67 | }
|
68 | },
|
69 | reset = function () {
|
70 | users = _.cloneDeep(USERS);
|
71 | if (mail && mail.removeAll) {
|
72 | mail.removeAll();
|
73 | }
|
74 | },
|
75 | userModelEmail = _.extend({},userModel,{find: function (login,cb) {
|
76 | this._find(login,function (err,res) {
|
77 | if (res && res.email) {
|
78 | res.funny = res.email;
|
79 | res.childObject = {
|
80 | funny: res.email
|
81 | };
|
82 | delete res.email;
|
83 | }
|
84 | cb(err,res);
|
85 | });
|
86 | }
|
87 | }),
|
88 | MAILPORT = 30111,
|
89 | url = "smtp://localhost:"+MAILPORT+"/activator.net",
|
90 | maileropts = { host: "localhost", port:MAILPORT, name: "activator.net", secureConnection: false },
|
91 | from = "test@activator.net",
|
92 | createUser = function (req,res,next) {
|
93 | users["2"] = {id:"2",childObject:{id:"2"},email:"you@foo.com",password:"5678"};
|
94 | req.activator = {id:"2",body:"2"};
|
95 | next();
|
96 | },
|
97 | splitTemplate = function (path) {
|
98 |
|
99 | var content = fs.readFileSync(path,'utf8');
|
100 | content = content.replace(/\r\n/g,'\n');
|
101 |
|
102 | content = content.match(/^([^\n]*)\n[^\n]*\n((.|\n)*)/m);
|
103 | return(content);
|
104 | },
|
105 | genHandler = function(email,path,data,cb) {
|
106 | return function(rcpt,msgid,content) {
|
107 | var url, ret, re = new RegExp('http:\\/\\/\\S*'+path.replace(/\//g,'\\/')+'\\?code=([^\\s\\&]+)\\&email=(\\S+)\\&user=([^\\s\\&]+)'),
|
108 | subject = data.subject;
|
109 | rcpt.should.eql(email);
|
110 |
|
111 | should.exist(content.data);
|
112 | content.headers.subject.should.eql(subject);
|
113 |
|
114 | if (data.text) {
|
115 | should.exist(content.text);
|
116 | should.exist(bodyMatcher(content.text,data.text));
|
117 | url = content.text.match(re);
|
118 | should.exist(url);
|
119 |
|
120 | url.length.should.eql(4);
|
121 | ret = _.zipObject(["path","code","email","user"],url);
|
122 | ret.email.should.eql(email);
|
123 | }
|
124 | if (data.html) {
|
125 | should.exist(content.html);
|
126 | should.exist(bodyMatcher(content.html,data.html));
|
127 | url = content.html.match(re);
|
128 | should.exist(url);
|
129 |
|
130 | url.length.should.eql(4);
|
131 | ret = _.zipObject(["path","code","email","user"],url);
|
132 | ret.email.should.eql(email);
|
133 | }
|
134 | if (!ret) {
|
135 | url = (content.text||content.html).match(re);
|
136 | should.exist(url);
|
137 |
|
138 | url.length.should.eql(4);
|
139 | ret = _.zipObject(["path","code","email","user"],url);
|
140 | ret.email.should.eql(email);
|
141 | }
|
142 | ret.content = content;
|
143 | cb(null,ret);
|
144 | };
|
145 | },
|
146 | aHandler = function (email,data,cb) {
|
147 | if (!cb) {
|
148 | cb = data;
|
149 | data = {};
|
150 | }
|
151 |
|
152 | data.subject = data.subject || "Activate Email";
|
153 | return genHandler(email,"/activate/my/account",data,cb);
|
154 | },
|
155 | rHandler = function(email,data,cb) {
|
156 | if (!cb) {
|
157 | cb = data;
|
158 | data = {};
|
159 | }
|
160 |
|
161 | data.subject = data.subject || "Password Reset Email";
|
162 | return genHandler(email,"/reset/my/password",data,cb);
|
163 | },
|
164 | createActivateHandler = function (req,res,next) {
|
165 |
|
166 | res.set("activator","createActivateHandler");
|
167 | res.status(req.activator.code).send(req.activator.message);
|
168 | },
|
169 | completeActivateHandler = function (req,res,next) {
|
170 |
|
171 | res.set("activator","completeActivateHandler");
|
172 | res.status(req.activator.code).send(req.activator.message);
|
173 | },
|
174 | createResetHandler = function (req,res,next) {
|
175 | var msg = req.activator.message;
|
176 |
|
177 | res.set("activator","createResetHandler");
|
178 | if (msg === null || msg === undefined || typeof(msg) === "number") {
|
179 | res.sendStatus(req.activator.code);
|
180 | } else {
|
181 | res.status(req.activator.code).send(req.activator.message);
|
182 | }
|
183 | },
|
184 | completeResetHandler = function (req,res,next) {
|
185 |
|
186 | res.set("activator","completeResetHandler");
|
187 | res.status(req.activator.code).send(req.activator.message);
|
188 | },
|
189 | setLang = function (req,res,next) {
|
190 | if (lang) {
|
191 | req.lang = lang;
|
192 | }
|
193 | next();
|
194 | },
|
195 | allTests;
|
196 |
|
197 |
|
198 | before(function(){
|
199 | debugger;
|
200 | });
|
201 |
|
202 | before(function(){
|
203 | reset();
|
204 | });
|
205 |
|
206 | allTests = function () {
|
207 | beforeEach(reset);
|
208 | describe('activate', function(){
|
209 | it('should send 500 for user property not added', function(done){
|
210 | r.post('/usersbad').expect(500,done);
|
211 | });
|
212 | describe('auth header', function(){
|
213 | it('should fail for known user but bad code', function(done){
|
214 | var email, handler;
|
215 | async.waterfall([
|
216 | function (cb) {r.post('/users').expect(201,"2",cb);},
|
217 | function (res,cb) {
|
218 | email = users["2"].email;
|
219 | handler = aHandler(email,cb);
|
220 | mail.bind(email,handler);
|
221 | },
|
222 | function (res,cb) {
|
223 | mail.unbind(email,handler);
|
224 | r.put('/users/'+res.user+'/activate').set({'authorization':"Bearer asasqsqsqs"}).expect(400,'invalidcode',cb);
|
225 | }
|
226 | ],done);
|
227 | });
|
228 | it('should fail for known user but bad code with handler', function(done){
|
229 | var email, handler;
|
230 | async.waterfall([
|
231 | function (cb) {r.post('/usersnext').expect('activator','createActivateHandler').expect(201,cb);},
|
232 | function (res,cb) {
|
233 | email = users["2"].email;
|
234 | handler = aHandler(email,cb);
|
235 | mail.bind(email,handler);
|
236 | },
|
237 | function (res,cb) {
|
238 | mail.unbind(email,handler);
|
239 | r.put('/usersnext/'+res.user+'/activate').set({'authorization':"Bearer asasqsqsqs"}).expect('activator','completeActivateHandler').expect(400,cb);
|
240 | }
|
241 | ],done);
|
242 | });
|
243 | it('should succeed for known user', function(done){
|
244 | var email, handler;
|
245 | async.waterfall([
|
246 | function (cb) {r.post('/users').expect(201,cb);},
|
247 | function (res,cb) {
|
248 | res.text.should.equal("2");
|
249 | email = users["2"].email;
|
250 | handler = aHandler(email,cb);
|
251 | mail.bind(email,handler);
|
252 | },
|
253 | function (res,cb) {
|
254 | mail.unbind(email,handler);
|
255 | r.put('/users/'+res.user+'/activate').set({"authorization":"Bearer "+res.code}).expect(200,cb);
|
256 | }
|
257 | ],done);
|
258 | });
|
259 | it('should succeed for known user with handler', function(done){
|
260 | var email, handler;
|
261 | async.waterfall([
|
262 | function (cb) {r.post('/usersnext').expect('activator','createActivateHandler').expect(201,cb);},
|
263 | function (res,cb) {
|
264 | res.text.should.equal("2");
|
265 | email = users["2"].email;
|
266 | handler = aHandler(email,cb);
|
267 | mail.bind(email,handler);
|
268 | },
|
269 | function (res,cb) {
|
270 | mail.unbind(email,handler);
|
271 |
|
272 | should(res.content.attachments).undefined();
|
273 | r.put('/usersnext/'+res.user+'/activate').set({"authorization":"Bearer "+res.code}).expect('activator','completeActivateHandler').expect(200,cb);
|
274 | }
|
275 | ],done);
|
276 | });
|
277 | });
|
278 | describe('auth query', function(){
|
279 | it('should fail for known user but bad code', function(done){
|
280 | var email, handler;
|
281 | async.waterfall([
|
282 | function (cb) {r.post('/users').expect(201,"2",cb);},
|
283 | function (res,cb) {
|
284 | email = users["2"].email;
|
285 | handler = aHandler(email,cb);
|
286 | mail.bind(email,handler);
|
287 | },
|
288 | function (res,cb) {
|
289 | mail.unbind(email,handler);
|
290 | r.put('/users/'+res.user+'/activate').type("json").query({authorization:"asasqsqsqs"}).expect(400,'invalidcode',cb);
|
291 | }
|
292 | ],done);
|
293 | });
|
294 | it('should fail for known user but bad code with handler', function(done){
|
295 | var email, handler;
|
296 | async.waterfall([
|
297 | function (cb) {r.post('/usersnext').expect('activator','createActivateHandler').expect(201,cb);},
|
298 | function (res,cb) {
|
299 | email = users["2"].email;
|
300 | handler = aHandler(email,cb);
|
301 | mail.bind(email,handler);
|
302 | },
|
303 | function (res,cb) {
|
304 | mail.unbind(email,handler);
|
305 | r.put('/usersnext/'+res.user+'/activate').type("json").query({authorization:"asasqsqsqs"}).expect('activator','completeActivateHandler').expect(400,cb);
|
306 | }
|
307 | ],done);
|
308 | });
|
309 | it('should succeed for known user', function(done){
|
310 | var email, handler;
|
311 | async.waterfall([
|
312 | function (cb) {r.post('/users').expect(201,cb);},
|
313 | function (res,cb) {
|
314 | res.text.should.equal("2");
|
315 | email = users["2"].email;
|
316 | handler = aHandler(email,cb);
|
317 | mail.bind(email,handler);
|
318 | },
|
319 | function (res,cb) {
|
320 | mail.unbind(email,handler);
|
321 | r.put('/users/'+res.user+'/activate').type("json").query({authorization:res.code}).expect(200,cb);
|
322 | }
|
323 | ],done);
|
324 | });
|
325 | it('should succeed for known user with handler', function(done){
|
326 | var email, handler;
|
327 | async.waterfall([
|
328 | function (cb) {r.post('/usersnext').expect('activator','createActivateHandler').expect(201,cb);},
|
329 | function (res,cb) {
|
330 | res.text.should.equal("2");
|
331 | email = users["2"].email;
|
332 | handler = aHandler(email,cb);
|
333 | mail.bind(email,handler);
|
334 | },
|
335 | function (res,cb) {
|
336 | mail.unbind(email,handler);
|
337 |
|
338 | should(res.content.attachments).undefined();
|
339 | r.put('/usersnext/'+res.user+'/activate').type("json").query({authorization:res.code}).expect('activator','completeActivateHandler').expect(200,cb);
|
340 | }
|
341 | ],done);
|
342 | });
|
343 | });
|
344 | describe('auth body', function(){
|
345 | it('should fail for known user but bad code', function(done){
|
346 | var email, handler;
|
347 | async.waterfall([
|
348 | function (cb) {r.post('/users').expect(201,"2",cb);},
|
349 | function (res,cb) {
|
350 | email = users["2"].email;
|
351 | handler = aHandler(email,cb);
|
352 | mail.bind(email,handler);
|
353 | },
|
354 | function (res,cb) {
|
355 | mail.unbind(email,handler);
|
356 | r.put('/users/'+res.user+'/activate').type("json").send({authorization:"asasqsqsqs"}).expect(400,'invalidcode',cb);
|
357 | }
|
358 | ],done);
|
359 | });
|
360 | it('should fail for known user but bad code with handler', function(done){
|
361 | var email, handler;
|
362 | async.waterfall([
|
363 | function (cb) {r.post('/usersnext').expect('activator','createActivateHandler').expect(201,cb);},
|
364 | function (res,cb) {
|
365 | email = users["2"].email;
|
366 | handler = aHandler(email,cb);
|
367 | mail.bind(email,handler);
|
368 | },
|
369 | function (res,cb) {
|
370 | mail.unbind(email,handler);
|
371 | r.put('/usersnext/'+res.user+'/activate').type("json").send({authorization:"asasqsqsqs"}).expect('activator','completeActivateHandler').expect(400,cb);
|
372 | }
|
373 | ],done);
|
374 | });
|
375 | it('should succeed for known user', function(done){
|
376 | var email, handler;
|
377 | async.waterfall([
|
378 | function (cb) {r.post('/users').expect(201,cb);},
|
379 | function (res,cb) {
|
380 | res.text.should.equal("2");
|
381 | email = users["2"].email;
|
382 | handler = aHandler(email,cb);
|
383 | mail.bind(email,handler);
|
384 | },
|
385 | function (res,cb) {
|
386 | mail.unbind(email,handler);
|
387 | r.put('/users/'+res.user+'/activate').type("json").send({authorization:res.code}).expect(200,cb);
|
388 | }
|
389 | ],done);
|
390 | });
|
391 | it('should succeed for known user with handler', function(done){
|
392 | var email, handler;
|
393 | async.waterfall([
|
394 | function (cb) {r.post('/usersnext').expect('activator','createActivateHandler').expect(201,cb);},
|
395 | function (res,cb) {
|
396 | res.text.should.equal("2");
|
397 | email = users["2"].email;
|
398 | handler = aHandler(email,cb);
|
399 | mail.bind(email,handler);
|
400 | },
|
401 | function (res,cb) {
|
402 | mail.unbind(email,handler);
|
403 |
|
404 | should(res.content.attachments).undefined();
|
405 | r.put('/usersnext/'+res.user+'/activate').type("json").send({authorization:res.code}).expect('activator','completeActivateHandler').expect(200,cb);
|
406 | }
|
407 | ],done);
|
408 | });
|
409 | });
|
410 | });
|
411 | describe('password reset', function(){
|
412 | it('should send 400 for no email or ID passed', function(done){
|
413 | r.post("/passwordreset").expect(400,done);
|
414 | });
|
415 | it('should send 400 for no email or ID passed with handler', function(done){
|
416 | r.post("/passwordresetnext").expect('activator','createResetHandler').expect(400,done);
|
417 | });
|
418 | it('should send 404 for unknown email or ID', function(done){
|
419 | r.post("/passwordreset").type('json').send({user:"john@john.com"}).expect(404,done);
|
420 | });
|
421 | it('should send 404 for unknown email or ID with handler', function(done){
|
422 | r.post("/passwordresetnext").type('json').send({user:"john@john.com"}).expect('activator','createResetHandler').expect(404,done);
|
423 | });
|
424 | describe('auth header', function(){
|
425 | it('should fail for known email but bad code', function(done){
|
426 | var email = users["1"].email, handler;
|
427 | async.waterfall([
|
428 | function (cb) {
|
429 | r.post('/passwordreset').type('json').send({user:email}).expect(201,cb);},
|
430 | function (res,cb) {
|
431 | handler = rHandler(email,cb);
|
432 | mail.bind(email,handler);},
|
433 | function (res,cb) {
|
434 | mail.unbind(email,handler);
|
435 | r.put('/passwordreset/'+res.user).set({"Authorization":"Bearer asasqsqsqs"}).type("json").send({password:"asasa"}).expect(400,cb);
|
436 | }
|
437 | ],done);
|
438 | });
|
439 | it('should fail for known email but bad code with handler', function(done){
|
440 | var email = users["1"].email, handler;
|
441 | async.waterfall([
|
442 | function (cb) {
|
443 | r.post('/passwordresetnext').type('json').send({user:email}).expect('activator','createResetHandler').expect(201,cb);},
|
444 | function (res,cb) {
|
445 | handler = rHandler(email,cb);
|
446 | mail.bind(email,handler);},
|
447 | function (res,cb) {
|
448 | mail.unbind(email,handler);
|
449 | r.put('/passwordresetnext/'+res.user).set({"Authorization":"Bearer asasqsqsqs"}).type("json").send({password:"asasa"}).expect('activator','completeResetHandler').expect(400,cb);
|
450 | }
|
451 | ],done);
|
452 | });
|
453 | it('should fail for known email with good code but missing new password', function(done){
|
454 | var email = users["1"].email, handler;
|
455 | async.waterfall([
|
456 | function (cb) {r.post('/passwordreset').type('json').send({user:email}).expect(201,cb);},
|
457 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
458 | function (res,cb) {
|
459 | mail.unbind(email,handler);
|
460 | r.put('/passwordreset/'+res.user).type("json").set({Authorization:"Bearer "+res.code}).expect(400,cb);
|
461 | }
|
462 | ],done);
|
463 | });
|
464 | it('should fail for known email with good code but missing new password with handler', function(done){
|
465 | var email = users["1"].email, handler;
|
466 | async.waterfall([
|
467 | function (cb) {r.post('/passwordresetnext').type('json').send({user:email}).expect('activator','createResetHandler').expect(201,cb);},
|
468 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
469 | function (res,cb) {
|
470 | mail.unbind(email,handler);
|
471 | r.put('/passwordresetnext/'+res.user).type("json").set({Authorization:"Bearer "+res.code}).expect('activator','completeResetHandler').expect(400,cb);
|
472 | }
|
473 | ],done);
|
474 | });
|
475 | it('should fail for expired reset code', function(done){
|
476 | var user = users["1"], email = user.email, handler;
|
477 | async.waterfall([
|
478 | function (cb) {r.post('/passwordreset').type('json').send({user:"1"}).expect(201,cb);},
|
479 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
480 | function (res,cb) {
|
481 | mail.unbind(email,handler);
|
482 |
|
483 | var code = changeResetTime(res.code,-100);
|
484 | r.put('/passwordreset/'+res.user).set({"Authorization":"Bearer "+code}).type("json").send({password:"abcdefgh"}).expect(400,cb);
|
485 | }
|
486 | ],done);
|
487 | });
|
488 | it('should fail for expired reset code with handler', function(done){
|
489 | var user = users["1"], email = user.email, handler;
|
490 | async.waterfall([
|
491 | function (cb) {r.post('/passwordresetnext').type('json').send({user:"1"}).expect('activator','createResetHandler').expect(201,cb);},
|
492 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
493 | function (res,cb) {
|
494 | mail.unbind(email,handler);
|
495 |
|
496 | var code = changeResetTime(res.code,-100);
|
497 | r.put('/passwordresetnext/'+res.user).set({"Authorization":"Bearer "+code}).type("json").send({password:"abcdefgh"}).expect('activator','completeResetHandler').expect(400,cb);
|
498 | }
|
499 | ],done);
|
500 | });
|
501 | it('should fail when trying to update password of another user', function(done){
|
502 | var user = users["1"], email = user.email, handler;
|
503 | async.waterfall([
|
504 | function (cb) {r.post('/passwordreset').type('json').send({user:"1"}).expect(201,cb);},
|
505 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
506 | function (res,cb) {
|
507 | createUser({}, {}, function() {
|
508 | mail.unbind(email,handler);
|
509 | r.put('/passwordreset/'+users["2"].id).set({"Authorization":"Bearer "+res.code}).type('json').send({password:"abcdefgh"}).expect(400,cb);
|
510 | });
|
511 | }
|
512 | ],done);
|
513 | });
|
514 | it('should fail when trying to update password of another user with handler', function(done){
|
515 | var user = users["1"], email = user.email, handler;
|
516 | async.waterfall([
|
517 | function (cb) {r.post('/passwordresetnext').type('json').send({user:"1"}).expect('activator','createResetHandler').expect(201,cb);},
|
518 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
519 | function (res,cb) {
|
520 | createUser({}, {}, function() {
|
521 | mail.unbind(email,handler);
|
522 | r.put('/passwordresetnext/'+users["2"].id).set({"Authorization":"Bearer "+res.code}).type('json').send({password:"abcdefgh"}).expect('activator','completeResetHandler').expect(400,cb);
|
523 | });
|
524 | }
|
525 | ],done);
|
526 | });
|
527 | it('should succeed for known ID', function(done){
|
528 | var email = users["1"].email, handler;
|
529 | async.waterfall([
|
530 | function (cb) {r.post('/passwordreset').type('json').send({user:"1"}).expect(201,cb);},
|
531 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
532 | function (res,cb) {
|
533 | mail.unbind(email,handler);
|
534 | r.put('/passwordreset/'+res.user).set({"Authorization":"Bearer "+res.code}).type("json").send({password:"abcdefgh"}).expect(200,cb);
|
535 | }
|
536 | ],done);
|
537 | });
|
538 | it('should succeed for known ID with handler', function(done){
|
539 | var email = users["1"].email, handler;
|
540 | async.waterfall([
|
541 | function (cb) {r.post('/passwordresetnext').type('json').send({user:"1"}).expect('activator','createResetHandler').expect(201,cb);},
|
542 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
543 | function (res,cb) {
|
544 | mail.unbind(email,handler);
|
545 | r.put('/passwordresetnext/'+res.user).set({"Authorization":"Bearer "+res.code}).type("json").send({password:"abcdefgh"}).expect('activator','completeResetHandler').expect(200,cb);
|
546 | }
|
547 | ],done);
|
548 | });
|
549 | it('should succeed for known email', function(done){
|
550 | var email = users["1"].email, handler;
|
551 | async.waterfall([
|
552 | function (cb) {r.post('/passwordreset').type('json').send({user:email}).expect(201,cb);},
|
553 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
554 | function (res,cb) {
|
555 | mail.unbind(email,handler);
|
556 |
|
557 | should(res.content.attachments).undefined();
|
558 | r.put('/passwordreset/'+res.user).set({"Authorization":"Bearer "+res.code}).type("json").send({password:"abcdefgh"}).expect(200,cb);
|
559 | }
|
560 | ],done);
|
561 | });
|
562 | it('should succeed for known email with handler', function(done){
|
563 | var email = users["1"].email, handler;
|
564 | async.waterfall([
|
565 | function (cb) {r.post('/passwordresetnext').type('json').send({user:email}).expect('activator','createResetHandler').expect(201,cb);},
|
566 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
567 | function (res,cb) {
|
568 | mail.unbind(email,handler);
|
569 | r.put('/passwordresetnext/'+res.user).set({"Authorization":"Bearer "+res.code}).type("json").send({password:"abcdefgh"}).expect('activator','completeResetHandler').expect(200,cb);
|
570 | }
|
571 | ],done);
|
572 | });
|
573 | });
|
574 | describe('auth query', function(){
|
575 | it('should fail for known email but bad code', function(done){
|
576 | var email = users["1"].email, handler;
|
577 | async.waterfall([
|
578 | function (cb) {
|
579 | r.post('/passwordreset').type('json').send({user:email}).expect(201,cb);},
|
580 | function (res,cb) {
|
581 | handler = rHandler(email,cb);
|
582 | mail.bind(email,handler);},
|
583 | function (res,cb) {
|
584 | mail.unbind(email,handler);
|
585 | r.put('/passwordreset/'+res.user).query({"Authorization":"asasqsqsas"}).type("json").send({password:"asasa"}).expect(400,cb);
|
586 | }
|
587 | ],done);
|
588 | });
|
589 | it('should fail for known email but bad code with handler', function(done){
|
590 | var email = users["1"].email, handler;
|
591 | async.waterfall([
|
592 | function (cb) {
|
593 | r.post('/passwordresetnext').type('json').send({user:email}).expect('activator','createResetHandler').expect(201,cb);},
|
594 | function (res,cb) {
|
595 | handler = rHandler(email,cb);
|
596 | mail.bind(email,handler);},
|
597 | function (res,cb) {
|
598 | mail.unbind(email,handler);
|
599 | r.put('/passwordresetnext/'+res.user).query({Authorization:"asasqsqsqs"}).type("json").send({password:"asasa"}).expect('activator','completeResetHandler').expect(400,cb);
|
600 | }
|
601 | ],done);
|
602 | });
|
603 | it('should fail for known email with good code but missing new password', function(done){
|
604 | var email = users["1"].email, handler;
|
605 | async.waterfall([
|
606 | function (cb) {r.post('/passwordreset').type('json').send({user:email}).expect(201,cb);},
|
607 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
608 | function (res,cb) {
|
609 | mail.unbind(email,handler);
|
610 | r.put('/passwordreset/'+res.user).query({"Authorization":res.code}).type("json").send({}).expect(400,cb);
|
611 | }
|
612 | ],done);
|
613 | });
|
614 | it('should fail for known email with good code but missing new password with handler', function(done){
|
615 | var email = users["1"].email, handler;
|
616 | async.waterfall([
|
617 | function (cb) {r.post('/passwordresetnext').type('json').send({user:email}).expect('activator','createResetHandler').expect(201,cb);},
|
618 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
619 | function (res,cb) {
|
620 | mail.unbind(email,handler);
|
621 | r.put('/passwordresetnext/'+res.user).query({"Authorization":res.code}).type("json").send({}).expect('activator','completeResetHandler').expect(400,cb);
|
622 | }
|
623 | ],done);
|
624 | });
|
625 | it('should fail for expired reset code', function(done){
|
626 | var user = users["1"], email = user.email, handler;
|
627 | async.waterfall([
|
628 | function (cb) {r.post('/passwordreset').type('json').send({user:"1"}).expect(201,cb);},
|
629 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
630 | function (res,cb) {
|
631 | mail.unbind(email,handler);
|
632 |
|
633 | var code = changeResetTime(res.code,-100);
|
634 | r.put('/passwordreset/'+res.user).query({Authorization:code}).type("json").send({password:"abcdefgh"}).expect(400,cb);
|
635 | }
|
636 | ],done);
|
637 | });
|
638 | it('should fail for expired reset code with handler', function(done){
|
639 | var user = users["1"], email = user.email, handler;
|
640 | async.waterfall([
|
641 | function (cb) {r.post('/passwordresetnext').type('json').send({user:"1"}).expect('activator','createResetHandler').expect(201,cb);},
|
642 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
643 | function (res,cb) {
|
644 | mail.unbind(email,handler);
|
645 |
|
646 | var code = changeResetTime(res.code,-100);
|
647 | r.put('/passwordresetnext/'+res.user).query({Authorization:code}).type("json").send({password:"abcdefgh"}).expect('activator','completeResetHandler').expect(400,cb);
|
648 | }
|
649 | ],done);
|
650 | });
|
651 | it('should fail when trying to update password of another user', function(done){
|
652 | var user = users["1"], email = user.email, handler;
|
653 | async.waterfall([
|
654 | function (cb) {r.post('/passwordreset').type('json').send({user:"1"}).expect(201,cb);},
|
655 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
656 | function (res,cb) {
|
657 | createUser({}, {}, function() {
|
658 | mail.unbind(email,handler);
|
659 | r.put('/passwordreset/'+users["2"].id).query({Authorization:res.code}).type('json').send({password:"abcdefgh"}).expect(400,cb);
|
660 | });
|
661 | }
|
662 | ],done);
|
663 | });
|
664 | it('should fail when trying to update password of another user with handler', function(done){
|
665 | var user = users["1"], email = user.email, handler;
|
666 | async.waterfall([
|
667 | function (cb) {r.post('/passwordresetnext').type('json').send({user:"1"}).expect('activator','createResetHandler').expect(201,cb);},
|
668 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
669 | function (res,cb) {
|
670 | createUser({}, {}, function() {
|
671 | mail.unbind(email,handler);
|
672 | r.put('/passwordresetnext/'+users["2"].id).query({Authorization:res.code}).type('json').send({password:"abcdefgh"}).expect('activator','completeResetHandler').expect(400,cb);
|
673 | });
|
674 | }
|
675 | ],done);
|
676 | });
|
677 | it('should succeed for known ID', function(done){
|
678 | var email = users["1"].email, handler;
|
679 | async.waterfall([
|
680 | function (cb) {r.post('/passwordreset').type('json').send({user:"1"}).expect(201,cb);},
|
681 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
682 | function (res,cb) {
|
683 | mail.unbind(email,handler);
|
684 | r.put('/passwordreset/'+res.user).query({Authorization:res.code}).type("json").send({password:"abcdefgh"}).expect(200,cb);
|
685 | }
|
686 | ],done);
|
687 | });
|
688 | it('should succeed for known ID with handler', function(done){
|
689 | var email = users["1"].email, handler;
|
690 | async.waterfall([
|
691 | function (cb) {r.post('/passwordresetnext').type('json').send({user:"1"}).expect('activator','createResetHandler').expect(201,cb);},
|
692 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
693 | function (res,cb) {
|
694 | mail.unbind(email,handler);
|
695 | r.put('/passwordresetnext/'+res.user).query({Authorization:res.code}).type("json").send({password:"abcdefgh"}).expect('activator','completeResetHandler').expect(200,cb);
|
696 | }
|
697 | ],done);
|
698 | });
|
699 | it('should succeed for known email', function(done){
|
700 | var email = users["1"].email, handler;
|
701 | async.waterfall([
|
702 | function (cb) {r.post('/passwordreset').type('json').send({user:email}).expect(201,cb);},
|
703 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
704 | function (res,cb) {
|
705 | mail.unbind(email,handler);
|
706 |
|
707 | should(res.content.attachments).undefined();
|
708 | r.put('/passwordreset/'+res.user).query({Authorization:res.code}).type("json").send({password:"abcdefgh"}).expect(200,cb);
|
709 | }
|
710 | ],done);
|
711 | });
|
712 | it('should succeed for known email with handler', function(done){
|
713 | var email = users["1"].email, handler;
|
714 | async.waterfall([
|
715 | function (cb) {r.post('/passwordresetnext').type('json').send({user:email}).expect('activator','createResetHandler').expect(201,cb);},
|
716 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
717 | function (res,cb) {
|
718 | mail.unbind(email,handler);
|
719 | r.put('/passwordresetnext/'+res.user).query({Authorization:res.code}).type("json").send({password:"abcdefgh"}).expect('activator','completeResetHandler').expect(200,cb);
|
720 | }
|
721 | ],done);
|
722 | });
|
723 | });
|
724 | describe('auth body', function(){
|
725 | it('should fail for known email but bad code', function(done){
|
726 | var email = users["1"].email, handler;
|
727 | async.waterfall([
|
728 | function (cb) {
|
729 | r.post('/passwordreset').type('json').send({user:email}).expect(201,cb);},
|
730 | function (res,cb) {
|
731 | handler = rHandler(email,cb);
|
732 | mail.bind(email,handler);},
|
733 | function (res,cb) {
|
734 | mail.unbind(email,handler);
|
735 | r.put('/passwordreset/'+res.user).type("json").send({Authorization:"asasqsqsqs",password:"asasa"}).expect(400,cb);
|
736 | }
|
737 | ],done);
|
738 | });
|
739 | it('should fail for known email but bad code with handler', function(done){
|
740 | var email = users["1"].email, handler;
|
741 | async.waterfall([
|
742 | function (cb) {
|
743 | r.post('/passwordresetnext').type('json').send({user:email}).expect('activator','createResetHandler').expect(201,cb);},
|
744 | function (res,cb) {
|
745 | handler = rHandler(email,cb);
|
746 | mail.bind(email,handler);},
|
747 | function (res,cb) {
|
748 | mail.unbind(email,handler);
|
749 | r.put('/passwordresetnext/'+res.user).type("json").send({Authorization:"asasqsqsqs",password:"asasa"}).expect('activator','completeResetHandler').expect(400,cb);
|
750 | }
|
751 | ],done);
|
752 | });
|
753 | it('should fail for known email with good code but missing new password', function(done){
|
754 | var email = users["1"].email, handler;
|
755 | async.waterfall([
|
756 | function (cb) {r.post('/passwordreset').type('json').send({user:email}).expect(201,cb);},
|
757 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
758 | function (res,cb) {
|
759 | mail.unbind(email,handler);
|
760 | r.put('/passwordreset/'+res.user).type("json").send({Authorization:res.code}).expect(400,cb);
|
761 | }
|
762 | ],done);
|
763 | });
|
764 | it('should fail for known email with good code but missing new password with handler', function(done){
|
765 | var email = users["1"].email, handler;
|
766 | async.waterfall([
|
767 | function (cb) {r.post('/passwordresetnext').type('json').send({user:email}).expect('activator','createResetHandler').expect(201,cb);},
|
768 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
769 | function (res,cb) {
|
770 | mail.unbind(email,handler);
|
771 | r.put('/passwordresetnext/'+res.user).type("json").send({Authorization:res.code}).expect('activator','completeResetHandler').expect(400,cb);
|
772 | }
|
773 | ],done);
|
774 | });
|
775 | it('should fail for expired reset code', function(done){
|
776 | var user = users["1"], email = user.email, handler;
|
777 | async.waterfall([
|
778 | function (cb) {r.post('/passwordreset').type('json').send({user:"1"}).expect(201,cb);},
|
779 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
780 | function (res,cb) {
|
781 | mail.unbind(email,handler);
|
782 |
|
783 | var code = changeResetTime(res.code,-100);
|
784 | r.put('/passwordreset/'+res.user).type("json").send({Authorization:code,password:"abcdefgh"}).expect(400,cb);
|
785 | }
|
786 | ],done);
|
787 | });
|
788 | it('should fail for expired reset code with handler', function(done){
|
789 | var user = users["1"], email = user.email, handler;
|
790 | async.waterfall([
|
791 | function (cb) {r.post('/passwordresetnext').type('json').send({user:"1"}).expect('activator','createResetHandler').expect(201,cb);},
|
792 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
793 | function (res,cb) {
|
794 | mail.unbind(email,handler);
|
795 |
|
796 | var code = changeResetTime(res.code,-100);
|
797 | r.put('/passwordresetnext/'+res.user).type("json").send({Authorization:code,password:"abcdefgh"}).expect('activator','completeResetHandler').expect(400,cb);
|
798 | }
|
799 | ],done);
|
800 | });
|
801 | it('should fail when trying to update password of another user', function(done){
|
802 | var user = users["1"], email = user.email, handler;
|
803 | async.waterfall([
|
804 | function (cb) {r.post('/passwordreset').type('json').send({user:"1"}).expect(201,cb);},
|
805 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
806 | function (res,cb) {
|
807 | createUser({}, {}, function() {
|
808 | mail.unbind(email,handler);
|
809 | r.put('/passwordreset/'+users["2"].id).type('json').send({Authorization:res.code,password:"abcdefgh"}).expect(400,cb);
|
810 | });
|
811 | }
|
812 | ],done);
|
813 | });
|
814 | it('should fail when trying to update password of another user with handler', function(done){
|
815 | var user = users["1"], email = user.email, handler;
|
816 | async.waterfall([
|
817 | function (cb) {r.post('/passwordresetnext').type('json').send({user:"1"}).expect('activator','createResetHandler').expect(201,cb);},
|
818 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
819 | function (res,cb) {
|
820 | createUser({}, {}, function() {
|
821 | mail.unbind(email,handler);
|
822 | r.put('/passwordresetnext/'+users["2"].id).type('json').send({Authorization:res.code,password:"abcdefgh"}).expect('activator','completeResetHandler').expect(400,cb);
|
823 | });
|
824 | }
|
825 | ],done);
|
826 | });
|
827 | it('should succeed for known ID', function(done){
|
828 | var email = users["1"].email, handler;
|
829 | async.waterfall([
|
830 | function (cb) {r.post('/passwordreset').type('json').send({user:"1"}).expect(201,cb);},
|
831 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
832 | function (res,cb) {
|
833 | mail.unbind(email,handler);
|
834 | r.put('/passwordreset/'+res.user).type("json").send({Authorization:res.code,password:"abcdefgh"}).expect(200,cb);
|
835 | }
|
836 | ],done);
|
837 | });
|
838 | it('should succeed for known ID with handler', function(done){
|
839 | var email = users["1"].email, handler;
|
840 | async.waterfall([
|
841 | function (cb) {r.post('/passwordresetnext').type('json').send({user:"1"}).expect('activator','createResetHandler').expect(201,cb);},
|
842 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
843 | function (res,cb) {
|
844 | mail.unbind(email,handler);
|
845 | r.put('/passwordresetnext/'+res.user).type("json").send({Authorization:res.code,password:"abcdefgh"}).expect('activator','completeResetHandler').expect(200,cb);
|
846 | }
|
847 | ],done);
|
848 | });
|
849 | it('should succeed for known email', function(done){
|
850 | var email = users["1"].email, handler;
|
851 | async.waterfall([
|
852 | function (cb) {r.post('/passwordreset').type('json').send({user:email}).expect(201,cb);},
|
853 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
854 | function (res,cb) {
|
855 | mail.unbind(email,handler);
|
856 |
|
857 | should(res.content.attachments).undefined();
|
858 | r.put('/passwordreset/'+res.user).type("json").send({Authorization:res.code,password:"abcdefgh"}).expect(200,cb);
|
859 | }
|
860 | ],done);
|
861 | });
|
862 | it('should succeed for known email with handler', function(done){
|
863 | var email = users["1"].email, handler;
|
864 | async.waterfall([
|
865 | function (cb) {r.post('/passwordresetnext').type('json').send({user:email}).expect('activator','createResetHandler').expect(201,cb);},
|
866 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
867 | function (res,cb) {
|
868 | mail.unbind(email,handler);
|
869 | r.put('/passwordresetnext/'+res.user).type("json").send({Authorization:res.code,password:"abcdefgh"}).expect('activator','completeResetHandler').expect(200,cb);
|
870 | }
|
871 | ],done);
|
872 | });
|
873 | });
|
874 | });
|
875 | describe('with attachments', function(){
|
876 | var attachments = {
|
877 | activate: [
|
878 | {
|
879 | filename: 'activate1.txt',
|
880 | content: 'hello activate!'
|
881 | },
|
882 | {
|
883 | filename: 'activate2.txt',
|
884 | content: new Buffer('goodbye activate!','utf-8')
|
885 | }
|
886 | ],
|
887 | passwordreset: [
|
888 | {
|
889 | filename: 'reset1.txt',
|
890 | content: 'hello reset!'
|
891 | },
|
892 | {
|
893 | filename: 'reset2.txt',
|
894 | content: new Buffer('goodbye reset!','utf-8')
|
895 | }
|
896 | ]
|
897 | };
|
898 | before(function(){
|
899 | activator.init({user:userModel,transport:url,templates:templates,from:from,attachments:attachments,signkey:SIGNKEY});
|
900 | });
|
901 | it('should include correct attachment for activate', function(done){
|
902 | var email, handler;
|
903 | async.waterfall([
|
904 | function (cb) {r.post('/users').expect(201,cb);},
|
905 | function (res,cb) {
|
906 | res.text.should.equal("2");
|
907 | email = users["2"].email;
|
908 | handler = aHandler(email,cb);
|
909 | mail.bind(email,handler);
|
910 | },
|
911 | function (res,cb) {
|
912 | var att = res.content.attachments, exp = attachments.activate;
|
913 | mail.unbind(email,handler);
|
914 |
|
915 | should(att).be.ok();
|
916 |
|
917 | att.length.should.eql(exp.length);
|
918 | att[0].fileName.should.eql(exp[0].filename);
|
919 | att[0].content.toString().should.eql(exp[0].content.toString());
|
920 | att[1].fileName.should.eql(exp[1].filename);
|
921 | att[1].content.toString().should.eql(exp[1].content.toString());
|
922 | cb();
|
923 | }
|
924 | ],done);
|
925 | });
|
926 | it('should include correct attachment for passwordreset', function(done){
|
927 | var email = users["1"].email, handler;
|
928 | async.waterfall([
|
929 | function (cb) {r.post('/passwordresetnext').type('json').send({user:email}).expect('activator','createResetHandler').expect(201,cb);},
|
930 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
931 | function (res,cb) {
|
932 | var att = res.content.attachments, exp = attachments.passwordreset;
|
933 | mail.unbind(email,handler);
|
934 |
|
935 | should(att).be.ok();
|
936 |
|
937 | att.length.should.eql(exp.length);
|
938 | att[0].fileName.should.eql(exp[0].filename);
|
939 | att[0].content.toString().should.eql(exp[0].content.toString());
|
940 | att[1].fileName.should.eql(exp[1].filename);
|
941 | att[1].content.toString().should.eql(exp[1].content.toString());
|
942 | cb();
|
943 | }
|
944 | ],done);
|
945 | });
|
946 | });
|
947 | describe('with styliner property', function() {
|
948 | var templatesPath;
|
949 | before(function(){
|
950 | templatesPath = templates+'/html';
|
951 | activator.init({user:userModel,transport:url,templates:templatesPath,from:from,styliner:true,signkey:SIGNKEY});
|
952 | });
|
953 |
|
954 | it('should inline style tags', function(done) {
|
955 | var email, handler;
|
956 | async.waterfall([
|
957 | function (cb) {r.post('/users').expect(201,cb);},
|
958 | function (res,cb) {
|
959 | res.text.should.equal("2");
|
960 | email = users["2"].email;
|
961 | handler = aHandler(email,cb);
|
962 | mail.bind(email,handler);
|
963 | },
|
964 | function (res,cb) {
|
965 | mail.unbind(email,handler);
|
966 | res.content.html.match(/style="background: blue;"/)[0].should.be.ok();
|
967 | cb();
|
968 | }
|
969 | ],done);
|
970 | });
|
971 | });
|
972 | describe('with email property override', function(){
|
973 | before(function(){
|
974 | activator.init({user:userModelEmail,emailProperty:"funny",transport:url,templates:templates,from:from,signkey:SIGNKEY});
|
975 | });
|
976 | it('activate should succeed for known user', function(done){
|
977 | var email, handler;
|
978 | async.waterfall([
|
979 | function (cb) {r.post('/users').expect(201,cb);},
|
980 | function (res,cb) {
|
981 | res.text.should.equal("2");
|
982 | email = users["2"].email;
|
983 | handler = aHandler(email,cb);
|
984 | mail.bind(email,handler);
|
985 | },
|
986 | function (res,cb) {
|
987 | mail.unbind(email,handler);
|
988 | r.put('/users/'+res.user+'/activate').type("json").send({Authorization:res.code}).expect(200,cb);
|
989 | }
|
990 | ],done);
|
991 | });
|
992 | it('password reset should succeed for known email', function(done){
|
993 | var email = users["1"].email, handler;
|
994 | async.waterfall([
|
995 | function (cb) {r.post('/passwordreset').type('json').send({user:email}).expect(201,cb);},
|
996 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
997 | function (res,cb) {
|
998 | mail.unbind(email,handler);
|
999 | r.put('/passwordreset/'+res.user).type("json").send({Authorization:res.code,password:"abcdefgh"}).expect(200,cb);
|
1000 | }
|
1001 | ],done);
|
1002 | });
|
1003 | });
|
1004 | describe('with email property override in model child object', function(){
|
1005 | it('activate should succeed for known user', function(done){
|
1006 | var email, handler;
|
1007 | async.waterfall([
|
1008 | function (cb) {
|
1009 | activator.init({user:userModelEmail,emailProperty:"childObject.funny",transport:url,templates:templates,from:from,signkey:SIGNKEY});
|
1010 | r.post('/users').expect(201,cb);
|
1011 | },
|
1012 | function (res,cb) {
|
1013 | res.text.should.equal("2");
|
1014 | email = users["2"].email;
|
1015 | handler = aHandler(email,cb);
|
1016 | mail.bind(email,handler);
|
1017 | },
|
1018 | function (res,cb) {
|
1019 | mail.unbind(email,handler);
|
1020 | r.put('/users/'+res.user+'/activate').type("json").send({Authorization:res.code}).expect(200,cb);
|
1021 | }
|
1022 | ],done);
|
1023 | });
|
1024 | it('activate should fail on bad property', function(done){
|
1025 | activator.init({user:userModelEmail,emailProperty:"childObject.badPath.funny",transport:url,templates:templates,from:from,signkey:SIGNKEY});
|
1026 | r.post('/users').expect(400,done);
|
1027 | });
|
1028 | it('password reset should succeed for known email', function(done){
|
1029 | var email = users["1"].email, handler;
|
1030 | async.waterfall([
|
1031 | function (cb) {
|
1032 | activator.init({user:userModelEmail,emailProperty:"childObject.funny",transport:url,templates:templates,from:from,signkey:SIGNKEY});
|
1033 | r.post('/passwordreset').type('json').send({user:email}).expect(201,cb);
|
1034 | },
|
1035 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
1036 | function (res,cb) {
|
1037 | mail.unbind(email,handler);
|
1038 | r.put('/passwordreset/'+res.user).type("json").send({Authorization:res.code,password:"abcdefgh"}).expect(200,cb);
|
1039 | }
|
1040 | ],done);
|
1041 | });
|
1042 | });
|
1043 | describe('with id property override', function(){
|
1044 | before(function(){
|
1045 | activator.init({user:userModel,transport:url,templates:templates,id:'id',from:from,signkey:SIGNKEY});
|
1046 | });
|
1047 | it('activate should succeed for known user', function(done){
|
1048 | var email, handler;
|
1049 | async.waterfall([
|
1050 | function (cb) {r.post('/users').type('json').send({email:"foo@bar.com"}).expect(201,cb);},
|
1051 | function (res,cb) {
|
1052 | res.text.should.equal("2");
|
1053 | email = users["2"].email;
|
1054 | handler = aHandler(email,cb);
|
1055 | mail.bind(email,handler);
|
1056 | },
|
1057 | function (res,cb) {
|
1058 | mail.unbind(email,handler);
|
1059 | r.put('/users/'+email+'/activate').type("json").send({Authorization:res.code}).expect(200,cb);
|
1060 | }
|
1061 | ],done);
|
1062 | });
|
1063 | it('password reset should succeed for known email', function(done){
|
1064 | var email = users["1"].email, handler;
|
1065 | async.waterfall([
|
1066 | function (cb) {r.post('/passwordreset').type('json').send({user:email}).expect(201,cb);},
|
1067 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
1068 | function (res,cb) {
|
1069 | mail.unbind(email,handler);
|
1070 | r.put('/passwordreset/'+email).type("json").send({Authorization:res.code,password:"abcdefgh"}).expect(200,cb);
|
1071 | }
|
1072 | ],done);
|
1073 | });
|
1074 | });
|
1075 | describe('with id property override in model child object', function(){
|
1076 | it('activate should succeed for known user', function(done){
|
1077 | var email, handler;
|
1078 | async.waterfall([
|
1079 | function (cb) {
|
1080 | activator.init({user:userModel,transport:url,templates:templates,id:'childObject.id',from:from,signkey:SIGNKEY});
|
1081 | r.post('/users').type('json').send({email:"foo@bar.com"}).expect(201,cb);
|
1082 | },
|
1083 | function (res,cb) {
|
1084 | res.text.should.equal("2");
|
1085 | email = users["2"].email;
|
1086 | handler = aHandler(email,cb);
|
1087 | mail.bind(email,handler);
|
1088 | },
|
1089 | function (res,cb) {
|
1090 | mail.unbind(email,handler);
|
1091 | r.put('/users/'+email+'/activate').type("json").send({Authorization:res.code}).expect(200,cb);
|
1092 | }
|
1093 | ],done);
|
1094 | });
|
1095 | it('activate should fail on bad property', function(done){
|
1096 | activator.init({user:userModel,transport:url,templates:templates,id:'childObject.badPath.id',from:from,signkey:SIGNKEY});
|
1097 | r.post('/users').type('json').send({email:"foo@bar.com"}).expect(404,done);
|
1098 | });
|
1099 | it('password reset should succeed for known email', function(done){
|
1100 | var email = users["1"].email, handler;
|
1101 | async.waterfall([
|
1102 | function (cb) {
|
1103 | activator.init({user:userModel,transport:url,templates:templates,id:'childObject.id',from:from,signkey:SIGNKEY});
|
1104 | r.post('/passwordreset').type('json').send({user:email}).expect(201,cb);
|
1105 | },
|
1106 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
1107 | function (res,cb) {
|
1108 | mail.unbind(email,handler);
|
1109 | r.put('/passwordreset/'+email).type("json").send({Authorization:res.code,password:"abcdefgh"}).expect(200,cb);
|
1110 | }
|
1111 | ],done);
|
1112 | });
|
1113 | });
|
1114 | describe('with html emails', function(){
|
1115 | var atemplate, htemplate, prtemplate, templatesPath;
|
1116 | before(function(){
|
1117 | templatesPath = templates+'/html';
|
1118 | activator.init({user:userModel,transport:url,templates:templatesPath,from:from,signkey:SIGNKEY});
|
1119 |
|
1120 | atemplate = splitTemplate(templatesPath+'/activate.txt');
|
1121 | htemplate = splitTemplate(templatesPath+'/activate.html');
|
1122 | prtemplate = splitTemplate(templatesPath+'/passwordreset.html');
|
1123 |
|
1124 | });
|
1125 | it('activate should send txt and html', function(done){
|
1126 | var email, handler;
|
1127 | async.waterfall([
|
1128 | function (cb) {r.post('/usersnext').expect(201,"2",cb);},
|
1129 | function (res,cb) {
|
1130 | res.text.should.equal("2");
|
1131 | email = users["2"].email;
|
1132 | handler = aHandler(email,{text:atemplate[2],html:htemplate[2]},cb);
|
1133 | mail.bind(email,handler);
|
1134 | },
|
1135 | function (res,cb) {
|
1136 | mail.unbind(email,handler);
|
1137 | cb();
|
1138 | }
|
1139 | ],done);
|
1140 | });
|
1141 | it('password reset should send only html', function(done){
|
1142 | var email = users["1"].email, handler;
|
1143 | async.waterfall([
|
1144 | function (cb) {r.post('/passwordreset').type('json').send({user:email}).expect(201,cb);},
|
1145 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
1146 | function (res,cb) {
|
1147 | mail.unbind(email,handler);
|
1148 | r.put('/passwordreset/1').type("json").send({Authorization:res.code,password:"abcdefgh"}).expect(200,cb);
|
1149 | }
|
1150 | ],done);
|
1151 | });
|
1152 | });
|
1153 |
|
1154 |
|
1155 | describe('localized', function(){
|
1156 | var parts, templatesPath;
|
1157 | before(function(){
|
1158 | templatesPath = templates+'/locale';
|
1159 | activator.init({user:userModel,transport:url,templates:templatesPath,from:from,signkey:SIGNKEY});
|
1160 |
|
1161 | parts = {
|
1162 | txt: {
|
1163 | en_GB : splitTemplate(templatesPath+'/activate_en_GB.txt'),
|
1164 | fr : splitTemplate(templatesPath+'/activate_fr.txt'),
|
1165 | fallback : splitTemplate(templatesPath+'/activate.txt')
|
1166 | },
|
1167 | html: {
|
1168 | en_GB : splitTemplate(templatesPath+'/activate_en_GB.html'),
|
1169 | fr : splitTemplate(templatesPath+'/activate_fr.html'),
|
1170 | fallback : splitTemplate(templatesPath+'/activate.html')
|
1171 | }
|
1172 | };
|
1173 |
|
1174 | });
|
1175 | it('activate should send txt and html for exact match', function(done){
|
1176 | var email, handler;
|
1177 | lang = 'en_GB';
|
1178 | async.waterfall([
|
1179 | function (cb) {r.post('/usersnext').expect(201,"2",cb);},
|
1180 | function (res,cb) {
|
1181 | res.text.should.equal("2");
|
1182 | email = users["2"].email;
|
1183 | handler = aHandler(email,{subject:parts.txt.en_GB[1],text:parts.txt.en_GB[2],html:parts.html.en_GB[2]},cb);
|
1184 | mail.bind(email,handler);
|
1185 | },
|
1186 | function (res,cb) {
|
1187 | mail.unbind(email,handler);
|
1188 | cb();
|
1189 | }
|
1190 | ],done);
|
1191 | });
|
1192 | it('activate should send txt and html fallback to lang', function(done){
|
1193 | var email, handler;
|
1194 | lang = 'fr_FR';
|
1195 | async.waterfall([
|
1196 | function (cb) {r.post('/usersnext').expect(201,"2",cb);},
|
1197 | function (res,cb) {
|
1198 | res.text.should.equal("2");
|
1199 | email = users["2"].email;
|
1200 | handler = aHandler(email,{subject:parts.txt.fr[1],text:parts.txt.fr[2],html:parts.html.fr[2]},cb);
|
1201 | mail.bind(email,handler);
|
1202 | },
|
1203 | function (res,cb) {
|
1204 | mail.unbind(email,handler);
|
1205 | cb();
|
1206 | }
|
1207 | ],done);
|
1208 | });
|
1209 | it('activate should send txt and html default for no match', function(done){
|
1210 | var email, handler;
|
1211 | lang = 'he_IL';
|
1212 | async.waterfall([
|
1213 | function (cb) {r.post('/usersnext').expect(201,"2",cb);},
|
1214 | function (res,cb) {
|
1215 | res.text.should.equal("2");
|
1216 | email = users["2"].email;
|
1217 | handler = aHandler(email,{subject:parts.txt.fallback[1],text:parts.txt.fallback[2],html:parts.html.fallback[2]},cb);
|
1218 | mail.bind(email,handler);
|
1219 | },
|
1220 | function (res,cb) {
|
1221 | mail.unbind(email,handler);
|
1222 | cb();
|
1223 | }
|
1224 | ],done);
|
1225 | });
|
1226 | });
|
1227 | };
|
1228 |
|
1229 | describe('activator', function(){
|
1230 | before(function(){
|
1231 | mail = smtp.init(MAILPORT,{disableDNSValidation:true,disableSTARTTLS:true});
|
1232 | app.use(bodyParser.json());
|
1233 | app.use(setLang);
|
1234 | app.post('/usersbad',activator.createActivate);
|
1235 | app.post('/users',createUser,activator.createActivate);
|
1236 | app.post('/usersnext',createUser,activator.createActivateNext,createActivateHandler);
|
1237 | app.put('/users/:user/activate',activator.completeActivate);
|
1238 | app.put('/usersnext/:user/activate',activator.completeActivateNext,completeActivateHandler);
|
1239 | app.post('/passwordreset',activator.createPasswordReset);
|
1240 | app.put('/passwordreset/:user',activator.completePasswordReset);
|
1241 | app.post('/passwordresetnext',activator.createPasswordResetNext,createResetHandler);
|
1242 | app.put('/passwordresetnext/:user',activator.completePasswordResetNext,completeResetHandler);
|
1243 | });
|
1244 | describe('not initialized', function(){
|
1245 | it('activate should send 500', function(done){
|
1246 | r.post('/users').type("json").send({name:"john"}).expect(500,done);
|
1247 | });
|
1248 | it('completeactivate should send 500', function(done){
|
1249 | r.put('/users/1/activate').type("json").send({Authorization:"12345"}).expect(500,done);
|
1250 | });
|
1251 | it('passwordreset should send 500', function(done){
|
1252 | r.post('/passwordreset').type("json").send({name:"john"}).expect(500,done);
|
1253 | });
|
1254 | it('completepasswordreset should send 500', function(done){
|
1255 | r.put('/passwordreset/1').type("json").send({password:"abcd",Authorization:"12345"}).expect(500,done);
|
1256 | });
|
1257 | it('activatenext should send 500', function(done){
|
1258 | r.post('/usersnext').type("json").send({name:"john"}).expect('activator','createActivateHandler').expect(500,done);
|
1259 | });
|
1260 | it('completeactivatenext should send 500', function(done){
|
1261 | r.put('/usersnext/1/activate').type("json").send({Authorization:"12345"}).expect('activator','completeActivateHandler').expect(500,done);
|
1262 | });
|
1263 | it('passwordresetnext should send 500', function(done){
|
1264 | r.post('/passwordresetnext').type("json").send({name:"john"}).expect('activator','createResetHandler').expect(500,done);
|
1265 | });
|
1266 | it('completepasswordresetnext should send 500', function(done){
|
1267 | r.put('/passwordresetnext/1').type("json").send({password:"abcd",Authorization:"12345"}).expect('activator','completeResetHandler').expect(500,done);
|
1268 | });
|
1269 | });
|
1270 | describe('initialized', function(){
|
1271 | describe('with string transport', function(){
|
1272 | before(function(){
|
1273 | activator.init({user:userModel,transport:url,templates:templates,from:from,signkey:SIGNKEY});
|
1274 | });
|
1275 | allTests();
|
1276 | });
|
1277 | describe('with nodemailer transport', function(){
|
1278 | before(function(){
|
1279 | activator.init({user:userModel,transport:mailer.createTransport(maileropts),templates:templates,from:from,signkey:SIGNKEY});
|
1280 | });
|
1281 | allTests();
|
1282 | });
|
1283 | });
|
1284 | }); |
\ | No newline at end of file |