Browse Source

Store sanitised types from layers parameter

This moves the list of types created by sanitising the layer API
parameter from clean.layers to clean.types.from_layers. In subsequent
commits, types created from address parsing, and the
yet-to-be-implemented source parameter will also live in the clean.types
object.

This will allow moving logic to set cmd.type out of controllers, and
into separate logic that can be a littler smarter. Also, it will no
longer require the clean.default_layers_set flag to be passed all around
like a nasty global variable.
pull/229/head
Julian Simioni 9 years ago
parent
commit
4037c49c4b
  1. 4
      controller/search.js
  2. 4
      sanitiser/_layers.js
  3. 24
      test/unit/sanitiser/reverse.js
  4. 24
      test/unit/sanitiser/search.js
  5. 2
      test/unit/sanitiser/suggest.js

4
controller/search.js

@ -15,8 +15,8 @@ function setup( backend, query ){
body: query( req.clean )
};
if (req.clean.layers) {
cmd.type = req.clean.layers;
if (req.clean.types && req.clean.types.from_layers) {
cmd.type = req.clean.types.from_layers;
}
// set type if input suggests targeting a layer(s)

4
sanitiser/_layers.js

@ -8,6 +8,8 @@ function sanitize( req ){
var clean = req.clean || {};
var params= req.query;
clean.types = clean.types || {};
// ensure the input params are a valid object
if( !isObject( params ) ){
params = {};
@ -39,7 +41,7 @@ function sanitize( req ){
}
// pass validated params to next middleware
clean.layers = get_layers(layers);
clean.types.from_layers = get_layers(layers);
req.clean = clean;
return { 'error': false };

24
test/unit/sanitiser/reverse.js

@ -5,8 +5,10 @@ var suggest = require('../../../sanitiser/reverse'),
delim = ',',
defaultError = 'missing param \'lat\'',
defaultClean = { lat:0,
layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
types: {
from_layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
},
lon: 0,
size: 10,
details: true,
@ -158,7 +160,7 @@ module.exports.tests.sanitize_details = function(test, common) {
module.exports.tests.sanitize_layers = function(test, common) {
test('unspecified', function(t) {
sanitize({ layers: undefined, input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, defaultClean.layers, 'default layers set');
t.deepEqual(clean.types.from_layers, defaultClean.types.from_layers, 'default layers set');
t.end();
});
});
@ -173,21 +175,21 @@ module.exports.tests.sanitize_layers = function(test, common) {
test('poi (alias) layer', function(t) {
var poi_layers = ['geoname','osmnode','osmway'];
sanitize({ layers: 'poi', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, poi_layers, 'poi layers set');
t.deepEqual(clean.types.from_layers, poi_layers, 'poi layers set');
t.end();
});
});
test('admin (alias) layer', function(t) {
var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin'];
sanitize({ layers: 'admin', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, admin_layers, 'admin layers set');
t.deepEqual(clean.types.from_layers, admin_layers, 'admin layers set');
t.end();
});
});
test('address (alias) layer', function(t) {
var address_layers = ['osmaddress','openaddresses'];
sanitize({ layers: 'address', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, address_layers, 'address layers set');
t.deepEqual(clean.types.from_layers, address_layers, 'address layers set');
t.end();
});
});
@ -195,7 +197,7 @@ module.exports.tests.sanitize_layers = function(test, common) {
var poi_layers = ['geoname','osmnode','osmway'];
var reg_layers = ['admin0', 'admin1'];
sanitize({ layers: 'poi,admin0,admin1', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, reg_layers.concat(poi_layers), 'poi + regular layers');
t.deepEqual(clean.types.from_layers, reg_layers.concat(poi_layers), 'poi + regular layers');
t.end();
});
});
@ -203,7 +205,7 @@ module.exports.tests.sanitize_layers = function(test, common) {
var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin'];
var reg_layers = ['geoname', 'osmway'];
sanitize({ layers: 'admin,geoname,osmway', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, reg_layers.concat(admin_layers), 'admin + regular layers set');
t.deepEqual(clean.types.from_layers, reg_layers.concat(admin_layers), 'admin + regular layers set');
t.end();
});
});
@ -211,21 +213,21 @@ module.exports.tests.sanitize_layers = function(test, common) {
var address_layers = ['osmaddress','openaddresses'];
var reg_layers = ['geoname', 'osmway'];
sanitize({ layers: 'address,geoname,osmway', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, reg_layers.concat(address_layers), 'address + regular layers set');
t.deepEqual(clean.types.from_layers, reg_layers.concat(address_layers), 'address + regular layers set');
t.end();
});
});
test('alias layer plus regular layers (no duplicates)', function(t) {
var poi_layers = ['geoname','osmnode','osmway'];
sanitize({ layers: 'poi,geoname,osmnode', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, poi_layers, 'poi layers found (no duplicates)');
t.deepEqual(clean.types.from_layers, poi_layers, 'poi layers found (no duplicates)');
t.end();
});
});
test('multiple alias layers (no duplicates)', function(t) {
var alias_layers = ['geoname','osmnode','osmway','admin0','admin1','admin2','neighborhood','locality','local_admin'];
sanitize({ layers: 'poi,admin', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, alias_layers, 'all layers found (no duplicates)');
t.deepEqual(clean.types.from_layers, alias_layers, 'all layers found (no duplicates)');
t.end();
});
});

24
test/unit/sanitiser/search.js

@ -8,8 +8,10 @@ var search = require('../../../sanitiser/search'),
delim = ',',
defaultError = 'invalid param \'input\': text length, must be >0',
defaultClean = { input: 'test',
layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
types: {
from_layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
},
size: 10,
details: true,
parsed_input: defaultParsed,
@ -320,7 +322,7 @@ module.exports.tests.sanitize_details = function(test, common) {
module.exports.tests.sanitize_layers = function(test, common) {
test('unspecified', function(t) {
sanitize({ layers: undefined, input: 'test' }, function( err, clean ){
t.deepEqual(clean.layers, defaultClean.layers, 'default layers set');
t.deepEqual(clean.types.from_layers, defaultClean.types.from_layers, 'default layers set');
t.end();
});
});
@ -335,21 +337,21 @@ module.exports.tests.sanitize_layers = function(test, common) {
test('poi (alias) layer', function(t) {
var poi_layers = ['geoname','osmnode','osmway'];
sanitize({ layers: 'poi', input: 'test' }, function( err, clean ){
t.deepEqual(clean.layers, poi_layers, 'poi layers set');
t.deepEqual(clean.types.from_layers, poi_layers, 'poi layers set');
t.end();
});
});
test('admin (alias) layer', function(t) {
var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin'];
sanitize({ layers: 'admin', input: 'test' }, function( err, clean ){
t.deepEqual(clean.layers, admin_layers, 'admin layers set');
t.deepEqual(clean.types.from_layers, admin_layers, 'admin layers set');
t.end();
});
});
test('address (alias) layer', function(t) {
var address_layers = ['osmaddress','openaddresses'];
sanitize({ layers: 'address', input: 'test' }, function( err, clean ){
t.deepEqual(clean.layers, address_layers, 'address layers set');
t.deepEqual(clean.types.from_layers, address_layers, 'address layers set');
t.end();
});
});
@ -357,7 +359,7 @@ module.exports.tests.sanitize_layers = function(test, common) {
var poi_layers = ['geoname','osmnode','osmway'];
var reg_layers = ['admin0', 'admin1'];
sanitize({ layers: 'poi,admin0,admin1', input: 'test' }, function( err, clean ){
t.deepEqual(clean.layers, reg_layers.concat(poi_layers), 'poi + regular layers');
t.deepEqual(clean.types.from_layers, reg_layers.concat(poi_layers), 'poi + regular layers');
t.end();
});
});
@ -365,7 +367,7 @@ module.exports.tests.sanitize_layers = function(test, common) {
var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin'];
var reg_layers = ['geoname', 'osmway'];
sanitize({ layers: 'admin,geoname,osmway', input: 'test' }, function( err, clean ){
t.deepEqual(clean.layers, reg_layers.concat(admin_layers), 'admin + regular layers set');
t.deepEqual(clean.types.from_layers, reg_layers.concat(admin_layers), 'admin + regular layers set');
t.end();
});
});
@ -373,21 +375,21 @@ module.exports.tests.sanitize_layers = function(test, common) {
var address_layers = ['osmaddress','openaddresses'];
var reg_layers = ['geoname', 'osmway'];
sanitize({ layers: 'address,geoname,osmway', input: 'test' }, function( err, clean ){
t.deepEqual(clean.layers, reg_layers.concat(address_layers), 'address + regular layers set');
t.deepEqual(clean.types.from_layers, reg_layers.concat(address_layers), 'address + regular layers set');
t.end();
});
});
test('alias layer plus regular layers (no duplicates)', function(t) {
var poi_layers = ['geoname','osmnode','osmway'];
sanitize({ layers: 'poi,geoname,osmnode', input: 'test' }, function( err, clean ){
t.deepEqual(clean.layers, poi_layers, 'poi layers found (no duplicates)');
t.deepEqual(clean.types.from_layers, poi_layers, 'poi layers found (no duplicates)');
t.end();
});
});
test('multiple alias layers (no duplicates)', function(t) {
var alias_layers = ['geoname','osmnode','osmway','admin0','admin1','admin2','neighborhood','locality','local_admin'];
sanitize({ layers: 'poi,admin', input: 'test' }, function( err, clean ){
t.deepEqual(clean.layers, alias_layers, 'all layers found (no duplicates)');
t.deepEqual(clean.types.from_layers, alias_layers, 'all layers found (no duplicates)');
t.end();
});
});

2
test/unit/sanitiser/suggest.js

@ -272,7 +272,7 @@ module.exports.tests.sanitize_details = function(test, common) {
module.exports.tests.sanitize_layers = function(test, common) {
test('unspecified', function(t) {
sanitize({ layers: undefined, input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, defaultClean.layers, 'default layers set');
t.deepEqual(clean.types.from_layers, defaultClean.types.from_layers, 'default layers set');
t.end();
});
});

Loading…
Cancel
Save