Fix #6076, `action:home.get:*` returned to previous functionality (#6077)

Added tests to confirm `buildHeader` is used and `/api` works
v1.18.x
Peter Jaszkowiak 7 years ago committed by Barış Soner Uşaklı
parent c839d1cbc0
commit 11b9cb7688

@ -36,7 +36,7 @@ function getRouteAllowUserHomePage(uid, next) {
pubsub.on('config:update', configUpdated); pubsub.on('config:update', configUpdated);
configUpdated(); configUpdated();
module.exports = function (req, res, next) { function rewrite(req, res, next) {
if (req.path !== '/' && req.path !== '/api/' && req.path !== '/api') { if (req.path !== '/' && req.path !== '/api/' && req.path !== '/api') {
return next(); return next();
} }
@ -48,15 +48,26 @@ module.exports = function (req, res, next) {
var hook = 'action:homepage.get:' + route; var hook = 'action:homepage.get:' + route;
if (plugins.hasListeners(hook)) { if (!plugins.hasListeners(hook)) {
return plugins.fireHook(hook, { req.url = req.path + (!req.path.endsWith('/') ? '/' : '') + route;
} else {
res.locals.homePageRoute = route;
}
next();
});
}
exports.rewrite = rewrite;
function pluginHook(req, res, next) {
var hook = 'action:homepage.get:' + res.locals.homePageRoute;
plugins.fireHook(hook, {
req: req, req: req,
res: res, res: res,
next: next, next: next,
}); });
} }
req.url = req.path + (!req.path.endsWith('/') ? '/' : '') + route; exports.pluginHook = pluginHook;
next();
});
};

@ -1,6 +1,6 @@
'use strict'; 'use strict';
var helpers = {}; var helpers = module.exports;
helpers.setupPageRoute = function (router, name, middleware, middlewares, controller) { helpers.setupPageRoute = function (router, name, middleware, middlewares, controller) {
middlewares = [middleware.maintenanceMode, middleware.registrationComplete, middleware.pageView, middleware.pluginHooks].concat(middlewares); 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(name, middleware.admin.buildHeader, middlewares, controller);
router.get('/api' + name, middlewares, controller); router.get('/api' + name, middlewares, controller);
}; };
module.exports = helpers;

@ -122,7 +122,9 @@ module.exports = function (app, middleware, hotswapIds, callback) {
app.use(middleware.stripLeadingSlashes); app.use(middleware.stripLeadingSlashes);
// handle custom homepage routes // 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); adminRoutes(router, middleware, controllers);
metaRoutes(router, middleware, controllers); metaRoutes(router, middleware, controllers);

@ -4,6 +4,8 @@ var async = require('async');
var assert = require('assert'); var assert = require('assert');
var nconf = require('nconf'); var nconf = require('nconf');
var request = require('request'); var request = require('request');
var fs = require('fs');
var path = require('path');
var db = require('./mocks/databasemock'); var db = require('./mocks/databasemock');
var categories = require('../src/categories'); var categories = require('../src/categories');
@ -15,6 +17,7 @@ var meta = require('../src/meta');
var translator = require('../src/translator'); var translator = require('../src/translator');
var privileges = require('../src/privileges'); var privileges = require('../src/privileges');
var plugins = require('../src/plugins'); var plugins = require('../src/plugins');
var utils = require('../src/utils');
var helpers = require('./helpers'); var helpers = require('./helpers');
describe('Controllers', function () { describe('Controllers', function () {
@ -58,8 +61,29 @@ describe('Controllers', function () {
}); });
}); });
describe('homepage', function () {
function hookMethod(hookData) {
assert(hookData.req);
assert(hookData.res);
assert(hookData.next);
hookData.res.render('custom', {
works: true,
});
}
var message = utils.generateUUID();
var tplPath = path.join(nconf.get('views_dir'), 'custom.tpl');
before(function () {
plugins.registerHook('myTestPlugin', {
hook: 'action:homepage.get:custom',
method: hookMethod,
});
it('should load default home route', function (done) { fs.writeFileSync(tplPath, message);
});
it('should load default', function (done) {
request(nconf.get('url'), function (err, res, body) { request(nconf.get('url'), function (err, res, body) {
assert.ifError(err); assert.ifError(err);
assert.equal(res.statusCode, 200); assert.equal(res.statusCode, 200);
@ -68,7 +92,7 @@ describe('Controllers', function () {
}); });
}); });
it('should load unread as home route', function (done) { it('should load unread', function (done) {
meta.configs.set('homePageRoute', 'unread', function (err) { meta.configs.set('homePageRoute', 'unread', function (err) {
assert.ifError(err); assert.ifError(err);
@ -81,7 +105,7 @@ describe('Controllers', function () {
}); });
}); });
it('should load recent as home route', function (done) { it('should load recent', function (done) {
meta.configs.set('homePageRoute', 'recent', function (err) { meta.configs.set('homePageRoute', 'recent', function (err) {
assert.ifError(err); assert.ifError(err);
@ -94,7 +118,7 @@ describe('Controllers', function () {
}); });
}); });
it('should load popular as home route', function (done) { it('should load popular', function (done) {
meta.configs.set('homePageRoute', 'popular', function (err) { meta.configs.set('homePageRoute', 'popular', function (err) {
assert.ifError(err); assert.ifError(err);
@ -107,7 +131,7 @@ describe('Controllers', function () {
}); });
}); });
it('should load category as home route', function (done) { it('should load category', function (done) {
meta.configs.set('homePageRoute', 'category/1/test-category', function (err) { meta.configs.set('homePageRoute', 'category/1/test-category', function (err) {
assert.ifError(err); assert.ifError(err);
@ -120,7 +144,7 @@ describe('Controllers', function () {
}); });
}); });
it('should load not load breadcrumbs on home page route', function (done) { it('should not load breadcrumbs on home page route', function (done) {
request(nconf.get('url') + '/api', { json: true }, function (err, res, body) { request(nconf.get('url') + '/api', { json: true }, function (err, res, body) {
assert.ifError(err); assert.ifError(err);
assert.equal(res.statusCode, 200); assert.equal(res.statusCode, 200);
@ -130,7 +154,7 @@ describe('Controllers', function () {
}); });
}); });
it('should redirect to custom homepage', function (done) { it('should redirect to custom', function (done) {
meta.configs.set('homePageRoute', 'groups', function (err) { meta.configs.set('homePageRoute', 'groups', function (err) {
assert.ifError(err); assert.ifError(err);
@ -143,7 +167,7 @@ describe('Controllers', function () {
}); });
}); });
it('should 404 if custom homepage does not exist', function (done) { it('should 404 if custom does not exist', function (done) {
meta.configs.set('homePageRoute', 'this-route-does-not-exist', function (err) { meta.configs.set('homePageRoute', 'this-route-does-not-exist', function (err) {
assert.ifError(err); assert.ifError(err);
@ -156,30 +180,44 @@ describe('Controllers', function () {
}); });
}); });
it('should render custom homepage with hook', function (done) { it('api should work with hook', function (done) {
function hookMethod(hookData) { meta.configs.set('homePageRoute', 'custom', function (err) {
assert(hookData.req); assert.ifError(err);
assert(hookData.res);
assert(hookData.next); request(nconf.get('url') + '/api', { json: true }, function (err, res, body) {
hookData.res.json('works'); assert.ifError(err);
} assert.equal(res.statusCode, 200);
plugins.registerHook('myTestPlugin', { assert.equal(body.works, true);
hook: 'action:homepage.get:custom', assert.equal(body.template.custom, true);
method: hookMethod,
done();
}); });
});
});
it('should render with hook', function (done) {
meta.configs.set('homePageRoute', 'custom', function (err) { meta.configs.set('homePageRoute', 'custom', function (err) {
assert.ifError(err); assert.ifError(err);
request(nconf.get('url'), function (err, res, body) { request(nconf.get('url'), function (err, res, body) {
assert.ifError(err); assert.ifError(err);
assert.equal(res.statusCode, 200); assert.equal(res.statusCode, 200);
assert.equal(body, '"works"'); assert.ok(body);
plugins.unregisterHook('myTestPlugin', 'action:homepage.get:custom', hookMethod); assert.ok(body.indexOf('<main id="panel"'));
assert.ok(body.indexOf(message) !== -1);
done(); done();
}); });
}); });
}); });
after(function () {
plugins.unregisterHook('myTestPlugin', 'action:homepage.get:custom', hookMethod);
fs.unlinkSync(tplPath);
fs.unlinkSync(tplPath.replace(/\.tpl$/, '.js'));
});
});
it('should load /reset without code', function (done) { it('should load /reset without code', function (done) {
request(nconf.get('url') + '/reset', function (err, res, body) { request(nconf.get('url') + '/reset', function (err, res, body) {
assert.ifError(err); assert.ifError(err);

Loading…
Cancel
Save