From bc499aa260fb4067fe778bd2fb0bf905a0e0e382 Mon Sep 17 00:00:00 2001 From: Jack Foltz Date: Wed, 1 Aug 2018 17:19:02 -0400 Subject: [PATCH] Add tests for verifyBody --- test/middleware.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 test/middleware.js diff --git a/test/middleware.js b/test/middleware.js new file mode 100644 index 0000000..22e8400 --- /dev/null +++ b/test/middleware.js @@ -0,0 +1,59 @@ +const chai = require('chai'); +chai.use(require('chai-http')); +const should = chai.should(); +const describe = require('mocha').describe; + +const verifyBody = require('../app/util/verifyBody').verifyBody; + +describe('Body Verification', () => { + const testVerifyBody = async (body, expected, code, message) => { + try { + await verifyBody(body, expected); + } catch (err) { + if (code) + err.code.should.equal(code); + if (message) + err.message.should.equal(message); + } + }; + + it('must continue properly with valid prop', () => { + const expected = [{name: 'test'}]; + return testVerifyBody({test: 'test'}, expected); + }); + + it('must continue with a missing but optional prop', () => { + const expected = [{name: 'test', optional: true}]; + return testVerifyBody({}, expected); + }); + + it('must error with a missing prop', () => { + const expected = [{name: 'test'}]; + return testVerifyBody({}, expected, 400, 'test not specified.'); + }); + + it('must error with an invalid type', () => { + const expected = [{name: 'test', type: 'string'}]; + return testVerifyBody({test: [1, 2, 3]}, expected, 400, 'test malformed.'); + }); + + it('must error with an invalid instance', () => { + const expected = [{name: 'test', instance: Array}]; + return testVerifyBody({test: 'test'}, expected, 400, 'test malformed.'); + }); + + it('must error with a length higher than the max', () => { + const expected = [{name: 'test', maxLength: 5}]; + return testVerifyBody({test: '123456'}, expected, 400, 'test too long.'); + }); + + it('must error with a dirty prop that gets sanitized', () => { + const expected = [{name: 'test', sanitize: true}]; + return testVerifyBody({test: 'test'}, expected, 400, 'test contains invalid characters.'); + }); + + it('must error with a restricted character', () => { + const expected = [{name: 'test', restrict: new RegExp("\\s")}]; + return testVerifyBody({test: 'test test'}, expected, 400, 'test contains invalid characters.'); + }) +}); \ No newline at end of file