mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 18:31:57 +03:00
f08a55c21f
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
255 lines
9.4 KiB
JavaScript
255 lines
9.4 KiB
JavaScript
const errors = require('@tryghost/errors');
|
|
const should = require('should');
|
|
const sinon = require('sinon');
|
|
const Promise = require('bluebird');
|
|
const shared = require('../../../../core/server/api/shared');
|
|
|
|
describe('Unit: api/shared/pipeline', function () {
|
|
afterEach(function () {
|
|
sinon.restore();
|
|
});
|
|
|
|
describe('stages', function () {
|
|
describe('validation', function () {
|
|
describe('input', function () {
|
|
beforeEach(function () {
|
|
sinon.stub(shared.validators.handle, 'input').resolves();
|
|
});
|
|
|
|
it('do it yourself', function () {
|
|
const apiUtils = {};
|
|
const apiConfig = {};
|
|
const apiImpl = {
|
|
validation: sinon.stub().resolves('response')
|
|
};
|
|
const frame = {};
|
|
|
|
return shared.pipeline.STAGES.validation.input(apiUtils, apiConfig, apiImpl, frame)
|
|
.then((response) => {
|
|
response.should.eql('response');
|
|
|
|
apiImpl.validation.calledOnce.should.be.true();
|
|
shared.validators.handle.input.called.should.be.false();
|
|
});
|
|
});
|
|
|
|
it('default', function () {
|
|
const apiUtils = {
|
|
validators: {
|
|
input: {
|
|
posts: {}
|
|
}
|
|
}
|
|
};
|
|
const apiConfig = {
|
|
docName: 'posts'
|
|
};
|
|
const apiImpl = {
|
|
options: ['include'],
|
|
validation: {
|
|
options: {
|
|
include: {
|
|
required: true
|
|
}
|
|
}
|
|
}
|
|
};
|
|
const frame = {
|
|
options: {}
|
|
};
|
|
|
|
return shared.pipeline.STAGES.validation.input(apiUtils, apiConfig, apiImpl, frame)
|
|
.then(() => {
|
|
shared.validators.handle.input.calledOnce.should.be.true();
|
|
shared.validators.handle.input.calledWith(
|
|
{
|
|
docName: 'posts',
|
|
options: {
|
|
include: {
|
|
required: true
|
|
}
|
|
}
|
|
},
|
|
{
|
|
posts: {}
|
|
},
|
|
{
|
|
options: {}
|
|
}).should.be.true();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('permissions', function () {
|
|
let apiUtils;
|
|
|
|
beforeEach(function () {
|
|
apiUtils = {
|
|
permissions: {
|
|
handle: sinon.stub().resolves()
|
|
}
|
|
};
|
|
});
|
|
|
|
it('key is missing', function () {
|
|
const apiConfig = {};
|
|
const apiImpl = {};
|
|
const frame = {};
|
|
|
|
return shared.pipeline.STAGES.permissions(apiUtils, apiConfig, apiImpl, frame)
|
|
.then(Promise.reject)
|
|
.catch((err) => {
|
|
(err instanceof errors.IncorrectUsageError).should.be.true();
|
|
apiUtils.permissions.handle.called.should.be.false();
|
|
});
|
|
});
|
|
|
|
it('do it yourself', function () {
|
|
const apiConfig = {};
|
|
const apiImpl = {
|
|
permissions: sinon.stub().resolves('lol')
|
|
};
|
|
const frame = {};
|
|
|
|
return shared.pipeline.STAGES.permissions(apiUtils, apiConfig, apiImpl, frame)
|
|
.then((response) => {
|
|
response.should.eql('lol');
|
|
apiImpl.permissions.calledOnce.should.be.true();
|
|
apiUtils.permissions.handle.called.should.be.false();
|
|
});
|
|
});
|
|
|
|
it('skip stage', function () {
|
|
const apiConfig = {};
|
|
const apiImpl = {
|
|
permissions: false
|
|
};
|
|
const frame = {};
|
|
|
|
return shared.pipeline.STAGES.permissions(apiUtils, apiConfig, apiImpl, frame)
|
|
.then(() => {
|
|
apiUtils.permissions.handle.called.should.be.false();
|
|
});
|
|
});
|
|
|
|
it('default', function () {
|
|
const apiConfig = {};
|
|
const apiImpl = {
|
|
permissions: true
|
|
};
|
|
const frame = {};
|
|
|
|
return shared.pipeline.STAGES.permissions(apiUtils, apiConfig, apiImpl, frame)
|
|
.then(() => {
|
|
apiUtils.permissions.handle.calledOnce.should.be.true();
|
|
});
|
|
});
|
|
|
|
it('with permission config', function () {
|
|
const apiConfig = {
|
|
docName: 'posts'
|
|
};
|
|
const apiImpl = {
|
|
permissions: {
|
|
unsafeAttrs: ['test']
|
|
}
|
|
};
|
|
const frame = {
|
|
options: {}
|
|
};
|
|
|
|
return shared.pipeline.STAGES.permissions(apiUtils, apiConfig, apiImpl, frame)
|
|
.then(() => {
|
|
apiUtils.permissions.handle.calledOnce.should.be.true();
|
|
apiUtils.permissions.handle.calledWith(
|
|
{
|
|
docName: 'posts',
|
|
unsafeAttrs: ['test']
|
|
},
|
|
{
|
|
options: {}
|
|
}).should.be.true();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('pipeline', function () {
|
|
beforeEach(function () {
|
|
sinon.stub(shared.pipeline.STAGES.validation, 'input');
|
|
sinon.stub(shared.pipeline.STAGES.serialisation, 'input');
|
|
sinon.stub(shared.pipeline.STAGES.serialisation, 'output');
|
|
sinon.stub(shared.pipeline.STAGES, 'permissions');
|
|
sinon.stub(shared.pipeline.STAGES, 'query');
|
|
});
|
|
|
|
it('ensure we receive a callable api controller fn', function () {
|
|
const apiController = {
|
|
add: {},
|
|
browse: {}
|
|
};
|
|
|
|
const apiUtils = {};
|
|
|
|
const result = shared.pipeline(apiController, apiUtils);
|
|
result.should.be.an.Object();
|
|
|
|
should.exist(result.add);
|
|
should.exist(result.browse);
|
|
result.add.should.be.a.Function();
|
|
result.browse.should.be.a.Function();
|
|
});
|
|
|
|
it('call api controller fn', function () {
|
|
const apiController = {
|
|
add: {}
|
|
};
|
|
|
|
const apiUtils = {};
|
|
const result = shared.pipeline(apiController, apiUtils);
|
|
|
|
shared.pipeline.STAGES.validation.input.resolves();
|
|
shared.pipeline.STAGES.serialisation.input.resolves();
|
|
shared.pipeline.STAGES.permissions.resolves();
|
|
shared.pipeline.STAGES.query.resolves('response');
|
|
shared.pipeline.STAGES.serialisation.output.callsFake(function (response, _apiUtils, apiConfig, apiImpl, frame) {
|
|
frame.response = response;
|
|
});
|
|
|
|
return result.add()
|
|
.then((response) => {
|
|
response.should.eql('response');
|
|
|
|
shared.pipeline.STAGES.validation.input.calledOnce.should.be.true();
|
|
shared.pipeline.STAGES.serialisation.input.calledOnce.should.be.true();
|
|
shared.pipeline.STAGES.permissions.calledOnce.should.be.true();
|
|
shared.pipeline.STAGES.query.calledOnce.should.be.true();
|
|
shared.pipeline.STAGES.serialisation.output.calledOnce.should.be.true();
|
|
});
|
|
});
|
|
|
|
it('api controller is fn, not config', function () {
|
|
const apiController = {
|
|
add() {
|
|
return Promise.resolve('response');
|
|
}
|
|
};
|
|
|
|
const apiUtils = {};
|
|
const result = shared.pipeline(apiController, apiUtils);
|
|
|
|
return result.add()
|
|
.then((response) => {
|
|
response.should.eql('response');
|
|
|
|
shared.pipeline.STAGES.validation.input.called.should.be.false();
|
|
shared.pipeline.STAGES.serialisation.input.called.should.be.false();
|
|
shared.pipeline.STAGES.permissions.called.should.be.false();
|
|
shared.pipeline.STAGES.query.called.should.be.false();
|
|
shared.pipeline.STAGES.serialisation.output.called.should.be.false();
|
|
});
|
|
});
|
|
});
|
|
});
|