From f67664c3e04a1168a37376060494e45bce21502d Mon Sep 17 00:00:00 2001 From: Lily He Date: Wed, 5 Jul 2017 17:18:46 -0400 Subject: [PATCH] location bias feature - sets default focus.point --- sanitizer/_location_bias.js | 18 ++++++ schema.js | 6 +- test/unit/run.js | 1 + test/unit/sanitizer/_location_bias.js | 90 +++++++++++++++++++++++++++ test/unit/schema.js | 78 +++++++++++++++++++++++ 5 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 sanitizer/_location_bias.js create mode 100644 test/unit/sanitizer/_location_bias.js diff --git a/sanitizer/_location_bias.js b/sanitizer/_location_bias.js new file mode 100644 index 00000000..ee6646f6 --- /dev/null +++ b/sanitizer/_location_bias.js @@ -0,0 +1,18 @@ +/* +Set a focus.lat and focus.lon if specified in pelias config +*/ +var _ = require('lodash'); + +function setup(defaultParameters){ + return function setLocationBias(req, res, next){ + if (_.isUndefined(req.clean) || _.isUndefined(defaultParameters['focus.point.lat']) || _.isUndefined(defaultParameters['focus.point.lon'])) { + return next(); + } + req.clean['focus.point.lat'] = defaultParameters['focus.point.lat']; + req.clean['focus.point.lon'] = defaultParameters['focus.point.lon']; + next(); + }; +} + + +module.exports = setup; diff --git a/schema.js b/schema.js index df544677..e261cfe6 100644 --- a/schema.js +++ b/schema.js @@ -39,7 +39,11 @@ module.exports = Joi.object().keys({ timeout: Joi.number().integer().optional().default(250).min(0), retries: Joi.number().integer().optional().default(3).min(0), }).unknown(false).requiredKeys('url') - }).unknown(false).default({}) // default api.services to an empty object + }).unknown(false).default({}), // default api.services to an empty object + defaultParameters: Joi.object().keys({ + 'focus.point.lat': Joi.number().optional().min(-90).max(90), + 'focus.point.lon': Joi.number().optional().min(-180).max(180), + }).unknown(true).default({}) }).requiredKeys('version', 'indexName', 'host').unknown(true), esclient: Joi.object().keys({ diff --git a/test/unit/run.js b/test/unit/run.js index 92d258fa..b60f06c5 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -64,6 +64,7 @@ var tests = [ require('./sanitizer/_ids'), require('./sanitizer/_iso2_to_iso3'), require('./sanitizer/_layers'), + require('./sanitizer/_location_bias'), require('./sanitizer/_city_name_standardizer'), require('./sanitizer/_single_scalar_parameters'), require('./sanitizer/_size'), diff --git a/test/unit/sanitizer/_location_bias.js b/test/unit/sanitizer/_location_bias.js new file mode 100644 index 00000000..c0f48cf9 --- /dev/null +++ b/test/unit/sanitizer/_location_bias.js @@ -0,0 +1,90 @@ +var setup = require('../../../sanitizer/_location_bias'); + +module.exports.tests = {}; + +module.exports.tests.setLocationBias = function(test, common) { + test('set focus point', t => { + var defaultParameters = { // specify focus point latitude and longitude + 'focus.point.lat': 12.12121212, + 'focus.point.lon': 21.21212121 + }; + var locationBias = setup(defaultParameters); + var req = { + clean: {} + }; + var expected = { + clean: { + 'focus.point.lat': 12.12121212, + 'focus.point.lon': 21.21212121 + } + }; + + locationBias(req, undefined, () => { + t.deepEqual(req, expected, 'focus point should be set'); + t.end(); + }); + }); + + test('undefined req.clean', t => { + var defaultParameters = { + 'focus.point.lat': 12.12121212, + 'focus.point.lon': 21.21212121 + }; + var locationBias = setup(defaultParameters); + var req = {}; + var expected = {}; + + locationBias(req, undefined, () => { + t.deepEqual(req, expected, 'should be unmodified' ); + t.end(); + }); + }); + + test('focusPointLat is undefined', t => { + var defaultParameters = { + 'focus.point.lon': 12.2121212 + }; + var locationBias = setup(defaultParameters); + var req = { + clean: {} + }; + var expected = { + clean: {} + }; + + locationBias(req, undefined, () => { + t.deepEqual(req, expected, 'should be unmodified' ); + t.end(); + }); + }); + + test('focusPointLon is undefined', t => { + var defaultParameters = { + 'focus.point.lat': 12.2121212 + }; + var locationBias = setup(defaultParameters); + var req = { + clean: {} + }; + var expected = { + clean: {} + }; + + locationBias(req, undefined, () => { + t.deepEqual(req, expected, 'should be unmodified' ); + t.end(); + }); + }); + +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('[middleware] locationBias: ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/test/unit/schema.js b/test/unit/schema.js index d48c7392..332347dd 100644 --- a/test/unit/schema.js +++ b/test/unit/schema.js @@ -26,6 +26,10 @@ module.exports.tests.completely_valid = (test, common) => { placeholder: { url: 'http://locahost' } + }, + defaultParameters: { + 'focus.point.lat': 19, + 'focus.point.lon': 91 } }, esclient: { @@ -457,6 +461,80 @@ module.exports.tests.api_validation = (test, common) => { }); + test('non-number defaultParameters.focus.point.lat should throw error', (t) => { + [null, 'string', {}, [], false].forEach((value) => { + const config = { + api: { + version: 'version value', + indexName: 'index name value', + host: 'host value', + defaultParameters: { + 'focus.point.lat': value + } + }, + esclient: {} + }; + + const result = Joi.validate(config, schema); + + t.equals(result.error.details.length, 1); + t.equals(result.error.details[0].message, '"focus.point.lat" must be a number'); + + }); + + t.end(); + + }); + + test('non-number defaultParameters.focus.point.lon should throw error', (t) => { + [null, 'string', {}, [], false].forEach((value) => { + const config = { + api: { + version: 'version value', + indexName: 'index name value', + host: 'host value', + defaultParameters: { + 'focus.point.lon': value + } + }, + esclient: {} + }; + + const result = Joi.validate(config, schema); + + t.equals(result.error.details.length, 1); + t.equals(result.error.details[0].message, '"focus.point.lon" must be a number'); + + }); + + t.end(); + + }); + + test('non-object api.defaultParameters should throw error', (t) => { + [null, 17, false, [], 'string'].forEach((value) => { + var config = { + api: { + version: 'version value', + indexName: 'index name value', + host: 'host value', + defaultParameters: value + }, + esclient: {} + }; + + const result = Joi.validate(config, schema); + + t.equals(result.error.details.length, 1); + t.equals(result.error.details[0].message, '"defaultParameters" must be an object'); + + }); + + t.end(); + + }); + + }; module.exports.tests.api_services_validation = (test, common) => {