0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/core/test/unit/config_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

364 lines
No EOL
14 KiB
JavaScript

/*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"),
testUtils = require('../utils'),
// Thing we are testing
defaultConfig = require('../../../config.example')[process.env.NODE_ENV],
theme = rewire('../../server/config/theme'),
config = rewire('../../server/config'),
configUpdate = config.__get__('updateConfig');
describe('Config', function () {
describe('Theme', function () {
var sandbox,
settings,
settingsStub;
beforeEach(function (done) {
sandbox = sinon.sandbox.create();
settings = {'read': function read() {}};
settingsStub = sandbox.stub(settings, 'read', function () {
return when({value: 'casper'});
});
theme.update(settings, 'http://my-ghost-blog.com')
.then(done)
.then(null, done);
});
afterEach(function (done) {
theme.update(settings, defaultConfig.url)
.then(done)
.then(null, done);
sandbox.restore();
});
it('should have exactly the right keys', function () {
var themeConfig = theme();
// This will fail if there are any extra keys
themeConfig.should.have.keys('url', 'title', 'description', 'logo', 'cover');
});
it('should have the correct values for each key', function () {
var themeConfig = theme();
// Check values are as we expect
themeConfig.should.have.property('url', 'http://my-ghost-blog.com');
themeConfig.should.have.property('title', 'casper');
themeConfig.should.have.property('description', 'casper');
themeConfig.should.have.property('logo', 'casper');
themeConfig.should.have.property('cover', 'casper');
// Check settings.read gets called exactly 4 times
settingsStub.callCount.should.equal(4);
});
});
describe('Index', function () {
var defaultContentPath = config().paths.contentPath;
afterEach(function () {
configUpdate({
url: defaultConfig.url,
paths: {
contentPath: defaultContentPath
}
});
});
it('should have exactly the right keys', function () {
var pathConfig = config().paths;
// This will fail if there are any extra keys
pathConfig.should.have.keys(
'appRoot',
'subdir',
'config',
'configExample',
'contentPath',
'corePath',
'themePath',
'appPath',
'imagesPath',
'imagesRelPath',
'adminViews',
'helperTemplates',
'exportPath',
'lang',
'debugPath',
'availableThemes',
'availableApps',
'builtScriptPath'
);
});
it('should have the correct values for each key', function () {
var pathConfig = config().paths,
appRoot = path.resolve(__dirname, '../../../');
pathConfig.should.have.property('appRoot', appRoot);
pathConfig.should.have.property('subdir', '');
});
it('should not return a slash for subdir', function () {
configUpdate({url: 'http://my-ghost-blog.com'});
config().paths.should.have.property('subdir', '');
configUpdate({url: 'http://my-ghost-blog.com/'});
config().paths.should.have.property('subdir', '');
});
it('should handle subdirectories properly', function () {
configUpdate({url: 'http://my-ghost-blog.com/blog'});
config().paths.should.have.property('subdir', '/blog');
configUpdate({url: 'http://my-ghost-blog.com/blog/'});
config().paths.should.have.property('subdir', '/blog');
configUpdate({url: 'http://my-ghost-blog.com/my/blog'});
config().paths.should.have.property('subdir', '/my/blog');
configUpdate({url: 'http://my-ghost-blog.com/my/blog/'});
config().paths.should.have.property('subdir', '/my/blog');
});
it('should set contentPath and sub-directories correctly', function () {
var contentPath = config().paths.appRoot + '/otherContent/';
configUpdate({
paths: {
contentPath: contentPath
}
});
config().paths.should.have.property('contentPath', contentPath);
config().paths.should.have.property('themePath', contentPath + 'themes');
config().paths.should.have.property('appPath', contentPath + 'apps');
config().paths.should.have.property('imagesPath', contentPath + 'images');
});
});
describe('urlFor', function () {
afterEach(function () {
configUpdate({url: defaultConfig.url});
});
it('should return the home url with no options', function () {
config.urlFor().should.equal('/');
configUpdate({url: 'http://my-ghost-blog.com/blog'});
config.urlFor().should.equal('/blog/');
});
it('should return home url when asked for', function () {
var testContext = 'home';
configUpdate({url: 'http://my-ghost-blog.com'});
config.urlFor(testContext).should.equal('/');
config.urlFor(testContext, true).should.equal('http://my-ghost-blog.com/');
configUpdate({url: 'http://my-ghost-blog.com/blog'});
config.urlFor(testContext).should.equal('/blog/');
config.urlFor(testContext, true).should.equal('http://my-ghost-blog.com/blog/');
});
it('should return rss url when asked for', function () {
var testContext = 'rss';
configUpdate({url: 'http://my-ghost-blog.com'});
config.urlFor(testContext).should.equal('/rss/');
config.urlFor(testContext, true).should.equal('http://my-ghost-blog.com/rss/');
configUpdate({url: 'http://my-ghost-blog.com/blog'});
config.urlFor(testContext).should.equal('/blog/rss/');
config.urlFor(testContext, true).should.equal('http://my-ghost-blog.com/blog/rss/');
});
it('should return url for a random path when asked for', function () {
var testContext = {relativeUrl: '/about/'};
configUpdate({url: 'http://my-ghost-blog.com'});
config.urlFor(testContext).should.equal('/about/');
config.urlFor(testContext, true).should.equal('http://my-ghost-blog.com/about/');
configUpdate({url: 'http://my-ghost-blog.com/blog'});
config.urlFor(testContext).should.equal('/blog/about/');
config.urlFor(testContext, true).should.equal('http://my-ghost-blog.com/blog/about/');
});
it('should return url for a post when asked for', function () {
var testContext = 'post',
testData = {post: testUtils.DataGenerator.Content.posts[2], permalinks: {value: '/:slug/'}};
configUpdate({url: 'http://my-ghost-blog.com'});
config.urlFor(testContext, testData).should.equal('/short-and-sweet/');
config.urlFor(testContext, testData, true).should.equal('http://my-ghost-blog.com/short-and-sweet/');
configUpdate({url: 'http://my-ghost-blog.com/blog'});
config.urlFor(testContext, testData).should.equal('/blog/short-and-sweet/');
config.urlFor(testContext, testData, true).should.equal('http://my-ghost-blog.com/blog/short-and-sweet/');
});
it('should return url for a dated post when asked for', function () {
var testContext = 'post',
testData = {
post: testUtils.DataGenerator.Content.posts[2],
permalinks: {value: '/:year/:month/:day/:slug/'}
},
today = new Date(),
dd = ("0" + today.getDate()).slice(-2),
mm = ("0" + (today.getMonth() + 1)).slice(-2),
yyyy = today.getFullYear(),
postLink = '/' + yyyy + '/' + mm + '/' + dd + '/short-and-sweet/';
configUpdate({url: 'http://my-ghost-blog.com'});
config.urlFor(testContext, testData).should.equal(postLink);
config.urlFor(testContext, testData, true).should.equal('http://my-ghost-blog.com' + postLink);
configUpdate({url: 'http://my-ghost-blog.com/blog'});
config.urlFor(testContext, testData).should.equal('/blog' + postLink);
config.urlFor(testContext, testData, true).should.equal('http://my-ghost-blog.com/blog' + postLink);
});
});
describe('urlForPost', function () {
var sandbox;
beforeEach(function () {
sandbox = sinon.sandbox.create();
});
afterEach(function () {
sandbox.restore();
configUpdate({url: defaultConfig.url});
});
it('should output correct url for post', function (done) {
var settings = {'read': function read() {}},
settingsStub = sandbox.stub(settings, 'read', function () {
return when({value: '/:slug/'});
}),
testData = testUtils.DataGenerator.Content.posts[2],
postLink = '/short-and-sweet/';
configUpdate({url: 'http://my-ghost-blog.com'});
// next test
config.urlForPost(settings, testData).then(function (url) {
url.should.equal(postLink);
// next test
return config.urlForPost(settings, testData, true);
}).then(function (url) {
url.should.equal('http://my-ghost-blog.com' + postLink);
return configUpdate({url: 'http://my-ghost-blog.com/blog'});
}).then(function () {
// next test
return config.urlForPost(settings, testData);
}).then(function (url) {
url.should.equal('/blog' + postLink);
// next test
return config.urlForPost(settings, testData, true);
}).then(function (url) {
url.should.equal('http://my-ghost-blog.com/blog' + postLink);
done();
}).then(null, done);
});
it('should output correct url for post with date permalink', function (done) {
var settings = {'read': function read() {}},
settingsStub = sandbox.stub(settings, 'read', function () {
return when({value: '/:year/:month/:day/:slug/'});
}),
testData = testUtils.DataGenerator.Content.posts[2],
today = new Date(),
dd = ("0" + today.getDate()).slice(-2),
mm = ("0" + (today.getMonth() + 1)).slice(-2),
yyyy = today.getFullYear(),
postLink = '/' + yyyy + '/' + mm + '/' + dd + '/short-and-sweet/';
configUpdate({url: 'http://my-ghost-blog.com'});
// next test
config.urlForPost(settings, testData).then(function (url) {
url.should.equal(postLink);
// next test
return config.urlForPost(settings, testData, true);
}).then(function (url) {
url.should.equal('http://my-ghost-blog.com' + postLink);
return configUpdate({url: 'http://my-ghost-blog.com/blog'});
}).then(function () {
// next test
return config.urlForPost(settings, testData);
}).then(function (url) {
url.should.equal('/blog' + postLink);
// next test
return config.urlForPost(settings, testData, true);
}).then(function (url) {
url.should.equal('http://my-ghost-blog.com/blog' + postLink);
done();
}).then(null, done);
});
it('should output correct url for page with date permalink', function (done) {
var settings = {'read': function read() {}},
settingsStub = sandbox.stub(settings, 'read', function () {
return when({value: '/:year/:month/:day/:slug/'});
}),
testData = testUtils.DataGenerator.Content.posts[5],
postLink = '/static-page-test/';
configUpdate({url: 'http://my-ghost-blog.com'});
// next test
config.urlForPost(settings, testData).then(function (url) {
url.should.equal(postLink);
// next test
return config.urlForPost(settings, testData, true);
}).then(function (url) {
url.should.equal('http://my-ghost-blog.com' + postLink);
return configUpdate({url: 'http://my-ghost-blog.com/blog'});
}).then(function () {
// next test
return config.urlForPost(settings, testData);
}).then(function (url) {
url.should.equal('/blog' + postLink);
// next test
return config.urlForPost(settings, testData, true);
}).then(function (url) {
url.should.equal('http://my-ghost-blog.com/blog' + postLink);
done();
}).then(null, done);
});
});
});