keyserver/test/unit/email-test.js

105 lines
2.4 KiB
JavaScript
Raw Normal View History

'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');
const nodemailer = require('nodemailer');
2016-05-31 11:52:18 +00:00
describe('Email Unit Tests', () => {
2017-08-16 04:27:03 +00:00
let sandbox;
2017-08-15 08:03:06 +00:00
let email;
let sendFnStub;
2017-08-15 08:03:06 +00:00
const template = {
2016-05-31 14:50:28 +00:00
subject: 'foo',
text: 'bar',
html: '<strong>bar</strong>'
};
2017-08-15 08:03:06 +00:00
const sender = {
name: 'Foo Bar',
email: 'foo@bar.com'
};
2017-08-15 08:03:06 +00:00
const userId1 = {
name: 'name1',
email: 'email1',
nonce: 'qwertzuioasdfghjkqwertzuio'
};
2017-08-15 08:03:06 +00:00
const keyId = '0123456789ABCDF0';
const origin = {
protocol: 'http',
host: 'localhost:8888'
};
2017-08-15 08:03:06 +00:00
const mailOptions = {
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(() => {
2017-08-16 04:27:03 +00:00
sandbox = sinon.sandbox.create();
sendFnStub = sinon.stub();
2017-08-16 04:27:03 +00:00
sandbox.stub(nodemailer, 'createTransport').returns({
2017-08-15 08:03:06 +00:00
templateSender: () => sendFnStub
});
2017-08-16 04:27:03 +00:00
sandbox.stub(log);
email = new Email(nodemailer);
email.init({
host: 'host',
2017-08-15 08:03:06 +00:00
auth: {user: 'user', pass: 'pass'},
sender
});
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-31 14:50:28 +00:00
describe("send", () => {
2016-05-31 11:52:18 +00:00
beforeEach(() => {
sandbox.stub(email, '_sendHelper').returns(Promise.resolve({response: '250'}));
});
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});
expect(info.response).to.match(/^250/);
});
});
2016-05-31 14:50:28 +00:00
describe("_sendHelper", () => {
2017-08-17 09:44:26 +00:00
it('should work', async () => {
sendFnStub.returns(Promise.resolve({response: '250'}));
2017-08-16 04:27:03 +00:00
const info = await email._sendHelper(mailOptions);
expect(info.response).to.match(/^250/);
});
2017-08-17 09:44:26 +00:00
it('should log warning for reponse error', async () => {
sendFnStub.returns(Promise.resolve({response: '554'}));
2017-08-16 04:27:03 +00:00
const info = await email._sendHelper(mailOptions);
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 () => {
sendFnStub.returns(Promise.reject(new Error('boom')));
try {
2017-08-16 04:27:03 +00:00
await email._sendHelper(mailOptions);
2017-08-15 08:03:06 +00:00
} catch (e) {
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
});