2017-03-21 11:24:11 +03:00
|
|
|
var should = require('should'),
|
|
|
|
sinon = require('sinon'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
mail = require('../../../server/mail'),
|
|
|
|
settingsCache = require('../../../server/settings/cache'),
|
|
|
|
configUtils = require('../../utils/configUtils'),
|
|
|
|
i18n = require('../../../server/i18n'),
|
|
|
|
sandbox = sinon.sandbox.create(),
|
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();
|
2017-02-03 16:15:11 +03:00
|
|
|
sandbox.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 () {
|
2017-02-27 18:53:04 +03:00
|
|
|
sandbox.stub(settingsCache, 'get').returns('Test');
|
2017-02-03 16:15:11 +03:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
// Standard domain
|
2017-02-03 16:15:11 +03:00
|
|
|
configUtils.set({url: 'http://default.com', mail: {from: 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('"Test" <ghost@default.com>');
|
2014-09-21 01:11:30 +04:00
|
|
|
|
2016-01-13 16:17:18 +03:00
|
|
|
// Trailing slash
|
2017-02-03 16:15:11 +03:00
|
|
|
configUtils.set({url: 'http://default.com/', mail: {from: null}});
|
|
|
|
|
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
|
|
|
// Strip Port
|
2017-02-03 16:15:11 +03:00
|
|
|
configUtils.set({url: 'http://default.com:2368/', mail: {from: null}});
|
2016-01-13 16:17:18 +03:00
|
|
|
mailer.from().should.equal('"Test" <ghost@default.com>');
|
2017-05-12 15:34:22 +03:00
|
|
|
|
|
|
|
settingsCache.get.restore();
|
|
|
|
sandbox.stub(settingsCache, 'get').returns('Test"');
|
|
|
|
|
|
|
|
// Escape title
|
|
|
|
configUtils.set({url: 'http://default.com:2368/', mail: {from: null}});
|
|
|
|
mailer.from().should.equal('"Test\\"" <ghost@default.com>');
|
2016-01-13 16:17:18 +03:00
|
|
|
});
|
|
|
|
|
2017-07-27 11:27:38 +03:00
|
|
|
it('should use mail.from', function () {
|
2016-01-13 16:17:18 +03:00
|
|
|
// Standard domain
|
2017-07-27 11:27:38 +03:00
|
|
|
configUtils.set({mail: {from: '"bar" <from@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
|
|
|
|
2017-07-27 11:27:38 +03:00
|
|
|
it('should attach blog title', function () {
|
2017-02-27 18:53:04 +03:00
|
|
|
sandbox.stub(settingsCache, 'get').returns('Test');
|
2017-02-03 16:15:11 +03:00
|
|
|
|
2017-07-27 11:27:38 +03:00
|
|
|
configUtils.set({mail: {from: 'from@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('"Test" <from@default.com>');
|
|
|
|
|
|
|
|
// only from set
|
2017-07-27 11:27:38 +03:00
|
|
|
configUtils.set({mail: {from: 'from@default.com'}});
|
2016-01-13 16:17:18 +03:00
|
|
|
mailer.from().should.equal('"Test" <from@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 () {
|
2017-07-27 11:27:38 +03:00
|
|
|
configUtils.set({mail: {from: '"R2D2" <from@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('"R2D2" <from@default.com>');
|
|
|
|
|
|
|
|
// only from set
|
2017-07-27 11:27:38 +03:00
|
|
|
configUtils.set({mail: {from: '"R2D2" <from@default.com>'}});
|
2016-01-13 16:17:18 +03:00
|
|
|
mailer.from().should.equal('"R2D2" <from@default.com>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use default title if not theme title is provided', function () {
|
2017-02-03 16:15:11 +03:00
|
|
|
configUtils.set({url: 'http://default.com:2368/', mail: {from: 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
|
|
|
});
|