mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 12:05:57 +01:00
Added tests to confirm `buildHeader` is used and `/api` works
This commit is contained in:
committed by
Barış Soner Uşaklı
parent
c839d1cbc0
commit
11b9cb7688
@@ -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, {
|
||||
if (!plugins.hasListeners(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,
|
||||
res: res,
|
||||
next: next,
|
||||
});
|
||||
}
|
||||
|
||||
req.url = req.path + (!req.path.endsWith('/') ? '/' : '') + route;
|
||||
next();
|
||||
});
|
||||
};
|
||||
exports.pluginHook = pluginHook;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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,8 +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) {
|
||||
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,
|
||||
});
|
||||
|
||||
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);
|
||||
@@ -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) {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
assert.ifError(err);
|
||||
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) {
|
||||
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) {
|
||||
assert.ifError(err);
|
||||
|
||||
@@ -156,30 +180,44 @@ describe('Controllers', function () {
|
||||
});
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should render with hook', function (done) {
|
||||
meta.configs.set('homePageRoute', 'custom', function (err) {
|
||||
assert.ifError(err);
|
||||
|
||||
request(nconf.get('url'), function (err, res, body) {
|
||||
assert.ifError(err);
|
||||
assert.equal(res.statusCode, 200);
|
||||
assert.equal(body, '"works"');
|
||||
plugins.unregisterHook('myTestPlugin', 'action:homepage.get:custom', hookMethod);
|
||||
assert.ok(body);
|
||||
assert.ok(body.indexOf('<main id="panel"'));
|
||||
assert.ok(body.indexOf(message) !== -1);
|
||||
|
||||
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) {
|
||||
request(nconf.get('url') + '/reset', function (err, res, body) {
|
||||
assert.ifError(err);
|
||||
|
||||
Reference in New Issue
Block a user