2017-03-13 16:30:35 +00:00
|
|
|
var sinon = require('sinon'),
|
|
|
|
should = require('should'),
|
|
|
|
express = require('express'),
|
|
|
|
fs = require('fs'),
|
|
|
|
hbs = require('express-hbs'),
|
|
|
|
themeList = require('../../../server/themes').list,
|
2015-07-15 18:01:23 +02:00
|
|
|
themeHandler = require('../../../server/middleware/theme-handler'),
|
2017-03-13 16:30:35 +00:00
|
|
|
settingsCache = require('../../../server/settings/cache'),
|
2017-03-02 22:05:35 +00:00
|
|
|
|
2017-03-13 16:30:35 +00:00
|
|
|
sandbox = sinon.sandbox.create();
|
2015-07-15 18:01:23 +02:00
|
|
|
|
|
|
|
describe('Theme Handler', function () {
|
2015-10-11 21:21:54 +01:00
|
|
|
var req, res, next, blogApp;
|
2015-07-15 18:01:23 +02:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
req = sinon.spy();
|
|
|
|
res = sinon.spy();
|
|
|
|
next = sinon.spy();
|
|
|
|
blogApp = express();
|
2015-10-11 21:21:54 +01:00
|
|
|
req.app = blogApp;
|
2015-07-15 18:01:23 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sandbox.restore();
|
2017-03-02 16:53:48 +00:00
|
|
|
themeList.init();
|
2015-07-15 18:01:23 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('activateTheme', function () {
|
2015-10-11 21:21:54 +01:00
|
|
|
it('should activate new theme with partials', function () {
|
2016-10-04 17:33:43 +02:00
|
|
|
var fsStub = sandbox.stub(fs, 'stat', function (path, cb) {
|
2015-10-11 21:21:54 +01:00
|
|
|
cb(null, {isDirectory: function () { return true; }});
|
|
|
|
}),
|
|
|
|
hbsStub = sandbox.spy(hbs, 'express3');
|
|
|
|
|
|
|
|
themeHandler.activateTheme(blogApp, 'casper');
|
|
|
|
|
2016-02-07 21:27:01 +00:00
|
|
|
fsStub.calledOnce.should.be.true();
|
|
|
|
hbsStub.calledOnce.should.be.true();
|
|
|
|
hbsStub.firstCall.args[0].should.be.an.Object().and.have.property('partialsDir');
|
2015-10-11 21:21:54 +01:00
|
|
|
hbsStub.firstCall.args[0].partialsDir.should.have.lengthOf(2);
|
|
|
|
blogApp.get('activeTheme').should.equal('casper');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should activate new theme without partials', function () {
|
2016-10-04 17:33:43 +02:00
|
|
|
var fsStub = sandbox.stub(fs, 'stat', function (path, cb) {
|
2015-10-11 21:21:54 +01:00
|
|
|
cb(null, null);
|
|
|
|
}),
|
|
|
|
hbsStub = sandbox.spy(hbs, 'express3');
|
|
|
|
|
|
|
|
themeHandler.activateTheme(blogApp, 'casper');
|
2015-07-15 18:01:23 +02:00
|
|
|
|
2016-02-07 21:27:01 +00:00
|
|
|
fsStub.calledOnce.should.be.true();
|
|
|
|
hbsStub.calledOnce.should.be.true();
|
|
|
|
hbsStub.firstCall.args[0].should.be.an.Object().and.have.property('partialsDir');
|
2015-10-11 21:21:54 +01:00
|
|
|
hbsStub.firstCall.args[0].partialsDir.should.have.lengthOf(1);
|
2015-07-15 18:01:23 +02:00
|
|
|
blogApp.get('activeTheme').should.equal('casper');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('configHbsForContext', function () {
|
2015-10-11 21:21:54 +01:00
|
|
|
it('handles non secure context', function () {
|
|
|
|
res.locals = {};
|
|
|
|
themeHandler.configHbsForContext(req, res, next);
|
|
|
|
|
|
|
|
should.not.exist(res.locals.secure);
|
2016-02-07 21:27:01 +00:00
|
|
|
next.called.should.be.true();
|
2015-10-11 21:21:54 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('sets view path', function () {
|
2015-07-15 18:01:23 +02:00
|
|
|
req.secure = true;
|
|
|
|
res.locals = {};
|
2015-10-11 21:21:54 +01:00
|
|
|
blogApp.set('activeTheme', 'casper');
|
2015-07-15 18:01:23 +02:00
|
|
|
|
2015-10-11 21:21:54 +01:00
|
|
|
themeHandler.configHbsForContext(req, res, next);
|
2015-07-15 18:01:23 +02:00
|
|
|
|
2016-02-07 21:27:01 +00:00
|
|
|
blogApp.get('views').should.not.be.undefined();
|
2015-07-15 18:01:23 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('sets view path', function () {
|
|
|
|
req.secure = true;
|
|
|
|
res.locals = {};
|
|
|
|
blogApp.set('activeTheme', 'casper');
|
|
|
|
|
2015-10-11 21:21:54 +01:00
|
|
|
themeHandler.configHbsForContext(req, res, next);
|
2015-07-15 18:01:23 +02:00
|
|
|
|
2016-02-07 21:27:01 +00:00
|
|
|
blogApp.get('views').should.not.be.undefined();
|
2015-07-15 18:01:23 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-10-11 21:21:54 +01:00
|
|
|
describe('updateActiveTheme', function () {
|
2017-03-02 16:53:48 +00:00
|
|
|
beforeEach(function () {
|
|
|
|
themeList.init({casper: {}});
|
|
|
|
});
|
|
|
|
|
2015-10-11 21:21:54 +01:00
|
|
|
it('updates the active theme if changed', function (done) {
|
|
|
|
var activateThemeSpy = sandbox.spy(themeHandler, 'activateTheme');
|
2016-10-06 14:27:35 +02:00
|
|
|
|
2017-03-02 22:05:35 +00:00
|
|
|
sandbox.stub(settingsCache, 'get').withArgs('activeTheme').returns('casper');
|
2016-07-21 12:26:16 +01:00
|
|
|
blogApp.set('activeTheme', 'not-casper');
|
2015-10-11 21:21:54 +01:00
|
|
|
|
2016-07-21 12:26:16 +01:00
|
|
|
themeHandler.updateActiveTheme(req, res, function () {
|
|
|
|
activateThemeSpy.called.should.be.true();
|
2015-10-11 21:21:54 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not update the active theme if not changed', function (done) {
|
|
|
|
var activateThemeSpy = sandbox.spy(themeHandler, 'activateTheme');
|
2017-03-02 22:05:35 +00:00
|
|
|
|
|
|
|
sandbox.stub(settingsCache, 'get').withArgs('activeTheme').returns('casper');
|
2016-07-21 12:26:16 +01:00
|
|
|
blogApp.set('activeTheme', 'casper');
|
2015-10-11 21:21:54 +01:00
|
|
|
|
2016-07-21 12:26:16 +01:00
|
|
|
themeHandler.updateActiveTheme(req, res, function () {
|
|
|
|
activateThemeSpy.called.should.be.false();
|
2015-10-11 21:21:54 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws error if theme is missing', function (done) {
|
2016-10-04 17:33:43 +02:00
|
|
|
var activateThemeSpy = sandbox.spy(themeHandler, 'activateTheme');
|
2015-10-11 21:21:54 +01:00
|
|
|
|
2017-03-02 22:05:35 +00:00
|
|
|
sandbox.stub(settingsCache, 'get').withArgs('activeTheme').returns('rasper');
|
2016-07-21 12:26:16 +01:00
|
|
|
blogApp.set('activeTheme', 'not-casper');
|
2015-10-11 21:21:54 +01:00
|
|
|
|
2016-07-21 12:26:16 +01:00
|
|
|
themeHandler.updateActiveTheme(req, res, function (err) {
|
2015-10-11 21:21:54 +01:00
|
|
|
should.exist(err);
|
2016-07-21 12:26:16 +01:00
|
|
|
activateThemeSpy.called.should.be.false();
|
2015-10-11 21:21:54 +01:00
|
|
|
err.message.should.eql('The currently active theme "rasper" is missing.');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-07-15 18:01:23 +02:00
|
|
|
});
|