2019-04-03 16:59:03 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
2021-04-19 17:09:35 +03:00
|
|
|
const hbs = require('../../../../core/frontend/services/theme-engine/engine');
|
2021-04-23 17:16:59 +03:00
|
|
|
const middleware = require('../../../../core/frontend/services/theme-engine').middleware;
|
2021-04-23 15:22:45 +03:00
|
|
|
// is only exposed via themeEngine.getActive()
|
|
|
|
const activeTheme = require('../../../../core/frontend/services/theme-engine/active');
|
2020-03-30 18:26:47 +03:00
|
|
|
const settingsCache = require('../../../../core/server/services/settings/cache');
|
2019-04-03 16:59:03 +03:00
|
|
|
|
2020-02-19 22:59:05 +03:00
|
|
|
const sandbox = sinon.createSandbox();
|
2019-04-03 16:59:03 +03:00
|
|
|
|
2020-10-19 07:45:26 +03:00
|
|
|
function executeMiddleware(toExecute, req, res, next) {
|
|
|
|
const [current, ...rest] = toExecute;
|
2019-04-03 16:59:03 +03:00
|
|
|
|
|
|
|
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 () {
|
2017-03-22 09:52:58 +03:00
|
|
|
afterEach(function () {
|
2019-04-03 16:59:03 +03:00
|
|
|
sandbox.restore();
|
2017-03-22 09:52:58 +03:00
|
|
|
});
|
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
let req;
|
|
|
|
let res;
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
let fakeActiveTheme;
|
|
|
|
let fakeActiveThemeName;
|
|
|
|
let fakeSiteData;
|
|
|
|
let fakeLabsData;
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
beforeEach(function () {
|
2021-05-05 16:47:07 +03:00
|
|
|
req = {app: {}, header: () => {}};
|
2019-04-03 16:59:03 +03:00
|
|
|
res = {locals: {}};
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
fakeActiveTheme = {
|
|
|
|
config: sandbox.stub().returns(2),
|
|
|
|
mount: sandbox.stub()
|
|
|
|
};
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
fakeActiveThemeName = 'bacon-sensation';
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
fakeSiteData = {};
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
fakeLabsData = {
|
|
|
|
// labs data is deep cloned,
|
|
|
|
// if we want to compare it
|
|
|
|
// we will need some unique content
|
2021-02-17 02:24:54 +03:00
|
|
|
members: true
|
2019-04-03 16:59:03 +03:00
|
|
|
};
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
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');
|
|
|
|
});
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
it('mounts active theme if not yet mounted', function (done) {
|
|
|
|
fakeActiveTheme.mounted = false;
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
executeMiddleware(middleware, req, res, function next(err) {
|
|
|
|
should.not.exist(err);
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
fakeActiveTheme.mount.called.should.be.true();
|
|
|
|
fakeActiveTheme.mount.calledWith(req.app).should.be.true();
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
it('does not mounts the active theme if it is already mounted', function (done) {
|
|
|
|
fakeActiveTheme.mounted = true;
|
2019-01-03 20:31:43 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
executeMiddleware(middleware, req, res, function next(err) {
|
|
|
|
should.not.exist(err);
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
fakeActiveTheme.mount.called.should.be.false();
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
it('throws error if theme is missing', function (done) {
|
|
|
|
activeTheme.get.restore();
|
|
|
|
sandbox.stub(activeTheme, 'get')
|
|
|
|
.returns(undefined);
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
executeMiddleware(middleware, req, res, function next(err) {
|
|
|
|
// Did throw an error
|
|
|
|
should.exist(err);
|
|
|
|
err.message.should.eql('The currently active theme "bacon-sensation" is missing.');
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
activeTheme.get.called.should.be.true();
|
|
|
|
fakeActiveTheme.mount.called.should.be.false();
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2021-04-21 14:10:09 +03:00
|
|
|
it('Sets res.locals.secure to the value of req.secure', function (done) {
|
|
|
|
req.secure = Math.random() < 0.5;
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-04-03 16:59:03 +03:00
|
|
|
executeMiddleware(middleware, req, res, function next(err) {
|
|
|
|
should.not.exist(err);
|
2019-01-03 20:31:43 +03:00
|
|
|
|
2021-04-21 14:10:09 +03:00
|
|
|
should.equal(res.locals.secure, req.secure);
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2021-04-21 14:10:09 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2021-04-21 14:10:09 +03:00
|
|
|
describe('updateTemplateOptions', function () {
|
|
|
|
it('is called with correct data', function (done) {
|
|
|
|
const themeDataExpectedProps = ['posts_per_page', 'image_sizes'];
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2021-04-21 14:10:09 +03:00
|
|
|
executeMiddleware(middleware, req, res, function next(err) {
|
|
|
|
should.not.exist(err);
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2021-04-21 14:10:09 +03:00
|
|
|
hbs.updateTemplateOptions.calledOnce.should.be.true();
|
|
|
|
const templateOptions = hbs.updateTemplateOptions.firstCall.args[0];
|
|
|
|
const data = templateOptions.data;
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2021-04-21 14:10:09 +03:00
|
|
|
data.should.be.an.Object().with.properties('site', 'labs', 'config');
|
|
|
|
|
|
|
|
// Check Theme Config
|
|
|
|
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
|
|
|
|
should.deepEqual(data.labs, fakeLabsData);
|
|
|
|
|
|
|
|
should.equal(data.site, fakeSiteData);
|
|
|
|
should.exist(data.site.signup_url);
|
|
|
|
data.site.signup_url.should.equal('#/portal');
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2019-04-03 16:59:03 +03:00
|
|
|
});
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2021-04-21 14:10:09 +03:00
|
|
|
it('switches @site.signup_url to RSS when signup is disabled', function (done) {
|
|
|
|
settingsCache.get
|
|
|
|
.withArgs('members_signup_access').returns('none');
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2021-04-21 14:10:09 +03:00
|
|
|
executeMiddleware(middleware, req, res, function next(err) {
|
|
|
|
const templateOptions = hbs.updateTemplateOptions.firstCall.args[0];
|
|
|
|
const data = templateOptions.data;
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2021-04-21 14:10:09 +03:00
|
|
|
should.exist(data.site.signup_url);
|
|
|
|
data.site.signup_url.should.equal('https://feedly.com/i/subscription/feed/http%3A%2F%2F127.0.0.1%3A2369%2Frss%2F');
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2021-04-21 14:10:09 +03:00
|
|
|
done();
|
|
|
|
});
|
2017-03-22 09:52:58 +03:00
|
|
|
});
|
|
|
|
});
|
2021-02-12 19:08:55 +03:00
|
|
|
|
|
|
|
describe('Preview Mode', function () {
|
|
|
|
it('calls updateTemplateOptions with correct data when one parameter is set', function (done) {
|
|
|
|
const previewString = 'c=%23000fff';
|
|
|
|
req.header = () => {
|
|
|
|
return previewString;
|
|
|
|
};
|
|
|
|
|
|
|
|
executeMiddleware(middleware, req, res, function next(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
|
|
|
|
hbs.updateTemplateOptions.calledOnce.should.be.true();
|
|
|
|
const templateOptions = hbs.updateTemplateOptions.firstCall.args[0];
|
|
|
|
const data = templateOptions.data;
|
|
|
|
|
|
|
|
data.should.be.an.Object().with.properties('site', 'labs', 'config');
|
|
|
|
|
|
|
|
should.equal(data.site, fakeSiteData);
|
|
|
|
|
|
|
|
data.site.should.be.an.Object().with.properties('accent_color', '_preview');
|
|
|
|
data.site._preview.should.eql(previewString);
|
|
|
|
data.site.accent_color.should.eql('#000fff');
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls updateTemplateOptions with correct data when two parameters are set', function (done) {
|
|
|
|
const previewString = 'c=%23000fff&icon=%2Fcontent%2Fimages%2Fmyimg.png';
|
|
|
|
req.header = () => {
|
|
|
|
return previewString;
|
|
|
|
};
|
|
|
|
|
|
|
|
executeMiddleware(middleware, req, res, function next(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
|
|
|
|
hbs.updateTemplateOptions.calledOnce.should.be.true();
|
|
|
|
const templateOptions = hbs.updateTemplateOptions.firstCall.args[0];
|
|
|
|
const data = templateOptions.data;
|
|
|
|
|
|
|
|
data.should.be.an.Object().with.properties('site', 'labs', 'config');
|
|
|
|
|
|
|
|
should.equal(data.site, fakeSiteData);
|
|
|
|
|
|
|
|
data.site.should.be.an.Object().with.properties('accent_color', 'icon', '_preview');
|
|
|
|
data.site._preview.should.eql(previewString);
|
|
|
|
data.site.accent_color.should.eql('#000fff');
|
|
|
|
data.site.icon.should.eql('/content/images/myimg.png');
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-03-22 09:52:58 +03:00
|
|
|
});
|