Move email code to src/email/

This commit is contained in:
Tankred Hase 2016-05-31 16:50:28 +02:00
parent c7ce99a6cb
commit 27155278a8
4 changed files with 48 additions and 104 deletions

View File

@ -5,15 +5,16 @@ require('co-mocha')(require('mocha')); // monkey patch mocha for generators
const expect = require('chai').expect; const expect = require('chai').expect;
const log = require('npmlog'); const log = require('npmlog');
const config = require('config'); const config = require('config');
const Email = require('../../src/dao/email'); const Email = require('../../src/email/email');
const nodemailer = require('nodemailer'); const nodemailer = require('nodemailer');
const tpl = require('../../src/email/templates.json');
log.level = config.log.level; log.level = config.log.level;
describe('Email Integration Tests', function() { describe('Email Integration Tests', function() {
this.timeout(20000); this.timeout(20000);
let email, credentials; let email, credentials, userId, origin;
before(() => { before(() => {
try { try {
@ -23,6 +24,16 @@ describe('Email Integration Tests', function() {
this.skip(); this.skip();
return; 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 = new Email(nodemailer);
email.init({ email.init({
host: process.env.SMTP_HOST || credentials.smtp.host, 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 *() { it('should work', function *() {
let mailOptions = { let mailOptions = {
from: credentials.sender, from: credentials.sender,
@ -46,27 +57,20 @@ describe('Email Integration Tests', function() {
text: 'Hello world 🐴', // plaintext body text: 'Hello world 🐴', // plaintext body
html: '<b>Hello world 🐴</b>' // html body html: '<b>Hello world 🐴</b>' // html body
}; };
let info = yield email.send(mailOptions); let info = yield email._sendHelper(mailOptions);
expect(info).to.exist; expect(info).to.exist;
}); });
}); });
describe("sendVerifyKey", () => { describe("send verifyKey template", () => {
it('should work', function *() { it('should work', function *() {
let options = { yield email.send({ template:tpl.verifyKey, userId, origin });
userIds: [{ });
name: credentials.sender.name, });
email: credentials.sender.email,
keyid: '0123456789ABCDF0', describe("send verifyRemove template", () => {
nonce: 'qwertzuioasdfghjkqwertzuio' it('should work', function *() {
}], yield email.send({ template:tpl.verifyRemove, userId, origin });
primaryEmail: credentials.sender.email,
origin: {
protocol: 'http',
host: 'localhost:' + config.server.port
}
};
yield email.sendVerifyKey(options);
}); });
}); });

View File

@ -4,7 +4,7 @@ require('co-mocha')(require('mocha')); // monkey patch mocha for generators
const expect = require('chai').expect; const expect = require('chai').expect;
const log = require('npmlog'); const log = require('npmlog');
const Email = require('../../src/dao/email'); const Email = require('../../src/email/email');
const nodemailer = require('nodemailer'); const nodemailer = require('nodemailer');
const sinon = require('sinon'); const sinon = require('sinon');
@ -12,6 +12,11 @@ const sinon = require('sinon');
describe('Email Unit Tests', () => { describe('Email Unit Tests', () => {
let email, sendFnStub; let email, sendFnStub;
let template = {
subject: 'foo',
text: 'bar',
html: '<strong>bar</strong>'
};
let sender = { let sender = {
name: 'Foo Bar', name: 'Foo Bar',
email: 'foo@bar.com' email: 'foo@bar.com'
@ -22,12 +27,6 @@ describe('Email Unit Tests', () => {
keyid: '0123456789ABCDF0', keyid: '0123456789ABCDF0',
nonce: 'qwertzuioasdfghjkqwertzuio' nonce: 'qwertzuioasdfghjkqwertzuio'
}; };
let userId2 = {
name: 'name2',
email: 'email2',
keyid: '0123456789ABCDF0',
nonce: 'qwertzuioasdfghjkqwertzuio'
};
let origin = { let origin = {
protocol: 'http', protocol: 'http',
host: 'localhost:8888' host: 'localhost:8888'
@ -64,86 +63,27 @@ describe('Email Unit Tests', () => {
log.error.restore(); 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", () => { 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 *() { it('should work', function *() {
sendFnStub.returns(Promise.resolve({ response:'250' })); sendFnStub.returns(Promise.resolve({ response:'250' }));
let info = yield email.send(mailOptions); let info = yield email._sendHelper(mailOptions);
expect(info.response).to.match(/^250/); expect(info.response).to.match(/^250/);
}); });
@ -151,7 +91,7 @@ describe('Email Unit Tests', () => {
it('should log warning for reponse error', function *() { it('should log warning for reponse error', function *() {
sendFnStub.returns(Promise.resolve({ response:'554' })); 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(info.response).to.match(/^554/);
expect(log.warn.calledOnce).to.be.true; expect(log.warn.calledOnce).to.be.true;
@ -161,7 +101,7 @@ describe('Email Unit Tests', () => {
sendFnStub.returns(Promise.reject(new Error('boom'))); sendFnStub.returns(Promise.reject(new Error('boom')));
try { try {
yield email.send(mailOptions); yield email._sendHelper(mailOptions);
} catch(e) { } catch(e) {
expect(log.error.calledOnce).to.be.true; expect(log.error.calledOnce).to.be.true;
expect(e.status).to.equal(500); expect(e.status).to.equal(500);