0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/test/unit/services/routing/controllers/preview.test.js
Hannah Wolfe f08a55c21f
Renamed tests to .test.js & updated commands
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
2021-07-06 20:45:01 +01:00

242 lines
6.9 KiB
JavaScript

const should = require('should');
const sinon = require('sinon');
const Promise = require('bluebird');
const testUtils = require('../../../../utils');
const configUtils = require('../../../../utils/configUtils');
const api = require('../../../../../core/server/api');
const controllers = require('../../../../../core/frontend/services/routing/controllers');
const helpers = require('../../../../../core/frontend/services/routing/helpers');
const urlService = require('../../../../../core/frontend/services/url');
const urlUtils = require('../../../../../core/shared/url-utils');
const EDITOR_URL = '/#/editor/post/';
describe('Unit - services/routing/controllers/preview', function () {
let secureStub;
let renderStub;
let req;
let res;
let post;
let apiResponse;
function failTest(done) {
return function (err) {
should.exist(err);
done(err);
};
}
afterEach(function () {
sinon.restore();
configUtils.restore();
});
describe('v2', function () {
let previewStub;
beforeEach(function () {
post = testUtils.DataGenerator.forKnex.createPost({status: 'draft'});
apiResponse = {
preview: [post]
};
req = {
path: '/',
params: {
uuid: 'something'
},
route: {}
};
res = {
routerOptions: {
query: {controller: 'preview', resource: 'preview'}
},
locals: {
apiVersion: 'v2'
},
render: sinon.spy(),
redirect: sinon.spy(),
set: sinon.spy()
};
secureStub = sinon.stub();
sinon.stub(urlUtils, 'redirectToAdmin');
sinon.stub(urlUtils, 'redirect301');
sinon.stub(urlService, 'getUrlByResourceId');
sinon.stub(helpers, 'secure').get(function () {
return secureStub;
});
renderStub = sinon.stub();
sinon.stub(helpers, 'renderEntry').get(function () {
return function () {
return renderStub;
};
});
previewStub = sinon.stub();
previewStub.withArgs({
uuid: req.params.uuid,
status: 'all',
include: 'authors,tags'
}).resolves(apiResponse);
sinon.stub(api.v2, 'preview').get(() => {
return {
read: previewStub
};
});
});
it('should render post', function (done) {
controllers.preview(req, res, failTest(done)).then(function () {
renderStub.called.should.be.true();
secureStub.called.should.be.true();
done();
}).catch(done);
});
});
describe('canary', function () {
let previewStub;
beforeEach(function () {
post = testUtils.DataGenerator.forKnex.createPost({status: 'draft'});
apiResponse = {
preview: [post]
};
req = {
path: '/',
params: {
uuid: 'something'
},
route: {}
};
res = {
routerOptions: {
query: {controller: 'preview', resource: 'preview'}
},
locals: {
apiVersion: 'canary'
},
render: sinon.spy(),
redirect: sinon.spy(),
set: sinon.spy()
};
secureStub = sinon.stub();
sinon.stub(urlUtils, 'redirectToAdmin');
sinon.stub(urlUtils, 'redirect301');
sinon.stub(urlService, 'getUrlByResourceId');
sinon.stub(helpers, 'secure').get(function () {
return secureStub;
});
renderStub = sinon.stub();
sinon.stub(helpers, 'renderEntry').get(function () {
return function () {
return renderStub;
};
});
previewStub = sinon.stub();
previewStub.withArgs({
uuid: req.params.uuid,
status: 'all',
include: 'authors,tags'
}).resolves(apiResponse);
sinon.stub(api.canary, 'preview').get(() => {
return {
read: previewStub
};
});
});
it('should render post', function (done) {
controllers.preview(req, res, failTest(done)).then(function () {
renderStub.called.should.be.true();
secureStub.called.should.be.true();
done();
}).catch(done);
});
});
describe('v3', function () {
let previewStub;
beforeEach(function () {
post = testUtils.DataGenerator.forKnex.createPost({status: 'draft'});
apiResponse = {
preview: [post]
};
req = {
path: '/',
params: {
uuid: 'something'
},
route: {}
};
res = {
routerOptions: {
query: {controller: 'preview', resource: 'preview'}
},
locals: {
apiVersion: 'v3'
},
render: sinon.spy(),
redirect: sinon.spy(),
set: sinon.spy()
};
secureStub = sinon.stub();
sinon.stub(urlUtils, 'redirectToAdmin');
sinon.stub(urlUtils, 'redirect301');
sinon.stub(urlService, 'getUrlByResourceId');
sinon.stub(helpers, 'secure').get(function () {
return secureStub;
});
renderStub = sinon.stub();
sinon.stub(helpers, 'renderEntry').get(function () {
return function () {
return renderStub;
};
});
previewStub = sinon.stub();
previewStub.withArgs({
uuid: req.params.uuid,
status: 'all',
include: 'authors,tags'
}).resolves(apiResponse);
sinon.stub(api.v3, 'preview').get(() => {
return {
read: previewStub
};
});
});
it('should render post', function (done) {
controllers.preview(req, res, failTest(done)).then(function () {
renderStub.called.should.be.true();
secureStub.called.should.be.true();
done();
}).catch(done);
});
});
});