mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
3.8 KiB
115 lines
3.8 KiB
8 years ago
|
var place = require('../../../sanitizer/place'),
|
||
9 years ago
|
sanitize = place.sanitize,
|
||
9 years ago
|
middleware = place.middleware,
|
||
9 years ago
|
defaultClean = { ids: [ { source: 'geonames', layer: 'venue', id: '123' } ], private: false };
|
||
10 years ago
|
|
||
9 years ago
|
// these are the default values you would expect when no input params are specified.
|
||
10 years ago
|
module.exports.tests = {};
|
||
|
|
||
|
module.exports.tests.interface = function(test, common) {
|
||
|
test('sanitize interface', function(t) {
|
||
|
t.equal(typeof sanitize, 'function', 'sanitize is a function');
|
||
|
t.equal(sanitize.length, 2, 'sanitize interface');
|
||
|
t.end();
|
||
|
});
|
||
|
test('middleware interface', function(t) {
|
||
|
t.equal(typeof middleware, 'function', 'middleware is a function');
|
||
9 years ago
|
t.equal(middleware.length, 3, 'sanitize has a valid middleware');
|
||
10 years ago
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
8 years ago
|
module.exports.tests.sanitizers = function(test, common) {
|
||
|
test('check sanitizer list', function (t) {
|
||
9 years ago
|
var expected = ['singleScalarParameters', 'ids', 'private' ];
|
||
8 years ago
|
t.deepEqual(Object.keys(place.sanitizer_list), expected);
|
||
9 years ago
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
9 years ago
|
module.exports.tests.sanitize_private = function(test, common) {
|
||
10 years ago
|
var invalid_values = [null, -1, 123, NaN, 'abc'];
|
||
9 years ago
|
invalid_values.forEach(function(value) {
|
||
|
test('invalid private param ' + value, function(t) {
|
||
9 years ago
|
var req = { query: { ids:'geonames:venue:123', 'private': value } };
|
||
9 years ago
|
sanitize(req, function(){
|
||
|
t.deepEqual( req.errors, [], 'no errors' );
|
||
|
t.deepEqual( req.warnings, [], 'no warnings' );
|
||
|
t.equal(req.clean.private, false, 'default private set (to false)');
|
||
10 years ago
|
t.end();
|
||
|
});
|
||
9 years ago
|
});
|
||
10 years ago
|
});
|
||
|
|
||
|
var valid_values = ['true', true, 1];
|
||
9 years ago
|
valid_values.forEach(function(value) {
|
||
|
test('valid private param ' + value, function(t) {
|
||
9 years ago
|
var req = { query: { ids:'geonames:venue:123', 'private': value } };
|
||
9 years ago
|
sanitize(req, function(){
|
||
|
t.deepEqual( req.errors, [], 'no errors' );
|
||
|
t.deepEqual( req.warnings, [], 'no warnings' );
|
||
|
t.equal(req.clean.private, true, 'private set to true');
|
||
10 years ago
|
t.end();
|
||
|
});
|
||
9 years ago
|
});
|
||
10 years ago
|
});
|
||
|
|
||
|
var valid_false_values = ['false', false, 0];
|
||
9 years ago
|
valid_false_values.forEach(function(value) {
|
||
|
test('test setting false explicitly ' + value, function(t) {
|
||
9 years ago
|
var req = { query: { ids:'geonames:venue:123', 'private': value } };
|
||
9 years ago
|
sanitize(req, function(){
|
||
|
t.deepEqual( req.errors, [], 'no errors' );
|
||
|
t.deepEqual( req.warnings, [], 'no warnings' );
|
||
|
t.equal(req.clean.private, false, 'private set to false');
|
||
10 years ago
|
t.end();
|
||
|
});
|
||
9 years ago
|
});
|
||
10 years ago
|
});
|
||
|
|
||
|
test('test default behavior', function(t) {
|
||
9 years ago
|
var req = { query: { ids:'geonames:venue:123' } };
|
||
9 years ago
|
sanitize(req, function(){
|
||
|
t.deepEqual( req.errors, [], 'no errors' );
|
||
|
t.deepEqual( req.warnings, [], 'no warnings' );
|
||
|
t.equal(req.clean.private, false, 'private set to false');
|
||
10 years ago
|
t.end();
|
||
|
});
|
||
|
});
|
||
10 years ago
|
};
|
||
|
|
||
10 years ago
|
module.exports.tests.invalid_params = function(test, common) {
|
||
9 years ago
|
test('no params', function(t) {
|
||
9 years ago
|
var req = { query: {} };
|
||
|
sanitize( req, function(){
|
||
9 years ago
|
t.equal( req.errors[0], 'invalid param \'ids\': length must be >0', 'error for missing `ids` param');
|
||
9 years ago
|
t.deepEqual( req.warnings, [], 'no warnings' );
|
||
10 years ago
|
t.end();
|
||
|
});
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports.tests.middleware_success = function(test, common) {
|
||
|
test('middleware success', function(t) {
|
||
9 years ago
|
var req = { query: { ids: 'geonames:venue:123' }};
|
||
9 years ago
|
var next = function(){
|
||
|
t.deepEqual( req.errors, [], 'no errors' );
|
||
|
t.deepEqual( req.warnings, [], 'no warnings' );
|
||
10 years ago
|
t.deepEqual(req.clean, defaultClean);
|
||
|
t.end();
|
||
|
};
|
||
|
middleware( req, undefined, next );
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports.all = function (tape, common) {
|
||
|
|
||
|
function test(name, testFunction) {
|
||
9 years ago
|
return tape('SANTIZE /place ' + name, testFunction);
|
||
10 years ago
|
}
|
||
|
|
||
|
for( var testCase in module.exports.tests ){
|
||
|
module.exports.tests[testCase](test, common);
|
||
|
}
|
||
9 years ago
|
};
|