aboutsummaryrefslogtreecommitdiff
path: root/node_modules/express-validator/test/sanitizeParamsTest.js
blob: 8d331f1b169ca308a0d78dc9153ed0d75d9dceda (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
var chai = require('chai');
var expect = chai.expect;
var request = require('supertest');

var app;
function validation(req, res) {
  req.sanitizeParams('testparam').whitelist(['a', 'b', 'c']);
  res.send({ params: req.params });
}

function pass(body) {
  expect(body).to.have.deep.property('params.testparam', 'abc');
}
function fail(body) {
  expect(body).to.not.have.property('params', 'testparam');
}

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

function postRoute(path, data, test, done) {
  request(app)
    .post(path)
    .send(data)
    .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);
});

describe('#sanitizeParams', function() {
  describe('GET tests', function() {
    it('should return property and sanitized value when param is present', function(done) {
      getRoute('/abcdef', pass, done);
    });
    it('should not return property when param is missing', function(done) {
      getRoute('/', fail, done);
    });


  });
  describe('POST tests', function() {
    it('should return property and sanitized value when param is present', function(done) {
      postRoute('/abcdef', null, pass, done);
    });

    it('should not return property when param is missing', function(done) {
      postRoute('/', null, fail, done);
    });

  });
});