diff options
Diffstat (limited to 'node_modules/express-validator/test')
27 files changed, 2366 insertions, 0 deletions
diff --git a/node_modules/express-validator/test/asyncTest.js b/node_modules/express-validator/test/asyncTest.js new file mode 100644 index 0000000..75ff520 --- /dev/null +++ b/node_modules/express-validator/test/asyncTest.js @@ -0,0 +1,111 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +var errorMessage = 'Parameter is not 42'; + +// There are three ways to pass parameters to express: +// - as part of the URL +// - as GET parameter in the querystring +// - as POST parameter in the body +// These test show that req.checkQuery are only interested in req.query values, all other +// parameters will be ignored. + +function validation(req, res) { + req.checkQuery('testparam', errorMessage).notEmpty().isAsyncTest(); + + req.asyncValidationErrors().then(function() { + res.send({ testparam: req.query.testparam }); + }, function(errors) { + return res.send(errors); + }); +} + +function fail(body, length) { + expect(body).to.have.length(length); + expect(body[0]).to.have.property('msg', errorMessage); +} + +function pass(body) { + expect(body).to.have.property('testparam', '42'); +} + +function getRoute(path, test, length, done) { + request(app) + .get(path) + .end(function(err, res) { + test(res.body, length); + done(); + }); +} + +function postRoute(path, data, test, length, done) { + request(app) + .post(path) + .send(data) + .end(function(err, res) { + test(res.body, length); + 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('#asyncTest()', function() { + describe('GET tests', function() { + it('should return two errors when query is missing, and unrelated param is present', function(done) { + getRoute('/test', fail, 2, done); + }); + + it('should return two errors when query is missing', function(done) { + getRoute('/', fail, 2, done); + }); + + it('should return a success when query validates and unrelated query is present', function(done) { + getRoute('/42?testparam=42', pass, null, done); + }); + + it('should return one error when query does not validate as int and unrelated query is present', function(done) { + getRoute('/test?testparam=blah', fail, 1, done); + }); + }); + + describe('POST tests', function() { + it('should return two errors when query is missing, and unrelated param is present', function(done) { + postRoute('/test', null, fail, 2, done); + }); + + it('should return two errors when query is missing', function(done) { + postRoute('/', null, fail, 2, done); + }); + + it('should return a success when query validates and unrelated query is present', function(done) { + postRoute('/42?testparam=42', null, pass, null, done); + }); + + it('should return one error when query does not validate as int and unrelated query is present', function(done) { + postRoute('/test?testparam=blah', null, fail, 1, done); + }); + + // POST only + + it('should return a success when query validates and unrelated param/body is present', function(done) { + postRoute('/test?testparam=42', { testparam: 'posttest' }, pass, null, done); + }); + + it('should return one error when query does not validate as int and unrelated param/body is present', function(done) { + postRoute('/test?testparam=blah', { testparam: '42' }, fail, 1, done); + }); + + it('should return two errors when query is missing and unrelated body is present', function(done) { + postRoute('/', { testparam: '42' }, fail, 2, done); + }); + }); + +}); diff --git a/node_modules/express-validator/test/checkBodySchemaTest.js b/node_modules/express-validator/test/checkBodySchemaTest.js new file mode 100644 index 0000000..92fc85f --- /dev/null +++ b/node_modules/express-validator/test/checkBodySchemaTest.js @@ -0,0 +1,117 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var errorMessage = 'Invalid param'; + +var app; + +function validation(req, res) { + req.checkBody({ + 'testparam': { + notEmpty: true, + isInt: true + }, + 'arrayParam': { + isArray: true + } + }); + + var errors = req.validationErrors(); + if (errors) { + return res.send(errors); + } + + res.send({ testparam: req.body.testparam }); +} + +function fail(body, length) { + expect(body).to.have.length(length); + expect(body[0]).to.have.property('msg', errorMessage); +} + +function pass(body) { + expect(body).to.have.property('testparam', '42'); +} + +function getRoute(path, test, length, done) { + request(app) + .get(path) + .end(function(err, res) { + test(res.body, length); + done(); + }); +} + +function postRoute(path, data, test, length, done) { + request(app) + .post(path) + .send(data) + .end(function(err, res) { + test(res.body, length); + 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('#checkBodySchema()', function() { + describe('GET tests', function() { + it('should return three errors when param is missing', function(done) { + getRoute('/', fail, 3, done); + }); + + it('should return three errors when param is present, but not in the body', function(done) { + getRoute('/42', fail, 3, done); + }); + }); + + describe('POST tests', function() { + it('should return three errors when param is missing', function(done) { + postRoute('/', null, fail, 3, done); + }); + + it('should return three errors when param is present, but not in the body', function(done) { + postRoute('/42', null, fail, 3, done); + }); + + // POST only + + it('should return three errors when params are not present', function(done) { + postRoute('/test?testparam=gettest', null, fail, 3, done); + }); + + it('should return three errors when param is present, but not in body', function(done) { + postRoute('/42?testparam=42', null, fail, 3, done); + }); + + it('should return two errors when one param is present, but does not validate', function(done) { + postRoute('/42?testparam=42', { testparam: 'posttest' }, fail, 2, done); + }); + + it('should return a success when params validate on the body', function(done) { + postRoute('/?testparam=blah', { testparam: '42', arrayParam: [1, 2, 3] }, pass, null, done); + }); + + it('should return two errors when two params are present, but do not validate', function(done) { + postRoute('/?testparam=42', { testparam: 'posttest', arrayParam: 123 }, fail, 2, done); + }); + + it('should return two errors when two params are present, but do not validate', function(done) { + postRoute('/?testparam=42', { testparam: 'posttest', arrayParam: {} }, fail, 2, done); + }); + + it('should return two errors when two params are present, but do not validate', function(done) { + postRoute('/', { testparam: 'test', arrayParam: '[]' }, fail, 2, done); + }); + + it('should return a success when params validate on the body', function(done) { + postRoute('/', { testparam: '42', arrayParam: [] }, pass, null, done); + }); + }); +}); diff --git a/node_modules/express-validator/test/checkBodyTest.js b/node_modules/express-validator/test/checkBodyTest.js new file mode 100644 index 0000000..cb14cc6 --- /dev/null +++ b/node_modules/express-validator/test/checkBodyTest.js @@ -0,0 +1,116 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +var errorMessage = 'Parameter is not an integer'; + +// There are three ways to pass parameters to express: +// - as part of the URL +// - as GET parameter in the querystring +// - as POST parameter in the body +// These test show that req.checkBody are only interested in req.body values, all other +// parameters will be ignored. + +function validation(req, res) { + req.checkBody('testparam', errorMessage).notEmpty().isInt(); + req.checkBody('arrayParam').isArray(); + + var errors = req.validationErrors(); + if (errors) { + return res.send(errors); + } + res.send({ testparam: req.body.testparam }); +} + +function fail(body, length) { + expect(body).to.have.length(length); + expect(body[0]).to.have.property('msg', errorMessage); +} + +function pass(body) { + expect(body).to.have.property('testparam', '42'); +} + +function getRoute(path, test, length, done) { + request(app) + .get(path) + .end(function(err, res) { + test(res.body, length); + done(); + }); +} + +function postRoute(path, data, test, length, done) { + request(app) + .post(path) + .send(data) + .end(function(err, res) { + test(res.body, length); + 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('#checkBody()', function() { + describe('GET tests', function() { + it('should return three errors when param is missing', function(done) { + getRoute('/', fail, 3, done); + }); + + it('should return three errors when param is present, but not in the body', function(done) { + getRoute('/42', fail, 3, done); + }); + }); + + describe('POST tests', function() { + it('should return three errors when param is missing', function(done) { + postRoute('/', null, fail, 3, done); + }); + + it('should return three errors when param is present, but not in the body', function(done) { + postRoute('/42', null, fail, 3, done); + }); + + // POST only + + it('should return three errors when params are not present', function(done) { + postRoute('/test?testparam=gettest', null, fail, 3, done); + }); + + it('should return three errors when param is present, but not in body', function(done) { + postRoute('/42?testparam=42', null, fail, 3, done); + }); + + it('should return two errors when one param is present, but does not validate', function(done) { + postRoute('/42?testparam=42', { testparam: 'posttest' }, fail, 2, done); + }); + + it('should return a success when params validate on the body', function(done) { + postRoute('/?testparam=blah', { testparam: '42', arrayParam: [1, 2, 3] }, pass, null, done); + }); + + it('should return two errors when two params are present, but do not validate', function(done) { + postRoute('/?testparam=42', { testparam: 'posttest', arrayParam: 123 }, fail, 2, done); + }); + + it('should return two errors when two params are present, but do not validate', function(done) { + postRoute('/?testparam=42', { testparam: 'posttest', arrayParam: {} }, fail, 2, done); + }); + + it('should return two errors when two params are present, but do not validate', function(done) { + postRoute('/', { testparam: 'test', arrayParam: '[]' }, fail, 2, done); + }); + + it('should return a success when params validate on the body', function(done) { + postRoute('/', { testparam: '42', arrayParam: [] }, pass, null, done); + }); + }); + +});
\ No newline at end of file diff --git a/node_modules/express-validator/test/checkParamsSchemaTest.js b/node_modules/express-validator/test/checkParamsSchemaTest.js new file mode 100644 index 0000000..8a128b5 --- /dev/null +++ b/node_modules/express-validator/test/checkParamsSchemaTest.js @@ -0,0 +1,132 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +var errorMessage = 'Parameter is not an integer'; + +// There are three ways to pass parameters to express: +// - as part of the URL +// - as GET parameter in the querystring +// - as POST parameter in the body +// These test show that req.checkParams are only interested in req.params values, all other +// parameters will be ignored. + +function validation(req, res) { + req.checkParams({ + 'testparam': { + notEmpty: true, + isInt: { + errorMessage: 'Parameter is not an integer' + } + } + }); + + var errors = req.validationErrors(); + if (errors) { + return res.send(errors); + } + res.send({ testparam: req.params.testparam }); +} + +function fail(body, length) { + expect(body).to.have.length(length); + expect(body[0]).to.have.property('msg', errorMessage); +} + +function failMulti(body, length) { + expect(body).to.have.length(length); + expect(body[0]).to.have.property('msg', 'Invalid param'); + expect(body[1]).to.have.property('msg', errorMessage); +} + +function pass(body) { + expect(body).to.have.property('testparam', '42'); +} + +function getRoute(path, test, length, done) { + request(app) + .get(path) + .end(function(err, res) { + test(res.body, length); + done(); + }); +} + +function postRoute(path, data, test, length, done) { + request(app) + .post(path) + .send(data) + .end(function(err, res) { + test(res.body, length); + 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('#checkParamsSchema()', function() { + describe('GET tests', function() { + it('should return one error when param does not validate as int', function(done) { + getRoute('/test', fail, 1, done); + }); + + it('should return two errors when param is missing', function(done) { + getRoute('/', failMulti, 2, done); + }); + + it('should return a success when param validates', function(done) { + getRoute('/42', pass, null, done); + }); + + it('should return a success when param validates and unrelated query is present', function(done) { + getRoute('/42?testparam=42', pass, null, done); + }); + + it('should return one error when param does not validate as int and unrelated query is present', function(done) { + getRoute('/test?testparam=blah', fail, 1, done); + }); + }); + + describe('POST tests', function() { + it('should return one error when param does not validate as int', function(done) { + postRoute('/test', null, fail, 1, done); + }); + + it('should return two errors when param is missing', function(done) { + postRoute('/', null, failMulti, 2, done); + }); + + it('should return a success when param validates', function(done) { + postRoute('/42', null, pass, null, done); + }); + + it('should return a success when param validates and unrelated query is present', function(done) { + postRoute('/42?testparam=42', null, pass, null, done); + }); + + it('should return one error when param does not validate as int and unrelated query is present', function(done) { + postRoute('/test?testparam=blah', null, fail, 1, done); + }); + + // POST only + + it('should return a success when param validates and unrelated query/body is present', function(done) { + postRoute('/42?testparam=blah', { testparam: 'posttest' }, pass, null, done); + }); + + it('should return one error when param does not validate as int and unrelated query/body is present', function(done) { + postRoute('/test?testparam=blah', { testparam: 'posttest' }, fail, 1, done); + }); + + it('should return two errors when param is missing and unrelated query/body is present', function(done) { + postRoute('/?testparam=blah', { testparam: 'posttest' }, failMulti, 2, done); + }); + }); + +}); diff --git a/node_modules/express-validator/test/checkParamsTest.js b/node_modules/express-validator/test/checkParamsTest.js new file mode 100644 index 0000000..db833eb --- /dev/null +++ b/node_modules/express-validator/test/checkParamsTest.js @@ -0,0 +1,119 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +var errorMessage = 'Parameter is not an integer'; + +// There are three ways to pass parameters to express: +// - as part of the URL +// - as GET parameter in the querystring +// - as POST parameter in the body +// These test show that req.checkParams are only interested in req.params values, all other +// parameters will be ignored. + +function validation(req, res) { + req.checkParams('testparam', errorMessage).notEmpty().isInt(); + + var errors = req.validationErrors(); + if (errors) { + return res.send(errors); + } + res.send({ testparam: req.params.testparam }); +} + +function fail(body, length) { + expect(body).to.have.length(length); + expect(body[0]).to.have.property('msg', errorMessage); +} + +function pass(body) { + expect(body).to.have.property('testparam', '42'); +} + +function getRoute(path, test, length, done) { + request(app) + .get(path) + .end(function(err, res) { + test(res.body, length); + done(); + }); +} + +function postRoute(path, data, test, length, done) { + request(app) + .post(path) + .send(data) + .end(function(err, res) { + test(res.body, length); + 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('#checkParams()', function() { + describe('GET tests', function() { + it('should return one error when param does not validate as int', function(done) { + getRoute('/test', fail, 1, done); + }); + + it('should return two errors when param is missing', function(done) { + getRoute('/', fail, 2, done); + }); + + it('should return a success when param validates', function(done) { + getRoute('/42', pass, null, done); + }); + + it('should return a success when param validates and unrelated query is present', function(done) { + getRoute('/42?testparam=42', pass, null, done); + }); + + it('should return one error when param does not validate as int and unrelated query is present', function(done) { + getRoute('/test?testparam=blah', fail, 1, done); + }); + }); + + describe('POST tests', function() { + it('should return one error when param does not validate as int', function(done) { + postRoute('/test', null, fail, 1, done); + }); + + it('should return two errors when param is missing', function(done) { + postRoute('/', null, fail, 2, done); + }); + + it('should return a success when param validates', function(done) { + postRoute('/42', null, pass, null, done); + }); + + it('should return a success when param validates and unrelated query is present', function(done) { + postRoute('/42?testparam=42', null, pass, null, done); + }); + + it('should return one error when param does not validate as int and unrelated query is present', function(done) { + postRoute('/test?testparam=blah', null, fail, 1, done); + }); + + // POST only + + it('should return a success when param validates and unrelated query/body is present', function(done) { + postRoute('/42?testparam=blah', { testparam: 'posttest' }, pass, null, done); + }); + + it('should return one error when param does not validate as int and unrelated query/body is present', function(done) { + postRoute('/test?testparam=blah', { testparam: 'posttest' }, fail, 1, done); + }); + + it('should return two errors when param is missing and unrelated query/body is present', function(done) { + postRoute('/?testparam=blah', { testparam: 'posttest' }, fail, 2, done); + }); + }); + +});
\ No newline at end of file diff --git a/node_modules/express-validator/test/checkQuerySchemaTest.js b/node_modules/express-validator/test/checkQuerySchemaTest.js new file mode 100644 index 0000000..8b334af --- /dev/null +++ b/node_modules/express-validator/test/checkQuerySchemaTest.js @@ -0,0 +1,123 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +var errorMessage = 'Parameter is not valid'; + +// There are three ways to pass parameters to express: +// - as part of the URL +// - as GET parameter in the querystring +// - as POST parameter in the body +// These test show that req.checkQuery are only interested in req.query values, all other +// parameters will be ignored. + +function validation(req, res) { + req.checkQuery({ + 'testparam': { + notEmpty: true, + errorMessage: errorMessage, + isInt: true + } + }); + + var errors = req.validationErrors(); + if (errors) { + return res.send(errors); + } + res.send({ testparam: req.query.testparam }); +} + +function fail(body, length) { + expect(body).to.have.length(length); + expect(body[0]).to.have.property('msg', errorMessage); +} + +function failMulti(body, length) { + expect(body).to.have.length(length); + expect(body[0]).to.have.property('msg', errorMessage); + expect(body[1]).to.have.property('msg', errorMessage); +} + +function pass(body) { + expect(body).to.have.property('testparam', '42'); +} + +function getRoute(path, test, length, done) { + request(app) + .get(path) + .end(function(err, res) { + test(res.body, length); + done(); + }); +} + +function postRoute(path, data, test, length, done) { + request(app) + .post(path) + .send(data) + .end(function(err, res) { + test(res.body, length); + 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('#checkQuerySchema()', function() { + describe('GET tests', function() { + it('should return two errors when query is missing, and unrelated param is present', function(done) { + getRoute('/test', failMulti, 2, done); + }); + + it('should return two errors when query is missing', function(done) { + getRoute('/', failMulti, 2, done); + }); + + it('should return a success when query validates and unrelated query is present', function(done) { + getRoute('/42?testparam=42', pass, null, done); + }); + + it('should return one error when query does not validate as int and unrelated query is present', function(done) { + getRoute('/test?testparam=blah', fail, 1, done); + }); + }); + + describe('POST tests', function() { + it('should return two errors when query is missing, and unrelated param is present', function(done) { + postRoute('/test', null, failMulti, 2, done); + }); + + it('should return two errors when query is missing', function(done) { + postRoute('/', null, failMulti, 2, done); + }); + + it('should return a success when query validates and unrelated query is present', function(done) { + postRoute('/42?testparam=42', null, pass, null, done); + }); + + it('should return one error when query does not validate as int and unrelated query is present', function(done) { + postRoute('/test?testparam=blah', null, fail, 1, done); + }); + + // POST only + + it('should return a success when query validates and unrelated param/body is present', function(done) { + postRoute('/test?testparam=42', { testparam: 'posttest' }, pass, null, done); + }); + + it('should return one error when query does not validate as int and unrelated param/body is present', function(done) { + postRoute('/test?testparam=blah', { testparam: '42' }, fail, 1, done); + }); + + it('should return two errors when query is missing and unrelated body is present', function(done) { + postRoute('/', { testparam: '42' }, failMulti, 2, done); + }); + }); + +}); diff --git a/node_modules/express-validator/test/checkQueryTest.js b/node_modules/express-validator/test/checkQueryTest.js new file mode 100644 index 0000000..b3cee22 --- /dev/null +++ b/node_modules/express-validator/test/checkQueryTest.js @@ -0,0 +1,111 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +var errorMessage = 'Parameter is not an integer'; + +// There are three ways to pass parameters to express: +// - as part of the URL +// - as GET parameter in the querystring +// - as POST parameter in the body +// These test show that req.checkQuery are only interested in req.query values, all other +// parameters will be ignored. + +function validation(req, res) { + req.checkQuery('testparam', errorMessage).notEmpty().isInt(); + + var errors = req.validationErrors(); + if (errors) { + return res.send(errors); + } + res.send({ testparam: req.query.testparam }); +} + +function fail(body, length) { + expect(body).to.have.length(length); + expect(body[0]).to.have.property('msg', errorMessage); +} + +function pass(body) { + expect(body).to.have.property('testparam', '42'); +} + +function getRoute(path, test, length, done) { + request(app) + .get(path) + .end(function(err, res) { + test(res.body, length); + done(); + }); +} + +function postRoute(path, data, test, length, done) { + request(app) + .post(path) + .send(data) + .end(function(err, res) { + test(res.body, length); + 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('#checkQuery()', function() { + describe('GET tests', function() { + it('should return two errors when query is missing, and unrelated param is present', function(done) { + getRoute('/test', fail, 2, done); + }); + + it('should return two errors when query is missing', function(done) { + getRoute('/', fail, 2, done); + }); + + it('should return a success when query validates and unrelated query is present', function(done) { + getRoute('/42?testparam=42', pass, null, done); + }); + + it('should return one error when query does not validate as int and unrelated query is present', function(done) { + getRoute('/test?testparam=blah', fail, 1, done); + }); + }); + + describe('POST tests', function() { + it('should return two errors when query is missing, and unrelated param is present', function(done) { + postRoute('/test', null, fail, 2, done); + }); + + it('should return two errors when query is missing', function(done) { + postRoute('/', null, fail, 2, done); + }); + + it('should return a success when query validates and unrelated query is present', function(done) { + postRoute('/42?testparam=42', null, pass, null, done); + }); + + it('should return one error when query does not validate as int and unrelated query is present', function(done) { + postRoute('/test?testparam=blah', null, fail, 1, done); + }); + + // POST only + + it('should return a success when query validates and unrelated param/body is present', function(done) { + postRoute('/test?testparam=42', { testparam: 'posttest' }, pass, null, done); + }); + + it('should return one error when query does not validate as int and unrelated param/body is present', function(done) { + postRoute('/test?testparam=blah', { testparam: '42' }, fail, 1, done); + }); + + it('should return two errors when query is missing and unrelated body is present', function(done) { + postRoute('/', { testparam: '42' }, fail, 2, done); + }); + }); + +});
\ No newline at end of file diff --git a/node_modules/express-validator/test/checkSchemaTest.js b/node_modules/express-validator/test/checkSchemaTest.js new file mode 100644 index 0000000..68982a1 --- /dev/null +++ b/node_modules/express-validator/test/checkSchemaTest.js @@ -0,0 +1,209 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +var errorMsg = 'Parameter is not an integer.'; +var errorMsgOutOfRange = 'Parameter is out of range or not int.'; + +// There are three ways to pass parameters to express: +// - as part of the URL +// - as GET parameter in the querystring +// - as POST parameter in the body +// These test show that req.checkParams are only interested in req.params values, all other +// parameters will be ignored. + +var schema = { + testparam: { + in: 'params', + notEmpty: true, + isInt: { + errorMessage: errorMsg + } + }, + testquery: { + in: 'query', + notEmpty: true, + isInt: { + options: [{ + min: 2, + max: 10 + }], + errorMessage: errorMsgOutOfRange + } + }, + 'skipped': { + // this validator is a fake validator which cannot raise any error, should be always skipped + in: 'notSupportedOne', + notEmpty: true, + isInt: { + options: [{ + min: 2, + max: 10 + }], + errorMessage: errorMsgOutOfRange + } + }, + 'numInQuery': { + notEmpty: true, + isInt: { + options: [{ + min: 0, + max: 665 + }], + errorMessage: errorMsgOutOfRange + } + } +}; + +function validationSendResponse(req, res) { + var errors = req.validationErrors(); + if (errors) { + return res.send(errors); + } + + res.send({ + testparam: req.params.testparam, + testquery: req.query.testquery, + skipped: req.query.skipped, + numInQuery: req.query.numInQuery + }); +} + +function validation(req, res) { + + req.check(schema); + validationSendResponse(req, res); +} + +function validationQuery(req, res) { + + req.checkQuery(schema); + validationSendResponse(req, res); +} + +function validationParams(req, res) { + + req.checkParams(schema); + validationSendResponse(req, res); +} + +function validationBody(req, res) { + + req.checkBody(schema); + validationSendResponse(req, res); +} + +function failParams(body, length) { + expect(body).to.have.length(length); + expect(body[0]).to.have.property('msg', errorMsg); +} + +function failQuery(body, length) { + expect(body).to.have.length(length); + expect(body[0]).to.have.property('msg', errorMsgOutOfRange); +} + +function failAll(body, length) { + expect(body).to.have.length(length); + expect(body[0]).to.have.property('msg', errorMsg); + expect(body[1]).to.have.property('msg', errorMsgOutOfRange); +} + +function pass(params) { + expect(params).to.have.property('testparam', '25'); + expect(params).to.have.property('testquery', '6'); + expect(params).to.have.property('skipped', '34'); + expect(params).to.have.property('numInQuery', '0'); +} + +function failQueryParams(params, length) { + expect(params).to.have.length(length); + expect(params[0]).to.have.property('msg', 'Invalid param'); + expect(params[1]).to.have.property('msg', errorMsgOutOfRange); +} + +function getRoute(path, test, length, done) { + request(app) + .get(path) + .end(function(err, res) { + test(res.body, length); + done(); + }); +} + +describe('Check defining validator location inside schema validators', function() { + + // 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); + }); + + it('should validate against schema with query and params locations', function(done) { + getRoute('/25?testquery=6&skipped=34&numInQuery=0', pass, 1, done); + }); + + it('should fail when param is not integer', function(done) { + getRoute('/ImNot?testquery=6&skipped=34&numInQuery=0', failParams, 1, done); + }); + + it('should fail when query param is out of range', function(done) { + getRoute('/25?testquery=20&skipped=34&numInQuery=0', failQuery, 1, done); + }); + + it('should fail when non of params are valid', function(done) { + getRoute('/ImNot?testquery=20&skipped=34&numInQuery=0', failAll, 2, done); + }); + +}); + +describe('Check defining validator location inside schema validators by checkQuery()', function() { + + // 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')(validationQuery); + }); + + it('should validate against schema with query and params locations', function(done) { + getRoute('/25?testquery=6&skipped=34&numInQuery=0', pass, 1, done); + }); + + it('should fail when query param is out of range', function(done) { + getRoute('/25?testquery=6&skipped=34&numInQuery=666', failQuery, 1, done); + }); + +}); + +describe('Check defining validator location inside schema validators by checkParams()', function() { + + // 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')(validationParams); + }); + + it('should fail when searching for query param in the path params', function(done) { + getRoute('/25?testquery=6&skipped=34&numInQuery=666', failQueryParams, 2, done); + }); + +}); + +describe('Check defining validator location inside schema validators by checkBody()', function() { + + // 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')(validationBody); + }); + + it('should fail when searching for query param in the body', function(done) { + getRoute('/25?testquery=6&skipped=34&numInQuery=666', failQueryParams, 2, done); + }); + +}); diff --git a/node_modules/express-validator/test/checkTest.js b/node_modules/express-validator/test/checkTest.js new file mode 100644 index 0000000..96f842a --- /dev/null +++ b/node_modules/express-validator/test/checkTest.js @@ -0,0 +1,125 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +var errorMessage = 'Parameter is not an integer'; + +// There are three ways to pass parameters to express: +// - as part of the URL +// - as GET parameter in the querystring +// - as POST parameter in the body +// URL params take precedence over GET params which take precedence over +// POST params. + +function validation(req, res) { + req.check('testparam', errorMessage).notEmpty().isInt(); + + var errors = req.validationErrors(); + if (errors) { + return res.send(errors); + } + res.send({ testparam: req.params.testparam || req.query.testparam || req.body.testparam }); +} + +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('testparam', '42'); +} + +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('#check()/#assert()/#validate()', function() { + describe('GET tests', function() { + it('should return an error when param does not validate', function(done) { + getRoute('/test', fail, done); + }); + + it('should return a success when param validates', function(done) { + getRoute('/42', pass, done); + }); + + // GET only: Test URL over GET param precedence + + it('should return an error when param and query does not validate', function(done) { + getRoute('/test?testparam=gettest', fail, done); + }); + + it('should return a success when param validates, but query does not', function(done) { + getRoute('/42?testparam=gettest', pass, done); + }); + + it('should return an error when query does not validate', function(done) { + getRoute('/?testparam=test', fail, done); + }); + + it('should return a success when query validates', function(done) { + getRoute('/?testparam=42', pass, done); + }); + }); + + describe('POST tests', function() { + it('should return an error when param does not validate', function(done) { + postRoute('/test', null, fail, done); + }); + + it('should return a success when param validates', function(done) { + postRoute('/42', null, pass, done); + }); + + // POST only: Test URL over GET over POST param precedence + + it('should return an error when body validates, but failing param/query is present', function(done) { + postRoute('/test?testparam=gettest', { testparam: '42' }, fail, done); + }); + + it('should return a success when param validates, but non-validating body is present', function(done) { + postRoute('/42?testparam=42', { testparam: 'posttest' }, pass, done); + }); + + it('should return an error when query does not validate, but body validates', function(done) { + postRoute('/?testparam=test', { testparam: '42' }, fail, done); + }); + + it('should return a success when query validates, but non-validating body is present', function(done) { + postRoute('/?testparam=42', { testparam: 'posttest' }, pass, done); + }); + + it('should return an error when body does not validate', function(done) { + postRoute('/', { testparam: 'test' }, fail, done); + }); + + it('should return a success when body validates', function(done) { + postRoute('/', { testparam: '42' }, pass, done); + }); + }); + +});
\ No newline at end of file diff --git a/node_modules/express-validator/test/customSanitizersTest.js b/node_modules/express-validator/test/customSanitizersTest.js new file mode 100644 index 0000000..98c23a0 --- /dev/null +++ b/node_modules/express-validator/test/customSanitizersTest.js @@ -0,0 +1,73 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +function validation(req, res) { + req.sanitize('testparam').toTestSanitize(); + res.send({ testparam: req.params.testparam || req.query.testparam || req.body.testparam }); +} + +function pass(body) { + expect(body).to.have.property('testparam', '!!!!'); +} +function fail(body) { + expect(body).not.to.have.property('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('#customSanitizers', function() { + describe('GET tests', function() { + it('should return property and sanitized value when param is present', function(done) { + getRoute('/valA', pass, done); + }); + it('should not return property when query and param is missing', function(done) { + getRoute('/', fail, done); + }); + + it('should return property and sanitized value when query and param is present', function(done) { + getRoute('/42?testparam=42', pass, done); + }); + + }); + describe('POST tests', function() { + it('should return property and sanitized value when param is present', function(done) { + postRoute('/valA', null, pass, done); + }); + + + it('should return property and sanitized value when body, param and query is present', function(done) { + postRoute('/vaA?testparam=gettest', { testparam: '42' }, pass, done); + }); + + it('should return property and sanitized value when body is present', function(done) { + postRoute('/', { testparam: '42' }, pass, done); + }); + + }); +});
\ No newline at end of file diff --git a/node_modules/express-validator/test/formatParamOutputTest.js b/node_modules/express-validator/test/formatParamOutputTest.js new file mode 100644 index 0000000..9d67b5f --- /dev/null +++ b/node_modules/express-validator/test/formatParamOutputTest.js @@ -0,0 +1,25 @@ +var chai = require('chai'); +var expect = chai.expect; +var formatParamOutput = require('../index').utils.formatParamOutput; + +describe('#formatParamOutput()', function() { + it('should correct return formatted string if all elements in array are strings', function() { + expect(formatParamOutput(['hey', 'yo', 'hello'])).to.equal('hey.yo.hello'); + }); + + it('should return a string with integers in brackets', function() { + expect(formatParamOutput(['hey', 'yo', '0', 'hello'])).to.equal('hey.yo[0].hello'); + expect(formatParamOutput(['hey', 'yo', 0, 'hello'])).to.equal('hey.yo[0].hello'); + expect(formatParamOutput(['hey', 'yo', 0, 0, 'hello'])).to.equal('hey.yo[0][0].hello'); + expect(formatParamOutput(['hey', 'yo', 2342342, 'hello'])).to.equal('hey.yo[2342342].hello'); + expect(formatParamOutput(['hey', 'yo', '2342342', 'hello'])).to.equal('hey.yo[2342342].hello'); + expect(formatParamOutput(['hey', 'yo', '234ALPHA2342', 'hello'])).to.not.equal('hey.yo[234ALPHA2342].hello'); + expect(formatParamOutput(['hey', 'yo', 'hello', 0])).to.equal('hey.yo.hello[0]'); + expect(formatParamOutput(['hey', 'yo', 'hello', 0, 0])).to.equal('hey.yo.hello[0][0]'); + expect(formatParamOutput(['hey', 'yo', 0, 'hello', 0, 0])).to.equal('hey.yo[0].hello[0][0]'); + }); + + it('should return the original param if not an array', function() { + expect(formatParamOutput('yo')).to.equal('yo'); + }); +});
\ No newline at end of file diff --git a/node_modules/express-validator/test/helpers/app.js b/node_modules/express-validator/test/helpers/app.js new file mode 100644 index 0000000..d86a4bb --- /dev/null +++ b/node_modules/express-validator/test/helpers/app.js @@ -0,0 +1,45 @@ +// Sample app +var express = require('express'); +var expressValidator = require('../../index'); +var bodyParser = require('body-parser'); + +var port = process.env.PORT || 8888; +var app = express(); + +// If no native implementation of Promise exists (less than Node v4), +// use Bluebird promises so we can test for both depending on the Node version +if (typeof Promise === 'undefined') { + Promise = require('bluebird'); +} + +module.exports = function(validation) { + + app.set('port', port); + app.use(bodyParser.json()); + app.use(expressValidator({ + customValidators: { + isArray: function(value) { + return Array.isArray(value); + }, + isAsyncTest: function(testparam) { + return new Promise(function(resolve, reject) { + setTimeout(function() { + if (testparam === '42') { return resolve(); } + reject(); + }, 200); + }); + } + }, + customSanitizers: { + toTestSanitize: function() { + return "!!!!"; + } + } + })); + + app.get(/\/test(\d+)/, validation); + app.get('/:testparam?', validation); + app.post('/:testparam?', validation); + + return app; +}; diff --git a/node_modules/express-validator/test/helpers/example.js b/node_modules/express-validator/test/helpers/example.js new file mode 100644 index 0000000..b50f8bf --- /dev/null +++ b/node_modules/express-validator/test/helpers/example.js @@ -0,0 +1,29 @@ +var util = require('util'), + express = require('express'), + expressValidator = require('../../index'), + app = express.createServer(); + +app.use(express.bodyParser()); +app.use(expressValidator); + +app.post('/:urlparam', function(req, res) { + + req.assert('postparam', 'Invalid postparam').notEmpty().isInt(); + req.assert('getparam', 'Invalid getparam').isInt(); + req.assert('urlparam', 'Invalid urlparam').isAlpha(); + + req.sanitize('postparam').toBoolean(); + + var errors = req.validationErrors(); + if (errors) { + res.send('There have been validation errors: ' + util.inspect(errors), 500); + return; + } + res.json({ + urlparam: req.param('urlparam'), + getparam: req.param('getparam'), + postparam: req.param('postparam') + }); +}); + +app.listen(8888); diff --git a/node_modules/express-validator/test/mappedOutputTest.js b/node_modules/express-validator/test/mappedOutputTest.js new file mode 100644 index 0000000..b8a4c63 --- /dev/null +++ b/node_modules/express-validator/test/mappedOutputTest.js @@ -0,0 +1,52 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +var errorMessage = 'valid email required'; + +function validation(req, res) { + req.assert('email', 'required').notEmpty(); + req.assert('email', errorMessage).isEmail(); + + var errors = req.validationErrors(true); + if (errors) { + return res.send(errors); + } + res.send({ email: req.params.email || req.query.email || req.body.email }); +} + +function fail(body) { + expect(body).to.have.deep.property('email.msg', errorMessage); +} + +function pass(body) { + expect(body).to.have.property('email', 'test@example.com'); +} + +function testRoute(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('#validationErrors(true)', function() { + it('should return a success when the correct data is passed on the body', function(done) { + testRoute('/', { email: 'test@example.com' }, pass, done); + }); + + it('should return a mapped error object with each failing param as a property data is invalid', function(done) { + testRoute('/path', { email: 'incorrect' }, fail, done); + }); +});
\ No newline at end of file diff --git a/node_modules/express-validator/test/nestedInputSanitizeTest.js b/node_modules/express-validator/test/nestedInputSanitizeTest.js new file mode 100644 index 0000000..18b6fcd --- /dev/null +++ b/node_modules/express-validator/test/nestedInputSanitizeTest.js @@ -0,0 +1,40 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +function validation(req, res) { + req.sanitize(['user', 'fields', 'email']).trim(); + res.send( req.body ); +} + +function pass(body) { + expect(body).to.have.deep.property('user.fields.email', 'test@example.com'); +} + +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('#nestedInputSanitizers', function() { + describe('POST tests', function() { + + it('should return property and sanitized value', function(done) { + postRoute('/', { user: { fields: { email: ' test@example.com ' } } }, pass, done); + }); + + }); +});
\ No newline at end of file diff --git a/node_modules/express-validator/test/nestedInputTest.js b/node_modules/express-validator/test/nestedInputTest.js new file mode 100644 index 0000000..bf08854 --- /dev/null +++ b/node_modules/express-validator/test/nestedInputTest.js @@ -0,0 +1,61 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; + +function validation(req, res) { + req.assert(['user', 'fields', 'email'], 'not empty').notEmpty(); + req.assert('user.fields.email', 'not empty').notEmpty(); + req.assert(['user', 'fields', 'email'], 'valid email required').isEmail(); + req.assert(['admins', '0', 'name'], 'must only contain letters').isAlpha(); + + var errors = req.validationErrors(); + if (errors) { + return res.send(errors); + } + res.send(req.body); +} + +function fail(body) { + expect(body[0]).to.have.property('msg', 'not empty'); + expect(body[1]).to.have.property('msg', 'not empty'); + expect(body[2]).to.have.property('msg', 'valid email required'); + + // Should convert ['user', 'fields', 'email'] to 'user.fields.email' + // when formatting the error output + expect(body[0]).to.have.property('param').and.to.be.a('string'); + expect(body[1]).to.have.property('param').and.to.be.a('string'); + expect(body[2]).to.have.property('param').and.to.be.a('string'); +} + +function pass(body) { + expect(body).to.have.deep.property('user.fields.email', 'test@example.com'); +} + +function testRoute(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('nested input as array or dot notation', function() { + it('should return a success when the correct data is passed on the body', function(done) { + testRoute('/', { user: { fields: { email: 'test@example.com' } }, admins: [{ name: 'Bobby' }] }, pass, done); + }); + + it('should return an error object with each failing param as a property data is invalid', function(done) { + testRoute('/', { user: { fields: { email: '' } }, admins: [{ name: 0 }] }, fail, done); + }); +});
\ No newline at end of file diff --git a/node_modules/express-validator/test/optionalSchemaTest.js b/node_modules/express-validator/test/optionalSchemaTest.js new file mode 100644 index 0000000..de2f378 --- /dev/null +++ b/node_modules/express-validator/test/optionalSchemaTest.js @@ -0,0 +1,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); + }); +}); diff --git a/node_modules/express-validator/test/optionalTest.js b/node_modules/express-validator/test/optionalTest.js new file mode 100644 index 0000000..44344bf --- /dev/null +++ b/node_modules/express-validator/test/optionalTest.js @@ -0,0 +1,81 @@ +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', errorMessage).optional().isInt(); + req.assert('optional_falsy_param', errorMessage).optional({ checkFalsy: true }).isInt(); + + 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('#optional()', 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); + }); +});
\ No newline at end of file diff --git a/node_modules/express-validator/test/regexRouteTest.js b/node_modules/express-validator/test/regexRouteTest.js new file mode 100644 index 0000000..1638e94 --- /dev/null +++ b/node_modules/express-validator/test/regexRouteTest.js @@ -0,0 +1,51 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +var errorMessage = 'Parameter is not a 3 digit integer'; + +function validation(req, res) { + req.assert(0, errorMessage).len(3, 3).isInt(); + + var errors = req.validationErrors(); + if (errors) { + return res.send(errors); + } + res.send([req.params[0]]); +} + +function fail(body) { + expect(body).to.have.length(2); + expect(body[0]).to.have.property('msg', errorMessage); +} + +function pass(body) { + expect(body[0]).to.equal('123'); +} + +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); +}); + +describe('Express routes can be defined using regular expressions', function() { + it('should return a success when regex route is validated', function(done) { + testRoute('/test123', pass, done); + }); + + it('should return an error when regex route is not validated', function(done) { + testRoute('/test0123', fail, done); + }); +});
\ No newline at end of file diff --git a/node_modules/express-validator/test/sanitizeBodyTest.js b/node_modules/express-validator/test/sanitizeBodyTest.js new file mode 100644 index 0000000..cf17b9c --- /dev/null +++ b/node_modules/express-validator/test/sanitizeBodyTest.js @@ -0,0 +1,46 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +function validation(req, res) { + req.sanitizeBody('testparam').whitelist(['a', 'b', 'c']); + res.send({ body: req.body }); +} + +function pass(body) { + expect(body).to.have.deep.property('body.testparam', 'abc'); +} +function fail(body) { + expect(body).to.not.have.property('body', 'testparam'); +} + +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('#sanitizeBody', function() { + describe('POST tests', function() { + it('should return property and sanitized value when body param is present', function(done) { + postRoute('/', { testparam: ' abcdf ' }, pass, done); + }); + + it('should not return property when body param is missing', function(done) { + postRoute('/', null, fail, done); + }); + + }); +}); diff --git a/node_modules/express-validator/test/sanitizeHeadersTest.js b/node_modules/express-validator/test/sanitizeHeadersTest.js new file mode 100644 index 0000000..9168eba --- /dev/null +++ b/node_modules/express-validator/test/sanitizeHeadersTest.js @@ -0,0 +1,45 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +function validation(req, res) { + req.sanitizeHeaders('x-custom-header').trim(); + res.send(req.headers); +} + +function pass(body) { + expect(body).to.have.property('x-custom-header', 'space'); +} +function fail(body) { + expect(body).to.have.property('x-custom-header').and.to.not.equal('space'); +} + +function getRoute(path, data, test, done) { + request(app) + .get(path) + .set('x-custom-header', 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('#sanitizeHeaders', function() { + describe('GET tests', function() { + it('should return property and sanitized value when headers param is present', function(done) { + getRoute('/', 'space ', pass, done); + }); + + it('should not return property when headers param is missing', function(done) { + getRoute('/', null, fail, done); + }); + }); +}); diff --git a/node_modules/express-validator/test/sanitizeParamsTest.js b/node_modules/express-validator/test/sanitizeParamsTest.js new file mode 100644 index 0000000..8d331f1 --- /dev/null +++ b/node_modules/express-validator/test/sanitizeParamsTest.js @@ -0,0 +1,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); + }); + + }); +}); diff --git a/node_modules/express-validator/test/sanitizeQueryTest.js b/node_modules/express-validator/test/sanitizeQueryTest.js new file mode 100644 index 0000000..974ab58 --- /dev/null +++ b/node_modules/express-validator/test/sanitizeQueryTest.js @@ -0,0 +1,65 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +function validation(req, res) { + req.sanitizeQuery('testparam').whitelist(['a', 'b', 'c']); + res.send({ query: req.query }); +} + +function pass(body) { + expect(body).to.have.deep.property('query.testparam', 'abc'); +} +function fail(body) { + expect(body).to.not.have.property('query', '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('#sanitizeQuery', function() { + describe('GET tests', function() { + it('should return property and sanitized value when query param is present', function(done) { + getRoute('/?testparam=abcdef', pass, done); + }); + it('should not return property when query param is missing', function(done) { + getRoute('/', fail, done); + }); + + + }); + describe('POST tests', function() { + it('should return property and sanitized value when query param is present', function(done) { + postRoute('/?testparam=abcdef', null, pass, done); + }); + + it('should not return property when query param is missing', function(done) { + postRoute('/', null, fail, done); + }); + + }); +}); diff --git a/node_modules/express-validator/test/sanitizerResultTest.js b/node_modules/express-validator/test/sanitizerResultTest.js new file mode 100644 index 0000000..de290f5 --- /dev/null +++ b/node_modules/express-validator/test/sanitizerResultTest.js @@ -0,0 +1,87 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +function validation(req, res) { + var body = req.sanitizeBody('testparam').whitelist(['a', 'b', 'c']); + var query = req.sanitizeQuery('testparam').whitelist(['a', 'b', 'c']); + var params = req.sanitizeParams('testparam').whitelist(['a', 'b', 'c']); + + res.send({ params: params, query: query, body: body }); +} + +function pass(body) { + if (body.params) { + expect(body).to.have.property('params', 'abc'); + } + + if (body.query) { + expect(body).to.have.property('query', 'abc'); + } + + if (body.body) { + expect(body).to.have.property('body', 'abc'); + } + +} +function fail(body) { + expect(body).not.to.have.deep.property('params.testparam'); + expect(body).not.to.have.deep.property('query.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('#sanitizers (check results)', 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 query and param is missing', function(done) { + getRoute('/', fail, done); + }); + + it('should return both query and param and sanitized values when they are both present', function(done) { + getRoute('/abcdef?testparam=abcdef', pass, 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 return both query and param and sanitized values when they are both present', function(done) { + postRoute('/abcdef?testparam=abcdef', { testparam: ' abcdef ' }, pass, done); + }); + + it('should return property and sanitized value when body is present', function(done) { + postRoute('/', { testparam: ' abcdef ' }, pass, done); + }); + + }); +}); diff --git a/node_modules/express-validator/test/sanitizerTest.js b/node_modules/express-validator/test/sanitizerTest.js new file mode 100644 index 0000000..94c52ef --- /dev/null +++ b/node_modules/express-validator/test/sanitizerTest.js @@ -0,0 +1,104 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +function validation(req, res) { + req.sanitize('zerotest').toString(); + req.sanitize('emptystrtest').toBoolean(); + req.sanitize('falsetest').toString(); + req.sanitize('testparam').whitelist(['a', 'b', 'c']); + res.send({ params: req.params, query: req.query, body: req.body }); +} + +function pass(body) { + if (Object.keys(body.params).length) { + expect(body).to.have.deep.property('params.testparam', 'abc'); + } + + if (Object.keys(body.query).length) { + expect(body).to.have.deep.property('query.testparam', 'abc'); + } + + if (Object.keys(body.body).length) { + expect(body).to.have.deep.property('body.testparam', 'abc'); + } + + if (body.body.hasOwnProperty('zerotest')) { + expect(body).to.have.deep.property('body.zerotest', '0'); + } + + if (body.body.hasOwnProperty('emptystrtest')) { + expect(body).to.have.deep.property('body.emptystrtest', false); + } + + if (body.body.hasOwnProperty('falsetest')) { + expect(body).to.have.deep.property('body.falsetest', 'false'); + } + +} + +function fail(body) { + expect(body).not.to.have.deep.property('params.testparam'); + expect(body).not.to.have.deep.property('query.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('#sanitizers', 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 query and param is missing', function(done) { + getRoute('/', fail, done); + }); + + it('should return both query and param and sanitized values when they are both present', function(done) { + getRoute('/abcdef?testparam=abcdef', pass, 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 return both query and param and sanitized values when they are both present', function(done) { + postRoute('/abcdef?testparam=abcdef', { testparam: ' abcdef ' }, pass, done); + }); + + it('should return property and sanitized value when body is present', function(done) { + postRoute('/', { testparam: ' abcdef ' }, pass, done); + }); + + it('should return properly sanitized values even if the original value is falsey, but not null/undefined', function(done) { + postRoute('/', { testparam: ' abcdef ', zerotest: 0, emptystrtest: '', falsetest: false }, pass, done); + }); + + }); +}); diff --git a/node_modules/express-validator/test/withMessageAsyncTest.js b/node_modules/express-validator/test/withMessageAsyncTest.js new file mode 100644 index 0000000..624dbe1 --- /dev/null +++ b/node_modules/express-validator/test/withMessageAsyncTest.js @@ -0,0 +1,143 @@ +var chai = require('chai'); +var expect = chai.expect; +var Promise = require('bluebird'); + +describe('#withMessage()', function() { + describe('on async validators', function() { + + before(function() { + delete require.cache[require.resolve('../lib/express_validator')]; + }); + + it('should not affect a passing validation', function(done) { + var validator = require('../lib/express_validator')({ + customValidators: { + test: function() { + // A dummy passing validator + return new Promise(function(resolve) { + resolve(); + }); + } + } + }); + + var req = { + body: { + testParam: 100 + } + }; + + validator(req, {}, function() {}); + req.check('testParam', 'Default Message') + .test().withMessage('Custom Message'); + + verify(done, function() { + return req.asyncValidationErrors() + .catch(function() { + expect.fail(null, null, 'No errors should have been generated by the test'); + }); + }); + + }); + + it('should provide a custom message when a validation fails', function(done) { + var validator = require('../lib/express_validator')({ + customValidators: { + test: function() { + // A dummy failing validator + return new Promise(function(resolve, reject) { + reject(); + }); + } + } + }); + + var req = { + body: { + testParam: 100 + } + }; + + validator(req, {}, function() {}); + + req.check('testParam', 'Default Message') + .test().withMessage('Custom Message'); + + verify(done, function() { + return req.asyncValidationErrors() + .catch(function(err) { + expect(err).to.deep.equal([{ + msg: 'Custom Message', + param: 'testParam', + value: 100 + }]); + }); + }); + }); + + it('should provide the default message if it was not the validation to fail', function(done) { + var validator = require('../lib/express_validator')({ + customValidators: { + pass: function() { + return new Promise(function(resolve) { resolve(); }); + }, + fail: function() { + return new Promise(function(resolve, reject) { reject(); }); + } + } + }); + + var req = { + body: { + testParam: 100 + } + }; + + validator(req, {}, function() {}); + + req.check('testParam', 'Default Message') + .fail() // Default Message + .pass().withMessage('Passing Message') + .fail() // Default Message + .fail().withMessage('Failing Message') + .fail() // Default Message + .fail().withMessage('Failing Message 1') + .fail().withMessage('Failing Message 2'); + + var expected = [ + 'Default Message', + 'Default Message', + 'Failing Message', + 'Default Message', + 'Failing Message 1', + 'Failing Message 2']; + + verify(done, function() { + return req.asyncValidationErrors() + .catch(function(err) { + expect(err.map(function(e) {return e.msg;})).to.deep.equal(expected); + }); + }); + }); + + // Helper to handle resolving tests async + function verify(done, f) { + try { + var test = f(); + if (test && test.then) { + test.then(function() { + done(); + }) + .catch(function(err) { + done(err); + }); + } else { + done(); + } + } + catch (e) { + done(e); + } + } + }); +});
\ No newline at end of file diff --git a/node_modules/express-validator/test/withMessageTest.js b/node_modules/express-validator/test/withMessageTest.js new file mode 100644 index 0000000..ad38684 --- /dev/null +++ b/node_modules/express-validator/test/withMessageTest.js @@ -0,0 +1,93 @@ +var chai = require('chai'); +var expect = chai.expect; +var request = require('supertest'); + +var app; +var errorMessage = 'Default error message'; +var mustBeTwoDigitsMessage = 'testparam must have two digits'; +var mustBeIntegerMessage = 'testparam must be an integer'; + +function validation(req, res) { + req.checkParams('testparam', errorMessage) + .notEmpty() // with default message + .isInt().withMessage(mustBeIntegerMessage) + .isInt() // with default message + .isLength({ min: 2, max: 2 }).withMessage(mustBeTwoDigitsMessage); + var errors = req.validationErrors(); + if (errors) { + return res.send(errors); + } + res.send({ testparam: req.params.testparam }); +} + +function fail(expectedMessage) { + if (Array.isArray(expectedMessage)) { + return function(body, length) { + expect(body).to.have.length(length); + expect(expectedMessage).to.have.length(length); + for (var i = 0; i < length; ++i) { + expect(body[i]).to.have.property('msg', expectedMessage[i]); + } + }; + } + return function(body, length) { + expect(body).to.have.length(length); + expect(body[0]).to.have.property('msg', expectedMessage); + }; +} + +function pass(body) { + expect(body).to.have.property('testparam', '42'); +} + +function getRoute(path, test, length, done) { + request(app) + .get(path) + .end(function(err, res) { + test(res.body, length); + 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('#withMessage()', function() { + it('should return one error per validation failure, with custom message where defined', function(done) { + getRoute('/test', fail([mustBeIntegerMessage, errorMessage, mustBeTwoDigitsMessage]), 3, done); + }); + + it('should return four errors when param is missing, with default message for the first and third errors, and custom messages for the rest, as defined', function(done) { + getRoute('/', fail([errorMessage, mustBeIntegerMessage, errorMessage, mustBeTwoDigitsMessage]), 4, done); + }); + + it('should return a success when param validates', function(done) { + getRoute('/42', pass, null, done); + }); + + it('should provide a custom message when an invalid value is provided, and the validation is followed by withMessage', function(done) { + getRoute('/199', fail(mustBeTwoDigitsMessage), 1, done); + }); + + it('should update the error message only if the preceeding validation was the one to fail', function() { + var validator = require('../lib/express_validator')(); + var req = { + body: { + testParam: 'abc' + } + }; + + validator(req, {}, function() {}); + req.check('testParam', 'Default Error Message') + .isInt() // should produce 'Default Error Message' + .isLength({ min: 2 }).withMessage('Custom Error Message'); + + expect(req.validationErrors()).to.deep.equal([ + { param: 'testParam', msg: 'Default Error Message', value: 'abc' } + ]); + }); +}); |
