0
Fork 0
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:
Alex Kleissner 2015-06-09 13:59:49 -07:00
parent 3f9560f11f
commit 511684c436
3 changed files with 76 additions and 7 deletions

View 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;

View file

@ -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)

View 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();
});
});