maintenance tests

v1.18.x
barisusakli 8 years ago
parent 5916530a35
commit 38bc8ad5ae

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

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

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

Loading…
Cancel
Save