cloudron-e2e-test/mailer.js
Girish Ramakrishnan 8f03929265 better debugs
2016-09-21 13:35:53 -07:00

88 lines
2.8 KiB
JavaScript

'use strict';
var assert = require('assert'),
debug = require('debug')('e2e:mailer'),
fs = require('fs'),
path = require('path'),
postmark = require('postmark')(process.env.POSTMARK_API_KEY_TOOLS),
util = require('util');
exports = module.exports = {
sendMailToCloudronUser: sendMailToCloudronUser,
sendEndToEndTestResult: sendEndToEndTestResult
};
function send(options, callback) {
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof options.to, 'string');
assert.strictEqual(typeof options.subject, 'string');
assert.strictEqual(typeof options.text, 'string');
debug('Sending email from %s to %s with subject "%s".', options.from, options.to, options.subject);
postmark.send({
'From': options.from || 'no-reply@cloudron.io',
'To': options.to,
'Bcc': options.bcc,
'Subject': options.subject,
'TextBody': options.text,
'HtmlBody': options.html,
'Tag': 'Important',
'Attachments': options.attachments || []
}, function (error, success) {
if (error) {
console.error('Unable to send via postmark: ', error);
return callback(error);
}
callback();
});
}
function sendEndToEndTestResult(topic, versionInfo, stdout, stderr, attachmentFolder, callback) {
debug('Sending e2e test result for %s', topic);
debug('Creating attachements from folder ', attachmentFolder);
fs.readdir(attachmentFolder, function (error, files) {
var attachments = [];
if (!error) {
attachments = files.filter(function (file) {
return fs.statSync(path.join(attachmentFolder, file)).isFile();
}).map(function (file) {
var filePath = path.join(attachmentFolder, file);
return {
Content: fs.readFileSync(filePath).toString('base64'),
Name: file,
ContentType: 'text/plain'
};
});
} else {
debug('Error reading attachment directory: ', error);
}
var mailOptions = {
to: 'test@cloudron.io',
subject: util.format('E2E test results for %s', topic),
text: versionInfo + '\n\nstdout\n------\n' + stdout.toString('utf8') + '\n\nstderr\n------\n' + stderr.toString('utf8') + '\n\n',
attachments: attachments
};
send(mailOptions, callback);
});
}
function sendMailToCloudronUser(email, callback) {
debug('Sending test mail to %s', email);
var mailOptions = {
to: email,
subject: 'Hi from e2e test - ' + email.split('@')[1],
text: 'This release depends on you!' // match this with cloudron.sendMail
};
send(mailOptions, callback);
}