Ghost/core/test/unit/middleware/theme-handler_spec.js
Hannah Wolfe b06f03b370 New fully-loaded & validated activeTheme concept (#8146)
📡 Add debug for the 3 theme activation methods
There are 3 different ways that a theme can be activated in Ghost:

A. On boot: we load the active theme from the file system, according to the `activeTheme` setting
B. On API "activate": when an /activate/ request is triggered for a theme, we validate & change the `activeTheme` setting
C. On API "override": if uploading a theme with the same name, we override. Using a dirty hack to make this work.

A: setting is done, should load & validate + next request does mounting
B: load is done, should validate & change setting + next request does mounting
C: load, validate & setting are all done + a hack is needed to ensure the next request does mounting

 Validate w/ gscan when theme activating on boot
- use the new gscan validation validate.check() method when activating on boot

 New concept of active theme
- add ActiveTheme class
- make it possible to set a theme to be active, and to get the active theme
- call the new themes.activate() method in all 3 cases where we activate a theme

🎨 Use new activeTheme to simplify theme code
- make use of the new concept where we can, to reduce & simplify code
- use new hasPartials() method so we don't have to do file lookups
- use path & name getters to reduce use of getContentPath etc
- remove requirement on req.app.get('activeTheme') from static-theme middleware (more on this soon)

🚨 Improve theme unit tests (TODO: fix inter-dep)
- The theme unit tests are borked! They all pass because they don't test the right things.
- This improves them, but they are still dependent on each-other
- configHbsForContext tests don't pass if the activateTheme tests aren't run first
- I will fix this in a later PR
2017-03-13 21:13:17 +01:00

173 lines
6.0 KiB
JavaScript

var sinon = require('sinon'),
should = require('should'),
express = require('express'),
hbs = require('express-hbs'),
configUtils = require('../../utils/configUtils'),
themeUtils = require('../../../server/themes'),
themeList = themeUtils.list,
themeHandler = require('../../../server/middleware/theme-handler'),
settingsCache = require('../../../server/settings/cache'),
sandbox = sinon.sandbox.create();
describe('Theme Handler', function () {
var req, res, blogApp, getActiveThemeStub;
beforeEach(function () {
req = sinon.spy();
res = sinon.spy();
blogApp = express();
req.app = blogApp;
getActiveThemeStub = sandbox.stub(themeUtils, 'getActive').returns({});
});
afterEach(function () {
sandbox.restore();
themeList.init();
});
describe('activateTheme', function () {
var hbsStub;
beforeEach(function () {
hbsStub = sandbox.spy(hbs, 'express3');
});
it('should activate new theme with partials', function () {
getActiveThemeStub.returns({
name: 'casper',
path: 'my/fake/path',
partialsPath: 'my/fake/path/partials',
hasPartials: function () {return true;}
});
themeHandler.activateTheme(blogApp);
// hasPartials, partialsPath, path & name
getActiveThemeStub.callCount.should.be.eql(4);
hbsStub.calledOnce.should.be.true();
hbsStub.firstCall.args[0].should.be.an.Object().and.have.property('partialsDir');
hbsStub.firstCall.args[0].partialsDir.should.be.an.Array().with.lengthOf(2);
hbsStub.firstCall.args[0].partialsDir[1].should.eql('my/fake/path/partials');
// Check the asset hash gets reset
should(configUtils.config.get('assetHash')).eql(null);
blogApp.get('activeTheme').should.equal('casper');
blogApp.get('views').should.eql('my/fake/path');
});
it('should activate new theme without partials', function () {
getActiveThemeStub.returns({
name: 'casper',
path: 'my/fake/path',
hasPartials: function () {return false;}
});
themeHandler.activateTheme(blogApp);
// hasPartials, path & name
getActiveThemeStub.callCount.should.eql(3);
hbsStub.calledOnce.should.be.true();
hbsStub.firstCall.args[0].should.be.an.Object().and.have.property('partialsDir');
hbsStub.firstCall.args[0].partialsDir.should.have.lengthOf(1);
// Check the asset hash gets reset
should(configUtils.config.get('assetHash')).eql(null);
blogApp.get('activeTheme').should.equal('casper');
blogApp.get('views').should.eql('my/fake/path');
});
});
// NOTE: These tests are totally dependent on the previous tests
// @TODO: properly fix these tests once theme refactor is finished
describe('configHbsForContext', function () {
var updateOptionsSpy;
beforeEach(function () {
updateOptionsSpy = sandbox.spy(hbs, 'updateTemplateOptions');
});
it('handles non secure context', function (done) {
res.locals = {};
themeHandler.configHbsForContext(req, res, function next() {
updateOptionsSpy.calledOnce.should.be.true();
should.not.exist(res.locals.secure);
done();
});
});
it('handles secure context', function (done) {
req.secure = true;
res.locals = {};
themeHandler.configHbsForContext(req, res, function next() {
updateOptionsSpy.calledOnce.should.be.true();
should.exist(res.locals.secure);
res.locals.secure.should.be.true();
done();
});
});
});
describe('updateActiveTheme', function () {
var activateThemeStub,
settingsCacheStub;
beforeEach(function () {
activateThemeStub = sandbox.stub(themeHandler, 'activateTheme');
settingsCacheStub = sandbox.stub(settingsCache, 'get').withArgs('activeTheme').returns('casper');
});
it('updates the active theme if changed', function (done) {
blogApp.set('activeTheme', 'not-casper');
themeHandler.updateActiveTheme(req, res, function next(err) {
// Did not throw an error
should.not.exist(err);
settingsCacheStub.calledWith('activeTheme').should.be.true();
getActiveThemeStub.called.should.be.true();
activateThemeStub.called.should.be.true();
activateThemeStub.calledWith(blogApp).should.be.true();
done();
});
});
it('does not update the active theme if not changed', function (done) {
blogApp.set('activeTheme', 'casper');
themeHandler.updateActiveTheme(req, res, function next(err) {
// Did not throw an error
should.not.exist(err);
settingsCacheStub.calledWith('activeTheme').should.be.true();
getActiveThemeStub.called.should.be.true();
activateThemeStub.called.should.be.false();
done();
});
});
it('throws error if theme is missing', function (done) {
getActiveThemeStub.returns(undefined);
themeHandler.updateActiveTheme(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('activeTheme').should.be.true();
getActiveThemeStub.called.should.be.true();
activateThemeStub.called.should.be.false();
done();
});
});
});
});