From 3383fa0321f44713aecc4a45acf21a46cec6d6b1 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Fri, 4 Sep 2015 14:45:37 -0400 Subject: [PATCH] Add source parameter sanitiser --- sanitiser/_source.js | 44 +++++++++++++ test/unit/run.js | 1 + test/unit/sanitiser/_source.js | 114 +++++++++++++++++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 sanitiser/_source.js create mode 100644 test/unit/sanitiser/_source.js diff --git a/sanitiser/_source.js b/sanitiser/_source.js new file mode 100644 index 00000000..f8f89a68 --- /dev/null +++ b/sanitiser/_source.js @@ -0,0 +1,44 @@ +var isObject = require('is-object'); +var sources_map = require( '../query/sources' ); +var all_sources = Object.keys(sources_map); + +function sanitize( req ) { + req.clean = req.clean || {}; + var params = req.query; + + req.clean.types = req.clean.types || {}; + + // ensure the input params are a valid object + if( !isObject( params ) ){ + params = {}; + } + + // default case (no layers specified in GET params) + // don't even set the from_layers key in this case + if('string' !== typeof params.source || !params.source.length){ + return { error: false }; + } + + var sources = params.source.split(','); + + var invalid_sources = sources.filter(function(source) { + return all_sources.indexOf(source) === -1; + }); + + if (invalid_sources.length > 0) { + return { + error: true, + msg: '`' + invalid_sources[0] + '` is an invalid source parameter. Valid options: ' + all_sources.join(', ') + }; + } + + var types = sources.reduce(function(acc, source) { + return acc.concat(sources_map[source]); + }, []); + + req.clean.types.from_source = types; + + return { error: false }; +} + +module.exports = sanitize; diff --git a/test/unit/run.js b/test/unit/run.js index 26a22f87..d5d55676 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -8,6 +8,7 @@ var tests = [ require('./controller/search'), require('./service/mget'), require('./service/search'), + require('./sanitiser/_source'), require('./sanitiser/search'), require('./sanitiser/reverse'), require('./sanitiser/place'), diff --git a/test/unit/sanitiser/_source.js b/test/unit/sanitiser/_source.js new file mode 100644 index 00000000..235e18c6 --- /dev/null +++ b/test/unit/sanitiser/_source.js @@ -0,0 +1,114 @@ +var sanitize = require( '../../../sanitiser/_source' ); + +var success_response = { error: false }; + +module.exports.tests = {}; + +module.exports.tests.no_sources = function(test, common) { + test('source is not set', function(t) { + var req = { + query: { } + }; + + var response = sanitize(req); + + t.deepEqual(req.clean.types, {}, 'clean.types should be empty object'); + t.deepEqual(response, success_response, 'no error returned'); + t.end(); + }); + + test('source is empty string', function(t) { + var req = { + query: { + source: '' + } + }; + + var response = sanitize(req); + + t.deepEqual(req.clean.types, {}, 'clean.types should be empty object'); + t.deepEqual(response, success_response, 'no error returned'); + t.end(); + }); +}; + +module.exports.tests.valid_sources = function(test, common) { + test('geonames source', function(t) { + var req = { + query: { + source: 'geonames' + } + }; + + var response = sanitize(req); + + t.deepEqual(req.clean.types, { from_source: ['geoname'] }, 'clean.types should contain from_source entry with geonames'); + t.deepEqual(response, success_response, 'no error returned'); + t.end(); + }); + + test('openstreetmap source', function(t) { + var req = { + query: { + source: 'openstreetmap' + } + }; + var expected_types = { + from_source: ['osmaddress', 'osmnode', 'osmway'] + }; + + var response = sanitize(req); + + t.deepEqual(req.clean.types, expected_types, 'clean.types should contain from_source entry with multiple entries for openstreetmap'); + t.deepEqual(response, success_response, 'no error returned'); + t.end(); + }); + + test('multiple sources', function(t) { + var req = { + query: { + source: 'openstreetmap,openaddresses' + } + }; + var expected_types = { + from_source: ['osmaddress', 'osmnode', 'osmway', 'openaddresses'] + }; + + var response = sanitize(req); + + t.deepEqual(req.clean.types, expected_types, + 'clean.types should contain from_source entry with multiple entries for openstreetmap and openadresses'); + t.deepEqual(response, success_response, 'no error returned'); + t.end(); + }); +}; + +module.exports.tests.invalid_sources = function(test, common) { + test('geonames source', function(t) { + var req = { + query: { + source: 'notasource' + } + }; + var expected_response = { + error: true, + msg: '`notasource` is an invalid source parameter. Valid options: geonames, openaddresses, quattroshapes, openstreetmap' + }; + + var response = sanitize(req); + + t.deepEqual(response, expected_response, 'error with message returned'); + t.deepEqual(req.clean.types, { }, 'clean.types should remain empty'); + t.end(); + }); +}; + +module.exports.all = function (tape, common) { + function test(name, testFunction) { + return tape('SANTIZE _source ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +};