|
|
|
@ -8,12 +8,11 @@ var search = require('../../../sanitiser/search'),
|
|
|
|
|
delim = ',', |
|
|
|
|
defaultError = 'invalid param \'input\': text length, must be >0', |
|
|
|
|
defaultClean = { input: 'test',
|
|
|
|
|
layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
|
|
|
|
|
'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
|
|
|
|
|
types: { |
|
|
|
|
}, |
|
|
|
|
size: 10, |
|
|
|
|
details: true, |
|
|
|
|
parsed_input: defaultParsed, |
|
|
|
|
default_layers_set: true |
|
|
|
|
}, |
|
|
|
|
sanitize = function(query, cb) { _sanitize({'query':query}, cb); }; |
|
|
|
|
|
|
|
|
@ -32,13 +31,10 @@ module.exports.tests.interface = function(test, common) {
|
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
module.exports.tests.sanitize_input = function(test, common) { |
|
|
|
|
var inputs = { |
|
|
|
|
invalid: [ '', 100, null, undefined, new Date() ], |
|
|
|
|
valid: [ 'a', 'aa', 'aaaaaaaa' ] |
|
|
|
|
}; |
|
|
|
|
module.exports.tests.sanitize_invalid_input = function(test, common) { |
|
|
|
|
test('invalid input', function(t) {
|
|
|
|
|
inputs.invalid.forEach( function( input ){ |
|
|
|
|
var invalid = [ '', 100, null, undefined, new Date() ]; |
|
|
|
|
invalid.forEach( function( input ){ |
|
|
|
|
sanitize({ input: input }, function( err, clean ){ |
|
|
|
|
t.equal(err, 'invalid param \'input\': text length, must be >0', input + ' is an invalid input'); |
|
|
|
|
t.equal(clean, undefined, 'clean not set'); |
|
|
|
@ -46,16 +42,26 @@ module.exports.tests.sanitize_input = function(test, common) {
|
|
|
|
|
}); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
|
test('valid input', function(t) {
|
|
|
|
|
inputs.valid.forEach( function( input ){ |
|
|
|
|
sanitize({ input: input }, function( err, clean ){ |
|
|
|
|
var expected = JSON.parse(JSON.stringify( defaultClean )); |
|
|
|
|
expected.input = input; |
|
|
|
|
expected.parsed_input.target_layer = _input.getTargetLayers(input); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
t.equal(err, undefined, 'no error'); |
|
|
|
|
t.deepEqual(clean, expected, 'clean set correctly (' + input + ')'); |
|
|
|
|
}); |
|
|
|
|
module.exports.tests.sanitise_valid_input = function(test, common) { |
|
|
|
|
test('valid short input', function(t) { |
|
|
|
|
sanitize({ input: 'a' }, function( err, clean ){ |
|
|
|
|
t.equal(err, undefined, 'no error'); |
|
|
|
|
}); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
test('valid not-quite-as-short input', function(t) { |
|
|
|
|
sanitize({ input: 'aa' }, function( err, clean ){ |
|
|
|
|
t.equal(err, undefined, 'no error'); |
|
|
|
|
}); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
test('valid longer input', function(t) { |
|
|
|
|
sanitize({ input: 'aaaaaaaa' }, function( err, clean ){ |
|
|
|
|
t.equal(err, undefined, 'no error'); |
|
|
|
|
}); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
@ -70,11 +76,9 @@ module.exports.tests.sanitize_input_with_delim = function(test, common) {
|
|
|
|
|
var expected = JSON.parse(JSON.stringify( defaultClean )); |
|
|
|
|
expected.input = input; |
|
|
|
|
|
|
|
|
|
expected.parsed_input = parser(input); |
|
|
|
|
expected.parsed_input = parser.get_parsed_address(input); |
|
|
|
|
t.equal(err, undefined, 'no error'); |
|
|
|
|
t.equal(clean.parsed_input.name, expected.parsed_input.name, 'clean name set correctly'); |
|
|
|
|
t.equal(clean.parsed_input.admin_parts, expected.parsed_input.admin_parts, 'clean admin_parts set correctly'); |
|
|
|
|
t.deepEqual(clean, expected, 'clean set correctly (' + input + ')'); |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
@ -101,10 +105,8 @@ module.exports.tests.sanitize_lat = function(test, common) {
|
|
|
|
|
sanitize({ input: 'test', lat: lat, lon: 0 }, function( err, clean ){ |
|
|
|
|
var expected = JSON.parse(JSON.stringify( defaultClean )); |
|
|
|
|
expected.lat = parseFloat( lat ); |
|
|
|
|
expected.lon = 0; |
|
|
|
|
t.equal(err, undefined, 'no error'); |
|
|
|
|
expected.parsed_input = parser('test'); |
|
|
|
|
t.deepEqual(clean, expected, 'clean set correctly (' + lat + ')'); |
|
|
|
|
t.deepEqual(clean.lat, expected.lat, 'clean lat set correctly (' + lat + ')'); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
t.end(); |
|
|
|
@ -120,10 +122,8 @@ module.exports.tests.sanitize_lon = function(test, common) {
|
|
|
|
|
sanitize({ input: 'test', lat: 0, lon: lon }, function( err, clean ){ |
|
|
|
|
var expected = JSON.parse(JSON.stringify( defaultClean )); |
|
|
|
|
expected.lon = parseFloat( lon ); |
|
|
|
|
expected.lat = 0; |
|
|
|
|
t.equal(err, undefined, 'no error'); |
|
|
|
|
expected.parsed_input = parser('test'); |
|
|
|
|
t.deepEqual(clean, expected, 'clean set correctly (' + lon + ')'); |
|
|
|
|
t.deepEqual(clean.lon, expected.lon, 'clean set correctly (' + lon + ')'); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
t.end(); |
|
|
|
@ -131,34 +131,27 @@ module.exports.tests.sanitize_lon = function(test, common) {
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
module.exports.tests.sanitize_optional_geo = function(test, common) { |
|
|
|
|
test('no lat/lon', function(t) {
|
|
|
|
|
test('no lat/lon', function(t) { |
|
|
|
|
sanitize({ input: 'test' }, function( err, clean ){ |
|
|
|
|
var expected = defaultClean; |
|
|
|
|
t.equal(err, undefined, 'no error'); |
|
|
|
|
t.equal(clean.lat, undefined, 'clean set without lat'); |
|
|
|
|
t.equal(clean.lon, undefined, 'clean set without lon'); |
|
|
|
|
expected.parsed_input = parser('test'); |
|
|
|
|
t.deepEqual(clean, expected, 'clean set without lat/lon'); |
|
|
|
|
}); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
|
test('no lat', function(t) {
|
|
|
|
|
test('no lat', function(t) { |
|
|
|
|
sanitize({ input: 'test', lon: 0 }, function( err, clean ){ |
|
|
|
|
var expected = JSON.parse(JSON.stringify( defaultClean )); |
|
|
|
|
expected.lon = 0; |
|
|
|
|
var expected_lon = 0; |
|
|
|
|
t.equal(err, undefined, 'no error'); |
|
|
|
|
expected.parsed_input = parser('test'); |
|
|
|
|
t.deepEqual(clean, expected, 'clean set correctly (without any lat)'); |
|
|
|
|
t.deepEqual(clean.lon, expected_lon, 'clean set correctly (without any lat)'); |
|
|
|
|
}); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
|
test('no lon', function(t) {
|
|
|
|
|
test('no lon', function(t) { |
|
|
|
|
sanitize({ input: 'test', lat: 0 }, function( err, clean ){ |
|
|
|
|
var expected = JSON.parse(JSON.stringify( defaultClean )); |
|
|
|
|
expected.lat = 0; |
|
|
|
|
var expected_lat = 0; |
|
|
|
|
t.equal(err, undefined, 'no error'); |
|
|
|
|
expected.parsed_input = parser('test'); |
|
|
|
|
t.deepEqual(clean, expected, 'clean set correctly (without any lon)'); |
|
|
|
|
t.deepEqual(clean.lat, expected_lat, 'clean set correctly (without any lon)'); |
|
|
|
|
}); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
@ -166,8 +159,6 @@ module.exports.tests.sanitize_optional_geo = function(test, common) {
|
|
|
|
|
|
|
|
|
|
module.exports.tests.sanitize_bbox = function(test, common) { |
|
|
|
|
var bboxes = { |
|
|
|
|
invalid_coordinates: [ |
|
|
|
|
], |
|
|
|
|
invalid: [ |
|
|
|
|
'91;-181,-91,181', // invalid - semicolon between coordinates
|
|
|
|
|
'these,are,not,numbers', |
|
|
|
@ -194,22 +185,11 @@ module.exports.tests.sanitize_bbox = function(test, common) {
|
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
test('invalid bbox coordinates', function(t) {
|
|
|
|
|
bboxes.invalid_coordinates.forEach( function( bbox ){ |
|
|
|
|
sanitize({ input: 'test', bbox: bbox }, function( err, clean ){ |
|
|
|
|
t.ok(err.match(/Invalid (lat|lon)/), bbox + ' is invalid'); |
|
|
|
|
t.equal(clean, undefined, 'clean not set'); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
|
test('invalid bbox', function(t) {
|
|
|
|
|
bboxes.invalid.forEach( function( bbox ){ |
|
|
|
|
sanitize({ input: 'test', bbox: bbox }, function( err, clean ){ |
|
|
|
|
var expected = JSON.parse(JSON.stringify( defaultClean )); |
|
|
|
|
t.equal(err, undefined, 'no error'); |
|
|
|
|
expected.parsed_input = parser('test'); |
|
|
|
|
t.deepEqual(clean, expected, 'falling back on 50km distance from centroid'); |
|
|
|
|
t.equal(clean.bbox, undefined, 'falling back on 50km distance from centroid'); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
t.end(); |
|
|
|
@ -217,19 +197,17 @@ module.exports.tests.sanitize_bbox = function(test, common) {
|
|
|
|
|
test('valid bbox', function(t) {
|
|
|
|
|
bboxes.valid.forEach( function( bbox ){ |
|
|
|
|
sanitize({ input: 'test', bbox: bbox }, function( err, clean ){ |
|
|
|
|
var expected = JSON.parse(JSON.stringify( defaultClean )); |
|
|
|
|
var bboxArray = bbox.split(',').map(function(i) { |
|
|
|
|
return parseInt(i); |
|
|
|
|
}); |
|
|
|
|
expected.bbox = { |
|
|
|
|
var expected_bbox = { |
|
|
|
|
right: Math.max(bboxArray[0], bboxArray[2]), |
|
|
|
|
top: Math.max(bboxArray[1], bboxArray[3]), |
|
|
|
|
left: Math.min(bboxArray[0], bboxArray[2]), |
|
|
|
|
bottom: Math.min(bboxArray[1], bboxArray[3]) |
|
|
|
|
}; |
|
|
|
|
t.equal(err, undefined, 'no error'); |
|
|
|
|
expected.parsed_input = parser('test'); |
|
|
|
|
t.deepEqual(clean, expected, 'clean set correctly (' + bbox + ')'); |
|
|
|
|
t.deepEqual(clean.bbox, expected_bbox, 'clean set correctly (' + bbox + ')'); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
t.end(); |
|
|
|
@ -320,7 +298,7 @@ module.exports.tests.sanitize_details = function(test, common) {
|
|
|
|
|
module.exports.tests.sanitize_layers = function(test, common) { |
|
|
|
|
test('unspecified', function(t) { |
|
|
|
|
sanitize({ layers: undefined, input: 'test' }, function( err, clean ){ |
|
|
|
|
t.deepEqual(clean.layers, defaultClean.layers, 'default layers set'); |
|
|
|
|
t.deepEqual(clean.types.from_layers, defaultClean.types.from_layers, 'default layers set'); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
@ -335,21 +313,22 @@ module.exports.tests.sanitize_layers = function(test, common) {
|
|
|
|
|
test('poi (alias) layer', function(t) { |
|
|
|
|
var poi_layers = ['geoname','osmnode','osmway']; |
|
|
|
|
sanitize({ layers: 'poi', input: 'test' }, function( err, clean ){ |
|
|
|
|
t.deepEqual(clean.layers, poi_layers, 'poi layers set'); |
|
|
|
|
t.deepEqual(clean.types.from_layers, poi_layers, 'poi layers set'); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
test('admin (alias) layer', function(t) { |
|
|
|
|
var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin']; |
|
|
|
|
sanitize({ layers: 'admin', input: 'test' }, function( err, clean ){ |
|
|
|
|
t.deepEqual(clean.layers, admin_layers, 'admin layers set'); |
|
|
|
|
t.deepEqual(clean.types.from_layers, admin_layers, 'admin layers set'); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
test('address (alias) layer', function(t) { |
|
|
|
|
var address_layers = ['osmaddress','openaddresses']; |
|
|
|
|
sanitize({ layers: 'address', input: 'test' }, function( err, clean ){ |
|
|
|
|
t.deepEqual(clean.layers, address_layers, 'address layers set'); |
|
|
|
|
t.deepEqual(clean.types.from_layers, address_layers, 'types from layers set'); |
|
|
|
|
t.deepEqual(clean.types.from_address_parser, _input.allLayers, 'address parser uses default layers'); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
@ -357,7 +336,7 @@ module.exports.tests.sanitize_layers = function(test, common) {
|
|
|
|
|
var poi_layers = ['geoname','osmnode','osmway']; |
|
|
|
|
var reg_layers = ['admin0', 'admin1']; |
|
|
|
|
sanitize({ layers: 'poi,admin0,admin1', input: 'test' }, function( err, clean ){ |
|
|
|
|
t.deepEqual(clean.layers, reg_layers.concat(poi_layers), 'poi + regular layers'); |
|
|
|
|
t.deepEqual(clean.types.from_layers, reg_layers.concat(poi_layers), 'poi + regular layers'); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
@ -365,7 +344,7 @@ module.exports.tests.sanitize_layers = function(test, common) {
|
|
|
|
|
var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin']; |
|
|
|
|
var reg_layers = ['geoname', 'osmway']; |
|
|
|
|
sanitize({ layers: 'admin,geoname,osmway', input: 'test' }, function( err, clean ){ |
|
|
|
|
t.deepEqual(clean.layers, reg_layers.concat(admin_layers), 'admin + regular layers set'); |
|
|
|
|
t.deepEqual(clean.types.from_layers, reg_layers.concat(admin_layers), 'admin + regular layers set'); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
@ -373,21 +352,21 @@ module.exports.tests.sanitize_layers = function(test, common) {
|
|
|
|
|
var address_layers = ['osmaddress','openaddresses']; |
|
|
|
|
var reg_layers = ['geoname', 'osmway']; |
|
|
|
|
sanitize({ layers: 'address,geoname,osmway', input: 'test' }, function( err, clean ){ |
|
|
|
|
t.deepEqual(clean.layers, reg_layers.concat(address_layers), 'address + regular layers set'); |
|
|
|
|
t.deepEqual(clean.types.from_layers, reg_layers.concat(address_layers), 'address + regular layers set'); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
test('alias layer plus regular layers (no duplicates)', function(t) { |
|
|
|
|
var poi_layers = ['geoname','osmnode','osmway']; |
|
|
|
|
sanitize({ layers: 'poi,geoname,osmnode', input: 'test' }, function( err, clean ){ |
|
|
|
|
t.deepEqual(clean.layers, poi_layers, 'poi layers found (no duplicates)'); |
|
|
|
|
t.deepEqual(clean.types.from_layers, poi_layers, 'poi layers found (no duplicates)'); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
test('multiple alias layers (no duplicates)', function(t) { |
|
|
|
|
var alias_layers = ['geoname','osmnode','osmway','admin0','admin1','admin2','neighborhood','locality','local_admin']; |
|
|
|
|
sanitize({ layers: 'poi,admin', input: 'test' }, function( err, clean ){ |
|
|
|
|
t.deepEqual(clean.layers, alias_layers, 'all layers found (no duplicates)'); |
|
|
|
|
t.deepEqual(clean.types.from_layers, alias_layers, 'all layers found (no duplicates)'); |
|
|
|
|
t.end(); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
@ -420,8 +399,6 @@ module.exports.tests.middleware_success = function(test, common) {
|
|
|
|
|
var req = { query: { input: 'test' }}; |
|
|
|
|
var next = function( message ){ |
|
|
|
|
t.equal(message, undefined, 'no error message set'); |
|
|
|
|
req.clean.parsed_input = parser('test'); |
|
|
|
|
t.deepEqual(req.clean, defaultClean); |
|
|
|
|
t.end(); |
|
|
|
|
}; |
|
|
|
|
middleware( req, undefined, next ); |
|
|
|
|