From ba671126dbab43067769cec46ee9627bb873510d Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Wed, 16 Aug 2017 12:27:03 +0800 Subject: [PATCH] Migrate email module --- src/email/email.js | 8 +++---- test/integration/email-test.js | 20 ++++++++--------- test/unit/email-test.js | 40 +++++++++++++++------------------- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/email/email.js b/src/email/email.js index 060bf22..dcaf996 100644 --- a/src/email/email.js +++ b/src/email/email.js @@ -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); } diff --git a/test/integration/email-test.js b/test/integration/email-test.js index e414132..cf4eaa3 100644 --- a/test/integration/email-test.js +++ b/test/integration/email-test.js @@ -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: 'Hello world 🐴' // 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}); }); }); }); diff --git a/test/unit/email-test.js b/test/unit/email-test.js index 7c14b6b..837a2e3 100644 --- a/test/unit/email-test.js +++ b/test/unit/email-test.js @@ -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);