mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Updated theme middleware tests
no-issue This allows the tests to not inspect the "internals" of the module, and deal with the middleware array it exports. - Removes two layers of indentation for readablity - Adds executeMiddleware helper - Reuses the global requires, rather than making new globals for stubs
This commit is contained in:
parent
35725f9537
commit
40d74ec3e0
1 changed files with 122 additions and 176 deletions
|
@ -1,208 +1,154 @@
|
||||||
var should = require('should'),
|
const should = require('should');
|
||||||
sinon = require('sinon'),
|
const sinon = require('sinon');
|
||||||
hbs = require('../../../../server/services/themes/engine'),
|
const hbs = require('../../../../server/services/themes/engine');
|
||||||
themes = require('../../../../server/services/themes'),
|
const themes = require('../../../../server/services/themes');
|
||||||
// is only exposed via themes.getActive()
|
// is only exposed via themes.getActive()
|
||||||
activeTheme = require('../../../../server/services/themes/active'),
|
const activeTheme = require('../../../../server/services/themes/active');
|
||||||
settingsCache = require('../../../../server/services/settings/cache'),
|
const settingsCache = require('../../../../server/services/settings/cache');
|
||||||
middleware = themes.middleware;
|
const middleware = themes.middleware;
|
||||||
|
|
||||||
describe('Themes', function () {
|
const sandbox = sinon.sandbox.create();
|
||||||
|
|
||||||
|
function executeMiddleware(middleware, req, res, next) {
|
||||||
|
const [current, ...rest] = middleware;
|
||||||
|
|
||||||
|
current(req, res, function (err) {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
if (!rest.length) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
return executeMiddleware(rest, req, res, next);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('Themes middleware', function () {
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
sinon.restore();
|
sandbox.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Middleware', function () {
|
let req;
|
||||||
var req, res, blogApp, getActiveThemeStub, settingsCacheStub, settingPublicStub;
|
let res;
|
||||||
|
|
||||||
beforeEach(function () {
|
let fakeActiveTheme;
|
||||||
req = sinon.spy();
|
let fakeActiveThemeName;
|
||||||
res = sinon.spy();
|
let fakeSiteData;
|
||||||
|
let fakeLabsData;
|
||||||
|
|
||||||
blogApp = {test: 'obj'};
|
beforeEach(function () {
|
||||||
req.app = blogApp;
|
req = {app: {}};
|
||||||
res.locals = {};
|
res = {locals: {}};
|
||||||
|
|
||||||
getActiveThemeStub = sinon.stub(activeTheme, 'get');
|
fakeActiveTheme = {
|
||||||
settingsCacheStub = sinon.stub(settingsCache, 'get');
|
config: sandbox.stub().returns(2),
|
||||||
settingPublicStub = sinon.stub(settingsCache, 'getPublic').returns({});
|
mount: sandbox.stub()
|
||||||
|
};
|
||||||
|
|
||||||
|
fakeActiveThemeName = 'bacon-sensation';
|
||||||
|
|
||||||
|
fakeSiteData = {};
|
||||||
|
|
||||||
|
fakeLabsData = {
|
||||||
|
// labs data is deep cloned,
|
||||||
|
// if we want to compare it
|
||||||
|
// we will need some unique content
|
||||||
|
'@@REQUIRED@@': true
|
||||||
|
};
|
||||||
|
|
||||||
|
sandbox.stub(activeTheme, 'get')
|
||||||
|
.returns(fakeActiveTheme);
|
||||||
|
|
||||||
|
sandbox.stub(settingsCache, 'get')
|
||||||
|
.withArgs('labs').returns(fakeLabsData)
|
||||||
|
.withArgs('active_theme').returns(fakeActiveThemeName);
|
||||||
|
|
||||||
|
sandbox.stub(settingsCache, 'getPublic')
|
||||||
|
.returns(fakeSiteData);
|
||||||
|
|
||||||
|
sandbox.stub(hbs, 'updateTemplateOptions');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('mounts active theme if not yet mounted', function (done) {
|
||||||
|
fakeActiveTheme.mounted = false;
|
||||||
|
|
||||||
|
executeMiddleware(middleware, req, res, function next(err) {
|
||||||
|
should.not.exist(err);
|
||||||
|
|
||||||
|
fakeActiveTheme.mount.called.should.be.true();
|
||||||
|
fakeActiveTheme.mount.calledWith(req.app).should.be.true();
|
||||||
|
|
||||||
|
done();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('ensureActiveTheme', function () {
|
it('does not mounts the active theme if it is already mounted', function (done) {
|
||||||
var ensureActiveTheme = middleware[0],
|
fakeActiveTheme.mounted = true;
|
||||||
mountThemeSpy;
|
|
||||||
|
|
||||||
beforeEach(function () {
|
executeMiddleware(middleware, req, res, function next(err) {
|
||||||
mountThemeSpy = sinon.spy();
|
should.not.exist(err);
|
||||||
settingsCacheStub.withArgs('active_theme').returns('casper');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('mounts active theme if not yet mounted', function (done) {
|
fakeActiveTheme.mount.called.should.be.false();
|
||||||
getActiveThemeStub.returns({
|
|
||||||
mounted: false,
|
|
||||||
mount: mountThemeSpy
|
|
||||||
});
|
|
||||||
|
|
||||||
ensureActiveTheme(req, res, function next(err) {
|
done();
|
||||||
// Did not throw an error
|
|
||||||
should.not.exist(err);
|
|
||||||
|
|
||||||
settingsCacheStub.called.should.be.false();
|
|
||||||
getActiveThemeStub.called.should.be.true();
|
|
||||||
mountThemeSpy.called.should.be.true();
|
|
||||||
mountThemeSpy.calledWith(blogApp).should.be.true();
|
|
||||||
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not mounts the active theme if it is already mounted', function (done) {
|
|
||||||
getActiveThemeStub.returns({
|
|
||||||
mounted: true,
|
|
||||||
mount: mountThemeSpy
|
|
||||||
});
|
|
||||||
|
|
||||||
ensureActiveTheme(req, res, function next(err) {
|
|
||||||
// Did not throw an error
|
|
||||||
should.not.exist(err);
|
|
||||||
|
|
||||||
settingsCacheStub.called.should.be.false();
|
|
||||||
getActiveThemeStub.called.should.be.true();
|
|
||||||
mountThemeSpy.called.should.be.false();
|
|
||||||
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('throws error if theme is missing', function (done) {
|
|
||||||
getActiveThemeStub.returns(undefined);
|
|
||||||
|
|
||||||
ensureActiveTheme(req, res, function next(err) {
|
|
||||||
// Did throw an error
|
|
||||||
should.exist(err);
|
|
||||||
err.message.should.eql('The currently active theme "casper" is missing.');
|
|
||||||
|
|
||||||
settingsCacheStub.calledWith('active_theme').should.be.true();
|
|
||||||
getActiveThemeStub.called.should.be.true();
|
|
||||||
mountThemeSpy.called.should.be.false();
|
|
||||||
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('updateTemplateData', function () {
|
it('throws error if theme is missing', function (done) {
|
||||||
var updateTemplateData = middleware[1],
|
activeTheme.get.restore();
|
||||||
themeDataExpectedProps = ['posts_per_page', 'image_sizes'],
|
sandbox.stub(activeTheme, 'get')
|
||||||
updateOptionsStub;
|
.returns(undefined);
|
||||||
|
|
||||||
beforeEach(function () {
|
executeMiddleware(middleware, req, res, function next(err) {
|
||||||
updateOptionsStub = sinon.stub(hbs, 'updateTemplateOptions');
|
// Did throw an error
|
||||||
|
should.exist(err);
|
||||||
|
err.message.should.eql('The currently active theme "bacon-sensation" is missing.');
|
||||||
|
|
||||||
settingsCacheStub.withArgs('labs').returns({});
|
activeTheme.get.called.should.be.true();
|
||||||
|
fakeActiveTheme.mount.called.should.be.false();
|
||||||
|
|
||||||
getActiveThemeStub.returns({
|
done();
|
||||||
config: sinon.stub().returns(2)
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
it('calls updateTemplateOptions with correct data', function (done) {
|
it('calls updateTemplateOptions with correct data', function (done) {
|
||||||
updateTemplateData(req, res, function next(err) {
|
const themeDataExpectedProps = ['posts_per_page', 'image_sizes'];
|
||||||
var templateOptions;
|
|
||||||
should.not.exist(err);
|
|
||||||
|
|
||||||
updateOptionsStub.calledOnce.should.be.true();
|
executeMiddleware(middleware, req, res, function next(err) {
|
||||||
templateOptions = updateOptionsStub.firstCall.args[0];
|
should.not.exist(err);
|
||||||
templateOptions.should.be.an.Object().with.property('data');
|
|
||||||
templateOptions.data.should.be.an.Object().with.properties('blog', 'labs', 'config');
|
|
||||||
|
|
||||||
// Check Theme Config
|
hbs.updateTemplateOptions.calledOnce.should.be.true();
|
||||||
templateOptions.data.config.should.be.an.Object()
|
const templateOptions = hbs.updateTemplateOptions.firstCall.args[0];
|
||||||
.with.properties(themeDataExpectedProps)
|
const data = templateOptions.data;
|
||||||
.and.size(themeDataExpectedProps.length);
|
|
||||||
// posts per page should be set according to the stub
|
|
||||||
templateOptions.data.config.posts_per_page.should.eql(2);
|
|
||||||
|
|
||||||
// Check blog config tried to call public settings
|
data.should.be.an.Object().with.properties('site', 'blog', 'labs', 'config');
|
||||||
settingPublicStub.calledOnce.should.be.true();
|
|
||||||
|
|
||||||
// url should be correct
|
// Check Theme Config
|
||||||
templateOptions.data.blog.url.should.eql('http://127.0.0.1:2369');
|
data.config.should.be.an.Object()
|
||||||
|
.with.properties(themeDataExpectedProps)
|
||||||
|
.and.size(themeDataExpectedProps.length);
|
||||||
|
// posts per page should be set according to the stub
|
||||||
|
data.config.posts_per_page.should.eql(2);
|
||||||
|
|
||||||
// Check labs config
|
// Check labs config
|
||||||
templateOptions.data.labs.should.be.an.Object();
|
should.deepEqual(data.labs, fakeLabsData);
|
||||||
|
|
||||||
// Check res.locals
|
should.equal(data.site, fakeSiteData);
|
||||||
should.not.exist(res.locals.secure);
|
should.equal(data.blog, fakeSiteData);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not error if there is no active theme', function (done) {
|
it('Sets res.locals.secure to the value of req.secure', function (done) {
|
||||||
getActiveThemeStub.returns(undefined);
|
req.secure = Math.random() < 0.5;
|
||||||
|
|
||||||
updateTemplateData(req, res, function next(err) {
|
executeMiddleware(middleware, req, res, function next(err) {
|
||||||
var templateOptions;
|
should.not.exist(err);
|
||||||
should.not.exist(err);
|
|
||||||
|
|
||||||
updateOptionsStub.calledOnce.should.be.true();
|
should.equal(res.locals.secure, req.secure);
|
||||||
templateOptions = updateOptionsStub.firstCall.args[0];
|
|
||||||
templateOptions.should.be.an.Object().with.property('data');
|
|
||||||
templateOptions.data.should.be.an.Object().with.properties('blog', 'labs', 'config');
|
|
||||||
|
|
||||||
// Check Theme Config
|
done();
|
||||||
templateOptions.data.config.should.be.an.Object();
|
|
||||||
// posts per page should NOT be set as there's no active theme
|
|
||||||
should.not.exist(templateOptions.data.config.posts_per_page);
|
|
||||||
|
|
||||||
// Check blog config tried to call public settings
|
|
||||||
settingPublicStub.calledOnce.should.be.true();
|
|
||||||
|
|
||||||
// url should be correct
|
|
||||||
templateOptions.data.blog.url.should.eql('http://127.0.0.1:2369');
|
|
||||||
|
|
||||||
// Check labs config
|
|
||||||
templateOptions.data.labs.should.be.an.Object();
|
|
||||||
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('calls updateTempalateOptions with correct info for secure context', function (done) {
|
|
||||||
req.secure = true;
|
|
||||||
|
|
||||||
updateTemplateData(req, res, function next(err) {
|
|
||||||
var templateOptions;
|
|
||||||
should.not.exist(err);
|
|
||||||
|
|
||||||
updateOptionsStub.calledOnce.should.be.true();
|
|
||||||
templateOptions = updateOptionsStub.firstCall.args[0];
|
|
||||||
templateOptions.should.be.an.Object().with.property('data');
|
|
||||||
templateOptions.data.should.be.an.Object().with.properties('blog', 'labs', 'config');
|
|
||||||
|
|
||||||
// Check Theme Config
|
|
||||||
templateOptions.data.config.should.be.an.Object()
|
|
||||||
.with.properties(themeDataExpectedProps)
|
|
||||||
.and.size(themeDataExpectedProps.length);
|
|
||||||
// posts per page should be set according to the stub
|
|
||||||
templateOptions.data.config.posts_per_page.should.eql(2);
|
|
||||||
|
|
||||||
// Check blog config tried to call public settings
|
|
||||||
settingPublicStub.calledOnce.should.be.true();
|
|
||||||
// url should be correct HTTPS!
|
|
||||||
templateOptions.data.blog.url.should.eql('https://127.0.0.1:2369');
|
|
||||||
|
|
||||||
// Check labs config
|
|
||||||
templateOptions.data.labs.should.be.an.Object();
|
|
||||||
|
|
||||||
// Check res.locals
|
|
||||||
should.exist(res.locals.secure);
|
|
||||||
res.locals.secure.should.be.true();
|
|
||||||
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue