From 30b7290990e3381d717fb0622f860f41d9947703 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Thu, 31 Aug 2017 14:38:48 -0400 Subject: [PATCH] added stub request_language sanitizer that just allows lang param --- sanitizer/_request_language.js | 15 +++++++++ sanitizer/autocomplete.js | 3 +- sanitizer/nearby.js | 3 +- sanitizer/place.js | 3 +- sanitizer/reverse.js | 3 +- sanitizer/search.js | 3 +- sanitizer/structured_geocoding.js | 3 +- test/unit/run.js | 1 + test/unit/sanitizer/_request_language.js | 35 +++++++++++++++++++++ test/unit/sanitizer/autocomplete.js | 11 ++++++- test/unit/sanitizer/nearby.js | 11 ++++++- test/unit/sanitizer/place.js | 11 ++++++- test/unit/sanitizer/reverse.js | 11 ++++++- test/unit/sanitizer/search.js | 11 ++++++- test/unit/sanitizer/structured_geocoding.js | 11 ++++++- 15 files changed, 123 insertions(+), 12 deletions(-) create mode 100644 sanitizer/_request_language.js create mode 100644 test/unit/sanitizer/_request_language.js diff --git a/sanitizer/_request_language.js b/sanitizer/_request_language.js new file mode 100644 index 00000000..bbc346c1 --- /dev/null +++ b/sanitizer/_request_language.js @@ -0,0 +1,15 @@ +// this sanitizer exists solely to allow `lang` as a request parameter +function _sanitize( raw, clean ){ + // error & warning messages + return { errors: [], warnings: [] }; +} + +function _expected(){ + return [{ 'name': 'lang' }]; +} + +// export function +module.exports = () => ({ + sanitize: _sanitize, + expected: _expected +}); diff --git a/sanitizer/autocomplete.js b/sanitizer/autocomplete.js index c26a3b44..26c5d0ac 100644 --- a/sanitizer/autocomplete.js +++ b/sanitizer/autocomplete.js @@ -17,7 +17,8 @@ module.exports.middleware = (_api_pelias_config) => { location_bias: require('../sanitizer/_location_bias')(_api_pelias_config.defaultParameters), geo_autocomplete: require('../sanitizer/_geo_autocomplete')(), boundary_country: require('../sanitizer/_boundary_country')(), - categories: require('../sanitizer/_categories')() + categories: require('../sanitizer/_categories')(), + request_language: require('../sanitizer/_request_language')() }; return ( req, res, next ) => { diff --git a/sanitizer/nearby.js b/sanitizer/nearby.js index 8aa727bd..1cd3b276 100644 --- a/sanitizer/nearby.js +++ b/sanitizer/nearby.js @@ -15,7 +15,8 @@ var sanitizers = { private: require('../sanitizer/_flag_bool')('private', false), geo_reverse: require('../sanitizer/_geo_reverse')(), boundary_country: require('../sanitizer/_boundary_country')(), - categories: require('../sanitizer/_categories')() + categories: require('../sanitizer/_categories')(), + request_language: require('../sanitizer/_request_language')() }; // middleware diff --git a/sanitizer/place.js b/sanitizer/place.js index ce220d64..4d158dc3 100644 --- a/sanitizer/place.js +++ b/sanitizer/place.js @@ -4,7 +4,8 @@ var sanitizeAll = require('../sanitizer/sanitizeAll'), singleScalarParameters: require('../sanitizer/_single_scalar_parameters')(), debug: require('../sanitizer/_debug')(), ids: require('../sanitizer/_ids')(), - private: require('../sanitizer/_flag_bool')('private', false) + private: require('../sanitizer/_flag_bool')('private', false), + request_language: require('../sanitizer/_request_language')() }; // middleware diff --git a/sanitizer/reverse.js b/sanitizer/reverse.js index f43d0845..457e81a5 100644 --- a/sanitizer/reverse.js +++ b/sanitizer/reverse.js @@ -13,7 +13,8 @@ var sanitizeAll = require('../sanitizer/sanitizeAll'), size: require('../sanitizer/_size')(/* use defaults*/), private: require('../sanitizer/_flag_bool')('private', false), geo_reverse: require('../sanitizer/_geo_reverse')(), - boundary_country: require('../sanitizer/_boundary_country')() + boundary_country: require('../sanitizer/_boundary_country')(), + request_language: require('../sanitizer/_request_language')() }; // middleware diff --git a/sanitizer/search.js b/sanitizer/search.js index c8e7282f..008ae693 100644 --- a/sanitizer/search.js +++ b/sanitizer/search.js @@ -18,7 +18,8 @@ module.exports.middleware = (_api_pelias_config) => { 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')() + geonames_warnings: require('../sanitizer/_geonames_warnings')(), + request_language: require('../sanitizer/_request_language')() }; return ( req, res, next ) => { diff --git a/sanitizer/structured_geocoding.js b/sanitizer/structured_geocoding.js index 388949ea..670a26aa 100644 --- a/sanitizer/structured_geocoding.js +++ b/sanitizer/structured_geocoding.js @@ -19,7 +19,8 @@ module.exports.middleware = (_api_pelias_config) => { 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')() + categories: require('../sanitizer/_categories')(), + request_language: require('../sanitizer/_request_language')() }; return ( req, res, next ) => { diff --git a/test/unit/run.js b/test/unit/run.js index 8299672d..e09bc037 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -76,6 +76,7 @@ var tests = [ require('./sanitizer/_layers'), require('./sanitizer/_location_bias'), require('./sanitizer/_city_name_standardizer'), + require('./sanitizer/_request_language'), require('./sanitizer/_single_scalar_parameters'), require('./sanitizer/_size'), require('./sanitizer/_sources'), diff --git a/test/unit/sanitizer/_request_language.js b/test/unit/sanitizer/_request_language.js new file mode 100644 index 00000000..0261859a --- /dev/null +++ b/test/unit/sanitizer/_request_language.js @@ -0,0 +1,35 @@ +const sanitizer = require('../../../sanitizer/_request_language')(); + +module.exports.tests = {}; + +module.exports.tests.do_nothing = (test, common) => { + test('sanitize should return empty warnings/erros', t => { + const messages = sanitizer.sanitize(); + + t.deepEquals(messages.errors, [], 'no errors'); + t.deepEquals(messages.warnings, [], 'no warnings'); + t.end(); + + }); + +}; + +module.exports.tests.expected = (test, common) => { + test('expected should contain only \'lang\'', t => { + const expected = sanitizer.expected(); + + t.deepEquals(expected, [{'name': 'lang'}]); + t.end(); + + }); +}; + +module.exports.all = (tape, common) => { + function test(name, testFunction) { + return tape(`SANITIZE _request_language: ${name}`, testFunction); + } + + for( const testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/test/unit/sanitizer/autocomplete.js b/test/unit/sanitizer/autocomplete.js index dadb546e..4ca62b97 100644 --- a/test/unit/sanitizer/autocomplete.js +++ b/test/unit/sanitizer/autocomplete.js @@ -120,6 +120,14 @@ module.exports.tests.sanitizers = function(test, common) { return { errors: [], warnings: [] }; } }; + }, + '../sanitizer/_request_language': () => { + return { + sanitize: () => { + called_sanitizers.push('_request_language'); + return { errors: [], warnings: [] }; + } + }; } }); @@ -136,7 +144,8 @@ module.exports.tests.sanitizers = function(test, common) { '_location_bias', '_geo_autocomplete', '_boundary_country', - '_categories' + '_categories', + '_request_language' ]; const req = {}; diff --git a/test/unit/sanitizer/nearby.js b/test/unit/sanitizer/nearby.js index a9ea6db6..cff81b71 100644 --- a/test/unit/sanitizer/nearby.js +++ b/test/unit/sanitizer/nearby.js @@ -109,6 +109,14 @@ module.exports.tests.sanitize = function(test, common) { return { errors: [], warnings: [] }; } }; + }, + '../sanitizer/_request_language': () => { + return { + sanitize: () => { + called_sanitizers.push('_request_language'); + return { errors: [], warnings: [] }; + } + }; } }); @@ -124,7 +132,8 @@ module.exports.tests.sanitize = function(test, common) { '_flag_bool', '_geo_reverse', '_boundary_country', - '_categories' + '_categories', + '_request_language' ]; const req = {}; diff --git a/test/unit/sanitizer/place.js b/test/unit/sanitizer/place.js index 2a47e391..7e0f9c8d 100644 --- a/test/unit/sanitizer/place.js +++ b/test/unit/sanitizer/place.js @@ -45,6 +45,14 @@ module.exports.tests.sanitize = function(test, common) { } else { throw new Error('incorrect parameters passed to _flag_bool'); } + }, + '../sanitizer/_request_language': () => { + return { + sanitize: () => { + called_sanitizers.push('_request_language'); + return { errors: [], warnings: [] }; + } + }; } }); @@ -52,7 +60,8 @@ module.exports.tests.sanitize = function(test, common) { '_single_scalar_parameters', '_debug', '_ids', - '_flag_bool' + '_flag_bool', + '_request_language' ]; const req = {}; diff --git a/test/unit/sanitizer/reverse.js b/test/unit/sanitizer/reverse.js index aafaa6e9..c979ec67 100644 --- a/test/unit/sanitizer/reverse.js +++ b/test/unit/sanitizer/reverse.js @@ -101,6 +101,14 @@ module.exports.tests.sanitize = function(test, common) { return { errors: [], warnings: [] }; } }; + }, + '../sanitizer/_request_language': () => { + return { + sanitize: () => { + called_sanitizers.push('_request_language'); + return { errors: [], warnings: [] }; + } + }; } }); @@ -115,7 +123,8 @@ module.exports.tests.sanitize = function(test, common) { '_size', '_flag_bool', '_geo_reverse', - '_boundary_country' + '_boundary_country', + '_request_language' ]; const req = {}; diff --git a/test/unit/sanitizer/search.js b/test/unit/sanitizer/search.js index e4192029..9ac585ad 100644 --- a/test/unit/sanitizer/search.js +++ b/test/unit/sanitizer/search.js @@ -133,6 +133,14 @@ module.exports.tests.sanitize = (test, common) => { } } }; + }, + '../sanitizer/_request_language': () => { + return { + sanitize: () => { + called_sanitizers.push('_request_language'); + return { errors: [], warnings: [] }; + } + }; } }); @@ -151,7 +159,8 @@ module.exports.tests.sanitize = (test, common) => { '_geo_search', '_boundary_country', '_categories', - '_geonames_warnings' + '_geonames_warnings', + '_request_language' ]; const req = {}; diff --git a/test/unit/sanitizer/structured_geocoding.js b/test/unit/sanitizer/structured_geocoding.js index a50ff3ef..963aa21d 100644 --- a/test/unit/sanitizer/structured_geocoding.js +++ b/test/unit/sanitizer/structured_geocoding.js @@ -137,6 +137,14 @@ module.exports.tests.sanitize = function(test, common) { } } }; + }, + '../sanitizer/_request_language': () => { + return { + sanitize: () => { + called_sanitizers.push('_request_language'); + return { errors: [], warnings: [] }; + } + }; } }); @@ -155,7 +163,8 @@ module.exports.tests.sanitize = function(test, common) { '_location_bias', '_geo_search', '_boundary_country', - '_categories' + '_categories', + '_request_language' ]; var req = {};