Browse Source

prevent overriding raw.focus.point.lat/lon

pull/928/head
Lily He 8 years ago
parent
commit
5504240016
  1. 12
      sanitizer/_location_bias.js
  2. 4
      schema.js
  3. 36
      test/unit/sanitizer/_location_bias.js

12
sanitizer/_location_bias.js

@ -7,9 +7,17 @@ Set a focus.lat and focus.lon if specified in 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'])) {
!_.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'];
@ -19,5 +27,5 @@ function setup(defaultParameters){
};
}
// if focus.point.lat and focus.point.lon already exists, don't change
module.exports = setup;

4
schema.js

@ -41,8 +41,8 @@ module.exports = Joi.object().keys({
}).unknown(false).requiredKeys('url')
}).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),
'focus.point.lat': Joi.number(),
'focus.point.lon': Joi.number(),
}).unknown(true).default({})
}).requiredKeys('version', 'indexName', 'host').unknown(true),

36
test/unit/sanitizer/_location_bias.js

@ -59,6 +59,42 @@ module.exports.tests.setLocationBias = function(test, common) {
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) => {

Loading…
Cancel
Save