From 040871cbe41c3f0188a7e80662840e9fab9eed1a Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Fri, 4 Sep 2015 17:10:02 -0400 Subject: [PATCH 1/7] Whitespace --- sanitiser/reverse.js | 1 - test/unit/query/reverse.js | 6 ++---- test/unit/query/search.js | 31 ++++++++++++++----------------- test/unit/sanitiser/_text.js | 8 ++++---- test/unit/sanitiser/reverse.js | 16 ++++++++-------- test/unit/sanitiser/search.js | 22 +++++++++++----------- 6 files changed, 39 insertions(+), 45 deletions(-) diff --git a/sanitiser/reverse.js b/sanitiser/reverse.js index 780b1c01..74607f3a 100644 --- a/sanitiser/reverse.js +++ b/sanitiser/reverse.js @@ -1,4 +1,3 @@ - var _sanitize = require('../sanitiser/_sanitize'), sanitiser = { latlonzoom: function( req ) { diff --git a/test/unit/query/reverse.js b/test/unit/query/reverse.js index 409f05de..c088bdf9 100644 --- a/test/unit/query/reverse.js +++ b/test/unit/query/reverse.js @@ -1,4 +1,3 @@ - var generate = require('../../../query/reverse'); module.exports.tests = {}; @@ -15,7 +14,7 @@ module.exports.tests.query = function(test, common) { var query = generate({ lat: 29.49136, lon: -82.50622 }); - + var compiled = JSON.parse( JSON.stringify( query ) ); var expected = require('../fixture/reverse_standard'); @@ -38,7 +37,6 @@ module.exports.tests.query = function(test, common) { }; module.exports.all = function (tape, common) { - function test(name, testFunction) { return tape('reverse query ' + name, testFunction); } @@ -46,4 +44,4 @@ module.exports.all = function (tape, common) { for( var testCase in module.exports.tests ){ module.exports.tests[testCase](test, common); } -}; \ No newline at end of file +}; diff --git a/test/unit/query/search.js b/test/unit/query/search.js index e72f8f4d..399fd20a 100644 --- a/test/unit/query/search.js +++ b/test/unit/query/search.js @@ -1,4 +1,3 @@ - var generate = require('../../../query/search'); var parser = require('../../../helper/query_parser'); @@ -19,9 +18,9 @@ module.exports.tests.query = function(test, common) { text: 'test', size: 10, lat: 29.49136, lon: -82.50622, bbox: { - top: 47.47, - right: -61.84, - bottom: 11.51, + top: 47.47, + right: -61.84, + bottom: 11.51, left: -103.16 }, layers: ['test'] @@ -39,9 +38,9 @@ module.exports.tests.query = function(test, common) { var query = generate({ text: 'test', size: 10, bbox: { - top: 47.47, - right: -61.84, - bottom: 11.51, + top: 47.47, + right: -61.84, + bottom: 11.51, left: -103.16 }, layers: ['test'] @@ -50,7 +49,7 @@ module.exports.tests.query = function(test, common) { var compiled = JSON.parse( JSON.stringify( query ) ); var expected = require('../fixture/search_linguistic_bbox'); expected.sort = sort; - + t.deepEqual(compiled, expected, 'valid search query'); t.end(); }); @@ -86,9 +85,9 @@ module.exports.tests.query = function(test, common) { test('valid query with a full valid address', function(t) { var address = '123 main st new york ny 10010 US'; - var query = generate({ text: address, - layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', - 'locality', 'local_admin', 'osmaddress', 'openaddresses' ], + var query = generate({ text: address, + layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', + 'locality', 'local_admin', 'osmaddress', 'openaddresses' ], size: 10, details: true, parsed_text: parser.get_parsed_address(address), @@ -101,12 +100,12 @@ module.exports.tests.query = function(test, common) { t.deepEqual(compiled, expected, 'valid search query'); t.end(); }); - + test('valid query with partial address', function(t) { var partial_address = 'soho grand, new york'; - var query = generate({ text: partial_address, - layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', - 'locality', 'local_admin', 'osmaddress', 'openaddresses' ], + var query = generate({ text: partial_address, + layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', + 'locality', 'local_admin', 'osmaddress', 'openaddresses' ], size: 10, details: true, parsed_text: parser.get_parsed_address(partial_address), @@ -137,11 +136,9 @@ module.exports.tests.query = function(test, common) { t.deepEqual(compiled, expected, 'valid search query'); t.end(); }); - }; module.exports.all = function (tape, common) { - function test(name, testFunction) { return tape('search query ' + name, testFunction); } diff --git a/test/unit/sanitiser/_text.js b/test/unit/sanitiser/_text.js index 18d37189..dfb11389 100644 --- a/test/unit/sanitiser/_text.js +++ b/test/unit/sanitiser/_text.js @@ -3,13 +3,13 @@ var text = require('../../../sanitiser/_text'), parser = require('../../../helper/query_parser'), delim = ',', defaultError = 'invalid param \'text\': text length, must be >0', - allLayers = [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', + allLayers = [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', 'locality', 'local_admin', 'osmaddress', 'openaddresses' ], - nonAddressLayers = [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', + nonAddressLayers = [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', 'locality', 'local_admin' ], defaultParsed= { }, - defaultClean = { text: 'test', - layers: allLayers, + defaultClean = { text: 'test', + layers: allLayers, size: 10, details: true, parsed_text: defaultParsed, diff --git a/test/unit/sanitiser/reverse.js b/test/unit/sanitiser/reverse.js index 559f3cea..cea1f913 100644 --- a/test/unit/sanitiser/reverse.js +++ b/test/unit/sanitiser/reverse.js @@ -35,7 +35,7 @@ module.exports.tests.sanitize_lat = function(test, common) { valid: [ 0, 45, 90, -0, '0', '45', '90', -181, -120, -91, 91, 120, 181 ], missing: ['', undefined, null] }; - test('invalid lat', function(t) { + test('invalid lat', function(t) { lats.invalid.forEach( function( lat ){ sanitize({ lat: lat, lon: 0 }, function( err, clean ){ t.equal(err, 'invalid param \'lat\': must be >-90 and <90', lat + ' is an invalid latitude'); @@ -44,7 +44,7 @@ module.exports.tests.sanitize_lat = function(test, common) { }); t.end(); }); - test('valid lat', function(t) { + test('valid lat', function(t) { lats.valid.forEach( function( lat ){ sanitize({ lat: lat, lon: 0 }, function( err, clean ){ var expected = JSON.parse(JSON.stringify( defaultClean )); @@ -55,7 +55,7 @@ module.exports.tests.sanitize_lat = function(test, common) { }); t.end(); }); - test('missing lat', function(t) { + test('missing lat', function(t) { lats.missing.forEach( function( lat ){ sanitize({ lat: lat, lon: 0 }, function( err, clean ){ t.equal(err, 'missing param \'lat\'', 'latitude is a required field'); @@ -71,7 +71,7 @@ module.exports.tests.sanitize_lon = function(test, common) { valid: [ -360, -181, 181, -180, -1, -0, 0, 45, 90, '-180', '0', '180' ], missing: ['', undefined, null] }; - test('valid lon', function(t) { + test('valid lon', function(t) { lons.valid.forEach( function( lon ){ sanitize({ lat: 0, lon: lon }, function( err, clean ){ var expected = JSON.parse(JSON.stringify( defaultClean )); @@ -82,7 +82,7 @@ module.exports.tests.sanitize_lon = function(test, common) { }); t.end(); }); - test('missing lon', function(t) { + test('missing lon', function(t) { lons.missing.forEach( function( lon ){ sanitize({ lat: 0, lon: lon }, function( err, clean ){ t.equal(err, 'missing param \'lon\'', 'longitude is a required field'); @@ -123,7 +123,7 @@ module.exports.tests.sanitize_details = function(test, common) { t.equal(clean.details, false, 'details set to false'); t.end(); }); - }); + }); }); var valid_values = [true, 'true', 1, '1', 'yes', 'y']; @@ -133,7 +133,7 @@ module.exports.tests.sanitize_details = function(test, common) { t.equal(clean.details, true, 'details set to true'); t.end(); }); - }); + }); }); test('test default behavior', function(t) { @@ -150,7 +150,7 @@ module.exports.tests.sanitize_details = function(test, common) { t.equal(clean.details, false, 'details set to false'); t.end(); }); - }); + }); }); }; diff --git a/test/unit/sanitiser/search.js b/test/unit/sanitiser/search.js index d0c20f18..868200e5 100644 --- a/test/unit/sanitiser/search.js +++ b/test/unit/sanitiser/search.js @@ -7,7 +7,7 @@ var search = require('../../../sanitiser/search'), middleware = search.middleware, delim = ',', defaultError = 'invalid param \'text\': text length, must be >0', - defaultClean = { text: 'test', + defaultClean = { text: 'test', types: { }, size: 10, @@ -32,7 +32,7 @@ module.exports.tests.interface = function(test, common) { }; module.exports.tests.sanitize_invalid_text = function(test, common) { - test('invalid text', function(t) { + test('invalid text', function(t) { var invalid = [ '', 100, null, undefined, new Date() ]; invalid.forEach( function( text ){ sanitize({ text: text }, function( err, clean ){ @@ -91,7 +91,7 @@ module.exports.tests.sanitize_lat = function(test, common) { invalid: [], valid: [ 0, 45, 90, -0, '0', '45', '90', -181, -120, -91, 91, 120, 181 ] }; - test('invalid lat', function(t) { + test('invalid lat', function(t) { lats.invalid.forEach( function( lat ){ sanitize({ text: 'test', lat: lat, lon: 0 }, function( err, clean ){ t.equal(err, 'invalid param \'lat\': must be >-90 and <90', lat + ' is an invalid latitude'); @@ -100,7 +100,7 @@ module.exports.tests.sanitize_lat = function(test, common) { }); t.end(); }); - test('valid lat', function(t) { + test('valid lat', function(t) { lats.valid.forEach( function( lat ){ sanitize({ text: 'test', lat: lat, lon: 0 }, function( err, clean ){ var expected = JSON.parse(JSON.stringify( defaultClean )); @@ -117,7 +117,7 @@ module.exports.tests.sanitize_lon = function(test, common) { var lons = { valid: [ -381, -181, -180, -1, -0, 0, 45, 90, '-180', '0', '180', 181 ] }; - test('valid lon', function(t) { + test('valid lon', function(t) { lons.valid.forEach( function( lon ){ sanitize({ text: 'test', lat: 0, lon: lon }, function( err, clean ){ var expected = JSON.parse(JSON.stringify( defaultClean )); @@ -183,9 +183,9 @@ module.exports.tests.sanitize_bbox = function(test, common) { '91, -181,-91,11', '91, -11,-91,181' ] - + }; - test('invalid bbox', function(t) { + test('invalid bbox', function(t) { bboxes.invalid.forEach( function( bbox ){ sanitize({ text: 'test', bbox: bbox }, function( err, clean ){ t.equal(err, undefined, 'no error'); @@ -194,7 +194,7 @@ module.exports.tests.sanitize_bbox = function(test, common) { }); t.end(); }); - test('valid bbox', function(t) { + test('valid bbox', function(t) { bboxes.valid.forEach( function( bbox ){ sanitize({ text: 'test', bbox: bbox }, function( err, clean ){ var bboxArray = bbox.split(',').map(function(i) { @@ -264,7 +264,7 @@ module.exports.tests.sanitize_details = function(test, common) { t.equal(clean.details, false, 'default details set (to false)'); t.end(); }); - }); + }); }); var valid_values = ['true', true, 1, '1', 'yes', 'y']; @@ -274,7 +274,7 @@ module.exports.tests.sanitize_details = function(test, common) { t.equal(clean.details, true, 'details set to true'); t.end(); }); - }); + }); }); var valid_false_values = ['false', false, 0, '0', 'no', 'n']; @@ -284,7 +284,7 @@ module.exports.tests.sanitize_details = function(test, common) { t.equal(clean.details, false, 'details set to false'); t.end(); }); - }); + }); }); test('test default behavior', function(t) { From fd3b12cfa923b816ad2e7e866bcb41db69378d39 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Fri, 4 Sep 2015 18:38:38 -0400 Subject: [PATCH 2/7] Remove all references to non-existent zoom parameter --- public/apiDoc.md | 3 --- sanitiser/_geo.js | 8 -------- test/unit/sanitiser/search.js | 21 --------------------- 3 files changed, 32 deletions(-) diff --git a/public/apiDoc.md b/public/apiDoc.md index edbc5f8d..76e7335f 100644 --- a/public/apiDoc.md +++ b/public/apiDoc.md @@ -7,7 +7,6 @@ The full text search endpoint that matches the name of a place to points on the #### Optional Parameters * `lat`, `lon`: the latitude/longitude coordinates to bias search results towards (may increase relevancy) -* `zoom`: zoom level from which you wish to view the world * `size` (default: `10`): the number of results to return * `layers` (default: `poi,admin,address`): the comma-separated names of datasets you wish to query. Valid values are: * aliases for multiple datasets like `poi`, `admin` or `address` @@ -48,7 +47,6 @@ results from around the provided lat/lon coordinates and also from precision lev * `lat`/`lon` are currently **required** because of this [open issue](https://github.com/elasticsearch/elasticsearch/issues/6444) #### Optional Parameters -* `zoom`: zoom level from which you wish to view the world * `size` (default: `10`): number of results requested * `layers` (default: `poi,admin,address`): datasets you wish to query * `details` (default: `true`) @@ -84,7 +82,6 @@ The reverse geocoding endpoint; matches a point on the planet to the name of tha * `lat`, `lon`: The coordinates of the point. #### Optional Parameters -* `zoom`: zoom level from which you wish to view the world * `layers` (default: `poi,admin,address`) * `details` (default: `true`) diff --git a/sanitiser/_geo.js b/sanitiser/_geo.js index b6b68f31..d7a90e59 100644 --- a/sanitiser/_geo.js +++ b/sanitiser/_geo.js @@ -17,7 +17,6 @@ module.exports = function sanitize( req, latlon_is_required ){ try { sanitize_coord( 'lat', clean, params.lat, latlon_is_required ); sanitize_coord( 'lon', clean, params.lon, latlon_is_required ); - sanitize_zoom_level(clean, params.zoom); sanitize_bbox(clean, params.bbox); } catch (err) { @@ -82,10 +81,3 @@ function sanitize_coord( coord, clean, param, latlon_is_required ) { throw new Error( util.format( 'missing param \'%s\'', coord ) ); } } - -function sanitize_zoom_level( clean, param ) { - var zoom = parseInt( param, 10 ); - if( !isNaN( zoom ) ){ - clean.zoom = Math.min( Math.max( zoom, 1 ), 18 ); // max - } -} diff --git a/test/unit/sanitiser/search.js b/test/unit/sanitiser/search.js index 868200e5..270fa93c 100644 --- a/test/unit/sanitiser/search.js +++ b/test/unit/sanitiser/search.js @@ -214,27 +214,6 @@ module.exports.tests.sanitize_bbox = function(test, common) { }); }; -module.exports.tests.sanitize_zoom = function(test, common) { - test('invalid zoom value', function(t) { - sanitize({ zoom: 'a', text: 'test', lat: 0, lon: 0 }, function( err, clean ){ - t.equal(clean.zoom, undefined, 'zoom not set'); - t.end(); - }); - }); - test('below min zoom value', function(t) { - sanitize({ zoom: -100, text: 'test', lat: 0, lon: 0 }, function( err, clean ){ - t.equal(clean.zoom, 1, 'min zoom set'); - t.end(); - }); - }); - test('above max zoom value', function(t) { - sanitize({ zoom: 9999, text: 'test', lat: 0, lon: 0 }, function( err, clean ){ - t.equal(clean.zoom, 18, 'max zoom set'); - t.end(); - }); - }); -}; - module.exports.tests.sanitize_size = function(test, common) { test('invalid size value', function(t) { sanitize({ size: 'a', text: 'test', lat: 0, lon: 0 }, function( err, clean ){ From 5d69c8016b1b728e44ecd29b62a7d95e711b3070 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Fri, 4 Sep 2015 18:44:45 -0400 Subject: [PATCH 3/7] Remove lat/lon params from tests that don't use it This just removes clutter and possible confusion from our test cases. --- test/unit/sanitiser/search.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit/sanitiser/search.js b/test/unit/sanitiser/search.js index 270fa93c..6715258e 100644 --- a/test/unit/sanitiser/search.js +++ b/test/unit/sanitiser/search.js @@ -249,7 +249,7 @@ module.exports.tests.sanitize_details = function(test, common) { var valid_values = ['true', true, 1, '1', 'yes', 'y']; valid_values.forEach(function(details) { test('valid details param ' + details, function(t) { - sanitize({ text: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){ + sanitize({ text: 'test', details: details }, function( err, clean ){ t.equal(clean.details, true, 'details set to true'); t.end(); }); @@ -259,7 +259,7 @@ module.exports.tests.sanitize_details = function(test, common) { var valid_false_values = ['false', false, 0, '0', 'no', 'n']; valid_false_values.forEach(function(details) { test('test setting false explicitly ' + details, function(t) { - sanitize({ text: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){ + sanitize({ text: 'test', details: details }, function( err, clean ){ t.equal(clean.details, false, 'details set to false'); t.end(); }); @@ -267,7 +267,7 @@ module.exports.tests.sanitize_details = function(test, common) { }); test('test default behavior', function(t) { - sanitize({ text: 'test', lat: 0, lon: 0 }, function( err, clean ){ + sanitize({ text: 'test' }, function( err, clean ){ t.equal(clean.details, true, 'details set to true'); t.end(); }); From 0d23792591de2ffd62c2446e4c077ee1825956e1 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Fri, 4 Sep 2015 17:49:55 -0400 Subject: [PATCH 4/7] Split geo sanitiser into geo_reverse and geo_search Common functions are moved to geo_common. This is required to allow separate behavior for search and reverse. --- sanitiser/{_geo.js => _geo_common.js} | 40 ++++++--------------------- sanitiser/_geo_reverse.js | 30 ++++++++++++++++++++ sanitiser/_geo_search.js | 30 ++++++++++++++++++++ sanitiser/reverse.js | 5 +--- sanitiser/search.js | 2 +- 5 files changed, 70 insertions(+), 37 deletions(-) rename sanitiser/{_geo.js => _geo_common.js} (63%) create mode 100644 sanitiser/_geo_reverse.js create mode 100644 sanitiser/_geo_search.js diff --git a/sanitiser/_geo.js b/sanitiser/_geo_common.js similarity index 63% rename from sanitiser/_geo.js rename to sanitiser/_geo_common.js index d7a90e59..c83fe47e 100644 --- a/sanitiser/_geo.js +++ b/sanitiser/_geo_common.js @@ -1,36 +1,7 @@ +/** + * helper sanitiser methods for geo parameters + */ var util = require( 'util' ); -var isObject = require('is-object'); - - -// validate inputs, convert types and apply defaults -module.exports = function sanitize( req, latlon_is_required ){ - - var clean = req.clean || {}; - var params = req.query; - latlon_is_required = latlon_is_required || false; - - // ensure the input params are a valid object - if( !isObject( params ) ){ - params = {}; - } - - try { - sanitize_coord( 'lat', clean, params.lat, latlon_is_required ); - sanitize_coord( 'lon', clean, params.lon, latlon_is_required ); - sanitize_bbox(clean, params.bbox); - } - catch (err) { - return { - 'error': true, - 'message': err.message - }; - } - - req.clean = clean; - - return { 'error': false }; -}; - /** * Parse and validate bbox parameter @@ -81,3 +52,8 @@ function sanitize_coord( coord, clean, param, latlon_is_required ) { throw new Error( util.format( 'missing param \'%s\'', coord ) ); } } + +module.exports = { + sanitize_bbox: sanitize_bbox, + sanitize_coord: sanitize_coord +}; diff --git a/sanitiser/_geo_reverse.js b/sanitiser/_geo_reverse.js new file mode 100644 index 00000000..39e1862b --- /dev/null +++ b/sanitiser/_geo_reverse.js @@ -0,0 +1,30 @@ +var isObject = require('is-object'); +var geo_common = require ('./_geo_common'); + +// validate inputs, convert types and apply defaults +module.exports = function sanitize( req ){ + var clean = req.clean || {}; + var params = req.query; + var latlon_is_required = true; + + // ensure the input params are a valid object + if( !isObject( params ) ){ + params = {}; + } + + try { + geo_common.sanitize_coord( 'lat', clean, params.lat, latlon_is_required ); + geo_common.sanitize_coord( 'lon', clean, params.lon, latlon_is_required ); + geo_common.sanitize_bbox(clean, params.bbox); + } + catch (err) { + return { + 'error': true, + 'message': err.message + }; + } + + req.clean = clean; + + return { 'error': false }; +}; diff --git a/sanitiser/_geo_search.js b/sanitiser/_geo_search.js new file mode 100644 index 00000000..93e34242 --- /dev/null +++ b/sanitiser/_geo_search.js @@ -0,0 +1,30 @@ +var isObject = require('is-object'); +var geo_common = require ('./_geo_common'); + +// validate inputs, convert types and apply defaults +module.exports = function sanitize( req ){ + var clean = req.clean || {}; + var params = req.query; + var latlon_is_required = false; + + // ensure the input params are a valid object + if( !isObject( params ) ){ + params = {}; + } + + try { + geo_common.sanitize_coord( 'lat', clean, params.lat, latlon_is_required ); + geo_common.sanitize_coord( 'lon', clean, params.lon, latlon_is_required ); + geo_common.sanitize_bbox(clean, params.bbox); + } + catch (err) { + return { + 'error': true, + 'message': err.message + }; + } + + req.clean = clean; + + return { 'error': false }; +}; diff --git a/sanitiser/reverse.js b/sanitiser/reverse.js index 74607f3a..dc300479 100644 --- a/sanitiser/reverse.js +++ b/sanitiser/reverse.js @@ -1,9 +1,6 @@ var _sanitize = require('../sanitiser/_sanitize'), sanitiser = { - latlonzoom: function( req ) { - var geo = require('../sanitiser/_geo'); - return geo(req, true); - }, + latlonzoom: require('../sanitiser/_geo_reverse'), layers: require('../sanitiser/_layers'), details: require('../sanitiser/_details'), size: require('../sanitiser/_size'), diff --git a/sanitiser/search.js b/sanitiser/search.js index ff0441d9..19263732 100644 --- a/sanitiser/search.js +++ b/sanitiser/search.js @@ -6,7 +6,7 @@ var _sanitize = require('../sanitiser/_sanitize'), layers: require('../sanitiser/_layers'), source: require('../sanitiser/_source'), details: require('../sanitiser/_details'), - latlonzoom: require('../sanitiser/_geo') + latlonzoom: require('../sanitiser/_geo_search') }; var sanitize = function(req, cb) { _sanitize(req, sanitizers, cb); }; From 6c1cfbb4ad5dabbae02a29095774715e488029d5 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Wed, 9 Sep 2015 10:59:57 -0400 Subject: [PATCH 5/7] Compare latitude values only, instead of full sanitiser output --- test/unit/sanitiser/search.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/unit/sanitiser/search.js b/test/unit/sanitiser/search.js index 6715258e..1ddd1a63 100644 --- a/test/unit/sanitiser/search.js +++ b/test/unit/sanitiser/search.js @@ -103,10 +103,9 @@ module.exports.tests.sanitize_lat = function(test, common) { test('valid lat', function(t) { lats.valid.forEach( function( lat ){ sanitize({ text: 'test', lat: lat, lon: 0 }, function( err, clean ){ - var expected = JSON.parse(JSON.stringify( defaultClean )); - expected.lat = parseFloat( lat ); + var expected_lat = parseFloat( lat ); t.equal(err, undefined, 'no error'); - t.deepEqual(clean.lat, expected.lat, 'clean lat set correctly (' + lat + ')'); + t.deepEqual(clean.lat, expected_lat, 'clean lat set correctly (' + lat + ')'); }); }); t.end(); From 3176a61596735ea4f665d86c70e5ff89a38a2bd4 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Fri, 4 Sep 2015 18:55:39 -0400 Subject: [PATCH 6/7] Rename /search lat/lon to focus.point.lat/lon --- sanitiser/_geo_search.js | 12 ++++++++++-- test/unit/sanitiser/search.js | 10 +++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/sanitiser/_geo_search.js b/sanitiser/_geo_search.js index 93e34242..a7ac66ba 100644 --- a/sanitiser/_geo_search.js +++ b/sanitiser/_geo_search.js @@ -12,9 +12,17 @@ module.exports = function sanitize( req ){ params = {}; } + if( !isObject( params.focus ) ){ + params.focus = {}; + } + + if( !isObject( params.focus.point ) ){ + params.focus.point = {}; + } + try { - geo_common.sanitize_coord( 'lat', clean, params.lat, latlon_is_required ); - geo_common.sanitize_coord( 'lon', clean, params.lon, latlon_is_required ); + geo_common.sanitize_coord( 'lat', clean, params.focus.point.lat, latlon_is_required ); + geo_common.sanitize_coord( 'lon', clean, params.focus.point.lon, latlon_is_required ); geo_common.sanitize_bbox(clean, params.bbox); } catch (err) { diff --git a/test/unit/sanitiser/search.js b/test/unit/sanitiser/search.js index 1ddd1a63..8969fab6 100644 --- a/test/unit/sanitiser/search.js +++ b/test/unit/sanitiser/search.js @@ -93,7 +93,7 @@ module.exports.tests.sanitize_lat = function(test, common) { }; test('invalid lat', function(t) { lats.invalid.forEach( function( lat ){ - sanitize({ text: 'test', lat: lat, lon: 0 }, function( err, clean ){ + sanitize({ text: 'test', focus: { point: { lat: lat, lon: 0 } } }, function( err, clean ){ t.equal(err, 'invalid param \'lat\': must be >-90 and <90', lat + ' is an invalid latitude'); t.equal(clean, undefined, 'clean not set'); }); @@ -102,7 +102,7 @@ module.exports.tests.sanitize_lat = function(test, common) { }); test('valid lat', function(t) { lats.valid.forEach( function( lat ){ - sanitize({ text: 'test', lat: lat, lon: 0 }, function( err, clean ){ + sanitize({ text: 'test', focus: { point: { lat: lat, lon: 0 } } }, function( err, clean ){ var expected_lat = parseFloat( lat ); t.equal(err, undefined, 'no error'); t.deepEqual(clean.lat, expected_lat, 'clean lat set correctly (' + lat + ')'); @@ -118,7 +118,7 @@ module.exports.tests.sanitize_lon = function(test, common) { }; test('valid lon', function(t) { lons.valid.forEach( function( lon ){ - sanitize({ text: 'test', lat: 0, lon: lon }, function( err, clean ){ + sanitize({ text: 'test', focus: { point: { lat: 0, lon: lon } } }, function( err, clean ){ var expected = JSON.parse(JSON.stringify( defaultClean )); expected.lon = parseFloat( lon ); t.equal(err, undefined, 'no error'); @@ -139,7 +139,7 @@ module.exports.tests.sanitize_optional_geo = function(test, common) { t.end(); }); test('no lat', function(t) { - sanitize({ text: 'test', lon: 0 }, function( err, clean ){ + sanitize({ text: 'test', focus: { point: { lon: 0 } } }, function( err, clean ){ var expected_lon = 0; t.equal(err, undefined, 'no error'); t.deepEqual(clean.lon, expected_lon, 'clean set correctly (without any lat)'); @@ -147,7 +147,7 @@ module.exports.tests.sanitize_optional_geo = function(test, common) { t.end(); }); test('no lon', function(t) { - sanitize({ text: 'test', lat: 0 }, function( err, clean ){ + sanitize({ text: 'test', focus: { point: { lat: 0 } } }, function( err, clean ){ var expected_lat = 0; t.equal(err, undefined, 'no error'); t.deepEqual(clean.lat, expected_lat, 'clean set correctly (without any lon)'); From 96c58e865868ff1c5f2512ef99e2c8ed9cbc872c Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Fri, 4 Sep 2015 18:55:25 -0400 Subject: [PATCH 7/7] Rename /reverse lat/lon to point.lat/lon --- sanitiser/_geo_reverse.js | 8 ++++-- test/unit/sanitiser/reverse.js | 48 +++++++++++++++++----------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/sanitiser/_geo_reverse.js b/sanitiser/_geo_reverse.js index 39e1862b..061b3244 100644 --- a/sanitiser/_geo_reverse.js +++ b/sanitiser/_geo_reverse.js @@ -12,9 +12,13 @@ module.exports = function sanitize( req ){ params = {}; } + if( !isObject( params.point ) ){ + params.point = {}; + } + try { - geo_common.sanitize_coord( 'lat', clean, params.lat, latlon_is_required ); - geo_common.sanitize_coord( 'lon', clean, params.lon, latlon_is_required ); + geo_common.sanitize_coord( 'lat', clean, params.point.lat, latlon_is_required ); + geo_common.sanitize_coord( 'lon', clean, params.point.lon, latlon_is_required ); geo_common.sanitize_bbox(clean, params.bbox); } catch (err) { diff --git a/test/unit/sanitiser/reverse.js b/test/unit/sanitiser/reverse.js index cea1f913..27f0ff3f 100644 --- a/test/unit/sanitiser/reverse.js +++ b/test/unit/sanitiser/reverse.js @@ -37,7 +37,7 @@ module.exports.tests.sanitize_lat = function(test, common) { }; test('invalid lat', function(t) { lats.invalid.forEach( function( lat ){ - sanitize({ lat: lat, lon: 0 }, function( err, clean ){ + sanitize({ point: { lat: lat, lon: 0 } }, function( err, clean ){ t.equal(err, 'invalid param \'lat\': must be >-90 and <90', lat + ' is an invalid latitude'); t.equal(clean, undefined, 'clean not set'); }); @@ -46,7 +46,7 @@ module.exports.tests.sanitize_lat = function(test, common) { }); test('valid lat', function(t) { lats.valid.forEach( function( lat ){ - sanitize({ lat: lat, lon: 0 }, function( err, clean ){ + sanitize({ point: { lat: lat, lon: 0 } }, function( err, clean ){ var expected = JSON.parse(JSON.stringify( defaultClean )); expected.lat = parseFloat( lat ); t.equal(err, undefined, 'no error'); @@ -57,7 +57,7 @@ module.exports.tests.sanitize_lat = function(test, common) { }); test('missing lat', function(t) { lats.missing.forEach( function( lat ){ - sanitize({ lat: lat, lon: 0 }, function( err, clean ){ + sanitize({ point: { lat: lat, lon: 0 } }, function( err, clean ){ t.equal(err, 'missing param \'lat\'', 'latitude is a required field'); t.equal(clean, undefined, 'clean not set'); }); @@ -73,7 +73,7 @@ module.exports.tests.sanitize_lon = function(test, common) { }; test('valid lon', function(t) { lons.valid.forEach( function( lon ){ - sanitize({ lat: 0, lon: lon }, function( err, clean ){ + sanitize({ point: { lat: 0, lon: lon } }, function( err, clean ){ var expected = JSON.parse(JSON.stringify( defaultClean )); expected.lon = parseFloat( lon ); t.equal(err, undefined, 'no error'); @@ -84,7 +84,7 @@ module.exports.tests.sanitize_lon = function(test, common) { }); test('missing lon', function(t) { lons.missing.forEach( function( lon ){ - sanitize({ lat: 0, lon: lon }, function( err, clean ){ + sanitize({ point: { lat: 0, lon: lon } }, function( err, clean ){ t.equal(err, 'missing param \'lon\'', 'longitude is a required field'); t.equal(clean, undefined, 'clean not set'); }); @@ -96,19 +96,19 @@ module.exports.tests.sanitize_lon = function(test, common) { module.exports.tests.sanitize_size = function(test, common) { test('invalid size value', function(t) { - sanitize({ size: 'a', lat: 0, lon: 0 }, function( err, clean ){ + sanitize({ size: 'a', point: { lat: 0, lon: 0 } }, function( err, clean ){ t.equal(clean.size, 10, 'default size set'); t.end(); }); }); test('below min size value', function(t) { - sanitize({ size: -100, lat: 0, lon: 0 }, function( err, clean ){ + sanitize({ size: -100, point: { lat: 0, lon: 0 } }, function( err, clean ){ t.equal(clean.size, 1, 'min size set'); t.end(); }); }); test('above max size value', function(t) { - sanitize({ size: 9999, lat: 0, lon: 0 }, function( err, clean ){ + sanitize({ size: 9999, point: { lat: 0, lon: 0 } }, function( err, clean ){ t.equal(clean.size, 40, 'max size set'); t.end(); }); @@ -119,7 +119,7 @@ module.exports.tests.sanitize_details = function(test, common) { var invalid_values = [null, -1, 123, NaN, 'abc']; invalid_values.forEach(function(details) { test('invalid details param ' + details, function(t) { - sanitize({ lat: 0, lon: 0, details: details }, function( err, clean ){ + sanitize({ point: { lat: 0, lon: 0 }, details: details }, function( err, clean ){ t.equal(clean.details, false, 'details set to false'); t.end(); }); @@ -129,7 +129,7 @@ module.exports.tests.sanitize_details = function(test, common) { var valid_values = [true, 'true', 1, '1', 'yes', 'y']; valid_values.forEach(function(details) { test('valid details param ' + details, function(t) { - sanitize({ lat: 0, lon: 0, details: details }, function( err, clean ){ + sanitize({ point: { lat: 0, lon: 0 }, details: details }, function( err, clean ){ t.equal(clean.details, true, 'details set to true'); t.end(); }); @@ -137,7 +137,7 @@ module.exports.tests.sanitize_details = function(test, common) { }); test('test default behavior', function(t) { - sanitize({ lat: 0, lon: 0 }, function( err, clean ){ + sanitize({ point: { lat: 0, lon: 0 } }, function( err, clean ){ t.equal(clean.details, true, 'details set to true'); t.end(); }); @@ -146,7 +146,7 @@ module.exports.tests.sanitize_details = function(test, common) { var valid_false_values = ['false', false, 0, '0', 'no', 'n']; valid_false_values.forEach(function(details) { test('test setting false explicitly ' + details, function(t) { - sanitize({ lat: 0, lon: 0, details: details }, function( err, clean ){ + sanitize({ point: { lat: 0, lon: 0 }, details: details }, function( err, clean ){ t.equal(clean.details, false, 'details set to false'); t.end(); }); @@ -156,13 +156,13 @@ module.exports.tests.sanitize_details = function(test, common) { module.exports.tests.sanitize_layers = function(test, common) { test('unspecified', function(t) { - sanitize({ layers: undefined, lat: 0, lon: 0 }, function( err, clean ){ + sanitize({ layers: undefined, point: { lat: 0, lon: 0 } }, function( err, clean ){ t.deepEqual(clean.types.from_layers, defaultClean.types.from_layers, 'default layers set'); t.end(); }); }); test('invalid layer', function(t) { - sanitize({ layers: 'test_layer', lat: 0, lon: 0 }, function( err, clean ){ + sanitize({ layers: 'test_layer', point: { lat: 0, lon: 0 } }, function( err, clean ){ var msg = 'invalid param \'layers\': must be one or more of '; t.true(err.match(msg), 'invalid layer requested'); t.true(err.length > msg.length, 'invalid error message'); @@ -171,21 +171,21 @@ module.exports.tests.sanitize_layers = function(test, common) { }); test('poi (alias) layer', function(t) { var poi_layers = ['geoname','osmnode','osmway']; - sanitize({ layers: 'poi', lat: 0, lon: 0 }, function( err, clean ){ + sanitize({ layers: 'poi', point: { lat: 0, lon: 0 } }, function( err, clean ){ 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', lat: 0, lon: 0 }, function( err, clean ){ + sanitize({ layers: 'admin', point: { lat: 0, lon: 0 } }, function( err, clean ){ 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', lat: 0, lon: 0 }, function( err, clean ){ + sanitize({ layers: 'address', point: { lat: 0, lon: 0 } }, function( err, clean ){ t.deepEqual(clean.types.from_layers, address_layers, 'address layers set'); t.end(); }); @@ -193,7 +193,7 @@ module.exports.tests.sanitize_layers = function(test, common) { test('poi alias layer plus regular layers', function(t) { var poi_layers = ['geoname','osmnode','osmway']; var reg_layers = ['admin0', 'admin1']; - sanitize({ layers: 'poi,admin0,admin1', lat: 0, lon: 0 }, function( err, clean ){ + sanitize({ layers: 'poi,admin0,admin1', point: { lat: 0, lon: 0 } }, function( err, clean ){ t.deepEqual(clean.types.from_layers, reg_layers.concat(poi_layers), 'poi + regular layers'); t.end(); }); @@ -201,7 +201,7 @@ module.exports.tests.sanitize_layers = function(test, common) { test('admin alias layer plus regular layers', function(t) { var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin']; var reg_layers = ['geoname', 'osmway']; - sanitize({ layers: 'admin,geoname,osmway', lat: 0, lon: 0 }, function( err, clean ){ + sanitize({ layers: 'admin,geoname,osmway', point: { lat: 0, lon: 0 } }, function( err, clean ){ t.deepEqual(clean.types.from_layers, reg_layers.concat(admin_layers), 'admin + regular layers set'); t.end(); }); @@ -209,21 +209,21 @@ module.exports.tests.sanitize_layers = function(test, common) { test('address alias layer plus regular layers', function(t) { var address_layers = ['osmaddress','openaddresses']; var reg_layers = ['geoname', 'osmway']; - sanitize({ layers: 'address,geoname,osmway', lat: 0, lon: 0 }, function( err, clean ){ + sanitize({ layers: 'address,geoname,osmway', point: { lat: 0, lon: 0 } }, function( err, clean ){ 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', lat: 0, lon: 0 }, function( err, clean ){ + sanitize({ layers: 'poi,geoname,osmnode', point: { lat: 0, lon: 0 } }, function( err, clean ){ 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', lat: 0, lon: 0 }, function( err, clean ){ + sanitize({ layers: 'poi,admin', point: { lat: 0, lon: 0 } }, function( err, clean ){ t.deepEqual(clean.types.from_layers, alias_layers, 'all layers found (no duplicates)'); t.end(); }); @@ -231,7 +231,7 @@ module.exports.tests.sanitize_layers = function(test, common) { }; module.exports.tests.sanitize_categories = function(test, common) { - var queryParams = { lat: 0, lon: 0 }; + var queryParams = { point: { lat: 0, lon: 0 } }; test('unspecified', function(t) { queryParams.categories = undefined; sanitize(queryParams, function( err, clean ){ @@ -284,7 +284,7 @@ module.exports.tests.middleware_failure = function(test, common) { module.exports.tests.middleware_success = function(test, common) { test('middleware success', function(t) { - var req = { query: { lat: 0, lon: 0 }}; + var req = { query: { point: { lat: 0, lon: 0 } }}; var next = function( message ){ t.equal(message, undefined, 'no error message set'); t.deepEqual(req.clean, defaultClean);