Ghost/core/test/unit/middleware/static-theme_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

126 lines
3.9 KiB
JavaScript

var sinon = require('sinon'),
should = require('should'),
express = require('express'),
themeUtils = require('../../../server/themes'),
staticTheme = require('../../../server/middleware/static-theme'),
sandbox = sinon.sandbox.create();
describe('staticTheme', function () {
var expressStaticStub, activeThemeStub, req, res;
beforeEach(function () {
req = {};
res = {};
activeThemeStub = sandbox.stub(themeUtils, 'getActive').returns({
path: 'my/fake/path'
});
expressStaticStub = sandbox.spy(express, 'static');
});
afterEach(function () {
sandbox.restore();
});
it('should skip for .hbs file', function (done) {
req.path = 'mytemplate.hbs';
staticTheme()(req, res, function next() {
activeThemeStub.called.should.be.false();
expressStaticStub.called.should.be.false();
done();
});
});
it('should skip for .md file', function (done) {
req.path = 'README.md';
staticTheme()(req, res, function next() {
activeThemeStub.called.should.be.false();
expressStaticStub.called.should.be.false();
done();
});
});
it('should skip for .json file', function (done) {
req.path = 'sample.json';
staticTheme()(req, res, function next() {
activeThemeStub.called.should.be.false();
expressStaticStub.called.should.be.false();
done();
});
});
it('should call express.static for .css file', function (done) {
req.path = 'myvalidfile.css';
staticTheme()(req, res, function next() {
// Specifically gets called twice
activeThemeStub.calledTwice.should.be.true();
expressStaticStub.called.should.be.true();
// Check that express static gets called with the theme path + maxAge
should.exist(expressStaticStub.firstCall.args);
expressStaticStub.firstCall.args[0].should.eql('my/fake/path');
expressStaticStub.firstCall.args[1].should.be.an.Object().with.property('maxAge');
done();
});
});
it('should call express.static for .js file', function (done) {
req.path = 'myvalidfile.js';
staticTheme()(req, res, function next() {
// Specifically gets called twice
activeThemeStub.calledTwice.should.be.true();
expressStaticStub.called.should.be.true();
// Check that express static gets called with the theme path + maxAge
should.exist(expressStaticStub.firstCall.args);
expressStaticStub.firstCall.args[0].should.eql('my/fake/path');
expressStaticStub.firstCall.args[1].should.be.an.Object().with.property('maxAge');
done();
});
});
it('should not error if active theme is missing', function (done) {
req.path = 'myvalidfile.css';
// make the active theme not exist
activeThemeStub.returns(undefined);
staticTheme()(req, res, function next() {
activeThemeStub.calledOnce.should.be.true();
expressStaticStub.called.should.be.false();
done();
});
});
it('should NOT skip if file is on whitelist', function (done) {
req.path = 'manifest.json';
staticTheme()(req, res, function next() {
// Specifically gets called twice
activeThemeStub.calledTwice.should.be.true();
expressStaticStub.called.should.be.true();
// Check that express static gets called with the theme path + maxAge
should.exist(expressStaticStub.firstCall.args);
expressStaticStub.firstCall.args[0].should.eql('my/fake/path');
expressStaticStub.firstCall.args[1].should.be.an.Object().with.property('maxAge');
done();
});
});
});