Cleanup email module

This commit is contained in:
Tankred Hase 2016-05-30 10:48:17 +02:00
parent e866cebc28
commit d56439cf8c
3 changed files with 41 additions and 54 deletions

View File

@ -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');
}
}

View File

@ -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": ""
}
}

View File

@ -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) {