From 608a7d120c3af6004524a049948b447f520e4b44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 10 May 2017 21:02:36 -0400 Subject: [PATCH] error handler tests --- src/controllers/errors.js | 4 +- src/middleware/index.js | 4 +- test/controllers.js | 137 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 4 deletions(-) diff --git a/src/controllers/errors.js b/src/controllers/errors.js index 0e61b557cc..99614f9e87 100644 --- a/src/controllers/errors.js +++ b/src/controllers/errors.js @@ -16,14 +16,14 @@ exports.handleURIErrors = function (err, req, res, next) { res.redirect(nconf.get('relative_path') + cidMatch[0]); } else { winston.warn('[controller] Bad request: ' + req.path); - if (res.locals.isAPI) { + if (req.path.startsWith(nconf.get('relative_path') + '/api')) { res.status(400).json({ error: '[[global:400.title]]', }); } else { var middleware = require('../middleware'); middleware.buildHeader(req, res, function () { - res.render('400', { error: validator.escape(String(err.message)) }); + res.status(400).render('400', { error: validator.escape(String(err.message)) }); }); } } diff --git a/src/middleware/index.js b/src/middleware/index.js index de0bd6797e..d4cdb0bfd2 100644 --- a/src/middleware/index.js +++ b/src/middleware/index.js @@ -122,9 +122,9 @@ middleware.pageView = function (req, res, next) { middleware.pluginHooks = function (req, res, next) { async.each(plugins.loadedHooks['filter:router.page'] || [], function (hookObj, next) { hookObj.method(req, res, next); - }, function () { + }, function (err) { // If it got here, then none of the subscribed hooks did anything, or there were no hooks - next(); + next(err); }); }; diff --git a/test/controllers.js b/test/controllers.js index 3e1000e39f..5c0f143758 100644 --- a/test/controllers.js +++ b/test/controllers.js @@ -1069,6 +1069,143 @@ describe('Controllers', function () { }); }); + describe('handle errors', function () { + var plugins = require('../src/plugins'); + after(function (done) { + plugins.loadedHooks['filter:router.page'] = undefined; + done(); + }); + + it('should handle topic malformed uri', function (done) { + request(nconf.get('url') + '/topic/1/a%AFc', function (err, res, body) { + assert.ifError(err); + assert(body); + done(); + }); + }); + + it('should handle category malformed uri', function (done) { + request(nconf.get('url') + '/category/1/a%AFc', function (err, res, body) { + assert.ifError(err); + assert(body); + done(); + }); + }); + + it('should handle malformed uri ', function (done) { + request(nconf.get('url') + '/user/a%AFc', function (err, res, body) { + assert.ifError(err); + assert(body); + assert.equal(res.statusCode, 400); + done(); + }); + }); + + it('should handle malformed uri in api', function (done) { + request(nconf.get('url') + '/api/user/a%AFc', { json: true }, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 400); + assert.equal(body.error, '[[global:400.title]]'); + done(); + }); + }); + + it('should handle CSRF error', function (done) { + plugins.loadedHooks['filter:router.page'] = plugins.loadedHooks['filter:router.page'] || []; + plugins.loadedHooks['filter:router.page'].push({ + method: function (req, res, next) { + var err = new Error('csrf-error'); + err.code = 'EBADCSRFTOKEN'; + next(err); + }, + }); + + request(nconf.get('url') + '/users', { }, function (err, res, body) { + plugins.loadedHooks['filter:router.page'] = []; + assert.ifError(err); + assert.equal(res.statusCode, 403); + done(); + }); + }); + + it('should handle black-list error', function (done) { + plugins.loadedHooks['filter:router.page'] = plugins.loadedHooks['filter:router.page'] || []; + plugins.loadedHooks['filter:router.page'].push({ + method: function (req, res, next) { + var err = new Error('blacklist error message'); + err.code = 'blacklisted-ip'; + next(err); + }, + }); + + request(nconf.get('url') + '/users', { }, function (err, res, body) { + plugins.loadedHooks['filter:router.page'] = []; + assert.ifError(err); + assert.equal(res.statusCode, 403); + assert.equal(body, 'blacklist error message'); + done(); + }); + }); + + it('should handle page redirect through error', function (done) { + plugins.loadedHooks['filter:router.page'] = plugins.loadedHooks['filter:router.page'] || []; + plugins.loadedHooks['filter:router.page'].push({ + method: function (req, res, next) { + var err = new Error('redirect'); + err.status = 302; + err.path = '/popular'; + plugins.loadedHooks['filter:router.page'] = []; + next(err); + }, + }); + + request(nconf.get('url') + '/users', { }, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body); + done(); + }); + }); + + it('should handle api page redirect through error', function (done) { + plugins.loadedHooks['filter:router.page'] = plugins.loadedHooks['filter:router.page'] || []; + plugins.loadedHooks['filter:router.page'].push({ + method: function (req, res, next) { + var err = new Error('redirect'); + err.status = 308; + err.path = '/api/popular'; + plugins.loadedHooks['filter:router.page'] = []; + next(err); + }, + }); + + request(nconf.get('url') + '/api/users', { json: true }, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 308); + assert(body, '/api/popular'); + done(); + }); + }); + + it('should handle error page', function (done) { + plugins.loadedHooks['filter:router.page'] = plugins.loadedHooks['filter:router.page'] || []; + plugins.loadedHooks['filter:router.page'].push({ + method: function (req, res, next) { + var err = new Error('regular error'); + next(err); + }, + }); + + request(nconf.get('url') + '/users', function (err, res, body) { + plugins.loadedHooks['filter:router.page'] = []; + assert.ifError(err); + assert.equal(res.statusCode, 500); + assert(body); + done(); + }); + }); + }); + after(function (done) { var analytics = require('../src/analytics');