From 11b9cb76886f194c8d8281645580494da81bf7a5 Mon Sep 17 00:00:00 2001
From: Peter Jaszkowiak
Date: Fri, 17 Nov 2017 06:11:33 -0700
Subject: [PATCH] Fix #6076, `action:home.get:*` returned to previous
functionality (#6077)
Added tests to confirm `buildHeader` is used and `/api` works
---
src/controllers/home.js | 29 ++++--
src/routes/helpers.js | 4 +-
src/routes/index.js | 4 +-
test/controllers.js | 194 ++++++++++++++++++++++++----------------
4 files changed, 140 insertions(+), 91 deletions(-)
diff --git a/src/controllers/home.js b/src/controllers/home.js
index c044f889c5..6c67e7aaa2 100644
--- a/src/controllers/home.js
+++ b/src/controllers/home.js
@@ -36,7 +36,7 @@ function getRouteAllowUserHomePage(uid, next) {
pubsub.on('config:update', configUpdated);
configUpdated();
-module.exports = function (req, res, next) {
+function rewrite(req, res, next) {
if (req.path !== '/' && req.path !== '/api/' && req.path !== '/api') {
return next();
}
@@ -48,15 +48,26 @@ module.exports = function (req, res, next) {
var hook = 'action:homepage.get:' + route;
- if (plugins.hasListeners(hook)) {
- return plugins.fireHook(hook, {
- req: req,
- res: res,
- next: next,
- });
+ if (!plugins.hasListeners(hook)) {
+ req.url = req.path + (!req.path.endsWith('/') ? '/' : '') + route;
+ } else {
+ res.locals.homePageRoute = route;
}
- req.url = req.path + (!req.path.endsWith('/') ? '/' : '') + route;
next();
});
-};
+}
+
+exports.rewrite = rewrite;
+
+function pluginHook(req, res, next) {
+ var hook = 'action:homepage.get:' + res.locals.homePageRoute;
+
+ plugins.fireHook(hook, {
+ req: req,
+ res: res,
+ next: next,
+ });
+}
+
+exports.pluginHook = pluginHook;
diff --git a/src/routes/helpers.js b/src/routes/helpers.js
index 0aae67cbec..8cf4e38c90 100644
--- a/src/routes/helpers.js
+++ b/src/routes/helpers.js
@@ -1,6 +1,6 @@
'use strict';
-var helpers = {};
+var helpers = module.exports;
helpers.setupPageRoute = function (router, name, middleware, middlewares, controller) {
middlewares = [middleware.maintenanceMode, middleware.registrationComplete, middleware.pageView, middleware.pluginHooks].concat(middlewares);
@@ -13,5 +13,3 @@ helpers.setupAdminPageRoute = function (router, name, middleware, middlewares, c
router.get(name, middleware.admin.buildHeader, middlewares, controller);
router.get('/api' + name, middlewares, controller);
};
-
-module.exports = helpers;
diff --git a/src/routes/index.js b/src/routes/index.js
index 9feaa0ff60..e9f9b26c0a 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -122,7 +122,9 @@ module.exports = function (app, middleware, hotswapIds, callback) {
app.use(middleware.stripLeadingSlashes);
// handle custom homepage routes
- app.use(relativePath, controllers.home);
+ app.use(relativePath, controllers.home.rewrite);
+ // homepage handled by `action:homepage.get:[route]`
+ setupPageRoute(app, '/', middleware, [], controllers.home.pluginHook);
adminRoutes(router, middleware, controllers);
metaRoutes(router, middleware, controllers);
diff --git a/test/controllers.js b/test/controllers.js
index b5f0c40b3f..213472d548 100644
--- a/test/controllers.js
+++ b/test/controllers.js
@@ -4,6 +4,8 @@ var async = require('async');
var assert = require('assert');
var nconf = require('nconf');
var request = require('request');
+var fs = require('fs');
+var path = require('path');
var db = require('./mocks/databasemock');
var categories = require('../src/categories');
@@ -15,6 +17,7 @@ var meta = require('../src/meta');
var translator = require('../src/translator');
var privileges = require('../src/privileges');
var plugins = require('../src/plugins');
+var utils = require('../src/utils');
var helpers = require('./helpers');
describe('Controllers', function () {
@@ -58,20 +61,29 @@ describe('Controllers', function () {
});
});
+ describe('homepage', function () {
+ function hookMethod(hookData) {
+ assert(hookData.req);
+ assert(hookData.res);
+ assert(hookData.next);
- it('should load default home route', function (done) {
- request(nconf.get('url'), function (err, res, body) {
- assert.ifError(err);
- assert.equal(res.statusCode, 200);
- assert(body);
- done();
- });
- });
+ hookData.res.render('custom', {
+ works: true,
+ });
+ }
+ var message = utils.generateUUID();
+ var tplPath = path.join(nconf.get('views_dir'), 'custom.tpl');
- it('should load unread as home route', function (done) {
- meta.configs.set('homePageRoute', 'unread', function (err) {
- assert.ifError(err);
+ before(function () {
+ plugins.registerHook('myTestPlugin', {
+ hook: 'action:homepage.get:custom',
+ method: hookMethod,
+ });
+ fs.writeFileSync(tplPath, message);
+ });
+
+ it('should load default', function (done) {
request(nconf.get('url'), function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
@@ -79,105 +91,131 @@ describe('Controllers', function () {
done();
});
});
- });
-
- it('should load recent as home route', function (done) {
- meta.configs.set('homePageRoute', 'recent', function (err) {
- assert.ifError(err);
- request(nconf.get('url'), function (err, res, body) {
+ it('should load unread', function (done) {
+ meta.configs.set('homePageRoute', 'unread', function (err) {
assert.ifError(err);
- assert.equal(res.statusCode, 200);
- assert(body);
- done();
+
+ request(nconf.get('url'), function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert(body);
+ done();
+ });
});
});
- });
-
- it('should load popular as home route', function (done) {
- meta.configs.set('homePageRoute', 'popular', function (err) {
- assert.ifError(err);
- request(nconf.get('url'), function (err, res, body) {
+ it('should load recent', function (done) {
+ meta.configs.set('homePageRoute', 'recent', function (err) {
assert.ifError(err);
- assert.equal(res.statusCode, 200);
- assert(body);
- done();
+
+ request(nconf.get('url'), function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert(body);
+ done();
+ });
});
});
- });
-
- it('should load category as home route', function (done) {
- meta.configs.set('homePageRoute', 'category/1/test-category', function (err) {
- assert.ifError(err);
- request(nconf.get('url'), function (err, res, body) {
+ it('should load popular', function (done) {
+ meta.configs.set('homePageRoute', 'popular', function (err) {
assert.ifError(err);
- assert.equal(res.statusCode, 200);
- assert(body);
- done();
+
+ request(nconf.get('url'), function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert(body);
+ done();
+ });
});
});
- });
- it('should load not load breadcrumbs on home page route', function (done) {
- request(nconf.get('url') + '/api', { json: true }, function (err, res, body) {
- assert.ifError(err);
- assert.equal(res.statusCode, 200);
- assert(body);
- assert(!body.breadcrumbs);
- done();
- });
- });
+ it('should load category', function (done) {
+ meta.configs.set('homePageRoute', 'category/1/test-category', function (err) {
+ assert.ifError(err);
- it('should redirect to custom homepage', function (done) {
- meta.configs.set('homePageRoute', 'groups', function (err) {
- assert.ifError(err);
+ request(nconf.get('url'), function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert(body);
+ done();
+ });
+ });
+ });
- request(nconf.get('url'), function (err, res, body) {
+ it('should not load breadcrumbs on home page route', function (done) {
+ request(nconf.get('url') + '/api', { json: true }, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body);
+ assert(!body.breadcrumbs);
done();
});
});
- });
- it('should 404 if custom homepage does not exist', function (done) {
- meta.configs.set('homePageRoute', 'this-route-does-not-exist', function (err) {
- assert.ifError(err);
+ it('should redirect to custom', function (done) {
+ meta.configs.set('homePageRoute', 'groups', function (err) {
+ assert.ifError(err);
- request(nconf.get('url'), function (err, res, body) {
+ request(nconf.get('url'), function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert(body);
+ done();
+ });
+ });
+ });
+
+ it('should 404 if custom does not exist', function (done) {
+ meta.configs.set('homePageRoute', 'this-route-does-not-exist', function (err) {
assert.ifError(err);
- assert.equal(res.statusCode, 404);
- assert(body);
- done();
+
+ request(nconf.get('url'), function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 404);
+ assert(body);
+ done();
+ });
});
});
- });
- it('should render custom homepage with hook', function (done) {
- function hookMethod(hookData) {
- assert(hookData.req);
- assert(hookData.res);
- assert(hookData.next);
- hookData.res.json('works');
- }
- plugins.registerHook('myTestPlugin', {
- hook: 'action:homepage.get:custom',
- method: hookMethod,
+ it('api should work with hook', function (done) {
+ meta.configs.set('homePageRoute', 'custom', function (err) {
+ assert.ifError(err);
+
+ request(nconf.get('url') + '/api', { json: true }, function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert.equal(body.works, true);
+ assert.equal(body.template.custom, true);
+
+ done();
+ });
+ });
});
- meta.configs.set('homePageRoute', 'custom', function (err) {
- assert.ifError(err);
- request(nconf.get('url'), function (err, res, body) {
+ it('should render with hook', function (done) {
+ meta.configs.set('homePageRoute', 'custom', function (err) {
assert.ifError(err);
- assert.equal(res.statusCode, 200);
- assert.equal(body, '"works"');
- plugins.unregisterHook('myTestPlugin', 'action:homepage.get:custom', hookMethod);
- done();
+
+ request(nconf.get('url'), function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert.ok(body);
+ assert.ok(body.indexOf('