UNPKG

6.92 kBJavaScriptView Raw
1/*jslint node:true, nomen:true, debug:true, unused:false */
2/*global escape */
3
4/*
5 * this is sample code for usage of activator, but it is not a fully functioning example. For that, you would need smtp, template
6 * email files, etc. For true working examples, see the test directory.
7 */
8
9var express = require('express'), request = require('supertest'),
10app = express(), _ = require('lodash'), async = require('async'), smtp = require('smtp-tester'),
11mail,
12activator = require('./lib/activator'), templates = activator.templates.file(__dirname+'/test/resources'),
13users = {
14 "1": {id:"1",email:"me@you.com",password:"1234"}
15},
16users,
17userModel = {
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},
54MAILPORT = 30111,
55PORT = 30110,
56URL = "http://localhost:"+PORT,
57r = request(URL),
58url = "smtp://localhost:"+MAILPORT+"/gopickup.net/"+escape("GoPickup Test <test@gopickup.net>"),
59createUser = 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},
64genHandler = 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 // check for the correct Subject in the email
73 // do we have actual content to test? if so, we should ignore templates, because we do not have the request stuff
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},
89aHandler = function (email,data,cb) {
90 return genHandler(email,"Activate Email","/activate/my/account",data,cb);
91},
92rHandler = function(email,data,cb) {
93 return genHandler(email,"Password Reset Email","/reset/my/password",data,cb);
94},
95createActivateHandler = function (req,res,next) {
96 // the header is not normally set, so we know we incurred the handler
97 res.set("activator","createActivateHandler");
98 res.send(req.activator.code,req.activator.message);
99},
100completeActivateHandler = function (req,res,next) {
101 // the header is not normally set, so we know we incurred the handler
102 res.set("activator","completeActivateHandler");
103 res.send(req.activator.code,req.activator.message);
104},
105createResetHandler = function (req,res,next) {
106 // the header is not normally set, so we know we incurred the handler
107 res.set("activator","createResetHandler");
108 res.send(req.activator.code,req.activator.message);
109},
110completeResetHandler = function (req,res,next) {
111 // the header is not normally set, so we know we incurred the handler
112 res.set("activator","completeResetHandler");
113 res.send(req.activator.code,req.activator.message);
114};
115
116
117mail = smtp.init(MAILPORT);
118app.use(express.bodyParser());
119app.use(app.router);
120app.get('/users',function (req,res,next) {
121 res.send(200,users);
122});
123app.post('/usersbad',activator.createActivate);
124app.post('/users',createUser,activator.createActivate);
125app.post('/usersnext',createUser,activator.createActivateNext,createActivateHandler);
126app.put('/users/:user/activate',activator.completeActivate);
127app.put('/usersnext/:user/activate',activator.completeActivateNext,completeActivateHandler);
128app.post('/passwordreset',activator.createPasswordReset);
129app.put('/passwordreset/:user',activator.completePasswordReset);
130app.post('/passwordresetnext',activator.createPasswordResetNext,createResetHandler);
131app.put('/passwordresetnext/:user',activator.completePasswordResetNext,completeResetHandler);
132app.listen(PORT);
133
134activator.init({user:userModel,url:url,templates:templates});
135
136var examples = {
137 // run this function to do a regular activation
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 // run this function to do a regular activation with a special handler
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 // run this function to do a regular password reset
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 // run this function to do a regular password reset with a special handler
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};