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