mirror of
https://github.com/atlanticbiomedical/biomedjs.git
synced 2025-07-02 00:47:26 -04:00
Changes
This commit is contained in:
111
node_modules/express-validator/test/asyncTest.js
generated
vendored
Normal file
111
node_modules/express-validator/test/asyncTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
117
node_modules/express-validator/test/checkBodySchemaTest.js
generated
vendored
Normal file
117
node_modules/express-validator/test/checkBodySchemaTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
116
node_modules/express-validator/test/checkBodyTest.js
generated
vendored
Normal file
116
node_modules/express-validator/test/checkBodyTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
132
node_modules/express-validator/test/checkParamsSchemaTest.js
generated
vendored
Normal file
132
node_modules/express-validator/test/checkParamsSchemaTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
119
node_modules/express-validator/test/checkParamsTest.js
generated
vendored
Normal file
119
node_modules/express-validator/test/checkParamsTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
123
node_modules/express-validator/test/checkQuerySchemaTest.js
generated
vendored
Normal file
123
node_modules/express-validator/test/checkQuerySchemaTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
111
node_modules/express-validator/test/checkQueryTest.js
generated
vendored
Normal file
111
node_modules/express-validator/test/checkQueryTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
125
node_modules/express-validator/test/checkTest.js
generated
vendored
Normal file
125
node_modules/express-validator/test/checkTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
73
node_modules/express-validator/test/customSanitizersTest.js
generated
vendored
Normal file
73
node_modules/express-validator/test/customSanitizersTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
25
node_modules/express-validator/test/formatParamOutputTest.js
generated
vendored
Normal file
25
node_modules/express-validator/test/formatParamOutputTest.js
generated
vendored
Normal file
@ -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');
|
||||
});
|
||||
});
|
40
node_modules/express-validator/test/helpers/app.js
generated
vendored
Normal file
40
node_modules/express-validator/test/helpers/app.js
generated
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
// Sample app
|
||||
var express = require('express');
|
||||
var expressValidator = require('../../index');
|
||||
var bodyParser = require('body-parser');
|
||||
var Promise = require('bluebird');
|
||||
|
||||
var port = process.env.PORT || 8888;
|
||||
var app = express();
|
||||
|
||||
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;
|
||||
};
|
29
node_modules/express-validator/test/helpers/example.js
generated
vendored
Normal file
29
node_modules/express-validator/test/helpers/example.js
generated
vendored
Normal file
@ -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);
|
52
node_modules/express-validator/test/mappedOutputTest.js
generated
vendored
Normal file
52
node_modules/express-validator/test/mappedOutputTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
});
|
40
node_modules/express-validator/test/nestedInputSanitizeTest.js
generated
vendored
Normal file
40
node_modules/express-validator/test/nestedInputSanitizeTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
61
node_modules/express-validator/test/nestedInputTest.js
generated
vendored
Normal file
61
node_modules/express-validator/test/nestedInputTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
});
|
98
node_modules/express-validator/test/optionalSchemaTest.js
generated
vendored
Normal file
98
node_modules/express-validator/test/optionalSchemaTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
});
|
81
node_modules/express-validator/test/optionalTest.js
generated
vendored
Normal file
81
node_modules/express-validator/test/optionalTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
});
|
51
node_modules/express-validator/test/regexRouteTest.js
generated
vendored
Normal file
51
node_modules/express-validator/test/regexRouteTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
});
|
46
node_modules/express-validator/test/sanitizeBodyTest.js
generated
vendored
Normal file
46
node_modules/express-validator/test/sanitizeBodyTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
45
node_modules/express-validator/test/sanitizeHeadersTest.js
generated
vendored
Normal file
45
node_modules/express-validator/test/sanitizeHeadersTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
65
node_modules/express-validator/test/sanitizeParamsTest.js
generated
vendored
Normal file
65
node_modules/express-validator/test/sanitizeParamsTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
65
node_modules/express-validator/test/sanitizeQueryTest.js
generated
vendored
Normal file
65
node_modules/express-validator/test/sanitizeQueryTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
87
node_modules/express-validator/test/sanitizerResultTest.js
generated
vendored
Normal file
87
node_modules/express-validator/test/sanitizerResultTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
104
node_modules/express-validator/test/sanitizerTest.js
generated
vendored
Normal file
104
node_modules/express-validator/test/sanitizerTest.js
generated
vendored
Normal file
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
93
node_modules/express-validator/test/withMessageTest.js
generated
vendored
Normal file
93
node_modules/express-validator/test/withMessageTest.js
generated
vendored
Normal file
@ -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(2, 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(2).withMessage('Custom Error Message');
|
||||
|
||||
expect(req.validationErrors()).to.deep.equal([
|
||||
{ param: 'testParam', msg: 'Default Error Message', value: 'abc' }
|
||||
]);
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user