mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Moves the decideIsAdmin into its own file.
refs #5286 - Moved the function into its own file - Added unit tests for the function
This commit is contained in:
parent
3f9560f11f
commit
511684c436
3 changed files with 76 additions and 7 deletions
17
core/server/middleware/decide-is-admin.js
Normal file
17
core/server/middleware/decide-is-admin.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
// # DecideIsAdmin Middleware
|
||||
// Usage: decideIsAdmin(request, result, next)
|
||||
// After:
|
||||
// Before:
|
||||
// App: Blog
|
||||
//
|
||||
// Helper function to determine if its an admin page.
|
||||
|
||||
var decideIsAdmin;
|
||||
|
||||
decideIsAdmin = function decideIsAdmin(req, res, next) {
|
||||
/*jslint unparam:true*/
|
||||
res.isAdmin = req.url.lastIndexOf('/ghost/', 0) === 0;
|
||||
next();
|
||||
};
|
||||
|
||||
module.exports = decideIsAdmin;
|
|
@ -23,6 +23,7 @@ var api = require('../api'),
|
|||
authStrategies = require('./auth-strategies'),
|
||||
utils = require('../utils'),
|
||||
sitemapHandler = require('../data/xml/sitemap/handler'),
|
||||
decideIsAdmin = require('./decide-is-admin'),
|
||||
|
||||
blogApp,
|
||||
setupMiddleware;
|
||||
|
@ -75,13 +76,6 @@ function activateTheme(activeTheme) {
|
|||
// Set active theme variable on the express server
|
||||
blogApp.set('activeTheme', activeTheme);
|
||||
}
|
||||
// ### decideIsAdmin Middleware
|
||||
// Uses the URL to detect whether this response should be an admin response
|
||||
// This is used to ensure the right content is served, and is not for security purposes
|
||||
function decideIsAdmin(req, res, next) {
|
||||
res.isAdmin = req.url.lastIndexOf('/ghost/', 0) === 0;
|
||||
next();
|
||||
}
|
||||
|
||||
// ### configHbsForContext Middleware
|
||||
// Setup handlebars for the current context (admin or theme)
|
||||
|
|
58
core/test/unit/middleware/decide-is-admin_spec.js
Normal file
58
core/test/unit/middleware/decide-is-admin_spec.js
Normal file
|
@ -0,0 +1,58 @@
|
|||
/*globals describe, beforeEach, afterEach, it*/
|
||||
/*jshint expr:true*/
|
||||
var sinon = require('sinon'),
|
||||
decideIsAdmin = require('../../../server/middleware/decide-is-admin');
|
||||
|
||||
describe('Middleware: decideIsAdmin', function () {
|
||||
var sandbox,
|
||||
res,
|
||||
req,
|
||||
next;
|
||||
|
||||
beforeEach(function () {
|
||||
sandbox = sinon.sandbox.create();
|
||||
|
||||
next = sinon.spy();
|
||||
res = sinon.spy();
|
||||
req = {};
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
it('sets the isAdmin flag if the url contains /ghost/.', function (done) {
|
||||
var trueUrls = [
|
||||
'/ghost/',
|
||||
'/ghost/foo?bar=foo'
|
||||
],
|
||||
falseUrls = [
|
||||
'/ghost',
|
||||
'ghost/',
|
||||
'/foobar/ghost',
|
||||
'/things/ghost/foo'
|
||||
];
|
||||
|
||||
trueUrls.forEach(function (url) {
|
||||
res = sinon.spy();
|
||||
next = sinon.spy();
|
||||
req.url = url;
|
||||
|
||||
decideIsAdmin(req, res, next);
|
||||
res.isAdmin.should.be.exactly(true);
|
||||
next.calledOnce.should.be.exactly(true);
|
||||
});
|
||||
|
||||
falseUrls.forEach(function (url) {
|
||||
res = sinon.spy();
|
||||
next = sinon.spy();
|
||||
req.url = url;
|
||||
|
||||
decideIsAdmin(req, res, next);
|
||||
res.isAdmin.should.be.exactly(false);
|
||||
next.calledOnce.should.be.exactly(true);
|
||||
});
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue