2014-01-05 10:40:53 +04:00
|
|
|
var should = require('should'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2013-09-06 20:07:25 +04:00
|
|
|
|
|
|
|
// Stuff we are testing
|
2016-06-28 21:13:01 +03:00
|
|
|
mail = require('../../../server/mail'),
|
|
|
|
configUtils = require('../../utils/configUtils'),
|
|
|
|
i18n = require('../../../server/i18n'),
|
2016-02-08 02:35:47 +03:00
|
|
|
mailer,
|
2014-09-21 01:11:30 +04:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
// Mock SMTP config
|
|
|
|
SMTP = {
|
|
|
|
transport: 'SMTP',
|
|
|
|
options: {
|
|
|
|
service: 'Gmail',
|
|
|
|
auth: {
|
|
|
|
user: 'nil',
|
|
|
|
pass: '123'
|
|
|
|
}
|
2013-08-21 00:19:47 +04:00
|
|
|
}
|
2016-01-13 16:17:18 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// test data
|
|
|
|
mailDataNoDomain = {
|
|
|
|
to: 'joe@doesntexistexample091283zalgo.com',
|
|
|
|
subject: 'testemail',
|
|
|
|
html: '<p>This</p>'
|
|
|
|
},
|
|
|
|
mailDataNoServer = {
|
|
|
|
to: 'joe@example.com',
|
|
|
|
subject: 'testemail',
|
|
|
|
html: '<p>This</p>'
|
|
|
|
},
|
|
|
|
mailDataIncomplete = {
|
|
|
|
subject: 'testemail',
|
|
|
|
html: '<p>This</p>'
|
|
|
|
};
|
|
|
|
|
|
|
|
i18n.init();
|
2013-08-21 00:19:47 +04:00
|
|
|
|
2016-06-28 21:13:01 +03:00
|
|
|
describe('Mail: Ghostmailer', function () {
|
2013-08-21 00:19:47 +04:00
|
|
|
afterEach(function () {
|
2016-02-08 02:35:47 +03:00
|
|
|
mailer = null;
|
|
|
|
|
2015-12-14 23:05:11 +03:00
|
|
|
configUtils.restore();
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should attach mail provider to ghost instance', function () {
|
2016-06-28 21:13:01 +03:00
|
|
|
mailer = new mail.GhostMailer();
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2013-11-28 06:45:01 +04:00
|
|
|
should.exist(mailer);
|
2016-02-08 00:27:01 +03:00
|
|
|
mailer.should.have.property('send').and.be.a.Function();
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|
|
|
|
|
2016-02-08 02:35:47 +03:00
|
|
|
it('should setup SMTP transport on initialization', function () {
|
2015-12-14 23:05:11 +03:00
|
|
|
configUtils.set({mail: SMTP});
|
2016-06-28 21:13:01 +03:00
|
|
|
mailer = new mail.GhostMailer();
|
2016-02-08 02:35:47 +03:00
|
|
|
|
|
|
|
mailer.should.have.property('transport');
|
|
|
|
mailer.transport.transportType.should.eql('SMTP');
|
|
|
|
mailer.transport.sendMail.should.be.a.Function();
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|
|
|
|
|
2016-02-08 02:35:47 +03:00
|
|
|
it('should fallback to direct if config is empty', function () {
|
2015-12-14 23:05:11 +03:00
|
|
|
configUtils.set({mail: {}});
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2016-06-28 21:13:01 +03:00
|
|
|
mailer = new mail.GhostMailer();
|
2016-02-08 02:35:47 +03:00
|
|
|
|
|
|
|
mailer.should.have.property('transport');
|
|
|
|
mailer.transport.transportType.should.eql('DIRECT');
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
it('sends valid message successfully ', function (done) {
|
|
|
|
configUtils.set({mail: {transport: 'stub'}});
|
|
|
|
|
2016-06-28 21:13:01 +03:00
|
|
|
mailer = new mail.GhostMailer();
|
2016-02-08 02:35:47 +03:00
|
|
|
|
|
|
|
mailer.transport.transportType.should.eql('STUB');
|
|
|
|
|
|
|
|
mailer.send(mailDataNoServer).then(function (response) {
|
2016-01-13 16:17:18 +03:00
|
|
|
should.exist(response.message);
|
|
|
|
should.exist(response.envelope);
|
|
|
|
response.envelope.to.should.containEql('joe@example.com');
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles failure', function (done) {
|
|
|
|
configUtils.set({mail: {transport: 'stub', options: {error: 'Stub made a boo boo :('}}});
|
|
|
|
|
2016-06-28 21:13:01 +03:00
|
|
|
mailer = new mail.GhostMailer();
|
2016-02-08 02:35:47 +03:00
|
|
|
|
|
|
|
mailer.transport.transportType.should.eql('STUB');
|
|
|
|
|
|
|
|
mailer.send(mailDataNoServer).then(function () {
|
2016-01-13 16:17:18 +03:00
|
|
|
done(new Error('Stub did not error'));
|
|
|
|
}).catch(function (error) {
|
|
|
|
error.message.should.eql('Error: Stub made a boo boo :(');
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2013-08-21 00:19:47 +04:00
|
|
|
it('should fail to send messages when given insufficient data', function (done) {
|
2016-06-28 21:13:01 +03:00
|
|
|
mailer = new mail.GhostMailer();
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2016-01-24 22:30:32 +03:00
|
|
|
Promise.all([
|
|
|
|
mailer.send().reflect(),
|
|
|
|
mailer.send({}).reflect(),
|
|
|
|
mailer.send({subject: '123'}).reflect(),
|
|
|
|
mailer.send({subject: '', html: '123'}).reflect()
|
2013-08-21 00:19:47 +04:00
|
|
|
]).then(function (descriptors) {
|
|
|
|
descriptors.forEach(function (d) {
|
2016-01-24 22:30:32 +03:00
|
|
|
d.isFulfilled().should.be.false();
|
2014-08-17 10:17:23 +04:00
|
|
|
d.reason().should.be.an.instanceOf(Error);
|
2015-10-16 13:05:24 +03:00
|
|
|
d.reason().message.should.eql('Error: Incomplete message data.');
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|
2014-02-24 20:28:07 +04:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
describe('Direct', function () {
|
2016-02-08 02:35:47 +03:00
|
|
|
beforeEach(function () {
|
2016-01-13 16:17:18 +03:00
|
|
|
configUtils.set({mail: {}});
|
|
|
|
|
2016-06-28 21:13:01 +03:00
|
|
|
mailer = new mail.GhostMailer();
|
2016-02-08 02:35:47 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
mailer = null;
|
2014-09-21 01:11:30 +04:00
|
|
|
});
|
2014-02-24 20:28:07 +04:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
it('return correct failure message for domain doesn\'t exist', function (done) {
|
|
|
|
mailer.transport.transportType.should.eql('DIRECT');
|
|
|
|
|
|
|
|
mailer.send(mailDataNoDomain).then(function () {
|
|
|
|
done(new Error('Error message not shown.'));
|
|
|
|
}, function (error) {
|
|
|
|
error.message.should.startWith('Error: Failed to send email');
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2014-02-24 20:28:07 +04:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
it('return correct failure message for no mail server at this address', function (done) {
|
|
|
|
mailer.transport.transportType.should.eql('DIRECT');
|
2014-02-24 20:28:07 +04:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
mailer.send(mailDataNoServer).then(function () {
|
|
|
|
done(new Error('Error message not shown.'));
|
|
|
|
}, function (error) {
|
|
|
|
error.message.should.eql('Error: Failed to send email.');
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2014-09-21 01:11:30 +04:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
it('return correct failure message for incomplete data', function (done) {
|
|
|
|
mailer.transport.transportType.should.eql('DIRECT');
|
|
|
|
|
|
|
|
mailer.send(mailDataIncomplete).then(function () {
|
|
|
|
done(new Error('Error message not shown.'));
|
|
|
|
}, function (error) {
|
|
|
|
error.message.should.eql('Error: Incomplete message data.');
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2014-09-21 01:11:30 +04:00
|
|
|
});
|
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
describe('From address', function () {
|
|
|
|
it('should use the config', function () {
|
|
|
|
configUtils.set({
|
|
|
|
mail: {
|
|
|
|
from: '"Blog Title" <static@example.com>'
|
|
|
|
}
|
|
|
|
});
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2016-06-28 21:13:01 +03:00
|
|
|
mailer = new mail.GhostMailer();
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
mailer.from().should.equal('"Blog Title" <static@example.com>');
|
|
|
|
});
|
2014-09-21 01:11:30 +04:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
it('should fall back to [blog.title] <ghost@[blog.url]>', function () {
|
|
|
|
// Standard domain
|
|
|
|
configUtils.set({url: 'http://default.com', mail: {from: null}, theme: {title: 'Test'}});
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2016-06-28 21:13:01 +03:00
|
|
|
mailer = new mail.GhostMailer();
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
mailer.from().should.equal('"Test" <ghost@default.com>');
|
2014-09-21 01:11:30 +04:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
// Trailing slash
|
|
|
|
configUtils.set({url: 'http://default.com/', mail: {from: null}, theme: {title: 'Test'}});
|
|
|
|
mailer.from().should.equal('"Test" <ghost@default.com>');
|
2014-09-21 01:11:30 +04:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
// Strip Port
|
|
|
|
configUtils.set({url: 'http://default.com:2368/', mail: {from: null}, theme: {title: 'Test'}});
|
|
|
|
mailer.from().should.equal('"Test" <ghost@default.com>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use mail.from if both from and fromaddress are present', function () {
|
|
|
|
// Standard domain
|
|
|
|
configUtils.set({mail: {from: '"bar" <from@default.com>', fromaddress: '"Qux" <fa@default.com>'}});
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2016-06-28 21:13:01 +03:00
|
|
|
mailer = new mail.GhostMailer();
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
mailer.from().should.equal('"bar" <from@default.com>');
|
|
|
|
});
|
2014-09-21 01:11:30 +04:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
it('should attach blog title if from or fromaddress are only email addresses', function () {
|
|
|
|
// from and fromaddress are both set
|
|
|
|
configUtils.set({mail: {from: 'from@default.com', fromaddress: 'fa@default.com'}, theme: {title: 'Test'}});
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2016-06-28 21:13:01 +03:00
|
|
|
mailer = new mail.GhostMailer();
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
mailer.from().should.equal('"Test" <from@default.com>');
|
|
|
|
|
|
|
|
// only from set
|
|
|
|
configUtils.set({mail: {from: 'from@default.com', fromaddress: null}, theme: {title: 'Test'}});
|
|
|
|
mailer.from().should.equal('"Test" <from@default.com>');
|
|
|
|
|
|
|
|
// only fromaddress set
|
|
|
|
configUtils.set({mail: {from: null, fromaddress: 'fa@default.com'}, theme: {title: 'Test'}});
|
|
|
|
mailer.from().should.equal('"Test" <fa@default.com>');
|
|
|
|
});
|
2014-09-21 01:11:30 +04:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
it('should ignore theme title if from address is Title <email@address.com> format', function () {
|
|
|
|
// from and fromaddress are both set
|
|
|
|
configUtils.set({mail: {from: '"R2D2" <from@default.com>', fromaddress: '"C3PO" <fa@default.com>'}, theme: {title: 'Test'}});
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2016-06-28 21:13:01 +03:00
|
|
|
mailer = new mail.GhostMailer();
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
mailer.from().should.equal('"R2D2" <from@default.com>');
|
|
|
|
|
|
|
|
// only from set
|
|
|
|
configUtils.set({mail: {from: '"R2D2" <from@default.com>', fromaddress: null}, theme: {title: 'Test'}});
|
|
|
|
mailer.from().should.equal('"R2D2" <from@default.com>');
|
|
|
|
|
|
|
|
// only fromaddress set
|
|
|
|
configUtils.set({mail: {from: null, fromaddress: '"C3PO" <fa@default.com>'}, theme: {title: 'Test'}});
|
|
|
|
mailer.from().should.equal('"C3PO" <fa@default.com>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use default title if not theme title is provided', function () {
|
|
|
|
configUtils.set({url: 'http://default.com:2368/', mail: {from: null}, theme: {title: null}});
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2016-06-28 21:13:01 +03:00
|
|
|
mailer = new mail.GhostMailer();
|
2016-02-08 02:35:47 +03:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
mailer.from().should.equal('"Ghost at default.com" <ghost@default.com>');
|
|
|
|
});
|
2014-02-24 20:28:07 +04:00
|
|
|
});
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|