diff --git a/src/middleware/maintenance.js b/src/middleware/maintenance.js index 5ef04ef8fa..e274c295eb 100644 --- a/src/middleware/maintenance.js +++ b/src/middleware/maintenance.js @@ -27,20 +27,6 @@ module.exports = function (middleware) { '^/language/.+', '^/uploads/system/site-logo.png' ]; - var render = function () { - res.status(503); - var data = { - site_title: meta.config.title || 'NodeBB', - message: meta.config.maintenanceModeMessage - }; - if (!isApiRoute.test(url)) { - middleware.buildHeader(req, res, function () { - res.render('503', data); - }); - } else { - res.json(data); - } - }; var isAllowed = function (url) { for(var x = 0,numAllowed = allowedRoutes.length,route; x < numAllowed; x++) { @@ -52,25 +38,28 @@ module.exports = function (middleware) { return false; }; - var isApiRoute = /^\/api/; - if (isAllowed(url)) { return next(); } - if (!req.user) { - return render(); - } - - user.isAdministrator(req.user.uid, function (err, isAdmin) { - if (err) { + user.isAdministrator(req.uid, function (err, isAdmin) { + if (err || isAdmin) { return next(err); } - if (!isAdmin) { - render(); - } else { - next(); + + res.status(503); + var data = { + site_title: meta.config.title || 'NodeBB', + message: meta.config.maintenanceModeMessage + }; + + if (res.locals.isAPI) { + return res.json(data); } + + middleware.buildHeader(req, res, function () { + res.render('503', data); + }); }); }; diff --git a/test/controllers-admin.js b/test/controllers-admin.js index cd379e33c7..55abe69127 100644 --- a/test/controllers-admin.js +++ b/test/controllers-admin.js @@ -416,6 +416,17 @@ describe('Admin Controllers', function () { }); }); + it('should load /recent in maintenance mode', function (done) { + var meta = require('../src/meta'); + meta.config.maintenanceMode = 1; + request(nconf.get('url') + '/api/recent', {jar: jar, json: true}, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body); + meta.config.maintenanceMode = 0; + done(); + }); + }); after(function (done) { diff --git a/test/controllers.js b/test/controllers.js index 6841fa3821..04afade0fc 100644 --- a/test/controllers.js +++ b/test/controllers.js @@ -665,6 +665,44 @@ describe('Controllers', function () { }); + describe('maintenance mode', function () { + + before(function (done) { + meta.config.maintenanceMode = 1; + done(); + }); + after(function (done) { + meta.config.maintenanceMode = 0; + done(); + }); + + it('should return 503 in maintenance mode', function (done) { + request(nconf.get('url') + '/recent', {json: true}, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 503); + done(); + }); + }); + + it('should return 503 in maintenance mode', function (done) { + request(nconf.get('url') + '/api/recent', {json: true}, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 503); + assert(body); + done(); + }); + }); + + it('should return 200 in maintenance mode', function (done) { + request(nconf.get('url') + '/api/login', {json: true}, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body); + done(); + }); + }); + }); + after(function (done) { var analytics = require('../src/analytics'); analytics.writeData(function (err) {