Ghost/core/test/unit/mail_spec.js

174 lines
5.3 KiB
JavaScript
Raw Normal View History

/*globals describe, beforeEach, afterEach, it*/
var testUtils = require('../utils'),
2013-08-21 00:19:47 +04:00
should = require('should'),
sinon = require('sinon'),
when = require('when'),
_ = require("underscore"),
cp = require('child_process'),
// Stuff we are testing
2013-08-21 00:19:47 +04:00
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;
2013-08-21 00:19:47 +04:00
});
it('should setup SMTP transport on initialization', function (done) {
fakeConfig.mail = SMTP;
2013-11-11 14:37:09 +04:00
ghost.mail.init(ghost).then(function () {
2013-08-21 00:19:47 +04:00
ghost.mail.should.have.property('transport');
ghost.mail.transport.transportType.should.eql('SMTP');
ghost.mail.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) {
fakeConfig.mail = SENDMAIL;
2013-11-11 14:37:09 +04:00
ghost.mail.init(ghost).then(function () {
2013-08-21 00:19:47 +04:00
ghost.mail.should.have.property('transport');
ghost.mail.transport.transportType.should.eql('SENDMAIL');
ghost.mail.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) {
fakeConfig.mail = null;
2013-11-11 14:37:09 +04:00
ghost.mail.init(ghost).then(function () {
2013-08-21 00:19:47 +04:00
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.mail = {};
2013-11-11 14:37:09 +04:00
ghost.mail.init(ghost).then(function () {
2013-08-21 00:19:47 +04:00
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.mail = {};
2013-08-21 00:19:47 +04:00
ghost.mail.detectSendmail.restore();
sandbox.stub(ghost.mail, "detectSendmail", when.reject);
2013-11-11 14:37:09 +04:00
ghost.mail.init(ghost).then(function () {
2013-08-21 00:19:47 +04:00
should.not.exist(ghost.mail.transport);
done();
}).then(null, done);
});
it('should disable transport if config is empty & platform is win32', function (done) {
fakeConfig.mail = {};
2013-08-21 00:19:47 +04:00
ghost.mail.detectSendmail.restore();
ghost.mail.isWindows.restore();
2013-11-11 14:37:09 +04:00
sandbox.stub(ghost.mail, 'isWindows', function () {
return true;
});
ghost.mail.init(ghost).then(function () {
2013-08-21 00:19:47 +04:00
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);
2013-11-11 14:37:09 +04:00
ghost.mail.init(ghost).then(function () {
ghost.mail.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([
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();
});
});
});