0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-13 22:41:32 -05:00
ghost/core/test/unit/bootstrap_spec.js

226 lines
7.9 KiB
JavaScript
Raw Normal View History

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-01-05 01:40:53 -05:00
/*globals describe, it, beforeEach, afterEach */
var should = require('should'),
sinon = require('sinon'),
when = require('when'),
path = require('path'),
fs = require('fs'),
_ = require('lodash'),
rewire = require("rewire"),
// Thing we are testing
defaultConfig = require('../../../config.example')[process.env.NODE_ENV],
bootstrap = rewire('../../bootstrap'),
config = rewire('../../server/config');
describe('Bootstrap', function () {
var sandbox,
rejectMessage = bootstrap.__get__('rejectMessage'),
overrideConfig = function (newConfig) {
bootstrap.__set__("readConfigFile", sandbox.stub().returns(
_.extend({}, defaultConfig, newConfig)
));
};
beforeEach(function () {
sandbox = sinon.sandbox.create();
});
afterEach(function () {
bootstrap = rewire('../../bootstrap');
sandbox.restore();
});
it('loads the config file if one exists', function (done) {
// the test infrastructure is setup so that there is always config present,
// but we want to overwrite the test to actually load config.example.js, so that any local changes
// don't break the tests
bootstrap.__set__("configFile", path.join(config().paths.appRoot, 'config.example.js'));
bootstrap().then(function (config) {
config.url.should.equal(defaultConfig.url);
config.database.client.should.equal(defaultConfig.database.client);
config.database.connection.should.eql(defaultConfig.database.connection);
config.server.host.should.equal(defaultConfig.server.host);
config.server.port.should.equal(defaultConfig.server.port);
done();
}).then(null, done);
});
it('uses the passed in config file location', function (done) {
bootstrap(path.join(config().paths.appRoot, 'config.example.js')).then(function (config) {
config.url.should.equal(defaultConfig.url);
config.database.client.should.equal(defaultConfig.database.client);
config.database.connection.should.eql(defaultConfig.database.connection);
config.server.host.should.equal(defaultConfig.server.host);
config.server.port.should.equal(defaultConfig.server.port);
done();
}).then(null, done);
});
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-01-05 01:40:53 -05:00
it('creates the config file if one does not exist', function (done) {
var deferred = when.defer(),
// trick bootstrap into thinking that the config file doesn't exist yet
existsStub = sandbox.stub(fs, 'exists', function (file, cb) { return cb(false); }),
// create a method which will return a pre-resolved promise
resolvedPromise = sandbox.stub().returns(deferred.promise);
deferred.resolve();
// ensure that the file creation is a stub, the tests shouldn't really create a file
bootstrap.__set__("writeConfigFile", resolvedPromise);
bootstrap.__set__("validateConfigEnvironment", resolvedPromise);
bootstrap().then(function () {
existsStub.calledOnce.should.be.true;
resolvedPromise.calledTwice.should.be.true;
done();
}).then(null, done);
});
it('accepts valid urls', function (done) {
// replace the config file with invalid data
overrideConfig({url: 'http://testurl.com'});
bootstrap().then(function (localConfig) {
localConfig.url.should.equal('http://testurl.com');
// Next test
overrideConfig({url: 'https://testurl.com'});
return bootstrap();
}).then(function (localConfig) {
localConfig.url.should.equal('https://testurl.com');
// Next test
overrideConfig({url: 'http://testurl.com/blog/'});
return bootstrap();
}).then(function (localConfig) {
localConfig.url.should.equal('http://testurl.com/blog/');
// Next test
overrideConfig({url: 'http://testurl.com/ghostly/'});
return bootstrap();
}).then(function (localConfig) {
localConfig.url.should.equal('http://testurl.com/ghostly/');
// Next test
overrideConfig({url: '//testurl.com'});
return bootstrap();
}).then(function (localConfig) {
localConfig.url.should.equal('//testurl.com');
done();
}).then(null, done);
});
it('rejects invalid urls', function (done) {
// replace the config file with invalid data
overrideConfig({url: 'notvalid'});
bootstrap().otherwise(function (error) {
error.should.include(rejectMessage);
// Next test
overrideConfig({url: 'something.com'});
return bootstrap();
}).otherwise(function (error) {
error.should.include(rejectMessage);
done();
}).then(function () {
should.fail('no error was thrown when it should have been');
done();
}).then(done, null);
});
it('does not permit subdirectories named ghost', function (done) {
// replace the config file with invalid data
overrideConfig({url: 'http://testurl.com/ghost/'});
bootstrap().otherwise(function (error) {
error.should.include(rejectMessage);
// Next test
overrideConfig({url: 'http://testurl.com/ghost/blog/'});
return bootstrap();
}).otherwise(function (error) {
error.should.include(rejectMessage);
// Next test
overrideConfig({url: 'http://testurl.com/blog/ghost'});
return bootstrap();
}).otherwise(function (error) {
error.should.include(rejectMessage);
done();
}).then(function () {
should.fail('no error was thrown when it should have been');
done();
}).then(done, null);
});
it('requires a database config', function (done) {
// replace the config file with invalid data
overrideConfig({database: null});
bootstrap().otherwise(function (error) {
error.should.include(rejectMessage);
// Next test
overrideConfig({database: {}});
return bootstrap();
}).otherwise(function (error) {
error.should.include(rejectMessage);
done();
}).then(function () {
should.fail('no error was thrown when it should have been');
done();
}).then(done, null);
});
it('requires a socket or a host and port', function (done) {
// replace the config file with invalid data
overrideConfig({server: {socket: 'test'}});
bootstrap().then(function (localConfig) {
localConfig.server.socket.should.equal('test');
// Next test
overrideConfig({server: null});
return bootstrap();
}).otherwise(function (error) {
error.should.include(rejectMessage);
// Next test
overrideConfig({server: {host: null}});
return bootstrap();
}).otherwise(function (error) {
error.should.include(rejectMessage);
// Next test
overrideConfig({server: {port: null}});
return bootstrap();
}).otherwise(function (error) {
error.should.include(rejectMessage);
// Next test
overrideConfig({server: {host: null, port: null}});
return bootstrap();
}).otherwise(function (error) {
error.should.include(rejectMessage);
done();
}).then(function () {
should.fail('no error was thrown when it should have been');
done();
}).then(done, null);
});
});