mirror of https://github.com/pelias/api.git
Lily He
8 years ago
committed by
GitHub
13 changed files with 454 additions and 80 deletions
@ -0,0 +1,31 @@ |
|||||||
|
const _ = require('lodash'); |
||||||
|
/** |
||||||
|
Set a focus.lat and focus.lon if specified in pelias config |
||||||
|
* @param {object} defaultParameters property of pelias config |
||||||
|
*/ |
||||||
|
|
||||||
|
function setup(defaultParameters){ |
||||||
|
|
||||||
|
return function setLocationBias(raw, clean){ |
||||||
|
/* |
||||||
|
check that: |
||||||
|
1. {object} raw exists |
||||||
|
2. pelias-config included the properties focus.point.lat and focus.point.lon |
||||||
|
3. raw.focus.point.lon and raw.focus.point.lat have not been set |
||||||
|
*/ |
||||||
|
if (!_.isUndefined(raw) && |
||||||
|
!_.isUndefined(defaultParameters['focus.point.lat']) && |
||||||
|
!_.isUndefined(defaultParameters['focus.point.lon']) && |
||||||
|
!_.has(raw, 'focus.point.lon') && |
||||||
|
!_.has(raw, 'focus.point.lat') ) { |
||||||
|
|
||||||
|
raw['focus.point.lat'] = defaultParameters['focus.point.lat']; |
||||||
|
raw['focus.point.lon'] = defaultParameters['focus.point.lon']; |
||||||
|
} |
||||||
|
|
||||||
|
return { errors: [], warnings: [] }; |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
// if focus.point.lat and focus.point.lon already exists, don't change
|
||||||
|
module.exports = setup; |
@ -1,30 +1,33 @@ |
|||||||
var type_mapping = require('../helper/type_mapping'); |
var type_mapping = require('../helper/type_mapping'); |
||||||
|
var sanitizeAll = require('../sanitizer/sanitizeAll'); |
||||||
|
// middleware
|
||||||
|
module.exports.middleware = (_api_pelias_config) => { |
||||||
|
var sanitizers = { |
||||||
|
singleScalarParameters: require('../sanitizer/_single_scalar_parameters'), |
||||||
|
quattroshapes_deprecation: require('../sanitizer/_deprecate_quattroshapes'), |
||||||
|
text: require('../sanitizer/_text'), |
||||||
|
iso2_to_iso3: require('../sanitizer/_iso2_to_iso3'), |
||||||
|
city_name_standardizer: require('../sanitizer/_city_name_standardizer'), |
||||||
|
size: require('../sanitizer/_size')(/* use defaults*/), |
||||||
|
layers: require('../sanitizer/_targets')('layers', type_mapping.layer_mapping), |
||||||
|
sources: require('../sanitizer/_targets')('sources', type_mapping.source_mapping), |
||||||
|
// depends on the layers and sources sanitizers, must be run after them
|
||||||
|
sources_and_layers: require('../sanitizer/_sources_and_layers'), |
||||||
|
private: require('../sanitizer/_flag_bool')('private', false), |
||||||
|
location_bias: require('../sanitizer/_location_bias')(_api_pelias_config.defaultParameters), |
||||||
|
geo_search: require('../sanitizer/_geo_search'), |
||||||
|
boundary_country: require('../sanitizer/_boundary_country'), |
||||||
|
categories: require('../sanitizer/_categories'), |
||||||
|
// this can go away once geonames has been abrogated
|
||||||
|
geonames_warnings: require('../sanitizer/_geonames_warnings') |
||||||
|
}; |
||||||
|
|
||||||
var sanitizeAll = require('../sanitizer/sanitizeAll'), |
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); }; |
||||||
sanitizers = { |
|
||||||
singleScalarParameters: require('../sanitizer/_single_scalar_parameters'), |
|
||||||
quattroshapes_deprecation: require('../sanitizer/_deprecate_quattroshapes'), |
|
||||||
text: require('../sanitizer/_text'), |
|
||||||
iso2_to_iso3: require('../sanitizer/_iso2_to_iso3'), |
|
||||||
city_name_standardizer: require('../sanitizer/_city_name_standardizer'), |
|
||||||
size: require('../sanitizer/_size')(/* use defaults*/), |
|
||||||
layers: require('../sanitizer/_targets')('layers', type_mapping.layer_mapping), |
|
||||||
sources: require('../sanitizer/_targets')('sources', type_mapping.source_mapping), |
|
||||||
// depends on the layers and sources sanitizers, must be run after them
|
|
||||||
sources_and_layers: require('../sanitizer/_sources_and_layers'), |
|
||||||
private: require('../sanitizer/_flag_bool')('private', false), |
|
||||||
geo_search: require('../sanitizer/_geo_search'), |
|
||||||
boundary_country: require('../sanitizer/_boundary_country'), |
|
||||||
categories: require('../sanitizer/_categories'), |
|
||||||
// this can go away once geonames has been abrogated
|
|
||||||
geonames_warnings: require('../sanitizer/_geonames_warnings') |
|
||||||
}; |
|
||||||
|
|
||||||
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); }; |
return function( req, res, next ){ |
||||||
|
sanitize( req, function( err, clean ){ |
||||||
|
next(); |
||||||
|
}); |
||||||
|
}; |
||||||
|
|
||||||
// middleware
|
|
||||||
module.exports.middleware = function( req, res, next ){ |
|
||||||
sanitize( req, function( err, clean ){ |
|
||||||
next(); |
|
||||||
}); |
|
||||||
}; |
}; |
||||||
|
@ -1,28 +1,30 @@ |
|||||||
var type_mapping = require('../helper/type_mapping'); |
var type_mapping = require('../helper/type_mapping'); |
||||||
|
var sanitizeAll = require('../sanitizer/sanitizeAll'); |
||||||
var sanitizeAll = require('../sanitizer/sanitizeAll'), |
|
||||||
sanitizers = { |
|
||||||
singleScalarParameters: require('../sanitizer/_single_scalar_parameters'), |
|
||||||
quattroshapes_deprecation: require('../sanitizer/_deprecate_quattroshapes'), |
|
||||||
synthesize_analysis: require('../sanitizer/_synthesize_analysis'), |
|
||||||
iso2_to_iso3: require('../sanitizer/_iso2_to_iso3'), |
|
||||||
city_name_standardizer: require('../sanitizer/_city_name_standardizer'), |
|
||||||
size: require('../sanitizer/_size')(/* use defaults*/), |
|
||||||
layers: require('../sanitizer/_targets')('layers', type_mapping.layer_mapping), |
|
||||||
sources: require('../sanitizer/_targets')('sources', type_mapping.source_mapping), |
|
||||||
// depends on the layers and sources sanitizers, must be run after them
|
|
||||||
sources_and_layers: require('../sanitizer/_sources_and_layers'), |
|
||||||
private: require('../sanitizer/_flag_bool')('private', false), |
|
||||||
geo_search: require('../sanitizer/_geo_search'), |
|
||||||
boundary_country: require('../sanitizer/_boundary_country'), |
|
||||||
categories: require('../sanitizer/_categories') |
|
||||||
}; |
|
||||||
|
|
||||||
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); }; |
|
||||||
|
|
||||||
// middleware
|
// middleware
|
||||||
module.exports.middleware = function( req, res, next ){ |
module.exports.middleware = (_api_pelias_config) => { |
||||||
sanitize( req, function( err, clean ){ |
var sanitizers = { |
||||||
next(); |
singleScalarParameters: require('../sanitizer/_single_scalar_parameters'), |
||||||
}); |
quattroshapes_deprecation: require('../sanitizer/_deprecate_quattroshapes'), |
||||||
|
synthesize_analysis: require('../sanitizer/_synthesize_analysis'), |
||||||
|
iso2_to_iso3: require('../sanitizer/_iso2_to_iso3'), |
||||||
|
city_name_standardizer: require('../sanitizer/_city_name_standardizer'), |
||||||
|
size: require('../sanitizer/_size')(/* use defaults*/), |
||||||
|
layers: require('../sanitizer/_targets')('layers', type_mapping.layer_mapping), |
||||||
|
sources: require('../sanitizer/_targets')('sources', type_mapping.source_mapping), |
||||||
|
// depends on the layers and sources sanitizers, must be run after them
|
||||||
|
sources_and_layers: require('../sanitizer/_sources_and_layers'), |
||||||
|
private: require('../sanitizer/_flag_bool')('private', false), |
||||||
|
location_bias: require('../sanitizer/_location_bias')(_api_pelias_config.defaultParameters), |
||||||
|
geo_search: require('../sanitizer/_geo_search'), |
||||||
|
boundary_country: require('../sanitizer/_boundary_country'), |
||||||
|
categories: require('../sanitizer/_categories') |
||||||
|
}; |
||||||
|
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); }; |
||||||
|
|
||||||
|
return function( req, res, next ){ |
||||||
|
sanitize( req, function( err, clean ){ |
||||||
|
next(); |
||||||
|
}); |
||||||
|
}; |
||||||
}; |
}; |
||||||
|
@ -0,0 +1,109 @@ |
|||||||
|
const setup = require('../../../sanitizer/_location_bias'); |
||||||
|
|
||||||
|
module.exports.tests = {}; |
||||||
|
|
||||||
|
module.exports.tests.setLocationBias = function(test, common) { |
||||||
|
test('set focus point', t => { |
||||||
|
const defaultParameters = { // specify focus point latitude and longitude
|
||||||
|
'focus.point.lat': 12.12121212, |
||||||
|
'focus.point.lon': 21.21212121 |
||||||
|
}; |
||||||
|
const locationBias = setup(defaultParameters); |
||||||
|
const raw = {}; |
||||||
|
const expected = { |
||||||
|
'focus.point.lat': 12.12121212, |
||||||
|
'focus.point.lon': 21.21212121 |
||||||
|
}; |
||||||
|
|
||||||
|
locationBias(raw, undefined); |
||||||
|
t.deepEqual(raw, expected, 'focus point should be set'); |
||||||
|
t.end(); |
||||||
|
|
||||||
|
}); |
||||||
|
|
||||||
|
test('undefined raw', t => { |
||||||
|
const defaultParameters = { |
||||||
|
'focus.point.lat': 12.12121212, |
||||||
|
'focus.point.lon': 21.21212121 |
||||||
|
}; |
||||||
|
const locationBias = setup(defaultParameters); |
||||||
|
|
||||||
|
locationBias(undefined, undefined); |
||||||
|
t.deepEqual(undefined, undefined, 'should be unmodified' ); |
||||||
|
t.end(); |
||||||
|
}); |
||||||
|
|
||||||
|
test('focusPointLat is undefined', t => { |
||||||
|
const defaultParameters = { |
||||||
|
'focus.point.lon': 12.2121212 |
||||||
|
}; |
||||||
|
const locationBias = setup(defaultParameters); |
||||||
|
const raw = {}; |
||||||
|
const expected = {}; |
||||||
|
|
||||||
|
locationBias(raw, undefined); |
||||||
|
t.deepEqual(raw, expected, 'should be unmodified' ); |
||||||
|
t.end(); |
||||||
|
}); |
||||||
|
|
||||||
|
test('focusPointLon is undefined', t => { |
||||||
|
const defaultParameters = { |
||||||
|
'focus.point.lat': 12.2121212 |
||||||
|
}; |
||||||
|
const locationBias = setup(defaultParameters); |
||||||
|
const raw = {}; |
||||||
|
const expected = {}; |
||||||
|
|
||||||
|
locationBias(raw, undefined); |
||||||
|
t.deepEqual(raw, expected, 'should be unmodified' ); |
||||||
|
t.end(); |
||||||
|
}); |
||||||
|
|
||||||
|
test('raw has focus.point.lon already', t => { |
||||||
|
const defaultParameters = { |
||||||
|
'focus.point.lon': 12.2121212, |
||||||
|
'focus.point.lat': 12.2121212 |
||||||
|
}; |
||||||
|
const locationBias = setup(defaultParameters); |
||||||
|
const raw = { |
||||||
|
'focus.point.lon': 43.4343434 |
||||||
|
}; |
||||||
|
const expected = { |
||||||
|
'focus.point.lon': 43.4343434 |
||||||
|
}; |
||||||
|
|
||||||
|
locationBias(raw, undefined); |
||||||
|
t.deepEqual(raw, expected, 'should be unmodified' ); |
||||||
|
t.end(); |
||||||
|
}); |
||||||
|
|
||||||
|
test('raw has focus.point.lat already', t => { |
||||||
|
const defaultParameters = { |
||||||
|
'focus.point.lon': 12.2121212, |
||||||
|
'focus.point.lat': 12.2121212 |
||||||
|
}; |
||||||
|
const locationBias = setup(defaultParameters); |
||||||
|
const raw = { |
||||||
|
'focus.point.lat': 34.3434343 |
||||||
|
}; |
||||||
|
const expected = { |
||||||
|
'focus.point.lat': 34.3434343 |
||||||
|
}; |
||||||
|
|
||||||
|
locationBias(raw, undefined); |
||||||
|
t.deepEqual(raw, expected, 'should be unmodified' ); |
||||||
|
t.end(); |
||||||
|
}); |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
module.exports.all = (tape, common) => { |
||||||
|
|
||||||
|
function test(name, testFunction) { |
||||||
|
return tape(`SANITIZE _location_bias: ${name}`, testFunction); |
||||||
|
} |
||||||
|
|
||||||
|
for( var testCase in module.exports.tests ){ |
||||||
|
module.exports.tests[testCase](test, common); |
||||||
|
} |
||||||
|
}; |
Loading…
Reference in new issue