1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var express = require('express'), request = require('supertest'),
|
10 | app = express(), _ = require('lodash'), async = require('async'), smtp = require('smtp-tester'),
|
11 | mail,
|
12 | activator = require('./lib/activator'), templates = __dirname+'/test/resources',
|
13 | users = {
|
14 | "1": {id:"1",email:"me@you.com",password:"1234"}
|
15 | },
|
16 | users,
|
17 | userModel = {
|
18 | _find: function (login,cb) {
|
19 | var found = null;
|
20 | if (!login) {
|
21 | cb("nologin");
|
22 | } else if (users[login]) {
|
23 | cb(null,_.cloneDeep(users[login]));
|
24 | } else {
|
25 | _.each(users,function (val) {
|
26 | if (val && val.email === login) {
|
27 | found = val;
|
28 | return(false);
|
29 | }
|
30 | });
|
31 | cb(null,_.cloneDeep(found));
|
32 | }
|
33 | },
|
34 | find: function() {
|
35 | this._find.apply(this,arguments);
|
36 | },
|
37 | activate: function (id,cb) {
|
38 | if (id && users[id]) {
|
39 | users[id].activated = true;
|
40 | cb(null);
|
41 | } else {
|
42 | cb(404);
|
43 | }
|
44 | },
|
45 | setPassword: function (id,password,cb) {
|
46 | if (id && users[id]) {
|
47 | users[id].password = password;
|
48 | cb(null);
|
49 | } else {
|
50 | cb(404);
|
51 | }
|
52 | }
|
53 | },
|
54 | MAILPORT = 30111,
|
55 | PORT = 30110,
|
56 | URL = "http://localhost:"+PORT,
|
57 | r = request(URL),
|
58 | url = "smtp://localhost:"+MAILPORT+"/gopickup.net/"+escape("GoPickup Test <test@gopickup.net>"),
|
59 | createUser = function (req,res,next) {
|
60 | users["2"] = {id:"2",email:"you@foo.com",password:"5678"};
|
61 | req.activator = {id:"2",body:"2"};
|
62 | next();
|
63 | },
|
64 | genHandler = function(email,subject,path,data,cb) {
|
65 | if (!cb) {
|
66 | cb = data;
|
67 | data = null;
|
68 | }
|
69 | return function(rcpt,msgid,content) {
|
70 | var url, ret, re = new RegExp('http:\\/\\/\\S*'+path.replace(/\//g,'\\/')+'\\?code=([^\\s\\&]+)\\&email=(\\S+)\\&user=([^\\s\\&]+)');
|
71 | rcpt.should.eql(email);
|
72 |
|
73 |
|
74 | if (data && data.text) {
|
75 | url = content.text.match(re);
|
76 | ret = _.object(["path","code","email","user"],url);
|
77 | }
|
78 | if (data && data.html) {
|
79 | url = content.html.match(re);
|
80 | ret = _.object(["path","code","email","user"],url);
|
81 | }
|
82 | if (!ret) {
|
83 | url = (content.text||content.html).match(re);
|
84 | ret = _.object(["path","code","email","user"],url);
|
85 | }
|
86 | cb(null,ret);
|
87 | };
|
88 | },
|
89 | aHandler = function (email,data,cb) {
|
90 | return genHandler(email,"Activate Email","/activate/my/account",data,cb);
|
91 | },
|
92 | rHandler = function(email,data,cb) {
|
93 | return genHandler(email,"Password Reset Email","/reset/my/password",data,cb);
|
94 | },
|
95 | createActivateHandler = function (req,res,next) {
|
96 |
|
97 | res.set("activator","createActivateHandler");
|
98 | res.send(req.activator.code,req.activator.message);
|
99 | },
|
100 | completeActivateHandler = function (req,res,next) {
|
101 |
|
102 | res.set("activator","completeActivateHandler");
|
103 | res.send(req.activator.code,req.activator.message);
|
104 | },
|
105 | createResetHandler = function (req,res,next) {
|
106 |
|
107 | res.set("activator","createResetHandler");
|
108 | res.send(req.activator.code,req.activator.message);
|
109 | },
|
110 | completeResetHandler = function (req,res,next) {
|
111 |
|
112 | res.set("activator","completeResetHandler");
|
113 | res.send(req.activator.code,req.activator.message);
|
114 | };
|
115 |
|
116 |
|
117 | mail = smtp.init(MAILPORT);
|
118 | app.use(express.bodyParser());
|
119 | app.use(app.router);
|
120 | app.get('/users',function (req,res,next) {
|
121 | res.send(200,users);
|
122 | });
|
123 | app.post('/usersbad',activator.createActivate);
|
124 | app.post('/users',createUser,activator.createActivate);
|
125 | app.post('/usersnext',createUser,activator.createActivateNext,createActivateHandler);
|
126 | app.put('/users/:user/activate',activator.completeActivate);
|
127 | app.put('/usersnext/:user/activate',activator.completeActivateNext,completeActivateHandler);
|
128 | app.post('/passwordreset',activator.createPasswordReset);
|
129 | app.put('/passwordreset/:user',activator.completePasswordReset);
|
130 | app.post('/passwordresetnext',activator.createPasswordResetNext,createResetHandler);
|
131 | app.put('/passwordresetnext/:user',activator.completePasswordResetNext,completeResetHandler);
|
132 | app.listen(PORT);
|
133 |
|
134 | activator.init({user:userModel,url:url,templates:templates});
|
135 |
|
136 | var examples = {
|
137 |
|
138 | activate: function () {
|
139 | var email, handler;
|
140 | async.waterfall([
|
141 | function (cb) {r.post('/users').expect(201,cb);},
|
142 | function (res,cb) {
|
143 | res.text.should.equal("2");
|
144 | email = users["2"].email;
|
145 | handler = aHandler(email,cb);
|
146 | mail.bind(email,handler);
|
147 | },
|
148 | function (res,cb) {
|
149 | mail.unbind(email,handler);
|
150 | r.put('/users/'+res.user+'/activate').type("json").send({Activation:res.code}).expect(200,cb);
|
151 | }
|
152 | ],function () {
|
153 | console.log("done");
|
154 | });
|
155 | },
|
156 |
|
157 | activateHandler: function () {
|
158 | var email, handler;
|
159 | async.waterfall([
|
160 | function (cb) {r.post('/usersnext').expect('activator','createActivateHandler').expect(201,cb);},
|
161 | function (res,cb) {
|
162 | res.text.should.equal("2");
|
163 | email = users["2"].email;
|
164 | handler = aHandler(email,cb);
|
165 | mail.bind(email,handler);
|
166 | },
|
167 | function (res,cb) {
|
168 | mail.unbind(email,handler);
|
169 | r.put('/usersnext/'+res.user+'/activate').type("json").send({Activation:res.code}).expect('activator','completeActivateHandler').expect(200,cb);
|
170 | }
|
171 | ],function () {
|
172 | console.log("done");
|
173 | });
|
174 | },
|
175 |
|
176 | passwordRest: function () {
|
177 | var email = users["1"].email, handler;
|
178 | async.waterfall([
|
179 | function (cb) {r.post('/passwordreset').type('json').send({user:"1"}).expect(201,cb);},
|
180 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
181 | function (res,cb) {
|
182 | mail.unbind(email,handler);
|
183 | r.put('/passwordreset/'+res.user).type("json").send({Activation:res.code,password:"abcdefgh"}).expect(200,cb);
|
184 | }
|
185 | ],function () {
|
186 | console.log("done");
|
187 | });
|
188 | },
|
189 |
|
190 | passwordResetHandler: function () {
|
191 | var email = users["1"].email, handler;
|
192 | async.waterfall([
|
193 | function (cb) {r.post('/passwordresetnext').type('json').send({user:"1"}).expect('activator','createResetHandler').expect(201,cb);},
|
194 | function (res,cb) {handler = rHandler(email,cb); mail.bind(email,handler);},
|
195 | function (res,cb) {
|
196 | mail.unbind(email,handler);
|
197 | r.put('/passwordresetnext/'+res.user).type("json").send({Activation:res.code,password:"abcdefgh"}).expect('activator','completeResetHandler').expect(200,cb);
|
198 | }
|
199 | ],function () {
|
200 | console.log("done");
|
201 | });
|
202 | }
|
203 | };
|