From 27155278a840025210ffa6baa2c4891122abc6d8 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Tue, 31 May 2016 16:50:28 +0200 Subject: [PATCH] Move email code to src/email/ --- src/{dao => email}/email.js | 0 .../message.json => email/templates.json} | 0 test/integration/email-test.js | 42 ++++--- test/unit/email-test.js | 110 ++++-------------- 4 files changed, 48 insertions(+), 104 deletions(-) rename src/{dao => email}/email.js (100%) rename src/{dao/message.json => email/templates.json} (100%) diff --git a/src/dao/email.js b/src/email/email.js similarity index 100% rename from src/dao/email.js rename to src/email/email.js diff --git a/src/dao/message.json b/src/email/templates.json similarity index 100% rename from src/dao/message.json rename to src/email/templates.json diff --git a/test/integration/email-test.js b/test/integration/email-test.js index dc772ae..c859869 100644 --- a/test/integration/email-test.js +++ b/test/integration/email-test.js @@ -5,15 +5,16 @@ require('co-mocha')(require('mocha')); // monkey patch mocha for generators const expect = require('chai').expect; const log = require('npmlog'); const config = require('config'); -const Email = require('../../src/dao/email'); +const Email = require('../../src/email/email'); const nodemailer = require('nodemailer'); +const tpl = require('../../src/email/templates.json'); log.level = config.log.level; describe('Email Integration Tests', function() { this.timeout(20000); - let email, credentials; + let email, credentials, userId, origin; before(() => { try { @@ -23,6 +24,16 @@ describe('Email Integration Tests', function() { this.skip(); return; } + userId = { + name: credentials.sender.name, + email: credentials.sender.email, + keyid: '0123456789ABCDF0', + nonce: 'qwertzuioasdfghjkqwertzuio' + }; + origin = { + protocol: 'http', + host: 'localhost:' + config.server.port + }; email = new Email(nodemailer); email.init({ host: process.env.SMTP_HOST || credentials.smtp.host, @@ -37,7 +48,7 @@ describe('Email Integration Tests', function() { }); }); - describe("send", () => { + describe("_sendHelper", () => { it('should work', function *() { let mailOptions = { from: credentials.sender, @@ -46,27 +57,20 @@ describe('Email Integration Tests', function() { text: 'Hello world 🐴', // plaintext body html: 'Hello world 🐴' // html body }; - let info = yield email.send(mailOptions); + let info = yield email._sendHelper(mailOptions); expect(info).to.exist; }); }); - describe("sendVerifyKey", () => { + describe("send verifyKey template", () => { it('should work', function *() { - let options = { - userIds: [{ - name: credentials.sender.name, - email: credentials.sender.email, - keyid: '0123456789ABCDF0', - nonce: 'qwertzuioasdfghjkqwertzuio' - }], - primaryEmail: credentials.sender.email, - origin: { - protocol: 'http', - host: 'localhost:' + config.server.port - } - }; - yield email.sendVerifyKey(options); + yield email.send({ template:tpl.verifyKey, userId, origin }); + }); + }); + + describe("send verifyRemove template", () => { + it('should work', function *() { + yield email.send({ template:tpl.verifyRemove, userId, origin }); }); }); diff --git a/test/unit/email-test.js b/test/unit/email-test.js index 7fe4504..3ec2108 100644 --- a/test/unit/email-test.js +++ b/test/unit/email-test.js @@ -4,7 +4,7 @@ require('co-mocha')(require('mocha')); // monkey patch mocha for generators const expect = require('chai').expect; const log = require('npmlog'); -const Email = require('../../src/dao/email'); +const Email = require('../../src/email/email'); const nodemailer = require('nodemailer'); const sinon = require('sinon'); @@ -12,6 +12,11 @@ const sinon = require('sinon'); describe('Email Unit Tests', () => { let email, sendFnStub; + let template = { + subject: 'foo', + text: 'bar', + html: 'bar' + }; let sender = { name: 'Foo Bar', email: 'foo@bar.com' @@ -22,12 +27,6 @@ describe('Email Unit Tests', () => { keyid: '0123456789ABCDF0', nonce: 'qwertzuioasdfghjkqwertzuio' }; - let userId2 = { - name: 'name2', - email: 'email2', - keyid: '0123456789ABCDF0', - nonce: 'qwertzuioasdfghjkqwertzuio' - }; let origin = { protocol: 'http', host: 'localhost:8888' @@ -64,86 +63,27 @@ describe('Email Unit Tests', () => { log.error.restore(); }); - describe("sendVerifyKey", () => { - - beforeEach(() => { - sinon.stub(email, '_sendVerifyKeyHelper').returns(Promise.resolve({ response:'250' })); - }); - - afterEach(() => { - email._sendVerifyKeyHelper.restore(); - }); - - it('should send one email if primary email is given', function *() { - let options = { - userIds: [userId1, userId2], - primaryEmail: userId1.email, - origin: origin - }; - yield email.sendVerifyKey(options); - - expect(email._sendVerifyKeyHelper.withArgs(userId1, origin).calledOnce).to.be.true; - }); - - it('should send two emails if primary email is not given', function *() { - let options = { - userIds: [userId1, userId2], - origin: origin - }; - yield email.sendVerifyKey(options); - - expect(email._sendVerifyKeyHelper.calledTwice).to.be.true; - }); - - it('should send two emails if primary email does not match', function *() { - let options = { - userIds: [userId1, userId2], - primaryEmail: 'other', - origin: origin - }; - yield email.sendVerifyKey(options); - - expect(email._sendVerifyKeyHelper.calledTwice).to.be.true; - }); - }); - - describe("_sendVerifyKeyHelper", () => { - beforeEach(() => { - sinon.stub(email, 'send').returns(Promise.resolve({ response:'250' })); - }); - - afterEach(() => { - email.send.restore(); - }); - - it('should work', function *() { - let info = yield email._sendVerifyKeyHelper(userId1, origin); - - expect(info.response).to.match(/^250/); - }); - }); - - describe("sendVerifyRemove", () => { - beforeEach(() => { - sinon.stub(email, 'send').returns(Promise.resolve({ response:'250' })); - }); - - afterEach(() => { - email.send.restore(); - }); - - it('should work', function *() { - let info = yield email.sendVerifyRemove({userId:userId1, origin}); - - expect(info.response).to.match(/^250/); - }); - }); - describe("send", () => { + beforeEach(() => { + sinon.stub(email, '_sendHelper').returns(Promise.resolve({ response:'250' })); + }); + + afterEach(() => { + email._sendHelper.restore(); + }); + + it('should work', function *() { + let info = yield email.send({ template, userId:userId1, origin}); + + expect(info.response).to.match(/^250/); + }); + }); + + describe("_sendHelper", () => { it('should work', function *() { sendFnStub.returns(Promise.resolve({ response:'250' })); - let info = yield email.send(mailOptions); + let info = yield email._sendHelper(mailOptions); expect(info.response).to.match(/^250/); }); @@ -151,7 +91,7 @@ describe('Email Unit Tests', () => { it('should log warning for reponse error', function *() { sendFnStub.returns(Promise.resolve({ response:'554' })); - let info = yield email.send(mailOptions); + let info = yield email._sendHelper(mailOptions); expect(info.response).to.match(/^554/); expect(log.warn.calledOnce).to.be.true; @@ -161,7 +101,7 @@ describe('Email Unit Tests', () => { sendFnStub.returns(Promise.reject(new Error('boom'))); try { - yield email.send(mailOptions); + yield email._sendHelper(mailOptions); } catch(e) { expect(log.error.calledOnce).to.be.true; expect(e.status).to.equal(500);