Ghost/core/test/unit/mail_spec.js

168 lines
5.3 KiB
JavaScript
Raw Normal View History

2013-08-21 00:19:47 +04:00
var cp = require('child_process'),
_ = require("underscore"),
when = require('when'),
sinon = require('sinon'),
should = require('should'),
Ghost = require('../../ghost'),
defaultConfig = require('../../../config'),
SMTP,
SENDMAIL,
fakeConfig,
fakeSettings,
fakeSendmail,
sandbox = sinon.sandbox.create(),
ghost;
// 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 () {
beforeEach(function () {
// Mock config and settings
fakeConfig = _.extend({}, defaultConfig);
fakeSettings = {
url: 'http://test.tryghost.org',
email: 'ghost-test@localhost'
};
fakeSendmail = '/fake/bin/sendmail';
ghost = new Ghost();
sandbox.stub(ghost, "config", function () {
return fakeConfig;
});
sandbox.stub(ghost, "settings", function () {
return fakeSettings;
});
sandbox.stub(ghost.mail, "isWindows", function () {
return false;
});
sandbox.stub(ghost.mail, "detectSendmail", function () {
return when.resolve(fakeSendmail);
});
});
afterEach(function () {
sandbox.restore();
});
it('should attach mail provider to ghost instance', function () {
should.exist(ghost.mail);
ghost.mail.should.have.property('init');
ghost.mail.should.have.property('transport');
ghost.mail.should.have.property('send').and.be.a('function');
});
it('should setup SMTP transport on initialization', function (done) {
fakeConfig.env[process.env.NODE_ENV].mail = SMTP;
2013-08-21 00:19:47 +04:00
ghost.mail.init(ghost).then(function(){
ghost.mail.should.have.property('transport');
ghost.mail.transport.transportType.should.eql('SMTP');
ghost.mail.transport.sendMail.should.be.a('function');
done();
}).then(null, done);
});
it('should setup sendmail transport on initialization', function (done) {
fakeConfig.env[process.env.NODE_ENV].mail = SENDMAIL;
2013-08-21 00:19:47 +04:00
ghost.mail.init(ghost).then(function(){
ghost.mail.should.have.property('transport');
ghost.mail.transport.transportType.should.eql('SENDMAIL');
ghost.mail.transport.sendMail.should.be.a('function');
done();
}).then(null, done);
});
it('should fallback to sendmail if no config set', function (done) {
fakeConfig.env[process.env.NODE_ENV].mail = null;
2013-08-21 00:19:47 +04:00
ghost.mail.init(ghost).then(function(){
ghost.mail.should.have.property('transport');
ghost.mail.transport.transportType.should.eql('SENDMAIL');
ghost.mail.transport.options.path.should.eql(fakeSendmail);
done();
}).then(null, done);
});
it('should fallback to sendmail if config is empty', function (done) {
fakeConfig.env[process.env.NODE_ENV].mail = {};
2013-08-21 00:19:47 +04:00
ghost.mail.init(ghost).then(function(){
ghost.mail.should.have.property('transport');
ghost.mail.transport.transportType.should.eql('SENDMAIL');
ghost.mail.transport.options.path.should.eql(fakeSendmail);
done();
}).then(null, done);
});
it('should disable transport if config is empty & sendmail not found', function (done) {
fakeConfig.env[process.env.NODE_ENV].mail = {};
2013-08-21 00:19:47 +04:00
ghost.mail.detectSendmail.restore();
sandbox.stub(ghost.mail, "detectSendmail", when.reject);
ghost.mail.init(ghost).then(function(){
should.not.exist(ghost.mail.transport);
done();
}).then(null, done);
});
it('should disable transport if config is empty & platform is win32', function (done) {
fakeConfig.env[process.env.NODE_ENV].mail = {};
2013-08-21 00:19:47 +04:00
ghost.mail.detectSendmail.restore();
ghost.mail.isWindows.restore();
sandbox.stub(ghost.mail, 'isWindows', function(){ return true });
2013-08-21 00:19:47 +04:00
ghost.mail.init(ghost).then(function(){
should.not.exist(ghost.mail.transport);
done();
}).then(null, done);
});
it('should fail to send messages when no transport is set', function (done) {
ghost.mail.detectSendmail.restore();
sandbox.stub(ghost.mail, "detectSendmail", when.reject);
ghost.mail.init(ghost).then(function(){
ghost.mail.send().then(function(){
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([
ghost.mail.send(),
ghost.mail.send({}),
ghost.mail.send({ subject: '123' }),
ghost.mail.send({ subject: '', html: '123' })
]).then(function (descriptors) {
descriptors.forEach(function (d) {
d.state.should.equal('rejected');
d.reason.should.be.an.instanceOf(Error);
});
done();
});
});
});