aboutsummaryrefslogtreecommitdiff
path: root/node_modules/express-validator/test/optionalSchemaTest.js
blob: de2f378aaa47539a0f9388d0b37ff262a76c1e6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var chai = require('chai');
var expect = chai.expect;
var request = require('supertest');

var app;
var errorMessage = 'Parameter is not an integer';

function validation(req, res) {
  req.assert({
    'optional_param': {
      optional: true,
      isInt: {
        errorMessage: errorMessage
      }
    }
  });

  req.assert({
    'optional_falsy_param': {
      optional: {
        options: [{ checkFalsy: true }]
      },
      isInt: {
        errorMessage: errorMessage
      }
    }
  });

  var errors = req.validationErrors();
  if (errors) {
    return res.send(errors);
  }
  res.send({ result: 'OK' });
}

function fail(body) {
  expect(body).to.have.length(1);
  expect(body[0]).to.have.property('msg', errorMessage);
}

function pass(body) {
  expect(body).to.have.property('result', 'OK');
}

function testRoute(path, test, done) {
  request(app)
    .get(path)
    .end(function(err, res) {
      test(res.body);
      done();
    });
}

// This before() is required in each set of tests in
// order to use a new validation function in each file
before(function() {
  delete require.cache[require.resolve('./helpers/app')];
  app = require('./helpers/app')(validation);
});

// TODO: Don't know if all of these are necessary, but we do need to test body and header
describe('#optionalSchema()', function() {
  it('should return a success when there is an empty route', function(done) {
    testRoute('/', pass, done);
  });

  it('should return a success when there are no params on a route', function(done) {
    testRoute('/path', pass, done);
  });

  it('should return a success when the non-optional param is present', function(done) {
    testRoute('/path?other_param=test', pass, done);
  });

  it('should return an error when param is provided, but empty', function(done) {
    testRoute('/path?optional_param', fail, done);
  });

  it('should return an error when param is provided with equals sign, but empty', function(done) {
    testRoute('/path?optional_param=', fail, done);
  });

  it('should return an error when param is provided, but fails validation', function(done) {
    testRoute('/path?optional_param=test', fail, done);
  });

  it('should return a success when param is provided and validated', function(done) {
    testRoute('/path?optional_param=123', pass, done);
  });

  it('should return a success when the optional falsy param is present, but false', function(done) {
    testRoute('/path?optional_falsy_param=', pass, done);
  });

  it('should return an error when the optional falsy param is present, but does not pass', function(done) {
    testRoute('/path?optional_falsy_param=hello', fail, done);
  });
});