A simple file sharing site with an easy to use API and online panel.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

90 linhas
3.3KB

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