Ghost/core/test/unit/mail_spec.js
Harry Wolff f16dc290b7 Improve bootstrap flow of a Ghost application
addresses #1789, #1364

- Moves ./core/server/loader -> ./core/bootstrap.
The bootstrap file is only accessed once during startup,
and it’s sole job is to ensure a config.js file exists
(creating one if it doesn’t) and then validates
the contents of the config file.

Since this is directly related to the initializing 
the application is is appropriate to have 
it in the ./core folder, named bootstrap as that
is what it does.

This also improves the dependency graph, as now
the bootstrap file require’s the ./core/server/config
module and is responsible for passing in the validated
config file.

Whereas before we had ./core/server/config
require’ing ./core/server/loader and running its
init code and then passing that value back to itself,
the flow is now more straight forward of
./core/bootstrap handling initialization and then
instatiation of config module

- Merges ./core/server/config/paths into 
./core/server/config
This flow was always confusing me to that some config
options were on the config object, and some were on
the paths object.

This change now incorporates all of the variables
previously defined in config/paths directly
into the config module, and in extension,
the config.js file.

This means that you now have the option of deciding
at startup where the content directory for ghost
should reside.

- broke out loader tests in config_spec to bootstrap_spec

- updated all relevant files to now use config().paths

- moved urlFor and urlForPost function into 
 ./server/config/url.js
2014-02-07 17:34:21 -05:00

171 lines
5.3 KiB
JavaScript

/*globals describe, beforeEach, afterEach, it*/
var should = require('should'),
sinon = require('sinon'),
when = require('when'),
_ = require("lodash"),
cp = require('child_process'),
rewire = require("rewire"),
testUtils = require('../utils'),
// Stuff we are testing
mailer = rewire('../../server/mail'),
defaultConfig = require('../../../config'),
SMTP,
SENDMAIL,
fakeConfig,
fakeSettings,
fakeSendmail,
sandbox = sinon.sandbox.create(),
config;
// 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 () {
var overrideConfig = function (newConfig) {
mailer.__set__('config', sandbox.stub().returns(
_.extend({}, defaultConfig, newConfig)
));
};
beforeEach(function () {
// Mock config and settings
fakeConfig = _.extend({}, defaultConfig);
fakeSettings = {
url: 'http://test.tryghost.org',
email: 'ghost-test@localhost'
};
fakeSendmail = '/fake/bin/sendmail';
config = sinon.stub().returns(fakeConfig);
sandbox.stub(mailer, "isWindows", function () {
return false;
});
sandbox.stub(mailer, "detectSendmail", function () {
return when.resolve(fakeSendmail);
});
});
afterEach(function () {
sandbox.restore();
});
it('should attach mail provider to ghost instance', function () {
should.exist(mailer);
mailer.should.have.property('init');
mailer.should.have.property('transport');
mailer.should.have.property('send').and.be.a.function;
});
it('should setup SMTP transport on initialization', function (done) {
overrideConfig({mail: SMTP});
mailer.init().then(function () {
mailer.should.have.property('transport');
mailer.transport.transportType.should.eql('SMTP');
mailer.transport.sendMail.should.be.a.function;
done();
}).then(null, done);
});
it('should setup sendmail transport on initialization', function (done) {
overrideConfig({mail: SENDMAIL});
mailer.init().then(function () {
mailer.should.have.property('transport');
mailer.transport.transportType.should.eql('SENDMAIL');
mailer.transport.sendMail.should.be.a.function;
done();
}).then(null, done);
});
it('should fallback to sendmail if no config set', function (done) {
overrideConfig({mail: null});
mailer.init().then(function () {
mailer.should.have.property('transport');
mailer.transport.transportType.should.eql('SENDMAIL');
mailer.transport.options.path.should.eql(fakeSendmail);
done();
}).then(null, done);
});
it('should fallback to sendmail if config is empty', function (done) {
overrideConfig({mail: {}});
mailer.init().then(function () {
mailer.should.have.property('transport');
mailer.transport.transportType.should.eql('SENDMAIL');
mailer.transport.options.path.should.eql(fakeSendmail);
done();
}).then(null, done);
});
it('should disable transport if config is empty & sendmail not found', function (done) {
overrideConfig({mail: {}});
mailer.detectSendmail.restore();
sandbox.stub(mailer, "detectSendmail", when.reject);
mailer.init().then(function () {
should.not.exist(mailer.transport);
done();
}).then(null, done);
});
it('should disable transport if config is empty & platform is win32', function (done) {
overrideConfig({mail: {}});
mailer.detectSendmail.restore();
mailer.isWindows.restore();
sandbox.stub(mailer, 'isWindows', function () {
return true;
});
mailer.init().then(function () {
should.not.exist(mailer.transport);
done();
}).then(null, done);
});
it('should fail to send messages when no transport is set', function (done) {
mailer.detectSendmail.restore();
sandbox.stub(mailer, "detectSendmail", when.reject);
mailer.init().then(function () {
mailer.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([
mailer.send(),
mailer.send({}),
mailer.send({ subject: '123' }),
mailer.send({ subject: '', html: '123' })
]).then(function (descriptors) {
descriptors.forEach(function (d) {
d.state.should.equal('rejected');
d.reason.should.be.an.instanceOf(Error);
});
done();
});
});
});