2020-05-25 03:49:38 -05:00
|
|
|
const errors = require('@tryghost/errors');
|
2018-10-04 17:50:45 -05:00
|
|
|
const Promise = require('bluebird');
|
|
|
|
const sinon = require('sinon');
|
2020-03-30 10:26:47 -05:00
|
|
|
const shared = require('../../../../../core/server/api/shared');
|
2018-10-04 17:50:45 -05:00
|
|
|
|
|
|
|
describe('Unit: api/shared/validators/handle', function () {
|
|
|
|
afterEach(function () {
|
2019-01-21 11:53:44 -05:00
|
|
|
sinon.restore();
|
2018-10-04 17:50:45 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('input', function () {
|
|
|
|
it('no api config passed', function () {
|
|
|
|
return shared.validators.handle.input()
|
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
2020-05-25 03:49:38 -05:00
|
|
|
(err instanceof errors.IncorrectUsageError).should.be.true();
|
2018-10-04 17:50:45 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('no api validators passed', function () {
|
|
|
|
return shared.validators.handle.input({})
|
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
2020-05-25 03:49:38 -05:00
|
|
|
(err instanceof errors.IncorrectUsageError).should.be.true();
|
2018-10-04 17:50:45 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ensure validators are called', function () {
|
2019-01-21 11:53:44 -05:00
|
|
|
const getStub = sinon.stub();
|
|
|
|
const addStub = sinon.stub();
|
2019-07-05 06:40:43 -05:00
|
|
|
sinon.stub(shared.validators.input.all, 'all').get(() => {
|
|
|
|
return getStub;
|
|
|
|
});
|
|
|
|
sinon.stub(shared.validators.input.all, 'add').get(() => {
|
|
|
|
return addStub;
|
|
|
|
});
|
2018-10-04 17:50:45 -05:00
|
|
|
|
|
|
|
const apiValidators = {
|
|
|
|
all: {
|
2019-01-21 11:53:44 -05:00
|
|
|
add: sinon.stub().resolves()
|
2018-10-04 17:50:45 -05:00
|
|
|
},
|
|
|
|
posts: {
|
2019-01-21 11:53:44 -05:00
|
|
|
add: sinon.stub().resolves()
|
2018-10-04 17:50:45 -05:00
|
|
|
},
|
|
|
|
users: {
|
2019-01-21 11:53:44 -05:00
|
|
|
add: sinon.stub().resolves()
|
2018-10-04 17:50:45 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return shared.validators.handle.input({docName: 'posts', method: 'add'}, apiValidators, {context: {}})
|
|
|
|
.then(() => {
|
|
|
|
getStub.calledOnce.should.be.true();
|
2018-10-11 16:05:49 -05:00
|
|
|
addStub.calledOnce.should.be.true();
|
2018-10-04 17:50:45 -05:00
|
|
|
apiValidators.all.add.calledOnce.should.be.true();
|
|
|
|
apiValidators.posts.add.calledOnce.should.be.true();
|
|
|
|
apiValidators.users.add.called.should.be.false();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|