2018-08-01 17:19:02 -04:00
|
|
|
const chai = require('chai');
|
|
|
|
chai.use(require('chai-http'));
|
|
|
|
const should = chai.should();
|
|
|
|
const describe = require('mocha').describe;
|
|
|
|
|
2019-01-02 14:20:10 -05:00
|
|
|
const verify = require('../app/util/verify.js');
|
2018-08-01 17:19:02 -04:00
|
|
|
|
|
|
|
describe('Body Verification', () => {
|
2019-01-02 14:20:10 -05:00
|
|
|
const testVerify = async (prop, expected, code, message) => {
|
2018-08-01 17:19:02 -04:00
|
|
|
try {
|
2019-01-02 14:20:10 -05:00
|
|
|
await verify(prop, expected);
|
2018-08-01 17:19:02 -04:00
|
|
|
} catch (err) {
|
2019-01-02 14:20:10 -05:00
|
|
|
err.code.should.equal(code);
|
|
|
|
err.message.should.equal(message);
|
2018-08-01 17:19:02 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
it('must continue properly with valid prop', () => {
|
2019-01-01 14:23:21 -05:00
|
|
|
const tests = [{
|
2019-01-02 14:20:10 -05:00
|
|
|
expected: {name: 'test'},
|
|
|
|
prop: 'test'
|
2019-01-01 14:23:21 -05:00
|
|
|
}, {
|
2019-01-02 14:20:10 -05:00
|
|
|
expected: {name: 'test', type: 'array'},
|
|
|
|
prop: ['1', '2', '3']
|
2019-01-01 14:23:21 -05:00
|
|
|
}, {
|
2019-01-02 14:20:10 -05:00
|
|
|
expected: {name: 'test', type: 'date'},
|
|
|
|
prop: '11/12/2018'
|
2019-01-01 14:23:21 -05:00
|
|
|
}, {
|
2019-01-02 14:20:10 -05:00
|
|
|
expected: {name: 'test', type: 'number'},
|
|
|
|
prop: '1546368715'
|
2019-01-01 14:29:42 -05:00
|
|
|
}, {
|
2019-01-02 14:20:10 -05:00
|
|
|
expected: {name: 'test', type: 'number', min: 12, max: 16},
|
|
|
|
prop: '16'
|
2019-01-01 14:23:21 -05:00
|
|
|
}];
|
|
|
|
|
2019-01-02 14:20:10 -05:00
|
|
|
return Promise.all(tests.map(test => testVerify(test.prop, test.expected)));
|
2018-08-01 17:19:02 -04:00
|
|
|
});
|
|
|
|
|
2019-01-02 14:20:10 -05:00
|
|
|
it('must continue with a missing but optional prop', () =>
|
|
|
|
testVerify(undefined, {name: 'test', optional: true}));
|
2018-08-01 17:19:02 -04:00
|
|
|
|
2019-01-02 14:20:10 -05:00
|
|
|
it('must error with a missing prop', () =>
|
|
|
|
testVerify(undefined, {name: 'test'}, 400, 'test not specified.'));
|
2018-08-01 17:19:02 -04:00
|
|
|
|
2019-01-02 14:20:10 -05:00
|
|
|
it('must error with an invalid primitive type', () =>
|
|
|
|
testVerify(['1', '2', '3'], {name: 'test', type: 'string'}, 400, 'test malformed.'));
|
2018-08-01 17:19:02 -04:00
|
|
|
|
2019-01-02 14:20:10 -05:00
|
|
|
it('must error with an invalid date type', () =>
|
|
|
|
testVerify('123abc', {name: 'test', type: 'date'}, 400, 'test malformed.'));
|
2019-01-01 14:23:21 -05:00
|
|
|
|
2019-01-02 14:20:10 -05:00
|
|
|
it('must error with an invalid array type', () =>
|
|
|
|
testVerify('test', {name: 'test', type: 'array'}, 400, 'test malformed.'));
|
2018-08-01 17:19:02 -04:00
|
|
|
|
2019-01-02 14:20:10 -05:00
|
|
|
it('must error when smaller than the minimum', () =>
|
|
|
|
testVerify('3', {name: 'test', type: 'number', min: 10}, 400, 'test too small.'));
|
2019-01-01 14:29:42 -05:00
|
|
|
|
2019-01-02 14:20:10 -05:00
|
|
|
it('must error when larger than the maximum', () =>
|
|
|
|
testVerify('15', {name: 'test', type: 'number', max: 10}, 400, 'test too large.'));
|
2019-01-01 14:29:42 -05:00
|
|
|
|
2019-01-02 14:20:10 -05:00
|
|
|
it('must error with a length higher than the max', () =>
|
|
|
|
testVerify('123456', {name: 'test', maxLength: 5}, 400, 'test too long.'));
|
2018-08-01 17:19:02 -04:00
|
|
|
|
2019-01-02 14:20:10 -05:00
|
|
|
it('must error with a dirty prop that gets sanitized', () =>
|
|
|
|
testVerify('test<svg/onload=alert("XSS")>', {name: 'test', sanitize: true}, 400, 'test contains invalid characters.'));
|
2018-08-01 17:19:02 -04:00
|
|
|
|
2019-01-02 14:20:10 -05:00
|
|
|
it('must error with a restricted character', () =>
|
|
|
|
testVerify('test test', {name: 'test', restrict: new RegExp("\\s")}, 400, 'test contains invalid characters.'));
|
2018-08-01 17:19:02 -04:00
|
|
|
});
|