2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
2021-10-06 12:52:46 +03:00
|
|
|
const configUtils = require('../../../../utils/configUtils');
|
|
|
|
const urlUtils = require('../../../../../core/shared/url-utils');
|
|
|
|
const ParentRouter = require('../../../../../core/frontend/services/routing/ParentRouter');
|
2018-06-24 01:32:03 +03:00
|
|
|
|
|
|
|
describe('UNIT - services/routing/ParentRouter', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let req;
|
|
|
|
let res;
|
|
|
|
let next;
|
2018-06-24 01:32:03 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2019-06-18 16:13:55 +03:00
|
|
|
sinon.stub(urlUtils, 'redirect301');
|
2018-06-24 01:32:05 +03:00
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
req = sinon.stub();
|
2018-06-24 01:32:05 +03:00
|
|
|
req.app = {
|
|
|
|
_router: {
|
|
|
|
stack: []
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
res = sinon.stub();
|
|
|
|
next = sinon.stub();
|
2018-06-24 01:32:03 +03:00
|
|
|
|
|
|
|
res.locals = {};
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2018-06-24 01:32:05 +03:00
|
|
|
configUtils.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fn: _getSiteRouter', function () {
|
|
|
|
it('find site router', function () {
|
|
|
|
const parentRouter = new ParentRouter();
|
|
|
|
|
|
|
|
req.app = {
|
|
|
|
_router: {
|
|
|
|
stack: [{
|
|
|
|
name: 'SiteRouter'
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
should.exist(parentRouter._getSiteRouter(req));
|
|
|
|
});
|
2018-06-24 01:32:03 +03:00
|
|
|
});
|
2018-06-24 01:32:05 +03:00
|
|
|
|
|
|
|
describe('fn: _respectDominantRouter', function () {
|
|
|
|
it('redirect', function () {
|
|
|
|
const parentRouter = new ParentRouter();
|
2019-01-21 19:53:44 +03:00
|
|
|
parentRouter.getResourceType = sinon.stub().returns('tags');
|
2018-06-24 01:32:05 +03:00
|
|
|
parentRouter.permalinks = {
|
2019-01-21 19:53:44 +03:00
|
|
|
getValue: sinon.stub().returns('/tag/:slug/')
|
2018-06-24 01:32:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
req.url = '/tag/bacon/';
|
|
|
|
req.originalUrl = '/tag/bacon/';
|
|
|
|
|
|
|
|
req.app._router.stack = [{
|
|
|
|
name: 'SiteRouter',
|
|
|
|
handle: {
|
|
|
|
stack: [{
|
|
|
|
name: 'StaticRoutesRouter',
|
|
|
|
handle: {
|
|
|
|
parent: {
|
2019-01-21 19:53:44 +03:00
|
|
|
isRedirectEnabled: sinon.stub().returns(true),
|
|
|
|
getRoute: sinon.stub().returns('/channel/')
|
2018-06-24 01:32:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
parentRouter._respectDominantRouter(req, res, next, 'bacon');
|
|
|
|
next.called.should.eql(false);
|
2019-06-18 16:13:55 +03:00
|
|
|
urlUtils.redirect301.withArgs(res, '/channel/').calledOnce.should.be.true();
|
2018-06-24 01:32:05 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('redirect with query params', function () {
|
|
|
|
const parentRouter = new ParentRouter('tag', '/tag/:slug/');
|
2019-01-21 19:53:44 +03:00
|
|
|
parentRouter.getResourceType = sinon.stub().returns('tags');
|
2018-06-24 01:32:05 +03:00
|
|
|
parentRouter.permalinks = {
|
2019-01-21 19:53:44 +03:00
|
|
|
getValue: sinon.stub().returns('/tag/:slug/')
|
2018-06-24 01:32:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
req.url = '/tag/bacon/';
|
|
|
|
req.originalUrl = '/tag/bacon/?a=b';
|
|
|
|
|
|
|
|
req.app._router.stack = [{
|
|
|
|
name: 'SiteRouter',
|
|
|
|
handle: {
|
|
|
|
stack: [{
|
|
|
|
name: 'StaticRoutesRouter',
|
|
|
|
handle: {
|
|
|
|
parent: {
|
2019-01-21 19:53:44 +03:00
|
|
|
isRedirectEnabled: sinon.stub().returns(true),
|
|
|
|
getRoute: sinon.stub().returns('/channel/')
|
2018-06-24 01:32:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
parentRouter._respectDominantRouter(req, res, next, 'bacon');
|
|
|
|
next.called.should.eql(false);
|
2019-06-18 16:13:55 +03:00
|
|
|
urlUtils.redirect301.withArgs(res, '/channel/?a=b').calledOnce.should.be.true();
|
2018-06-24 01:32:05 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('redirect rss', function () {
|
|
|
|
const parentRouter = new ParentRouter('tag', '/tag/:slug/');
|
2019-01-21 19:53:44 +03:00
|
|
|
parentRouter.getResourceType = sinon.stub().returns('tags');
|
2018-06-24 01:32:05 +03:00
|
|
|
parentRouter.permalinks = {
|
2019-01-21 19:53:44 +03:00
|
|
|
getValue: sinon.stub().returns('/tag/:slug/')
|
2018-06-24 01:32:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
req.url = '/tag/bacon/rss/';
|
|
|
|
req.originalUrl = '/tag/bacon/rss/';
|
|
|
|
|
|
|
|
req.app._router.stack = [{
|
|
|
|
name: 'SiteRouter',
|
|
|
|
handle: {
|
|
|
|
stack: [{
|
|
|
|
name: 'StaticRoutesRouter',
|
|
|
|
handle: {
|
|
|
|
parent: {
|
2019-01-21 19:53:44 +03:00
|
|
|
isRedirectEnabled: sinon.stub().returns(true),
|
|
|
|
getRoute: sinon.stub().returns('/channel/')
|
2018-06-24 01:32:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
parentRouter._respectDominantRouter(req, res, next, 'bacon');
|
|
|
|
next.called.should.eql(false);
|
2019-06-18 16:13:55 +03:00
|
|
|
urlUtils.redirect301.withArgs(res, '/channel/rss/').calledOnce.should.be.true();
|
2018-06-24 01:32:05 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('redirect pagination', function () {
|
|
|
|
const parentRouter = new ParentRouter('tag', '/tag/:slug/');
|
2019-01-21 19:53:44 +03:00
|
|
|
parentRouter.getResourceType = sinon.stub().returns('tags');
|
2018-06-24 01:32:05 +03:00
|
|
|
parentRouter.permalinks = {
|
2019-01-21 19:53:44 +03:00
|
|
|
getValue: sinon.stub().returns('/tag/:slug/')
|
2018-06-24 01:32:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
req.url = '/tag/bacon/page/2/';
|
|
|
|
req.originalUrl = '/tag/bacon/page/2/';
|
|
|
|
|
|
|
|
req.app._router.stack = [{
|
|
|
|
name: 'SiteRouter',
|
|
|
|
handle: {
|
|
|
|
stack: [{
|
|
|
|
name: 'StaticRoutesRouter',
|
|
|
|
handle: {
|
|
|
|
parent: {
|
2019-01-21 19:53:44 +03:00
|
|
|
isRedirectEnabled: sinon.stub().returns(true),
|
|
|
|
getRoute: sinon.stub().returns('/channel/')
|
2018-06-24 01:32:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
parentRouter._respectDominantRouter(req, res, next, 'bacon');
|
|
|
|
next.called.should.eql(false);
|
2019-06-18 16:13:55 +03:00
|
|
|
urlUtils.redirect301.withArgs(res, '/channel/page/2/').calledOnce.should.be.true();
|
2018-06-24 01:32:05 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('redirect correctly with subdirectory', function () {
|
2019-06-18 16:13:55 +03:00
|
|
|
sinon.stub(urlUtils, 'createUrl').returns('/blog/channel/');
|
2018-06-24 01:32:05 +03:00
|
|
|
|
|
|
|
const parentRouter = new ParentRouter('tag', '/tag/:slug/');
|
2019-01-21 19:53:44 +03:00
|
|
|
parentRouter.getResourceType = sinon.stub().returns('tags');
|
2018-06-24 01:32:05 +03:00
|
|
|
parentRouter.permalinks = {
|
2019-01-21 19:53:44 +03:00
|
|
|
getValue: sinon.stub().returns('/tag/:slug/')
|
2018-06-24 01:32:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
req.url = '/tag/bacon/';
|
|
|
|
req.originalUrl = '/blog/tag/bacon/';
|
|
|
|
|
|
|
|
req.app._router.stack = [{
|
|
|
|
name: 'SiteRouter',
|
|
|
|
handle: {
|
|
|
|
stack: [{
|
|
|
|
name: 'StaticRoutesRouter',
|
|
|
|
handle: {
|
|
|
|
parent: {
|
2019-01-21 19:53:44 +03:00
|
|
|
isRedirectEnabled: sinon.stub().returns(true),
|
|
|
|
getRoute: sinon.stub().returns('/channel/')
|
2018-06-24 01:32:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
parentRouter._respectDominantRouter(req, res, next, 'bacon');
|
|
|
|
next.called.should.eql(false);
|
2019-06-18 16:13:55 +03:00
|
|
|
urlUtils.redirect301.withArgs(res, '/blog/channel/').calledOnce.should.be.true();
|
2018-06-24 01:32:05 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('no redirect: different data key', function () {
|
|
|
|
const parentRouter = new ParentRouter('tag', '/tag/:slug/');
|
2019-01-21 19:53:44 +03:00
|
|
|
parentRouter.getResourceType = sinon.stub().returns('tags');
|
2018-06-24 01:32:05 +03:00
|
|
|
parentRouter.permalinks = {
|
2019-01-21 19:53:44 +03:00
|
|
|
getValue: sinon.stub().returns('/tag/:slug/')
|
2018-06-24 01:32:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
req.app._router.stack = [{
|
|
|
|
name: 'SiteRouter',
|
|
|
|
handle: {
|
|
|
|
stack: [{
|
|
|
|
name: 'StaticRoutesRouter',
|
|
|
|
handle: {
|
|
|
|
parent: {
|
2019-01-21 19:53:44 +03:00
|
|
|
isRedirectEnabled: sinon.stub().returns(false),
|
|
|
|
getRoute: sinon.stub().returns('/channel/')
|
2018-06-24 01:32:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
parentRouter._respectDominantRouter(req, res, next, 'bacon');
|
|
|
|
next.called.should.eql(true);
|
2019-06-18 16:13:55 +03:00
|
|
|
urlUtils.redirect301.called.should.be.false();
|
2018-06-24 01:32:05 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('no redirect: no channel defined', function () {
|
|
|
|
const parentRouter = new ParentRouter('tag', '/tag/:slug/');
|
2019-01-21 19:53:44 +03:00
|
|
|
parentRouter.getResourceType = sinon.stub().returns('tags');
|
2018-06-24 01:32:05 +03:00
|
|
|
parentRouter.permalinks = {
|
2019-01-21 19:53:44 +03:00
|
|
|
getValue: sinon.stub().returns('/tag/:slug/')
|
2018-06-24 01:32:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
req.app._router.stack = [{
|
|
|
|
name: 'SiteRouter',
|
|
|
|
handle: {
|
|
|
|
stack: [{
|
|
|
|
name: 'StaticPagesRouter',
|
|
|
|
handle: {}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
parentRouter._respectDominantRouter(req, res, next, 'bacon');
|
|
|
|
next.called.should.eql(true);
|
2019-06-18 16:13:55 +03:00
|
|
|
urlUtils.redirect301.called.should.be.false();
|
2018-06-24 01:32:05 +03:00
|
|
|
});
|
2018-12-03 22:31:48 +03:00
|
|
|
|
|
|
|
it('redirect primary tag permalink', function () {
|
|
|
|
const parentRouter = new ParentRouter('index');
|
2019-01-21 19:53:44 +03:00
|
|
|
parentRouter.getResourceType = sinon.stub().returns('posts');
|
2018-12-03 22:31:48 +03:00
|
|
|
parentRouter.permalinks = {
|
2019-01-21 19:53:44 +03:00
|
|
|
getValue: sinon.stub().returns('/:primary_tag/:slug/')
|
2018-12-03 22:31:48 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
req.url = '/bacon/welcome/';
|
|
|
|
req.originalUrl = `${req.url}?x=y`;
|
|
|
|
|
|
|
|
req.app._router.stack = [{
|
|
|
|
name: 'SiteRouter',
|
|
|
|
handle: {
|
|
|
|
stack: [{
|
|
|
|
name: 'StaticRoutesRouter',
|
|
|
|
handle: {
|
|
|
|
parent: {
|
2019-01-21 19:53:44 +03:00
|
|
|
isRedirectEnabled: sinon.stub().returns(true),
|
|
|
|
getRoute: sinon.stub().returns('/route/')
|
2018-12-03 22:31:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
parentRouter._respectDominantRouter(req, res, next, 'welcome');
|
|
|
|
next.called.should.eql(false);
|
2019-06-18 16:13:55 +03:00
|
|
|
urlUtils.redirect301.withArgs(res, '/route/?x=y').calledOnce.should.be.true();
|
2018-12-03 22:31:48 +03:00
|
|
|
});
|
2018-06-24 01:32:05 +03:00
|
|
|
});
|
|
|
|
|
2018-06-24 01:32:03 +03:00
|
|
|
describe('fn: isRedirectEnabled', function () {
|
|
|
|
it('no data key defined', function () {
|
|
|
|
const parentRouter = new ParentRouter();
|
|
|
|
parentRouter.data = undefined;
|
|
|
|
parentRouter.isRedirectEnabled('tags', 'bacon').should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('no data key defined', function () {
|
|
|
|
const parentRouter = new ParentRouter();
|
|
|
|
parentRouter.data = {query: {}, router: {}};
|
|
|
|
should.not.exist(parentRouter.isRedirectEnabled('tags', 'bacon'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('no redirect', function () {
|
|
|
|
const parentRouter = new ParentRouter();
|
|
|
|
|
|
|
|
parentRouter.data = {
|
|
|
|
query: {},
|
|
|
|
router: {
|
|
|
|
tags: [{redirect: true}]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
should.not.exist(parentRouter.isRedirectEnabled('tags', 'bacon'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('no redirect', function () {
|
|
|
|
const parentRouter = new ParentRouter();
|
|
|
|
|
|
|
|
parentRouter.data = {
|
|
|
|
query: {},
|
|
|
|
router: {
|
|
|
|
tags: [{redirect: true, slug: 'cheese'}]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
should.not.exist(parentRouter.isRedirectEnabled('tags', 'bacon'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('no redirect', function () {
|
|
|
|
const parentRouter = new ParentRouter();
|
|
|
|
|
|
|
|
parentRouter.data = {
|
|
|
|
query: {},
|
|
|
|
router: {
|
|
|
|
tags: [{redirect: false, slug: 'bacon'}]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
should.not.exist(parentRouter.isRedirectEnabled('tags', 'bacon'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('redirect', function () {
|
|
|
|
const parentRouter = new ParentRouter();
|
|
|
|
|
|
|
|
parentRouter.data = {
|
|
|
|
query: {},
|
|
|
|
router: {
|
|
|
|
tags: [{redirect: true, slug: 'bacon'}]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
should.exist(parentRouter.isRedirectEnabled('tags', 'bacon'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('redirect', function () {
|
|
|
|
const parentRouter = new ParentRouter();
|
|
|
|
|
|
|
|
parentRouter.data = {
|
|
|
|
query: {},
|
|
|
|
router: {
|
|
|
|
pages: [{redirect: true, slug: 'home'}]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
should.exist(parentRouter.isRedirectEnabled('pages', 'home'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|