mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
refs: https://github.com/TryGhost/Team/issues/856 refs: https://github.com/TryGhost/Team/issues/756 - The .test.js extension is better than _spec.js as it's more obvious that it's an extension - It also meaans we can use the --extension parameter in mocha, which should result in a better default behaviour for `yarn test` - It also highlights that some of our tests were named incorrectly and were not (and still will not be) run (see https://github.com/TryGhost/Team/issues/856) - Note: even with this change, `yarn test` is throwing errors, I believe because of this issue https://github.com/TryGhost/Team/issues/756
239 lines
6.9 KiB
JavaScript
239 lines
6.9 KiB
JavaScript
const should = require('should');
|
|
const sinon = require('sinon');
|
|
const testUtils = require('../../../../utils');
|
|
const configUtils = require('../../../../utils/configUtils');
|
|
const urlService = require('../../../../../core/frontend/services/url');
|
|
const urlUtils = require('../../../../../core/shared/url-utils');
|
|
const controllers = require('../../../../../core/frontend/services/routing/controllers');
|
|
const helpers = require('../../../../../core/frontend/services/routing/helpers');
|
|
const EDITOR_URL = `/#/editor/post/`;
|
|
|
|
describe('Unit - services/routing/controllers/entry', function () {
|
|
let req;
|
|
let res;
|
|
let entryLookUpStub;
|
|
let secureStub;
|
|
let renderStub;
|
|
let post;
|
|
let page;
|
|
|
|
beforeEach(function () {
|
|
post = testUtils.DataGenerator.forKnex.createPost();
|
|
post.url = '/does-exist/';
|
|
|
|
page = testUtils.DataGenerator.forKnex.createPost({page: 1});
|
|
|
|
secureStub = sinon.stub();
|
|
entryLookUpStub = sinon.stub();
|
|
renderStub = sinon.stub();
|
|
|
|
sinon.stub(helpers, 'entryLookup').get(function () {
|
|
return entryLookUpStub;
|
|
});
|
|
|
|
sinon.stub(helpers, 'secure').get(function () {
|
|
return secureStub;
|
|
});
|
|
|
|
sinon.stub(helpers, 'renderEntry').get(function () {
|
|
return renderStub;
|
|
});
|
|
|
|
sinon.stub(urlUtils, 'redirectToAdmin');
|
|
sinon.stub(urlUtils, 'redirect301');
|
|
sinon.stub(urlService, 'getResourceById');
|
|
|
|
req = {
|
|
path: '/',
|
|
params: {},
|
|
route: {}
|
|
};
|
|
|
|
res = {
|
|
routerOptions: {
|
|
resourceType: 'posts'
|
|
},
|
|
render: sinon.spy(),
|
|
redirect: sinon.spy(),
|
|
locals: {}
|
|
};
|
|
});
|
|
|
|
afterEach(function () {
|
|
sinon.restore();
|
|
});
|
|
|
|
it('resource not found', function (done) {
|
|
req.path = '/does-not-exist/';
|
|
|
|
entryLookUpStub.withArgs(req.path, res.routerOptions)
|
|
.resolves(null);
|
|
|
|
controllers.entry(req, res, function (err) {
|
|
should.not.exist(err);
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('resource found', function (done) {
|
|
req.path = post.url;
|
|
req.originalUrl = req.path;
|
|
|
|
res.routerOptions.resourceType = 'posts';
|
|
|
|
urlService.getResourceById.withArgs(post.id).returns({
|
|
config: {
|
|
type: 'posts'
|
|
}
|
|
});
|
|
|
|
entryLookUpStub.withArgs(req.path, res.routerOptions)
|
|
.resolves({
|
|
entry: post
|
|
});
|
|
|
|
controllers.entry(req, res, function () {
|
|
secureStub.calledOnce.should.be.true();
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
describe('[edge cases] resource found', function () {
|
|
it('isUnknownOption: true', function (done) {
|
|
req.path = post.url;
|
|
|
|
entryLookUpStub.withArgs(req.path, res.routerOptions)
|
|
.resolves({
|
|
isUnknownOption: true,
|
|
entry: post
|
|
});
|
|
|
|
controllers.entry(req, res, function (err) {
|
|
should.not.exist(err);
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('isEditURL: true', function (done) {
|
|
req.path = post.url;
|
|
|
|
entryLookUpStub.withArgs(req.path, res.routerOptions)
|
|
.resolves({
|
|
isEditURL: true,
|
|
entry: post
|
|
});
|
|
|
|
urlUtils.redirectToAdmin.callsFake(function (statusCode, _res, editorUrl) {
|
|
statusCode.should.eql(302);
|
|
editorUrl.should.eql(EDITOR_URL + post.id);
|
|
done();
|
|
});
|
|
|
|
controllers.entry(req, res, (err) => {
|
|
done(err);
|
|
});
|
|
});
|
|
|
|
it('isEditURL: true with admin redirects disabled', function (done) {
|
|
configUtils.set('admin:redirects', false);
|
|
|
|
req.path = post.url;
|
|
|
|
entryLookUpStub.withArgs(req.path, res.routerOptions)
|
|
.resolves({
|
|
isEditURL: true,
|
|
entry: post
|
|
});
|
|
|
|
urlUtils.redirectToAdmin.callsFake(function (statusCode, _res, editorUrl) {
|
|
configUtils.restore();
|
|
done(new Error('redirectToAdmin was called'));
|
|
});
|
|
|
|
controllers.entry(req, res, (err) => {
|
|
configUtils.restore();
|
|
urlUtils.redirectToAdmin.called.should.eql(false);
|
|
should.not.exist(err);
|
|
done(err);
|
|
});
|
|
});
|
|
|
|
it('type of router !== type of resource', function (done) {
|
|
req.path = post.url;
|
|
res.routerOptions.resourceType = 'posts';
|
|
|
|
urlService.getResourceById.withArgs(post.id).returns({
|
|
config: {
|
|
type: 'pages'
|
|
}
|
|
});
|
|
|
|
entryLookUpStub.withArgs(req.path, res.routerOptions)
|
|
.resolves({
|
|
entry: post
|
|
});
|
|
|
|
controllers.entry(req, res, function (err) {
|
|
should.not.exist(err);
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('requested url !== resource url', function (done) {
|
|
post.url = '/2017/08' + post.url;
|
|
req.path = '/2017/07' + post.url;
|
|
req.originalUrl = req.path;
|
|
|
|
res.routerOptions.resourceType = 'posts';
|
|
|
|
urlService.getResourceById.withArgs(post.id).returns({
|
|
config: {
|
|
type: 'posts'
|
|
}
|
|
});
|
|
|
|
entryLookUpStub.withArgs(req.path, res.routerOptions)
|
|
.resolves({
|
|
entry: post
|
|
});
|
|
|
|
urlUtils.redirect301.callsFake(function (_res, postUrl) {
|
|
postUrl.should.eql(post.url);
|
|
done();
|
|
});
|
|
|
|
controllers.entry(req, res, function (err) {
|
|
should.exist(err);
|
|
done(err);
|
|
});
|
|
});
|
|
|
|
it('requested url !== resource url: with query params', function (done) {
|
|
post.url = '/2017/08' + post.url;
|
|
req.path = '/2017/07' + post.url;
|
|
req.originalUrl = req.path + '?query=true';
|
|
|
|
res.routerOptions.resourceType = 'posts';
|
|
|
|
urlService.getResourceById.withArgs(post.id).returns({
|
|
config: {
|
|
type: 'posts'
|
|
}
|
|
});
|
|
|
|
entryLookUpStub.withArgs(req.path, res.routerOptions)
|
|
.resolves({
|
|
entry: post
|
|
});
|
|
|
|
urlUtils.redirect301.callsFake(function (_res, postUrl) {
|
|
postUrl.should.eql(post.url + '?query=true');
|
|
done();
|
|
});
|
|
|
|
controllers.entry(req, res, function (err) {
|
|
done(err);
|
|
});
|
|
});
|
|
});
|
|
});
|