Ghost/core/test/unit/middleware/serve-favicon_spec.js
Katharina Irrgang 0201c431d7 🔥 do not store settings in config (#7924)
* 🎨  🔥  do not store settings in config and make settings cache easier available

- remove remembering settings value in theme config
- if we need a cache value, we are asking the settings cache directly
- instead of settings.getSettingSync we use settings.cache.get

- added TODO:
  - think about moving the settings cache out of api/settings
  - we could create a folder named cache cache/settings
  - this settings cache listens on model changes for settings
  - decoupling

* 🔥  remove timezone from config

- no need to store in overrides config and in defaults settings

* 🎨  context object helper

- replace config.get('theme') by settings cache

* 🎨  replace config.get('theme') by settings.cache.get

* 🎨  adapt tests

* fixes from comments
2017-02-03 13:15:11 +00:00

163 lines
5.3 KiB
JavaScript

var sinon = require('sinon'),
should = require('should'),
express = require('express'),
serveFavicon = require('../../../server/middleware/serve-favicon'),
settingsCache = require('../../../server/api/settings').cache,
configUtils = require('../../utils/configUtils'),
path = require('path'),
sandbox = sinon.sandbox.create();
should.equal(true, true);
describe('Serve Favicon', function () {
var req, res, next, blogApp, localSettingsCache = {};
beforeEach(function () {
req = sinon.spy();
res = sinon.spy();
next = sinon.spy();
blogApp = express();
req.app = blogApp;
sandbox.stub(settingsCache, 'get', function (key) {
return localSettingsCache[key];
});
});
afterEach(function () {
sandbox.restore();
configUtils.restore();
localSettingsCache = {};
});
describe('serveFavicon', function () {
it('should return a middleware', function () {
var middleware = serveFavicon();
middleware.should.be.a.Function();
});
it('should skip if the request does NOT match the file', function () {
var middleware = serveFavicon();
req.path = '/robots.txt';
middleware(req, res, next);
next.called.should.be.true();
});
describe('serves', function () {
it('custom uploaded favicon.png', function (done) {
var middleware = serveFavicon();
req.path = '/favicon.png';
configUtils.set('paths:contentPath', path.join(__dirname, '../../../test/utils/fixtures/'));
localSettingsCache.icon = 'favicon.png';
res = {
writeHead: function (statusCode) {
statusCode.should.eql(200);
},
end: function (body) {
body.length.should.eql(6792);
done();
}
};
middleware(req, res, next);
});
it('custom uploaded favicon.ico', function (done) {
var middleware = serveFavicon();
req.path = '/favicon.ico';
configUtils.set('paths:contentPath', path.join(__dirname, '../../../test/utils/fixtures/'));
localSettingsCache.icon = 'favicon.ico';
res = {
writeHead: function (statusCode) {
statusCode.should.eql(200);
},
end: function (body) {
body.length.should.eql(15086);
done();
}
};
middleware(req, res, next);
});
it('default favicon.ico', function (done) {
var middleware = serveFavicon();
req.path = '/favicon.ico';
configUtils.set('paths:corePath', path.join(__dirname, '../../../test/utils/fixtures/'));
localSettingsCache.icon = '';
res = {
writeHead: function (statusCode) {
statusCode.should.eql(200);
},
end: function (body) {
body.length.should.eql(15086);
done();
}
};
middleware(req, res, next);
});
});
describe('redirects', function () {
it('to custom favicon.ico when favicon.png is requested', function (done) {
var middleware = serveFavicon();
req.path = '/favicon.png';
configUtils.set('paths:contentPath', path.join(__dirname, '../../../test/utils/fixtures/'));
localSettingsCache.icon = 'favicon.ico';
res = {
redirect: function (statusCode) {
statusCode.should.eql(302);
done();
}
};
middleware(req, res, next);
});
it('to custom favicon.png when favicon.ico is requested', function (done) {
var middleware = serveFavicon();
req.path = '/favicon.ico';
configUtils.set('paths:contentPath', path.join(__dirname, '../../../test/utils/fixtures/'));
localSettingsCache.icon = 'favicon.png';
res = {
redirect: function (statusCode) {
statusCode.should.eql(302);
done();
}
};
middleware(req, res, next);
});
it('to favicon.ico when favicon.png is requested', function (done) {
var middleware = serveFavicon();
req.path = '/favicon.png';
configUtils.set('paths:corePath', path.join(__dirname, '../../../test/utils/fixtures/'));
localSettingsCache.icon = '';
res = {
redirect: function (statusCode) {
statusCode.should.eql(302);
done();
}
};
middleware(req, res, next);
});
});
});
});