2016-05-30 13:36:32 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-08-18 10:01:34 +00:00
|
|
|
const log = require('winston');
|
2016-05-31 14:50:28 +00:00
|
|
|
const Email = require('../../src/email/email');
|
2016-05-30 13:36:32 +00:00
|
|
|
const nodemailer = require('nodemailer');
|
|
|
|
|
2016-05-31 11:52:18 +00:00
|
|
|
describe('Email Unit Tests', () => {
|
2019-02-08 16:04:28 +00:00
|
|
|
const sandbox = sinon.createSandbox();
|
2017-08-15 08:03:06 +00:00
|
|
|
let email;
|
|
|
|
let sendFnStub;
|
2016-05-30 13:36:32 +00:00
|
|
|
|
2017-08-19 09:17:22 +00:00
|
|
|
const template = () => ({
|
2016-05-31 14:50:28 +00:00
|
|
|
subject: 'foo',
|
|
|
|
text: 'bar',
|
|
|
|
html: '<strong>bar</strong>'
|
2017-08-19 09:17:22 +00:00
|
|
|
});
|
2017-08-15 08:03:06 +00:00
|
|
|
const sender = {
|
2016-05-30 13:36:32 +00:00
|
|
|
name: 'Foo Bar',
|
|
|
|
email: 'foo@bar.com'
|
|
|
|
};
|
2017-08-15 08:03:06 +00:00
|
|
|
const userId1 = {
|
2016-05-30 13:36:32 +00:00
|
|
|
name: 'name1',
|
|
|
|
email: 'email1',
|
|
|
|
nonce: 'qwertzuioasdfghjkqwertzuio'
|
|
|
|
};
|
2017-08-15 08:03:06 +00:00
|
|
|
const keyId = '0123456789ABCDF0';
|
|
|
|
const origin = {
|
2016-05-30 13:36:32 +00:00
|
|
|
protocol: 'http',
|
|
|
|
host: 'localhost:8888'
|
|
|
|
};
|
2017-08-15 08:03:06 +00:00
|
|
|
const mailOptions = {
|
2016-05-30 13:36:32 +00:00
|
|
|
from: sender,
|
|
|
|
to: sender,
|
|
|
|
subject: 'Hello ✔', // Subject line
|
|
|
|
text: 'Hello world 🐴', // plaintext body
|
|
|
|
html: '<b>Hello world 🐴</b>' // html body
|
|
|
|
};
|
|
|
|
|
2016-05-31 11:52:18 +00:00
|
|
|
beforeEach(() => {
|
2019-02-08 16:04:28 +00:00
|
|
|
sendFnStub = sandbox.stub();
|
2017-08-16 04:27:03 +00:00
|
|
|
sandbox.stub(nodemailer, 'createTransport').returns({
|
2017-08-19 09:17:22 +00:00
|
|
|
sendMail: sendFnStub
|
2016-05-30 13:36:32 +00:00
|
|
|
});
|
|
|
|
|
2017-08-16 04:27:03 +00:00
|
|
|
sandbox.stub(log);
|
2016-05-30 13:36:32 +00:00
|
|
|
|
|
|
|
email = new Email(nodemailer);
|
|
|
|
email.init({
|
|
|
|
host: 'host',
|
2017-08-15 08:03:06 +00:00
|
|
|
auth: {user: 'user', pass: 'pass'},
|
|
|
|
sender
|
2016-05-30 13:36:32 +00:00
|
|
|
});
|
|
|
|
expect(email._sender).to.equal(sender);
|
|
|
|
});
|
|
|
|
|
2016-05-31 11:52:18 +00:00
|
|
|
afterEach(() => {
|
2017-08-16 04:27:03 +00:00
|
|
|
sandbox.restore();
|
2016-05-30 13:36:32 +00:00
|
|
|
});
|
|
|
|
|
2019-03-06 09:21:29 +00:00
|
|
|
describe('send', () => {
|
2016-05-31 11:52:18 +00:00
|
|
|
beforeEach(() => {
|
2017-08-16 09:57:33 +00:00
|
|
|
sandbox.stub(email, '_sendHelper').returns(Promise.resolve({response: '250'}));
|
2016-05-30 13:36:32 +00:00
|
|
|
});
|
|
|
|
|
2017-08-17 09:44:26 +00:00
|
|
|
it('should work', async () => {
|
2017-08-16 04:27:03 +00:00
|
|
|
const info = await email.send({template, userId: userId1, keyId, origin});
|
2016-05-30 13:36:32 +00:00
|
|
|
|
|
|
|
expect(info.response).to.match(/^250/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-03-06 09:21:29 +00:00
|
|
|
describe('_sendHelper', () => {
|
2017-08-17 09:44:26 +00:00
|
|
|
it('should work', async () => {
|
2017-08-16 09:57:33 +00:00
|
|
|
sendFnStub.returns(Promise.resolve({response: '250'}));
|
2016-05-30 13:36:32 +00:00
|
|
|
|
2017-08-16 04:27:03 +00:00
|
|
|
const info = await email._sendHelper(mailOptions);
|
2016-05-30 13:36:32 +00:00
|
|
|
|
|
|
|
expect(info.response).to.match(/^250/);
|
|
|
|
});
|
|
|
|
|
2017-08-17 09:44:26 +00:00
|
|
|
it('should log warning for reponse error', async () => {
|
2017-08-16 09:57:33 +00:00
|
|
|
sendFnStub.returns(Promise.resolve({response: '554'}));
|
2016-05-30 13:36:32 +00:00
|
|
|
|
2017-08-16 04:27:03 +00:00
|
|
|
const info = await email._sendHelper(mailOptions);
|
2016-05-30 13:36:32 +00:00
|
|
|
|
|
|
|
expect(info.response).to.match(/^554/);
|
|
|
|
expect(log.warn.calledOnce).to.be.true;
|
|
|
|
});
|
|
|
|
|
2017-08-17 09:44:26 +00:00
|
|
|
it('should fail', async () => {
|
2017-08-16 09:57:33 +00:00
|
|
|
sendFnStub.returns(Promise.reject(new Error('boom')));
|
2016-05-30 13:36:32 +00:00
|
|
|
|
|
|
|
try {
|
2017-08-16 04:27:03 +00:00
|
|
|
await email._sendHelper(mailOptions);
|
2017-08-15 08:03:06 +00:00
|
|
|
} catch (e) {
|
2016-05-30 13:36:32 +00:00
|
|
|
expect(log.error.calledOnce).to.be.true;
|
|
|
|
expect(e.status).to.equal(500);
|
|
|
|
expect(e.message).to.match(/failed/);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-08-15 08:03:06 +00:00
|
|
|
});
|