A simple file sharing site with an easy to use API and online panel.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

68 lignes
2.6KB

  1. const chai = require('chai');
  2. chai.use(require('chai-http'));
  3. const should = chai.should();
  4. const describe = require('mocha').describe;
  5. const verify = require('../app/util/verify.js');
  6. describe('Body Verification', () => {
  7. const testVerify = async (prop, expected, code, message) => {
  8. try {
  9. await verify(prop, expected);
  10. } catch (err) {
  11. err.code.should.equal(code);
  12. err.message.should.equal(message);
  13. }
  14. };
  15. it('must continue properly with valid prop', () => {
  16. const tests = [{
  17. expected: {name: 'test'},
  18. prop: 'test'
  19. }, {
  20. expected: {name: 'test', type: 'array'},
  21. prop: ['1', '2', '3']
  22. }, {
  23. expected: {name: 'test', type: 'date'},
  24. prop: '11/12/2018'
  25. }, {
  26. expected: {name: 'test', type: 'number'},
  27. prop: '1546368715'
  28. }, {
  29. expected: {name: 'test', type: 'number', min: 12, max: 16},
  30. prop: '16'
  31. }];
  32. return Promise.all(tests.map(test => testVerify(test.prop, test.expected)));
  33. });
  34. it('must continue with a missing but optional prop', () =>
  35. testVerify(undefined, {name: 'test', optional: true}));
  36. it('must error with a missing prop', () =>
  37. testVerify(undefined, {name: 'test'}, 400, 'test not specified.'));
  38. it('must error with an invalid primitive type', () =>
  39. testVerify(['1', '2', '3'], {name: 'test', type: 'string'}, 400, 'test malformed.'));
  40. it('must error with an invalid date type', () =>
  41. testVerify('123abc', {name: 'test', type: 'date'}, 400, 'test malformed.'));
  42. it('must error with an invalid array type', () =>
  43. testVerify('test', {name: 'test', type: 'array'}, 400, 'test malformed.'));
  44. it('must error when smaller than the minimum', () =>
  45. testVerify('3', {name: 'test', type: 'number', min: 10}, 400, 'test too small.'));
  46. it('must error when larger than the maximum', () =>
  47. testVerify('15', {name: 'test', type: 'number', max: 10}, 400, 'test too large.'));
  48. it('must error with a length higher than the max', () =>
  49. testVerify('123456', {name: 'test', maxLength: 5}, 400, 'test too long.'));
  50. it('must error with a dirty prop that gets sanitized', () =>
  51. testVerify('test<svg/onload=alert("XSS")>', {name: 'test', sanitize: true}, 400, 'test contains invalid characters.'));
  52. it('must error with a restricted character', () =>
  53. testVerify('test test', {name: 'test', restrict: new RegExp("\\s")}, 400, 'test contains invalid characters.'));
  54. });