loopbackjs - loopback email verification testing -
i've built rest api loopbackjs , writing tests, starting testing authentication of user. have project set email verification user should not able log in until have verified email address. test have written passes status 200 res.body.email , fail user has not verified email. how can configure test http status reflects unauthorized user needs verify email before gaining access api:
here's user.spec.js
'use strict'; const app = require('../server.js'); const request = require('supertest'); const api = request('http://0.0.0.0:3000/api'); let member, uid, token; describe('member', () => { beforeall((done) => { app.listen(done); member = app.models.member; }); afterall((done) => { app.close(done); }); describe('it should resolve', () => { it('a member.find', () => ( member .find() .then(res => console.log(res)) )); }); describe('/member', () => { it('should return new member', (done) => { return api.post('/members') .set({ 'accept': 'application/json', 'content-type': 'application/json', }) .send({ firstname: 'user', lastname: 'user', email: 'user@user.com', password: '123456', }) .end((err, res) => { if (err) { throw err; done(); } else { expect(res.status).toequal(200); expect(res.body.email).toequal('user@user.com'); done(); } }); });
here user.js have renamed member.js extend user functionality:
'use strict'; var config = require('../config.json'); var path = require('path'); module.exports = function(member) { member.afterremote('create', (context, user, next) => { console.log('user after remote triggered'); var options = { type: 'email', to: user.email, from: 'noreply@bluorbit.com', subject: 'thanks registering bluorbit', template: path.resolve(__dirname, '../views/verify.ejs'), redirect: '/verified', user: user, }; user.verify(options, (err, response) => { if (err) { member.deletebyid(user.id); return next(err); } context.res.render('response', { title: 'signed successfully', content: 'please check email , verify email address', redirectto: '/', redirecttolinktext: 'log in', }); }); }); };
and here's datasources.test.json:
{ "db": { "name": "db", "connector": "memory", "file": false }, "emaildata": { "name": "emaildata", "connector": "mail", "transports": [ { "type": "smtp", "host": "smtp.gmail.com", "secure": true, "port": 465, "tls": { "rejectunauthorized": false }, "auth": { "user": "myemail@email.com", "pass": "pass" } } ] } }
is there other config need loopback use email datasource during tests? or there else missing?
Comments
Post a Comment