2013-09-06 20:07:25 +04:00
|
|
|
/*globals describe, beforeEach, afterEach, it*/
|
2014-01-05 10:40:53 +04:00
|
|
|
var should = require('should'),
|
|
|
|
sinon = require('sinon'),
|
|
|
|
when = require('when'),
|
|
|
|
_ = require("lodash"),
|
|
|
|
cp = require('child_process'),
|
|
|
|
rewire = require("rewire"),
|
|
|
|
testUtils = require('../utils'),
|
2013-09-06 20:07:25 +04:00
|
|
|
|
|
|
|
// Stuff we are testing
|
2014-01-05 10:40:53 +04:00
|
|
|
mailer = rewire('../../server/mail'),
|
|
|
|
defaultConfig = require('../../../config'),
|
2013-08-21 00:19:47 +04:00
|
|
|
SMTP,
|
|
|
|
SENDMAIL,
|
|
|
|
fakeConfig,
|
|
|
|
fakeSettings,
|
|
|
|
fakeSendmail,
|
|
|
|
sandbox = sinon.sandbox.create(),
|
2013-11-20 17:58:52 +04:00
|
|
|
config;
|
2013-08-21 00:19:47 +04:00
|
|
|
|
|
|
|
// Mock SMTP config
|
|
|
|
SMTP = {
|
|
|
|
transport: 'SMTP',
|
|
|
|
options: {
|
|
|
|
service: 'Gmail',
|
|
|
|
auth: {
|
|
|
|
user: 'nil',
|
|
|
|
pass: '123'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Mock Sendmail config
|
|
|
|
SENDMAIL = {
|
|
|
|
transport: 'sendmail',
|
|
|
|
options: {
|
|
|
|
path: '/nowhere/sendmail'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
describe("Mail", function () {
|
2014-01-05 10:40:53 +04:00
|
|
|
var overrideConfig = function (newConfig) {
|
|
|
|
mailer.__set__('config', sandbox.stub().returns(
|
|
|
|
_.extend({}, defaultConfig, newConfig)
|
|
|
|
));
|
|
|
|
};
|
2013-08-21 00:19:47 +04:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
// Mock config and settings
|
|
|
|
fakeConfig = _.extend({}, defaultConfig);
|
|
|
|
fakeSettings = {
|
|
|
|
url: 'http://test.tryghost.org',
|
|
|
|
email: 'ghost-test@localhost'
|
|
|
|
};
|
|
|
|
fakeSendmail = '/fake/bin/sendmail';
|
|
|
|
|
2013-11-20 17:58:52 +04:00
|
|
|
config = sinon.stub().returns(fakeConfig);
|
2013-08-21 00:19:47 +04:00
|
|
|
|
2013-11-28 06:45:01 +04:00
|
|
|
sandbox.stub(mailer, "isWindows", function () {
|
2013-08-21 00:19:47 +04:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2013-11-28 06:45:01 +04:00
|
|
|
sandbox.stub(mailer, "detectSendmail", function () {
|
2013-08-21 00:19:47 +04:00
|
|
|
return when.resolve(fakeSendmail);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should attach mail provider to ghost instance', function () {
|
2013-11-28 06:45:01 +04:00
|
|
|
should.exist(mailer);
|
|
|
|
mailer.should.have.property('init');
|
|
|
|
mailer.should.have.property('transport');
|
|
|
|
mailer.should.have.property('send').and.be.a.function;
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should setup SMTP transport on initialization', function (done) {
|
2014-01-05 10:40:53 +04:00
|
|
|
overrideConfig({mail: SMTP});
|
2013-12-06 12:51:35 +04:00
|
|
|
mailer.init().then(function () {
|
2013-11-28 06:45:01 +04: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
|
|
|
done();
|
|
|
|
}).then(null, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should setup sendmail transport on initialization', function (done) {
|
2014-01-05 10:40:53 +04:00
|
|
|
overrideConfig({mail: SENDMAIL});
|
2013-12-06 12:51:35 +04:00
|
|
|
mailer.init().then(function () {
|
2013-11-28 06:45:01 +04:00
|
|
|
mailer.should.have.property('transport');
|
|
|
|
mailer.transport.transportType.should.eql('SENDMAIL');
|
|
|
|
mailer.transport.sendMail.should.be.a.function;
|
2013-08-21 00:19:47 +04:00
|
|
|
done();
|
|
|
|
}).then(null, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fallback to sendmail if no config set', function (done) {
|
2014-01-05 10:40:53 +04:00
|
|
|
overrideConfig({mail: null});
|
2013-12-06 12:51:35 +04:00
|
|
|
mailer.init().then(function () {
|
2013-11-28 06:45:01 +04:00
|
|
|
mailer.should.have.property('transport');
|
|
|
|
mailer.transport.transportType.should.eql('SENDMAIL');
|
|
|
|
mailer.transport.options.path.should.eql(fakeSendmail);
|
2013-08-21 00:19:47 +04:00
|
|
|
done();
|
|
|
|
}).then(null, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fallback to sendmail if config is empty', function (done) {
|
2014-01-05 10:40:53 +04:00
|
|
|
overrideConfig({mail: {}});
|
2013-12-06 12:51:35 +04:00
|
|
|
mailer.init().then(function () {
|
2013-11-28 06:45:01 +04:00
|
|
|
mailer.should.have.property('transport');
|
|
|
|
mailer.transport.transportType.should.eql('SENDMAIL');
|
|
|
|
mailer.transport.options.path.should.eql(fakeSendmail);
|
2013-08-21 00:19:47 +04:00
|
|
|
done();
|
|
|
|
}).then(null, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should disable transport if config is empty & sendmail not found', function (done) {
|
2014-01-05 10:40:53 +04:00
|
|
|
overrideConfig({mail: {}});
|
2013-11-28 06:45:01 +04:00
|
|
|
mailer.detectSendmail.restore();
|
|
|
|
sandbox.stub(mailer, "detectSendmail", when.reject);
|
2013-12-06 12:51:35 +04:00
|
|
|
mailer.init().then(function () {
|
2013-11-28 06:45:01 +04:00
|
|
|
should.not.exist(mailer.transport);
|
2013-08-21 00:19:47 +04:00
|
|
|
done();
|
|
|
|
}).then(null, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should disable transport if config is empty & platform is win32', function (done) {
|
2014-01-05 10:40:53 +04:00
|
|
|
overrideConfig({mail: {}});
|
2013-11-28 06:45:01 +04:00
|
|
|
mailer.detectSendmail.restore();
|
|
|
|
mailer.isWindows.restore();
|
|
|
|
sandbox.stub(mailer, 'isWindows', function () {
|
2013-11-11 14:37:09 +04:00
|
|
|
return true;
|
|
|
|
});
|
2013-12-06 12:51:35 +04:00
|
|
|
mailer.init().then(function () {
|
2013-11-28 06:45:01 +04:00
|
|
|
should.not.exist(mailer.transport);
|
2013-08-21 00:19:47 +04:00
|
|
|
done();
|
|
|
|
}).then(null, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail to send messages when no transport is set', function (done) {
|
2013-11-28 06:45:01 +04:00
|
|
|
mailer.detectSendmail.restore();
|
|
|
|
sandbox.stub(mailer, "detectSendmail", when.reject);
|
2013-12-06 12:51:35 +04:00
|
|
|
mailer.init().then(function () {
|
2013-11-28 06:45:01 +04:00
|
|
|
mailer.send().then(function () {
|
2013-08-21 00:19:47 +04:00
|
|
|
should.fail();
|
|
|
|
done();
|
|
|
|
}, function (err) {
|
|
|
|
err.should.be.an.instanceOf(Error);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail to send messages when given insufficient data', function (done) {
|
|
|
|
when.settle([
|
2013-11-28 06:45:01 +04:00
|
|
|
mailer.send(),
|
|
|
|
mailer.send({}),
|
|
|
|
mailer.send({ subject: '123' }),
|
|
|
|
mailer.send({ subject: '', html: '123' })
|
2013-08-21 00:19:47 +04:00
|
|
|
]).then(function (descriptors) {
|
|
|
|
descriptors.forEach(function (d) {
|
|
|
|
d.state.should.equal('rejected');
|
|
|
|
d.reason.should.be.an.instanceOf(Error);
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-02-24 20:28:07 +04:00
|
|
|
|
|
|
|
it('should use from address as configured in config.js', function (done) {
|
|
|
|
overrideConfig({mail:{fromaddress: 'static@example.com'}});
|
|
|
|
mailer.fromAddress().should.equal('static@example.com');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2014-03-06 15:03:00 +04:00
|
|
|
it('should fall back to ghost@[blog.url] as from address', function (done) {
|
2014-02-24 20:28:07 +04:00
|
|
|
// Standard domain
|
|
|
|
overrideConfig({url: 'http://default.com', mail:{fromaddress: null}});
|
2014-03-06 15:03:00 +04:00
|
|
|
mailer.fromAddress().should.equal('ghost@default.com');
|
2014-02-24 20:28:07 +04:00
|
|
|
|
|
|
|
// Trailing slash
|
|
|
|
overrideConfig({url: 'http://default.com/', mail:{}});
|
2014-03-06 15:03:00 +04:00
|
|
|
mailer.fromAddress().should.equal('ghost@default.com');
|
2014-02-24 20:28:07 +04:00
|
|
|
|
|
|
|
// Strip Port
|
|
|
|
overrideConfig({url: 'http://default.com:2368/', mail:{}});
|
2014-03-06 15:03:00 +04:00
|
|
|
mailer.fromAddress().should.equal('ghost@default.com');
|
2014-02-24 20:28:07 +04:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|