From d56439cf8c273e43a63f0f317c9dd5f81449f7f9 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Mon, 30 May 2016 10:48:17 +0200 Subject: [PATCH] Cleanup email module --- src/dao/email.js | 87 +++++++++++++++++------------------- src/dao/message.json | 2 +- test/integration/app-test.js | 6 --- 3 files changed, 41 insertions(+), 54 deletions(-) diff --git a/src/dao/email.js b/src/dao/email.js index 2f6ff5d..57021bf 100644 --- a/src/dao/email.js +++ b/src/dao/email.js @@ -54,47 +54,6 @@ class Email { this._sender = options.sender; } - /** - * A generic method to send an email message via nodemailer. - * @param {Object} from sender user id object: { name:'Jon Smith', email:'j@smith.com' } - * @param {Object} to recipient user id object: { name:'Jon Smith', email:'j@smith.com' } - * @param {string} subject message subject - * @param {string} text message plaintext body template - * @param {string} html message html body template - * @param {Object} params (optional) nodermailer template parameters - * @yield {Object} reponse object containing SMTP info - */ - *send(options) { - let template = { - subject: options.subject, - text: options.text, - html: options.html - }; - let sender = { - from: { - name: options.from.name, - address: options.from.email - } - }; - let recipient = { - to: { - name: options.to.name, - address: options.to.email - } - }; - let params = options.params || {}; - - try { - let sendFn = this._transport.templateSender(template, sender); - let info = yield sendFn(recipient, params); - log.silly('email', 'Email sent.', info); - return info; - } catch(error) { - log.error('email', 'Sending email failed.', error, options); - throw error; - } - } - /** * Send the verification email to the user to verify email address * ownership. If the primary email address is provided, only one email @@ -131,20 +90,54 @@ class Email { html: message.verifyKey.html, params: { name: userId.name, - protocol: origin.protocol, - host: origin.host, + baseUrl: origin.protocol + '://' + origin.host, keyid: encodeURIComponent(userId.keyid), nonce: encodeURIComponent(userId.nonce) } }; + return yield this.send(msg); + } + + /** + * A generic method to send an email message via nodemailer. + * @param {Object} from sender user id object: { name:'Jon Smith', email:'j@smith.com' } + * @param {Object} to recipient user id object: { name:'Jon Smith', email:'j@smith.com' } + * @param {string} subject message subject + * @param {string} text message plaintext body template + * @param {string} html message html body template + * @param {Object} params (optional) nodermailer template parameters + * @yield {Object} reponse object containing SMTP info + */ + *send(options) { + let template = { + subject: options.subject, + text: options.text, + html: options.html + }; + let sender = { + from: { + name: options.from.name, + address: options.from.email + } + }; + let recipient = { + to: { + name: options.to.name, + address: options.to.email + } + }; + let params = options.params || {}; + try { - let info = yield this.send(msg); + let sendFn = this._transport.templateSender(template, sender); + let info = yield sendFn(recipient, params); if (!this._checkResponse(info)) { - log.warn('email', 'Verification mail may not have been received.', info); + log.warn('email', 'Message may not have been received.', info); } return info; - } catch(e) { - util.throw(500, 'Sending verification email failed'); + } catch(error) { + log.error('email', 'Sending message failed.', error, options); + util.throw(500, 'Sending email to user failed'); } } diff --git a/src/dao/message.json b/src/dao/message.json index de71166..c1b3e9f 100644 --- a/src/dao/message.json +++ b/src/dao/message.json @@ -1,7 +1,7 @@ { "verifyKey": { "subject": "Verify Your Key", - "text": "Hello {{name}},\n\nplease click here to verify your key: {{protocol}}://{{host}}/api/v1/verify/?keyid={{keyid}}&nonce={{nonce}}", + "text": "Hello {{name}},\n\nplease click here to verify your key: {{baseUrl}}/api/v1/verify/?keyid={{keyid}}&nonce={{nonce}}", "html": "" } } \ No newline at end of file diff --git a/test/integration/app-test.js b/test/integration/app-test.js index 5b7c514..0d9d3d1 100644 --- a/test/integration/app-test.js +++ b/test/integration/app-test.js @@ -17,12 +17,6 @@ describe.skip('Koa App (HTTP Server) Integration Tests', function() { app = yield init(); }); - beforeEach(function () {}); - - afterEach(function() {}); - - after(function () {}); - describe('REST api', function() { describe('POST /api/v1/key', function() { it('should return 400 for an invalid body', function (done) {