2020-04-25 23:06:33 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2021-10-16 18:26:05 +03:00
|
|
|
const express = require('../../../../../core/shared/express');
|
|
|
|
const themeEngine = require('../../../../../core/frontend/services/theme-engine');
|
|
|
|
const staticTheme = require('../../../../../core/frontend/web/middleware/static-theme');
|
2015-07-15 19:01:23 +03:00
|
|
|
|
|
|
|
describe('staticTheme', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let expressStaticStub;
|
|
|
|
let activeThemeStub;
|
|
|
|
let req;
|
|
|
|
let res;
|
2015-07-15 19:01:23 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2017-03-13 23:13:17 +03:00
|
|
|
req = {};
|
|
|
|
res = {};
|
|
|
|
|
2021-04-23 15:22:45 +03:00
|
|
|
activeThemeStub = sinon.stub(themeEngine, 'getActive').returns({
|
2017-03-13 23:13:17 +03:00
|
|
|
path: 'my/fake/path'
|
|
|
|
});
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
expressStaticStub = sinon.spy(express, 'static');
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
2017-03-13 23:13:17 +03:00
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2017-03-13 23:13:17 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip for .hbs file', function (done) {
|
|
|
|
req.path = 'mytemplate.hbs';
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2017-03-13 23:13:17 +03:00
|
|
|
staticTheme()(req, res, function next() {
|
|
|
|
activeThemeStub.called.should.be.false();
|
|
|
|
expressStaticStub.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
2017-03-13 23:13:17 +03:00
|
|
|
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();
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2017-03-13 23:13:17 +03:00
|
|
|
done();
|
|
|
|
});
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
2017-03-13 23:13:17 +03:00
|
|
|
it('should skip for .json file', function (done) {
|
|
|
|
req.path = 'sample.json';
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2017-03-13 23:13:17 +03:00
|
|
|
staticTheme()(req, res, function next() {
|
|
|
|
activeThemeStub.called.should.be.false();
|
|
|
|
expressStaticStub.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
2017-03-13 23:13:17 +03:00
|
|
|
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');
|
|
|
|
|
2015-07-15 19:01:23 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-01-23 23:34:11 +03:00
|
|
|
|
|
|
|
it('should not error if active theme is missing', function (done) {
|
2017-03-13 23:13:17 +03:00
|
|
|
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();
|
|
|
|
|
2016-01-23 23:34:11 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-06-29 23:44:01 +03:00
|
|
|
|
2017-03-13 23:13:17 +03:00
|
|
|
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');
|
|
|
|
|
2016-06-29 23:44:01 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|