2013-09-06 20:07:25 +04:00
|
|
|
/*globals describe, beforeEach, afterEach, it*/
|
2014-06-05 01:26:03 +04:00
|
|
|
/*jshint expr:true*/
|
2014-01-05 10:40:53 +04:00
|
|
|
var should = require('should'),
|
|
|
|
sinon = require('sinon'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2014-06-05 01:26:03 +04:00
|
|
|
_ = require('lodash'),
|
|
|
|
rewire = require('rewire'),
|
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,
|
|
|
|
fakeConfig,
|
|
|
|
fakeSettings,
|
2014-07-17 18:33:21 +04:00
|
|
|
sandbox = sinon.sandbox.create();
|
2013-08-21 00:19:47 +04:00
|
|
|
|
|
|
|
// Mock SMTP config
|
|
|
|
SMTP = {
|
|
|
|
transport: 'SMTP',
|
|
|
|
options: {
|
|
|
|
service: 'Gmail',
|
|
|
|
auth: {
|
|
|
|
user: 'nil',
|
|
|
|
pass: '123'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
describe('Mail', function () {
|
2014-01-05 10:40:53 +04:00
|
|
|
var overrideConfig = function (newConfig) {
|
2014-07-17 18:33:21 +04:00
|
|
|
var config = rewire('../../server/config'),
|
|
|
|
existingConfig = mailer.__get__('config');
|
|
|
|
|
2014-08-23 20:19:13 +04:00
|
|
|
config.set(_.extend(existingConfig, newConfig));
|
2014-01-05 10:40:53 +04:00
|
|
|
};
|
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'
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-08-31 19:36:32 +04:00
|
|
|
overrideConfig(fakeConfig);
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|
|
|
|
|
2014-08-31 19:36:32 +04:00
|
|
|
it('should fallback to direct 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');
|
2014-08-31 19:36:32 +04:00
|
|
|
mailer.transport.transportType.should.eql('DIRECT');
|
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
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail to send messages when given insufficient data', function (done) {
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise.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) {
|
2014-08-17 10:17:23 +04:00
|
|
|
d.isRejected().should.be.true;
|
|
|
|
d.reason().should.be.an.instanceOf(Error);
|
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
|
|
|
|
2014-05-28 01:57:53 +04:00
|
|
|
it('should use from address as configured in config.js', function () {
|
2014-09-03 19:42:55 +04:00
|
|
|
overrideConfig({mail: {fromaddress: 'static@example.com'}});
|
2014-02-24 20:28:07 +04:00
|
|
|
mailer.fromAddress().should.equal('static@example.com');
|
|
|
|
});
|
|
|
|
|
2014-05-28 01:57:53 +04:00
|
|
|
it('should fall back to ghost@[blog.url] as from address', function () {
|
2014-02-24 20:28:07 +04:00
|
|
|
// Standard domain
|
2014-09-03 19:42:55 +04:00
|
|
|
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
|
2014-09-03 19:42:55 +04:00
|
|
|
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
|
2014-09-03 19:42:55 +04:00
|
|
|
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
|
|
|
});
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|