2020-05-25 11:49:38 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const testUtils = require('../../../../utils');
|
2020-08-11 16:01:16 +03:00
|
|
|
const security = require('@tryghost/security');
|
2021-04-23 15:22:45 +03:00
|
|
|
const themeEngine = require('../../../../../core/frontend/services/theme-engine');
|
2020-04-29 18:44:27 +03:00
|
|
|
const controllers = require('../../../../../core/frontend/services/routing/controllers');
|
|
|
|
const helpers = require('../../../../../core/frontend/services/routing/helpers');
|
2018-06-24 01:32:08 +03:00
|
|
|
|
|
|
|
function failTest(done) {
|
|
|
|
return function (err) {
|
|
|
|
should.exist(err);
|
|
|
|
done(err);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('Unit - services/routing/controllers/channel', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let req;
|
|
|
|
let res;
|
|
|
|
let fetchDataStub;
|
|
|
|
let secureStub;
|
|
|
|
let renderStub;
|
|
|
|
let posts;
|
|
|
|
let postsPerPage;
|
2018-06-24 01:32:08 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
postsPerPage = 5;
|
|
|
|
|
|
|
|
posts = [
|
|
|
|
testUtils.DataGenerator.forKnex.createPost()
|
|
|
|
];
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
secureStub = sinon.stub();
|
|
|
|
fetchDataStub = sinon.stub();
|
|
|
|
renderStub = sinon.stub();
|
2018-06-24 01:32:08 +03:00
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(helpers, 'fetchData').get(function () {
|
2018-06-24 01:32:08 +03:00
|
|
|
return fetchDataStub;
|
|
|
|
});
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(security.string, 'safe').returns('safe');
|
2018-06-24 01:32:08 +03:00
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(helpers, 'secure').get(function () {
|
2018-06-24 01:32:08 +03:00
|
|
|
return secureStub;
|
|
|
|
});
|
|
|
|
|
2021-04-23 15:22:45 +03:00
|
|
|
sinon.stub(themeEngine, 'getActive').returns({
|
2019-01-21 19:53:44 +03:00
|
|
|
updateTemplateOptions: sinon.stub(),
|
2018-06-24 01:32:08 +03:00
|
|
|
config: function (key) {
|
2019-08-19 14:41:09 +03:00
|
|
|
key.should.eql('posts_per_page');
|
|
|
|
return postsPerPage;
|
|
|
|
}
|
2018-06-24 01:32:08 +03:00
|
|
|
});
|
|
|
|
|
2019-04-15 17:15:32 +03:00
|
|
|
sinon.stub(helpers, 'renderEntries').returns(renderStub);
|
2018-06-24 01:32:08 +03:00
|
|
|
|
|
|
|
req = {
|
|
|
|
path: '/',
|
|
|
|
params: {},
|
|
|
|
route: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
res = {
|
2018-06-26 02:12:50 +03:00
|
|
|
routerOptions: {},
|
2018-06-24 01:32:08 +03:00
|
|
|
render: sinon.spy(),
|
|
|
|
redirect: sinon.spy()
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2018-06-24 01:32:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('no params', function (done) {
|
2018-06-26 02:12:50 +03:00
|
|
|
fetchDataStub.withArgs({page: 1, slug: undefined, limit: postsPerPage}, res.routerOptions)
|
2018-06-24 01:32:08 +03:00
|
|
|
.resolves({
|
|
|
|
posts: posts,
|
|
|
|
meta: {
|
|
|
|
pagination: {
|
|
|
|
pages: 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-15 17:15:32 +03:00
|
|
|
controllers.channel(req, res, failTest(done)).then(function () {
|
2021-04-23 15:22:45 +03:00
|
|
|
themeEngine.getActive.calledOnce.should.be.true();
|
2018-06-24 01:32:08 +03:00
|
|
|
security.string.safe.calledOnce.should.be.false();
|
|
|
|
fetchDataStub.calledOnce.should.be.true();
|
|
|
|
secureStub.calledOnce.should.be.true();
|
|
|
|
done();
|
2019-04-15 17:15:32 +03:00
|
|
|
}).catch(done);
|
2018-06-24 01:32:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('pass page param', function (done) {
|
|
|
|
req.params.page = 2;
|
|
|
|
|
2018-06-26 02:12:50 +03:00
|
|
|
fetchDataStub.withArgs({page: 2, slug: undefined, limit: postsPerPage}, res.routerOptions)
|
2018-06-24 01:32:08 +03:00
|
|
|
.resolves({
|
|
|
|
posts: posts,
|
|
|
|
meta: {
|
|
|
|
pagination: {
|
|
|
|
pages: 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-15 17:15:32 +03:00
|
|
|
controllers.channel(req, res, failTest(done)).then(function () {
|
2021-04-23 15:22:45 +03:00
|
|
|
themeEngine.getActive.calledOnce.should.be.true();
|
2018-06-24 01:32:08 +03:00
|
|
|
security.string.safe.calledOnce.should.be.false();
|
|
|
|
fetchDataStub.calledOnce.should.be.true();
|
|
|
|
secureStub.calledOnce.should.be.true();
|
|
|
|
done();
|
2019-04-15 17:15:32 +03:00
|
|
|
}).catch(done);
|
2018-06-24 01:32:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('update hbs engine: router defines limit', function (done) {
|
2018-06-26 02:12:50 +03:00
|
|
|
res.routerOptions.limit = 3;
|
2018-06-24 01:32:08 +03:00
|
|
|
req.params.page = 2;
|
|
|
|
|
2018-06-26 02:12:50 +03:00
|
|
|
fetchDataStub.withArgs({page: 2, slug: undefined, limit: 3}, res.routerOptions)
|
2018-06-24 01:32:08 +03:00
|
|
|
.resolves({
|
|
|
|
posts: posts,
|
|
|
|
meta: {
|
|
|
|
pagination: {
|
|
|
|
pages: 3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-15 17:15:32 +03:00
|
|
|
controllers.channel(req, res, failTest(done)).then(function () {
|
2021-04-23 15:22:45 +03:00
|
|
|
themeEngine.getActive.calledOnce.should.be.true();
|
|
|
|
themeEngine.getActive().updateTemplateOptions.withArgs({data: {config: {posts_per_page: 3}}}).calledOnce.should.be.true();
|
2018-06-24 01:32:08 +03:00
|
|
|
security.string.safe.calledOnce.should.be.false();
|
|
|
|
fetchDataStub.calledOnce.should.be.true();
|
|
|
|
secureStub.calledOnce.should.be.true();
|
|
|
|
done();
|
2019-04-15 17:15:32 +03:00
|
|
|
}).catch(done);
|
2018-06-24 01:32:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('page param too big', function (done) {
|
|
|
|
req.params.page = 6;
|
|
|
|
|
2018-06-26 02:12:50 +03:00
|
|
|
fetchDataStub.withArgs({page: 6, slug: undefined, limit: postsPerPage}, res.routerOptions)
|
2018-06-24 01:32:08 +03:00
|
|
|
.resolves({
|
|
|
|
posts: posts,
|
|
|
|
meta: {
|
|
|
|
pagination: {
|
|
|
|
pages: 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
controllers.channel(req, res, function (err) {
|
2020-05-25 11:49:38 +03:00
|
|
|
(err instanceof errors.NotFoundError).should.be.true();
|
2018-06-24 01:32:08 +03:00
|
|
|
|
2021-04-23 15:22:45 +03:00
|
|
|
themeEngine.getActive.calledOnce.should.be.true();
|
2018-06-24 01:32:08 +03:00
|
|
|
security.string.safe.calledOnce.should.be.false();
|
|
|
|
fetchDataStub.calledOnce.should.be.true();
|
|
|
|
renderStub.calledOnce.should.be.false();
|
|
|
|
secureStub.calledOnce.should.be.false();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('slug param', function (done) {
|
|
|
|
req.params.slug = 'unsafe';
|
|
|
|
|
2018-06-26 02:12:50 +03:00
|
|
|
fetchDataStub.withArgs({page: 1, slug: 'safe', limit: postsPerPage}, res.routerOptions)
|
2018-06-24 01:32:08 +03:00
|
|
|
.resolves({
|
|
|
|
posts: posts,
|
|
|
|
meta: {
|
|
|
|
pagination: {
|
|
|
|
pages: 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-15 17:15:32 +03:00
|
|
|
controllers.channel(req, res, failTest(done)).then(function () {
|
2021-04-23 15:22:45 +03:00
|
|
|
themeEngine.getActive.calledOnce.should.be.true();
|
2018-06-24 01:32:08 +03:00
|
|
|
security.string.safe.calledOnce.should.be.true();
|
|
|
|
fetchDataStub.calledOnce.should.be.true();
|
|
|
|
secureStub.calledOnce.should.be.true();
|
|
|
|
done();
|
2019-04-15 17:15:32 +03:00
|
|
|
}).catch(done);
|
2018-06-24 01:32:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('invalid posts per page', function (done) {
|
|
|
|
postsPerPage = -1;
|
|
|
|
|
2018-06-26 02:12:50 +03:00
|
|
|
fetchDataStub.withArgs({page: 1, slug: undefined}, res.routerOptions)
|
2018-06-24 01:32:08 +03:00
|
|
|
.resolves({
|
|
|
|
posts: posts,
|
|
|
|
meta: {
|
|
|
|
pagination: {
|
|
|
|
pages: 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-15 17:15:32 +03:00
|
|
|
controllers.channel(req, res, failTest(done)).then(function () {
|
2021-04-23 15:22:45 +03:00
|
|
|
themeEngine.getActive.calledOnce.should.be.true();
|
2018-06-24 01:32:08 +03:00
|
|
|
security.string.safe.calledOnce.should.be.false();
|
|
|
|
fetchDataStub.calledOnce.should.be.true();
|
|
|
|
secureStub.calledOnce.should.be.true();
|
|
|
|
done();
|
2019-04-15 17:15:32 +03:00
|
|
|
}).catch(done);
|
2018-06-24 01:32:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('ensure secure helper get\'s called for data object', function (done) {
|
2018-06-26 02:12:50 +03:00
|
|
|
fetchDataStub.withArgs({page: 1, slug: undefined, limit: postsPerPage}, res.routerOptions)
|
2018-06-24 01:32:08 +03:00
|
|
|
.resolves({
|
|
|
|
posts: posts,
|
|
|
|
data: {
|
|
|
|
tag: [testUtils.DataGenerator.forKnex.createTag()]
|
|
|
|
},
|
|
|
|
meta: {
|
|
|
|
pagination: {
|
|
|
|
pages: 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-15 17:15:32 +03:00
|
|
|
controllers.channel(req, res, failTest(done)).then(function () {
|
2021-04-23 15:22:45 +03:00
|
|
|
themeEngine.getActive.calledOnce.should.be.true();
|
2018-06-24 01:32:08 +03:00
|
|
|
security.string.safe.calledOnce.should.be.false();
|
|
|
|
fetchDataStub.calledOnce.should.be.true();
|
|
|
|
secureStub.calledTwice.should.be.true();
|
|
|
|
done();
|
2019-04-15 17:15:32 +03:00
|
|
|
}).catch(done);
|
2018-06-24 01:32:08 +03:00
|
|
|
});
|
|
|
|
});
|