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.
203 lines
6.7 KiB
203 lines
6.7 KiB
10 years ago
|
|
||
9 years ago
|
// @todo: refactor this test, it's pretty messy, brittle and hard to follow
|
||
|
|
||
8 years ago
|
var reverse = require('../../../sanitizer/reverse'),
|
||
9 years ago
|
sanitize = reverse.sanitize,
|
||
9 years ago
|
middleware = reverse.middleware,
|
||
9 years ago
|
defaults = require('../../../query/reverse_defaults'),
|
||
10 years ago
|
defaultError = 'missing param \'lat\'',
|
||
9 years ago
|
defaultClean = { 'point.lat': 0,
|
||
9 years ago
|
'point.lon': 0,
|
||
|
'boundary.circle.lat': 0,
|
||
|
'boundary.circle.lon': 0,
|
||
9 years ago
|
'boundary.circle.radius': parseFloat(defaults['boundary:circle:radius']),
|
||
10 years ago
|
size: 10,
|
||
9 years ago
|
private: false
|
||
9 years ago
|
};
|
||
|
|
||
|
// these are the default values you would expect when no input params are specified.
|
||
|
// @todo: why is this different from $defaultClean?
|
||
9 years ago
|
var emptyClean = { private: false, size: 10 };
|
||
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');
|
||
|
t.equal(middleware.length, 3, 'sanitizee has a valid middleware');
|
||
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
8 years ago
|
module.exports.tests.sanitizers = function(test, common) {
|
||
|
test('check sanitizer list', function (t) {
|
||
9 years ago
|
var expected = ['quattroshapes_deprecation', 'singleScalarParameters', 'layers',
|
||
|
'sources', 'sources_and_layers', 'size', 'private', 'geo_reverse', 'boundary_country'];
|
||
8 years ago
|
t.deepEqual(Object.keys(reverse.sanitizer_list), expected);
|
||
9 years ago
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
10 years ago
|
module.exports.tests.sanitize_lat = function(test, common) {
|
||
|
var lats = {
|
||
10 years ago
|
invalid: [],
|
||
|
valid: [ 0, 45, 90, -0, '0', '45', '90', -181, -120, -91, 91, 120, 181 ],
|
||
10 years ago
|
missing: ['', undefined, null]
|
||
10 years ago
|
};
|
||
9 years ago
|
test('invalid lat', function(t) {
|
||
10 years ago
|
lats.invalid.forEach( function( lat ){
|
||
9 years ago
|
var req = { query: { 'point.lat': lat, 'point.lon': 0 } };
|
||
|
sanitize(req, function(){
|
||
9 years ago
|
t.equal(req.errors[0], 'invalid param \'point.lat\': must be >-90 and <90', lat + ' is an invalid latitude');
|
||
9 years ago
|
t.deepEqual(req.clean, emptyClean, 'clean only has default values set');
|
||
10 years ago
|
});
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
9 years ago
|
test('valid lat', function(t) {
|
||
10 years ago
|
lats.valid.forEach( function( lat ){
|
||
9 years ago
|
var req = { query: { 'point.lat': lat, 'point.lon': 0 } };
|
||
|
sanitize(req, function(){
|
||
9 years ago
|
var expected_lat = parseFloat( lat );
|
||
9 years ago
|
t.deepEqual(req.errors, [], 'no errors');
|
||
10 years ago
|
});
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
9 years ago
|
test('missing lat', function(t) {
|
||
10 years ago
|
lats.missing.forEach( function( lat ){
|
||
9 years ago
|
var req = { query: { 'point.lat': lat, 'point.lon': 0 } };
|
||
|
sanitize(req, function(){
|
||
9 years ago
|
t.equal(req.errors[0], 'missing param \'point.lat\'', 'latitude is a required field');
|
||
9 years ago
|
t.deepEqual(req.clean, emptyClean, 'clean only has default values set');
|
||
10 years ago
|
});
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports.tests.sanitize_lon = function(test, common) {
|
||
|
var lons = {
|
||
10 years ago
|
valid: [ -360, -181, 181, -180, -1, -0, 0, 45, 90, '-180', '0', '180' ],
|
||
10 years ago
|
missing: ['', undefined, null]
|
||
10 years ago
|
};
|
||
9 years ago
|
test('valid lon', function(t) {
|
||
10 years ago
|
lons.valid.forEach( function( lon ){
|
||
9 years ago
|
var req = { query: { 'point.lat': 0, 'point.lon': lon } };
|
||
|
sanitize(req, function(){
|
||
9 years ago
|
var expected_lon = parseFloat( lon );
|
||
9 years ago
|
t.deepEqual(req.errors, [], 'no errors');
|
||
10 years ago
|
});
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
9 years ago
|
test('missing lon', function(t) {
|
||
10 years ago
|
lons.missing.forEach( function( lon ){
|
||
9 years ago
|
var req = { query: { 'point.lat': 0, 'point.lon': lon } };
|
||
|
|
||
|
// @todo: why is lat set?
|
||
9 years ago
|
var expected = { 'point.lat': 0, private: false, size: 10 };
|
||
9 years ago
|
sanitize(req, function(){
|
||
9 years ago
|
t.equal(req.errors[0], 'missing param \'point.lon\'', 'longitude is a required field');
|
||
9 years ago
|
t.deepEqual(req.clean, expected, 'clean only has default values set');
|
||
10 years ago
|
});
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports.tests.sanitize_size = function(test, common) {
|
||
|
test('invalid size value', function(t) {
|
||
9 years ago
|
var req = { query: { size: 'a', 'point.lat': 0, 'point.lon': 0 } };
|
||
|
sanitize(req, function(){
|
||
|
t.equal(req.clean.size, 10, 'default size set');
|
||
10 years ago
|
t.end();
|
||
|
});
|
||
|
});
|
||
|
test('below min size value', function(t) {
|
||
9 years ago
|
var req = { query: { size: -100, 'point.lat': 0, 'point.lon': 0 } };
|
||
|
sanitize(req, function(){
|
||
|
t.equal(req.clean.size, 1, 'min size set');
|
||
10 years ago
|
t.end();
|
||
|
});
|
||
|
});
|
||
|
test('above max size value', function(t) {
|
||
9 years ago
|
var req = { query: { size: 9999, 'point.lat': 0, 'point.lon': 0 } };
|
||
|
sanitize(req, function(){
|
||
|
t.equal(req.clean.size, 40, 'max size set');
|
||
10 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: { 'point.lat': 0, 'point.lon': 0, 'private': value } };
|
||
|
sanitize(req, function(){
|
||
|
t.equal(req.clean.private, false, 'default private set (to false)');
|
||
10 years ago
|
t.end();
|
||
|
});
|
||
9 years ago
|
});
|
||
10 years ago
|
});
|
||
|
|
||
9 years ago
|
var valid_values = ['true', true, 1, '1'];
|
||
9 years ago
|
valid_values.forEach(function(value) {
|
||
|
test('valid private param ' + value, function(t) {
|
||
9 years ago
|
var req = { query: { 'point.lat': 0, 'point.lon': 0, 'private': value } };
|
||
|
sanitize(req, function(){
|
||
|
t.equal(req.clean.private, true, 'private set to true');
|
||
10 years ago
|
t.end();
|
||
|
});
|
||
9 years ago
|
});
|
||
10 years ago
|
});
|
||
|
|
||
9 years ago
|
var valid_false_values = ['false', false, 0];
|
||
|
valid_false_values.forEach(function(value) {
|
||
|
test('test setting false explicitly ' + value, function(t) {
|
||
9 years ago
|
var req = { query: { 'point.lat': 0, 'point.lon': 0, 'private': value } };
|
||
|
sanitize(req, function(){
|
||
|
t.equal(req.clean.private, false, 'private set to false');
|
||
10 years ago
|
t.end();
|
||
|
});
|
||
9 years ago
|
});
|
||
10 years ago
|
});
|
||
|
|
||
9 years ago
|
test('test default behavior', function(t) {
|
||
9 years ago
|
var req = { query: { 'point.lat': 0, 'point.lon': 0 } };
|
||
|
sanitize(req, function(){
|
||
|
t.equal(req.clean.private, false, 'private set to false');
|
||
10 years ago
|
t.end();
|
||
|
});
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports.tests.middleware_success = function(test, common) {
|
||
|
test('middleware success', function(t) {
|
||
9 years ago
|
var req = { query: { 'point.lat': 0, 'point.lon': 0 }};
|
||
9 years ago
|
var next = function(){
|
||
|
t.deepEqual(req.errors, [], 'no error message set');
|
||
10 years ago
|
t.deepEqual(req.clean, defaultClean);
|
||
|
t.end();
|
||
|
};
|
||
|
middleware( req, undefined, next );
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports.all = function (tape, common) {
|
||
|
|
||
|
function test(name, testFunction) {
|
||
|
return tape('SANTIZE /reverse ' + name, testFunction);
|
||
|
}
|
||
|
|
||
|
for( var testCase in module.exports.tests ){
|
||
|
module.exports.tests[testCase](test, common);
|
||
|
}
|
||
10 years ago
|
};
|