2015-12-14 23:05:11 +03:00
|
|
|
var sinon = require('sinon'),
|
2015-07-15 19:01:23 +03:00
|
|
|
should = require('should'),
|
|
|
|
express = require('express'),
|
2015-10-11 23:21:54 +03:00
|
|
|
Promise = require('bluebird'),
|
|
|
|
fs = require('fs'),
|
|
|
|
hbs = require('express-hbs'),
|
2015-07-15 19:01:23 +03:00
|
|
|
themeHandler = require('../../../server/middleware/theme-handler'),
|
2016-10-04 18:33:43 +03:00
|
|
|
logging = require('../../../server/logging'),
|
2015-12-14 23:05:11 +03:00
|
|
|
api = require('../../../server/api'),
|
|
|
|
configUtils = require('../../utils/configUtils'),
|
|
|
|
sandbox = sinon.sandbox.create();
|
2015-07-15 19:01:23 +03:00
|
|
|
|
|
|
|
describe('Theme Handler', function () {
|
2015-10-11 23:21:54 +03:00
|
|
|
var req, res, next, blogApp;
|
2015-07-15 19:01:23 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
req = sinon.spy();
|
|
|
|
res = sinon.spy();
|
|
|
|
next = sinon.spy();
|
|
|
|
blogApp = express();
|
2015-10-11 23:21:54 +03:00
|
|
|
req.app = blogApp;
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sandbox.restore();
|
2015-12-14 23:05:11 +03:00
|
|
|
configUtils.restore();
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('ghostLocals', function () {
|
|
|
|
it('sets all locals', function () {
|
|
|
|
req.path = '/awesome-post';
|
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
themeHandler.ghostLocals(req, res, next);
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
res.locals.should.be.an.Object();
|
|
|
|
should.exist(res.locals.version);
|
|
|
|
should.exist(res.locals.safeVersion);
|
2015-07-15 19:01:23 +03:00
|
|
|
res.locals.relativeUrl.should.equal(req.path);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.true();
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('activateTheme', function () {
|
2015-10-11 23:21:54 +03:00
|
|
|
it('should activate new theme with partials', function () {
|
2016-10-04 18:33:43 +03:00
|
|
|
var fsStub = sandbox.stub(fs, 'stat', function (path, cb) {
|
2015-10-11 23:21:54 +03:00
|
|
|
cb(null, {isDirectory: function () { return true; }});
|
|
|
|
}),
|
|
|
|
hbsStub = sandbox.spy(hbs, 'express3');
|
|
|
|
|
|
|
|
themeHandler.activateTheme(blogApp, 'casper');
|
|
|
|
|
2016-02-08 00:27:01 +03: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 23:21:54 +03: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 18:33:43 +03:00
|
|
|
var fsStub = sandbox.stub(fs, 'stat', function (path, cb) {
|
2015-10-11 23:21:54 +03:00
|
|
|
cb(null, null);
|
|
|
|
}),
|
|
|
|
hbsStub = sandbox.spy(hbs, 'express3');
|
|
|
|
|
|
|
|
themeHandler.activateTheme(blogApp, 'casper');
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2016-02-08 00:27:01 +03: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 23:21:54 +03:00
|
|
|
hbsStub.firstCall.args[0].partialsDir.should.have.lengthOf(1);
|
2015-07-15 19:01:23 +03:00
|
|
|
blogApp.get('activeTheme').should.equal('casper');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('configHbsForContext', function () {
|
2015-10-11 23:21:54 +03:00
|
|
|
it('handles non secure context', function () {
|
|
|
|
res.locals = {};
|
|
|
|
themeHandler.configHbsForContext(req, res, next);
|
|
|
|
|
|
|
|
should.not.exist(res.locals.secure);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.true();
|
2015-10-11 23:21:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('handles secure context', function () {
|
|
|
|
var themeOptSpy = sandbox.stub(hbs, 'updateTemplateOptions');
|
2015-07-15 19:01:23 +03:00
|
|
|
req.secure = true;
|
|
|
|
res.locals = {};
|
2015-12-14 23:05:11 +03:00
|
|
|
configUtils.set({urlSSL: 'https://secure.blog'});
|
2015-10-11 23:21:54 +03:00
|
|
|
|
|
|
|
themeHandler.configHbsForContext(req, res, next);
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
themeOptSpy.calledOnce.should.be.true();
|
|
|
|
themeOptSpy.firstCall.args[0].should.be.an.Object().and.have.property('data');
|
|
|
|
themeOptSpy.firstCall.args[0].data.should.be.an.Object().and.have.property('blog');
|
|
|
|
themeOptSpy.firstCall.args[0].data.blog.should.be.an.Object().and.have.property('url');
|
2015-10-11 23:21:54 +03:00
|
|
|
themeOptSpy.firstCall.args[0].data.blog.url.should.eql('https://secure.blog');
|
|
|
|
res.locals.secure.should.equal(true);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.true();
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
it('sets view path', function () {
|
2015-07-15 19:01:23 +03:00
|
|
|
req.secure = true;
|
|
|
|
res.locals = {};
|
2015-10-11 23:21:54 +03:00
|
|
|
blogApp.set('activeTheme', 'casper');
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
themeHandler.configHbsForContext(req, res, next);
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
blogApp.get('views').should.not.be.undefined();
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('sets view path', function () {
|
|
|
|
req.secure = true;
|
|
|
|
res.locals = {};
|
|
|
|
blogApp.set('activeTheme', 'casper');
|
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
themeHandler.configHbsForContext(req, res, next);
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
blogApp.get('views').should.not.be.undefined();
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
describe('updateActiveTheme', function () {
|
|
|
|
it('updates the active theme if changed', function (done) {
|
|
|
|
var activateThemeSpy = sandbox.spy(themeHandler, 'activateTheme');
|
2016-10-06 15:27:35 +03:00
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
sandbox.stub(api.settings, 'read').withArgs(sandbox.match.has('key', 'activeTheme')).returns(Promise.resolve({
|
|
|
|
settings: [{
|
2016-07-21 14:26:16 +03:00
|
|
|
key: 'activeKey',
|
|
|
|
value: 'casper'
|
2015-10-11 23:21:54 +03:00
|
|
|
}]
|
|
|
|
}));
|
2016-07-21 14:26:16 +03:00
|
|
|
blogApp.set('activeTheme', 'not-casper');
|
|
|
|
configUtils.set({paths: {availableThemes: {casper: {}}}});
|
2015-10-11 23:21:54 +03:00
|
|
|
|
2016-07-21 14:26:16 +03:00
|
|
|
themeHandler.updateActiveTheme(req, res, function () {
|
|
|
|
activateThemeSpy.called.should.be.true();
|
2015-10-11 23:21:54 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not update the active theme if not changed', function (done) {
|
|
|
|
var activateThemeSpy = sandbox.spy(themeHandler, 'activateTheme');
|
|
|
|
sandbox.stub(api.settings, 'read').withArgs(sandbox.match.has('key', 'activeTheme')).returns(Promise.resolve({
|
|
|
|
settings: [{
|
2016-07-21 14:26:16 +03:00
|
|
|
key: 'activeKey',
|
2015-10-11 23:21:54 +03:00
|
|
|
value: 'casper'
|
|
|
|
}]
|
|
|
|
}));
|
2016-07-21 14:26:16 +03:00
|
|
|
blogApp.set('activeTheme', 'casper');
|
2015-12-14 23:05:11 +03:00
|
|
|
configUtils.set({paths: {availableThemes: {casper: {}}}});
|
2015-10-11 23:21:54 +03:00
|
|
|
|
2016-07-21 14:26:16 +03:00
|
|
|
themeHandler.updateActiveTheme(req, res, function () {
|
|
|
|
activateThemeSpy.called.should.be.false();
|
2015-10-11 23:21:54 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws error if theme is missing', function (done) {
|
2016-10-04 18:33:43 +03:00
|
|
|
var activateThemeSpy = sandbox.spy(themeHandler, 'activateTheme');
|
2015-10-11 23:21:54 +03:00
|
|
|
|
|
|
|
sandbox.stub(api.settings, 'read').withArgs(sandbox.match.has('key', 'activeTheme')).returns(Promise.resolve({
|
|
|
|
settings: [{
|
|
|
|
key: 'activeKey',
|
|
|
|
value: 'rasper'
|
|
|
|
}]
|
|
|
|
}));
|
2016-10-04 18:33:43 +03:00
|
|
|
|
2016-07-21 14:26:16 +03:00
|
|
|
blogApp.set('activeTheme', 'not-casper');
|
2015-12-14 23:05:11 +03:00
|
|
|
configUtils.set({paths: {availableThemes: {casper: {}}}});
|
2015-10-11 23:21:54 +03:00
|
|
|
|
2016-07-21 14:26:16 +03:00
|
|
|
themeHandler.updateActiveTheme(req, res, function (err) {
|
2015-10-11 23:21:54 +03:00
|
|
|
should.exist(err);
|
2016-07-21 14:26:16 +03:00
|
|
|
activateThemeSpy.called.should.be.false();
|
2015-10-11 23:21:54 +03:00
|
|
|
err.message.should.eql('The currently active theme "rasper" is missing.');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws only warns if theme is missing for admin req', function (done) {
|
2016-10-06 15:27:35 +03:00
|
|
|
var activateThemeSpy = sandbox.spy(themeHandler, 'activateTheme'),
|
|
|
|
loggingWarnStub = sandbox.spy(logging, 'warn');
|
2015-10-11 23:21:54 +03:00
|
|
|
|
|
|
|
sandbox.stub(api.settings, 'read').withArgs(sandbox.match.has('key', 'activeTheme')).returns(Promise.resolve({
|
|
|
|
settings: [{
|
|
|
|
key: 'activeKey',
|
|
|
|
value: 'rasper'
|
|
|
|
}]
|
|
|
|
}));
|
2016-10-06 15:27:35 +03:00
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
res.isAdmin = true;
|
2016-07-21 14:26:16 +03:00
|
|
|
blogApp.set('activeTheme', 'not-casper');
|
2015-12-14 23:05:11 +03:00
|
|
|
configUtils.set({paths: {availableThemes: {casper: {}}}});
|
2015-10-11 23:21:54 +03:00
|
|
|
|
2016-07-21 14:26:16 +03:00
|
|
|
themeHandler.updateActiveTheme(req, res, function () {
|
|
|
|
activateThemeSpy.called.should.be.false();
|
2016-10-06 15:27:35 +03:00
|
|
|
loggingWarnStub.called.should.be.true();
|
|
|
|
loggingWarnStub.calledWith('The currently active theme "rasper" is missing.').should.be.true();
|
2015-10-11 23:21:54 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|