Migrate email module
This commit is contained in:
parent
874903c64b
commit
ba671126db
@ -58,7 +58,7 @@ class Email {
|
|||||||
* @param {Object} origin origin of the server
|
* @param {Object} origin origin of the server
|
||||||
* @yield {Object} send response from the SMTP server
|
* @yield {Object} send response from the SMTP server
|
||||||
*/
|
*/
|
||||||
*send({template, userId, keyId, origin}) {
|
async send({template, userId, keyId, origin}) {
|
||||||
const message = {
|
const message = {
|
||||||
from: this._sender,
|
from: this._sender,
|
||||||
to: userId,
|
to: userId,
|
||||||
@ -72,7 +72,7 @@ class Email {
|
|||||||
nonce: userId.nonce
|
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
|
* @param {Object} params (optional) nodermailer template parameters
|
||||||
* @yield {Object} reponse object containing SMTP info
|
* @yield {Object} reponse object containing SMTP info
|
||||||
*/
|
*/
|
||||||
*_sendHelper({from, to, subject, text, html, params = {}}) {
|
async _sendHelper({from, to, subject, text, html, params = {}}) {
|
||||||
const template = {
|
const template = {
|
||||||
subject,
|
subject,
|
||||||
text,
|
text,
|
||||||
@ -107,7 +107,7 @@ class Email {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const sendFn = this._transport.templateSender(template, sender);
|
const sendFn = this._transport.templateSender(template, sender);
|
||||||
const info = yield sendFn(recipient, params);
|
const info = await sendFn(recipient, params);
|
||||||
if (!this._checkResponse(info)) {
|
if (!this._checkResponse(info)) {
|
||||||
log.warn('email', 'Message may not have been received.', info);
|
log.warn('email', 'Message may not have been received.', info);
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ describe('Email Integration Tests', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("_sendHelper", () => {
|
describe("_sendHelper", () => {
|
||||||
it('should work', function *() {
|
it('should work', async() => {
|
||||||
const mailOptions = {
|
const mailOptions = {
|
||||||
from: email._sender,
|
from: email._sender,
|
||||||
to: recipient,
|
to: recipient,
|
||||||
@ -44,30 +44,30 @@ 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
|
||||||
};
|
};
|
||||||
const info = yield email._sendHelper(mailOptions);
|
const info = await email._sendHelper(mailOptions);
|
||||||
expect(info).to.exist;
|
expect(info).to.exist;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("send verifyKey template", () => {
|
describe("send verifyKey template", () => {
|
||||||
it('should send plaintext email', function *() {
|
it('should send plaintext email', async() => {
|
||||||
delete userId.publicKeyArmored;
|
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 *() {
|
it('should send pgp encrypted email', async() => {
|
||||||
yield email.send({template: tpl.verifyKey, userId, keyId, origin});
|
await email.send({template: tpl.verifyKey, userId, keyId, origin});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("send verifyRemove template", () => {
|
describe("send verifyRemove template", () => {
|
||||||
it('should send plaintext email', function *() {
|
it('should send plaintext email', async() => {
|
||||||
delete userId.publicKeyArmored;
|
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 *() {
|
it('should send pgp encrypted email', async() => {
|
||||||
yield email.send({template: tpl.verifyRemove, userId, keyId, origin});
|
await email.send({template: tpl.verifyRemove, userId, keyId, origin});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -5,6 +5,7 @@ const Email = require('../../src/email/email');
|
|||||||
const nodemailer = require('nodemailer');
|
const nodemailer = require('nodemailer');
|
||||||
|
|
||||||
describe('Email Unit Tests', () => {
|
describe('Email Unit Tests', () => {
|
||||||
|
let sandbox;
|
||||||
let email;
|
let email;
|
||||||
let sendFnStub;
|
let sendFnStub;
|
||||||
|
|
||||||
@ -36,13 +37,14 @@ describe('Email Unit Tests', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
sandbox = sinon.sandbox.create();
|
||||||
|
|
||||||
sendFnStub = sinon.stub();
|
sendFnStub = sinon.stub();
|
||||||
sinon.stub(nodemailer, 'createTransport').returns({
|
sandbox.stub(nodemailer, 'createTransport').returns({
|
||||||
templateSender: () => sendFnStub
|
templateSender: () => sendFnStub
|
||||||
});
|
});
|
||||||
|
|
||||||
sinon.stub(log, 'warn');
|
sandbox.stub(log);
|
||||||
sinon.stub(log, 'error');
|
|
||||||
|
|
||||||
email = new Email(nodemailer);
|
email = new Email(nodemailer);
|
||||||
email.init({
|
email.init({
|
||||||
@ -54,50 +56,44 @@ describe('Email Unit Tests', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
nodemailer.createTransport.restore();
|
sandbox.restore();
|
||||||
log.warn.restore();
|
|
||||||
log.error.restore();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("send", () => {
|
describe("send", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
sinon.stub(email, '_sendHelper').returns(Promise.resolve({response: '250'}));
|
sandbox.stub(email, '_sendHelper').resolves({response: '250'});
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
it('should work', async() => {
|
||||||
email._sendHelper.restore();
|
const info = await email.send({template, userId: userId1, keyId, origin});
|
||||||
});
|
|
||||||
|
|
||||||
it('should work', function *() {
|
|
||||||
const info = yield email.send({template, userId: userId1, keyId, origin});
|
|
||||||
|
|
||||||
expect(info.response).to.match(/^250/);
|
expect(info.response).to.match(/^250/);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("_sendHelper", () => {
|
describe("_sendHelper", () => {
|
||||||
it('should work', function *() {
|
it('should work', async() => {
|
||||||
sendFnStub.returns(Promise.resolve({response: '250'}));
|
sendFnStub.resolves({response: '250'});
|
||||||
|
|
||||||
const info = yield email._sendHelper(mailOptions);
|
const info = await email._sendHelper(mailOptions);
|
||||||
|
|
||||||
expect(info.response).to.match(/^250/);
|
expect(info.response).to.match(/^250/);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should log warning for reponse error', function *() {
|
it('should log warning for reponse error', async() => {
|
||||||
sendFnStub.returns(Promise.resolve({response: '554'}));
|
sendFnStub.resolves({response: '554'});
|
||||||
|
|
||||||
const info = yield email._sendHelper(mailOptions);
|
const info = await 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;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail', function *() {
|
it('should fail', async() => {
|
||||||
sendFnStub.returns(Promise.reject(new Error('boom')));
|
sendFnStub.rejects(new Error('boom'));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
yield email._sendHelper(mailOptions);
|
await 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);
|
||||||
|
Loading…
Reference in New Issue
Block a user