Migrate email module

This commit is contained in:
Tankred Hase 2017-08-16 12:27:03 +08:00
parent 874903c64b
commit ba671126db
3 changed files with 32 additions and 36 deletions

View File

@ -58,7 +58,7 @@ class Email {
* @param {Object} origin origin of the server
* @yield {Object} send response from the SMTP server
*/
*send({template, userId, keyId, origin}) {
async send({template, userId, keyId, origin}) {
const message = {
from: this._sender,
to: userId,
@ -72,7 +72,7 @@ class Email {
nonce: userId.nonce
}
};
return yield this._sendHelper(message);
return this._sendHelper(message);
}
/**
@ -85,7 +85,7 @@ class Email {
* @param {Object} params (optional) nodermailer template parameters
* @yield {Object} reponse object containing SMTP info
*/
*_sendHelper({from, to, subject, text, html, params = {}}) {
async _sendHelper({from, to, subject, text, html, params = {}}) {
const template = {
subject,
text,
@ -107,7 +107,7 @@ class Email {
try {
const sendFn = this._transport.templateSender(template, sender);
const info = yield sendFn(recipient, params);
const info = await sendFn(recipient, params);
if (!this._checkResponse(info)) {
log.warn('email', 'Message may not have been received.', info);
}

View File

@ -36,7 +36,7 @@ describe('Email Integration Tests', function() {
});
describe("_sendHelper", () => {
it('should work', function *() {
it('should work', async() => {
const mailOptions = {
from: email._sender,
to: recipient,
@ -44,30 +44,30 @@ describe('Email Integration Tests', function() {
text: 'Hello world 🐴', // plaintext body
html: '<b>Hello world 🐴</b>' // html body
};
const info = yield email._sendHelper(mailOptions);
const info = await email._sendHelper(mailOptions);
expect(info).to.exist;
});
});
describe("send verifyKey template", () => {
it('should send plaintext email', function *() {
it('should send plaintext email', async() => {
delete userId.publicKeyArmored;
yield email.send({template: tpl.verifyKey, userId, keyId, origin});
await email.send({template: tpl.verifyKey, userId, keyId, origin});
});
it('should send pgp encrypted email', function *() {
yield email.send({template: tpl.verifyKey, userId, keyId, origin});
it('should send pgp encrypted email', async() => {
await email.send({template: tpl.verifyKey, userId, keyId, origin});
});
});
describe("send verifyRemove template", () => {
it('should send plaintext email', function *() {
it('should send plaintext email', async() => {
delete userId.publicKeyArmored;
yield email.send({template: tpl.verifyRemove, userId, keyId, origin});
await email.send({template: tpl.verifyRemove, userId, keyId, origin});
});
it('should send pgp encrypted email', function *() {
yield email.send({template: tpl.verifyRemove, userId, keyId, origin});
it('should send pgp encrypted email', async() => {
await email.send({template: tpl.verifyRemove, userId, keyId, origin});
});
});
});

View File

@ -5,6 +5,7 @@ const Email = require('../../src/email/email');
const nodemailer = require('nodemailer');
describe('Email Unit Tests', () => {
let sandbox;
let email;
let sendFnStub;
@ -36,13 +37,14 @@ describe('Email Unit Tests', () => {
};
beforeEach(() => {
sandbox = sinon.sandbox.create();
sendFnStub = sinon.stub();
sinon.stub(nodemailer, 'createTransport').returns({
sandbox.stub(nodemailer, 'createTransport').returns({
templateSender: () => sendFnStub
});
sinon.stub(log, 'warn');
sinon.stub(log, 'error');
sandbox.stub(log);
email = new Email(nodemailer);
email.init({
@ -54,50 +56,44 @@ describe('Email Unit Tests', () => {
});
afterEach(() => {
nodemailer.createTransport.restore();
log.warn.restore();
log.error.restore();
sandbox.restore();
});
describe("send", () => {
beforeEach(() => {
sinon.stub(email, '_sendHelper').returns(Promise.resolve({response: '250'}));
sandbox.stub(email, '_sendHelper').resolves({response: '250'});
});
afterEach(() => {
email._sendHelper.restore();
});
it('should work', function *() {
const info = yield email.send({template, userId: userId1, keyId, origin});
it('should work', async() => {
const info = await email.send({template, userId: userId1, keyId, origin});
expect(info.response).to.match(/^250/);
});
});
describe("_sendHelper", () => {
it('should work', function *() {
sendFnStub.returns(Promise.resolve({response: '250'}));
it('should work', async() => {
sendFnStub.resolves({response: '250'});
const info = yield email._sendHelper(mailOptions);
const info = await email._sendHelper(mailOptions);
expect(info.response).to.match(/^250/);
});
it('should log warning for reponse error', function *() {
sendFnStub.returns(Promise.resolve({response: '554'}));
it('should log warning for reponse error', async() => {
sendFnStub.resolves({response: '554'});
const info = yield email._sendHelper(mailOptions);
const info = await email._sendHelper(mailOptions);
expect(info.response).to.match(/^554/);
expect(log.warn.calledOnce).to.be.true;
});
it('should fail', function *() {
sendFnStub.returns(Promise.reject(new Error('boom')));
it('should fail', async() => {
sendFnStub.rejects(new Error('boom'));
try {
yield email._sendHelper(mailOptions);
await email._sendHelper(mailOptions);
} catch (e) {
expect(log.error.calledOnce).to.be.true;
expect(e.status).to.equal(500);