mirror of https://github.com/pelias/api.git
Browse Source
* merged master into branch * removed `details` * refactored `details` and `private` into a common _flag_bool sanitizer type * cleaned up `private`pull/241/head
Diana Shkolnikov
9 years ago
48 changed files with 945 additions and 1040 deletions
@ -0,0 +1,3 @@
|
||||
module.exports = function controller( req, res, next) { |
||||
res.send('status: ok'); |
||||
}; |
@ -1,25 +0,0 @@
|
||||
module.exports = function(alias_layers) { |
||||
// make a copy of the array so, you are not modifying original ref
|
||||
var layers = alias_layers.slice(0); |
||||
|
||||
// expand aliases
|
||||
var expand_aliases = function(alias, layers, layer_indeces) { |
||||
var alias_index = layers.indexOf(alias); |
||||
if (alias_index !== -1 ) { |
||||
layers.splice(alias_index, 1); |
||||
layers = layers.concat(layer_indeces); |
||||
} |
||||
return layers; |
||||
}; |
||||
|
||||
layers = expand_aliases('poi', layers, ['geoname','osmnode','osmway']); |
||||
layers = expand_aliases('admin', layers, ['admin0','admin1','admin2','neighborhood','locality','local_admin']); |
||||
layers = expand_aliases('address', layers, ['osmaddress','openaddresses']); |
||||
|
||||
// de-dupe
|
||||
layers = layers.filter(function(item, pos) { |
||||
return layers.indexOf(item) === pos; |
||||
}); |
||||
|
||||
return layers; |
||||
}; |
@ -1,100 +1 @@
|
||||
## /search |
||||
|
||||
The full text search endpoint that matches the name of a place to points on the planet. |
||||
|
||||
#### Required Parameters |
||||
* `text`: the string to search for (eg `new york city` or `london`) |
||||
|
||||
#### Optional Parameters |
||||
* `lat`, `lon`: the latitude/longitude coordinates to bias search results towards (may increase relevancy) |
||||
* `size` (default: `10`): the number of results to return |
||||
* `layers` (default: `poi,admin,address`): the comma-separated names of datasets you wish to query. Valid values are: |
||||
* aliases for multiple datasets like `poi`, `admin` or `address` |
||||
* `poi` expands internally to `geoname`, `osmnode`, `osmway` |
||||
* `admin` expands to `admin0`, `admin1`, `admin2`, `neighborhood`, `locality`, `local_admin` |
||||
* `address` expands to `osmaddress`, `openaddresses` |
||||
* the name of one particular dataset, like `geoname` or `osmaddress` |
||||
* `bbox`: the bounding box from which you want all your results to come |
||||
* can be one of the following comma separated string values |
||||
* "southwest_lng,southwest_lat,northeast_lng,northeast_lat" `L.latLngBounds(southwestLatLng, northeastLatLng).toBBoxString()` |
||||
* bottom left lon, bottom left lat, top right lon, top right lat |
||||
* left, bottom, right, top |
||||
* min longitude, min latitude, max longitude, max latitude |
||||
* `details` (default: `true`): indicates if results should contain detailed. Valid values: |
||||
* `false`: results will only contain `id`, `layer`, and `text` properties |
||||
* `true`: all available properties will be included in results |
||||
|
||||
|
||||
## /search/coarse |
||||
|
||||
Like the `/search` endpoint, but performs a "coarse" search, meaning that it only searches administrative regions |
||||
(countries, states, counties, neighborhoods, etc.). |
||||
|
||||
#### Required Parameters |
||||
Same as `/search`. |
||||
|
||||
#### Optional Parameters |
||||
Same as `/search`. |
||||
|
||||
## /suggest |
||||
|
||||
The autocompletion endpoint that offers very fast response times; ideal for completing partial user input. Mixes |
||||
results from around the provided lat/lon coordinates and also from precision level 1 and 3. |
||||
|
||||
#### Required Parameters |
||||
* `text`: query string |
||||
* `lat`, `lon`: The latitude/longitude coordinates to bias results towards. |
||||
* `lat`/`lon` are currently **required** because of this [open issue](https://github.com/elasticsearch/elasticsearch/issues/6444) |
||||
|
||||
#### Optional Parameters |
||||
* `size` (default: `10`): number of results requested |
||||
* `layers` (default: `poi,admin,address`): datasets you wish to query |
||||
* `details` (default: `true`) |
||||
|
||||
## /suggest/coarse |
||||
|
||||
Only queries the admin layers. |
||||
|
||||
#### Required Parameters |
||||
Same as `/suggest`. |
||||
|
||||
#### Optional Parameters |
||||
Same as `/suggest`. |
||||
|
||||
|
||||
## /suggest/nearby |
||||
|
||||
Works as autocomplete for only the places located near a latitude/longitude; this endpoint is the same as `/suggest` |
||||
but the results are all from within 50 kilometers of the specified point. Unlike `/suggest`, `/suggest/nearby` does |
||||
not mix results from different precision levels (500km, 1000km etc from lat/lon). |
||||
|
||||
#### Required Parameters |
||||
Same as `/suggest`. |
||||
|
||||
#### Optional Parameters |
||||
Same as `/suggest`. |
||||
|
||||
## /reverse |
||||
|
||||
The reverse geocoding endpoint; matches a point on the planet to the name of that place. |
||||
|
||||
#### Required Parameters |
||||
* `lat`, `lon`: The coordinates of the point. |
||||
|
||||
#### Optional Parameters |
||||
* `layers` (default: `poi,admin,address`) |
||||
* `details` (default: `true`) |
||||
|
||||
|
||||
## /place |
||||
|
||||
The endpoint for retrieving one or more places with specific ids. These correspond |
||||
directly with Elasticsearch documents. |
||||
|
||||
#### Required Parameters |
||||
* one of `id` or `ids` |
||||
* `id`: |
||||
* unique id of the places to be retrieved |
||||
* should be in the form of type:id, for example: `geoname:4163334` |
||||
* `ids`: |
||||
* if multiple places are to be fetched in bulk, an array of ids |
||||
## DETAILED DOCUMENTATION COMING SOON! |
@ -0,0 +1,15 @@
|
||||
/* |
||||
* Mapping from data layers to type values |
||||
*/ |
||||
|
||||
module.exports = { |
||||
'venue': ['geoname','osmnode','osmway'], |
||||
'address': ['osmaddress','openaddresses'], |
||||
'country': ['admin0'], |
||||
'region': ['admin1'], |
||||
'county': ['admin2'], |
||||
'locality': ['locality'], |
||||
'localadmin': ['local_admin'], |
||||
'neighbourhood': ['neighborhood'], |
||||
'coarse': ['admin0','admin1','admin2','neighborhood','locality','local_admin'], |
||||
}; |
@ -1,31 +0,0 @@
|
||||
var isObject = require('is-object'); |
||||
var isTruthy = require('./_truthy'); |
||||
|
||||
// validate inputs, convert types and apply defaults
|
||||
function sanitize( req, default_value ){ |
||||
|
||||
req.clean = req.clean || {}; |
||||
var params= req.query; |
||||
|
||||
if (default_value === undefined) { |
||||
default_value = true; |
||||
} |
||||
|
||||
default_value = !!default_value; |
||||
|
||||
// ensure the input params are a valid object
|
||||
if( !isObject( params ) ){ |
||||
params = {}; |
||||
} |
||||
|
||||
if (params.details === undefined) { |
||||
req.clean.details = default_value; |
||||
} else { |
||||
req.clean.details = isTruthy(params.details); |
||||
} |
||||
|
||||
return {'error':false}; |
||||
|
||||
} |
||||
|
||||
module.exports = sanitize; |
@ -0,0 +1,50 @@
|
||||
var _ = require('lodash'); |
||||
|
||||
/** |
||||
* Returns sanitizer function for boolean flag parameters |
||||
* |
||||
* @param {string} paramName name of parameter being sanitized |
||||
* @param {boolean} defaultValue value to set variable to if none specified |
||||
* @returns {Function} |
||||
*/ |
||||
function setup( paramName, defaultValue ) { |
||||
return function( raw, clean ){ |
||||
return sanitize( raw, clean, { |
||||
paramName: paramName, |
||||
defaultValue: defaultValue |
||||
}); |
||||
}; |
||||
} |
||||
|
||||
/** |
||||
* Validate inputs, convert types and apply defaults |
||||
* |
||||
* @param {object} raw |
||||
* @param {object} clean |
||||
* @param {object} opts |
||||
* @returns {{errors: Array, warnings: Array}} |
||||
*/ |
||||
function sanitize( raw, clean, opts ){ |
||||
|
||||
// error & warning messages`1
|
||||
var messages = { errors: [], warnings: [] }; |
||||
|
||||
if( !_.isUndefined( raw[opts.paramName] ) ){ |
||||
clean[opts.paramName] = isTruthy( raw[opts.paramName] ); |
||||
} |
||||
else { |
||||
clean[opts.paramName] = opts.defaultValue; |
||||
} |
||||
return messages; |
||||
} |
||||
|
||||
/** |
||||
* Determine if param value is "truthy" |
||||
* @param {*} val |
||||
* @returns {boolean} |
||||
*/ |
||||
function isTruthy(val) { |
||||
return _.contains( ['true', '1', 1, true], val ); |
||||
} |
||||
|
||||
module.exports = setup; |
@ -1,39 +1,26 @@
|
||||
var isObject = require('is-object'); |
||||
|
||||
var geo_common = require ('./_geo_common'); |
||||
var LAT_LON_IS_REQUIRED = true, |
||||
CIRCLE_IS_REQUIRED = false, |
||||
CIRCLE_MUST_BE_COMPLETE = false; |
||||
|
||||
// validate inputs, convert types and apply defaults
|
||||
module.exports = function sanitize( req ){ |
||||
var clean = req.clean || {}; |
||||
var params = req.query; |
||||
var latlon_is_required = true; |
||||
var circle_is_required = false; |
||||
var circle_must_be_complete = false; |
||||
module.exports = function sanitize( raw, clean ){ |
||||
|
||||
// ensure the input params are a valid object
|
||||
if( !isObject( params ) ){ |
||||
params = {}; |
||||
} |
||||
|
||||
if( !isObject( params.point ) ){ |
||||
params.point = {}; |
||||
} |
||||
// error & warning messages
|
||||
var messages = { errors: [], warnings: [] }; |
||||
|
||||
try { |
||||
geo_common.sanitize_coord( 'lat', clean, params['point.lat'], latlon_is_required ); |
||||
geo_common.sanitize_coord( 'lon', clean, params['point.lon'], latlon_is_required ); |
||||
geo_common.sanitize_coord( 'lat', clean, raw['point.lat'], LAT_LON_IS_REQUIRED ); |
||||
geo_common.sanitize_coord( 'lon', clean, raw['point.lon'], LAT_LON_IS_REQUIRED ); |
||||
|
||||
// boundary.circle.* is not mandatory, and only specifying radius is fine,
|
||||
// as point.lat/lon will be used to fill those values by default
|
||||
geo_common.sanitize_boundary_circle( clean, params, circle_is_required, circle_must_be_complete); |
||||
geo_common.sanitize_boundary_circle( clean, raw, CIRCLE_IS_REQUIRED, CIRCLE_MUST_BE_COMPLETE); |
||||
} |
||||
catch (err) { |
||||
return { |
||||
'error': true, |
||||
'message': err.message |
||||
}; |
||||
messages.errors.push( err.message ); |
||||
} |
||||
|
||||
req.clean = clean; |
||||
|
||||
return { 'error': false }; |
||||
return messages; |
||||
}; |
||||
|
@ -1,38 +1,21 @@
|
||||
var isObject = require('is-object'); |
||||
|
||||
var geo_common = require ('./_geo_common'); |
||||
var LAT_LON_IS_REQUIRED = false; |
||||
|
||||
// validate inputs, convert types and apply defaults
|
||||
module.exports = function sanitize( req ){ |
||||
var clean = req.clean || {}; |
||||
var params = req.query; |
||||
var latlon_is_required = false; |
||||
|
||||
// ensure the input params are a valid object
|
||||
if( !isObject( params ) ){ |
||||
params = {}; |
||||
} |
||||
module.exports = function sanitize( raw, clean ){ |
||||
|
||||
if( !isObject( params.focus ) ){ |
||||
params.focus = {}; |
||||
} |
||||
|
||||
if( !isObject( params.focus.point ) ){ |
||||
params.focus.point = {}; |
||||
} |
||||
// error & warning messages
|
||||
var messages = { errors: [], warnings: [] }; |
||||
|
||||
try { |
||||
geo_common.sanitize_coord( 'lat', clean, params['focus.point.lat'], latlon_is_required ); |
||||
geo_common.sanitize_coord( 'lon', clean, params['focus.point.lon'], latlon_is_required ); |
||||
geo_common.sanitize_bbox(clean, params.bbox); |
||||
geo_common.sanitize_coord( 'lat', clean, raw['focus.point.lat'], LAT_LON_IS_REQUIRED ); |
||||
geo_common.sanitize_coord( 'lon', clean, raw['focus.point.lon'], LAT_LON_IS_REQUIRED ); |
||||
geo_common.sanitize_bbox(raw, clean); |
||||
} |
||||
catch (err) { |
||||
return { |
||||
'error': true, |
||||
'message': err.message |
||||
}; |
||||
messages.errors.push( err.message ); |
||||
} |
||||
|
||||
req.clean = clean; |
||||
|
||||
return { 'error': false }; |
||||
return messages; |
||||
}; |
||||
|
@ -1,52 +0,0 @@
|
||||
|
||||
var isObject = require('is-object'), |
||||
types = require('../query/types'), |
||||
get_layers = require('../helper/layers'); |
||||
|
||||
// validate inputs, convert types and apply defaults
|
||||
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 = {}; |
||||
} |
||||
|
||||
// default case (no layers specified in GET params)
|
||||
// don't even set the from_layers key in this case
|
||||
if('string' !== typeof params.layers || !params.layers.length){ |
||||
return { 'error': false }; |
||||
} |
||||
|
||||
// decide which layers can be queried
|
||||
var alias_layers = ['poi', 'admin', 'address']; |
||||
var alias_types = types.concat(alias_layers); |
||||
|
||||
// parse GET params
|
||||
var layers = params.layers.split(',').map( function( layer ){ |
||||
return layer.toLowerCase(); // lowercase inputs
|
||||
}); |
||||
|
||||
// validate layer names
|
||||
for( var x=0; x<layers.length; x++ ){ |
||||
if( -1 === alias_types.indexOf( layers[x] ) ){ |
||||
return { |
||||
'error': true, |
||||
'message': 'invalid param \'layers\': must be one or more of ' + alias_types.join(',') |
||||
}; |
||||
} |
||||
} |
||||
|
||||
// pass validated params to next middleware
|
||||
clean.types.from_layers = get_layers(layers); |
||||
req.clean = clean; |
||||
|
||||
return { 'error': false }; |
||||
|
||||
} |
||||
|
||||
// export function
|
||||
module.exports = sanitize; |
@ -1,31 +0,0 @@
|
||||
var isObject = require('is-object'); |
||||
var isTruthy = require('./_truthy'); |
||||
|
||||
// validate inputs, convert types and apply defaults
|
||||
function sanitize( req, default_value ){ |
||||
|
||||
req.clean = req.clean || {}; |
||||
var params= req.query; |
||||
|
||||
if (default_value === undefined) { |
||||
default_value = true; |
||||
} |
||||
|
||||
default_value = !!default_value; |
||||
|
||||
// ensure the input params are a valid object
|
||||
if( !isObject( params ) ){ |
||||
params = {}; |
||||
} |
||||
|
||||
if (params.private === undefined) { |
||||
req.clean.private = default_value; |
||||
} else { |
||||
req.clean.private = isTruthy(params.private); |
||||
} |
||||
|
||||
return {'error':false}; |
||||
|
||||
} |
||||
|
||||
module.exports = sanitize; |
@ -1,17 +0,0 @@
|
||||
function sanitize( req, sanitiser, cb ){ |
||||
|
||||
req.clean = req.clean || {}; |
||||
|
||||
for (var s in sanitiser) {
|
||||
var sanity = sanitiser[s](req); |
||||
if (sanity.error) { |
||||
return cb(sanity.message); |
||||
} |
||||
} |
||||
|
||||
return cb( undefined, req.clean ); |
||||
|
||||
} |
||||
|
||||
// export function
|
||||
module.exports = sanitize; |
@ -1,44 +1,45 @@
|
||||
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; |
||||
var check = require('check-types'), |
||||
sources_map = require( '../query/sources' ); |
||||
|
||||
req.clean.types = req.clean.types || {}; |
||||
var ALL_SOURCES = Object.keys(sources_map), |
||||
ALL_SOURCES_JOINED = ALL_SOURCES.join(','); |
||||
|
||||
// ensure the input params are a valid object
|
||||
if( !isObject( params ) ){ |
||||
params = {}; |
||||
} |
||||
function sanitize( raw, clean ) { |
||||
|
||||
// error & warning messages
|
||||
var messages = { errors: [], warnings: [] }; |
||||
|
||||
// init clean.types (if not already init)
|
||||
clean.types = clean.types || {}; |
||||
|
||||
// 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 }; |
||||
} |
||||
if( check.unemptyString( raw.source ) ){ |
||||
|
||||
var sources = params.source.split(','); |
||||
var sources = raw.source.split(','); |
||||
|
||||
var invalid_sources = sources.filter(function(source) { |
||||
return all_sources.indexOf(source) === -1; |
||||
}); |
||||
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(', ') |
||||
}; |
||||
} |
||||
if( invalid_sources.length > 0 ){ |
||||
invalid_sources.forEach( function( invalid ){ |
||||
messages.errors.push('\'' + invalid + '\' is an invalid source parameter. Valid options: ' + ALL_SOURCES_JOINED); |
||||
}); |
||||
} |
||||
|
||||
else { |
||||
var types = sources.reduce(function(acc, source) { |
||||
return acc.concat(sources_map[source]); |
||||
}, []); |
||||
|
||||
var types = sources.reduce(function(acc, source) { |
||||
return acc.concat(sources_map[source]); |
||||
}, []); |
||||
clean.types.from_source = types; |
||||
} |
||||
|
||||
req.clean.types.from_source = types; |
||||
} |
||||
|
||||
return { error: false }; |
||||
return messages; |
||||
} |
||||
|
||||
module.exports = sanitize; |
||||
|
@ -0,0 +1,81 @@
|
||||
|
||||
var _ = require('lodash'), |
||||
check = require('check-types'); |
||||
|
||||
function setup( paramName, targetMap ) { |
||||
return function( raw, clean ){ |
||||
return sanitize( raw, clean, { |
||||
paramName: paramName, |
||||
targetMap: targetMap, |
||||
targetMapKeysString: Object.keys(targetMap).join(',') |
||||
}); |
||||
}; |
||||
} |
||||
|
||||
function sanitize( raw, clean, opts ) { |
||||
|
||||
// error & warning messages
|
||||
var messages = { errors: [], warnings: [] }; |
||||
|
||||
// init clean.types
|
||||
clean.types = clean.types || {}; |
||||
|
||||
// the string of targets (comma delimeted)
|
||||
var targetsString = raw[opts.paramName]; |
||||
|
||||
// trim whitespace
|
||||
if( check.unemptyString( targetsString ) ){ |
||||
targetsString = targetsString.trim(); |
||||
|
||||
// param must be a valid non-empty string
|
||||
if( !check.unemptyString( targetsString ) ){ |
||||
messages.errors.push( |
||||
opts.paramName + ' parameter cannot be an empty string. Valid options: ' + opts.targetMapKeysString |
||||
); |
||||
} |
||||
else { |
||||
|
||||
// split string in to array and lowercase each target string
|
||||
var targets = targetsString.split(',').map( function( target ){ |
||||
return target.toLowerCase(); // lowercase inputs
|
||||
}); |
||||
|
||||
// emit an error for each target *not* present in the targetMap
|
||||
targets.filter( function( target ){ |
||||
return !opts.targetMap.hasOwnProperty(target); |
||||
}).forEach( function( target ){ |
||||
messages.errors.push( |
||||
'\'' + target + '\' is an invalid ' + opts.paramName + ' parameter. Valid options: ' + opts.targetMapKeysString |
||||
); |
||||
}); |
||||
|
||||
// only set types value when no error occured
|
||||
if( !messages.errors.length ){ |
||||
|
||||
// store the values under a new key as 'clean.types.from_*'
|
||||
var typesKey = 'from_' + opts.paramName; |
||||
|
||||
// ?
|
||||
clean.types[typesKey] = targets.reduce(function(acc, target) { |
||||
return acc.concat(opts.targetMap[target]); |
||||
}, []); |
||||
|
||||
// dedupe in case aliases expanded to common things or user typed in duplicates
|
||||
clean.types[typesKey] = _.unique(clean.types[typesKey]); |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
// string is empty
|
||||
else if( check.string( targetsString ) ){ |
||||
messages.errors.push( |
||||
opts.paramName + ' parameter cannot be an empty string. Valid options: ' + opts.targetMapKeysString |
||||
); |
||||
} |
||||
|
||||
|
||||
return messages; |
||||
} |
||||
|
||||
module.exports = setup; |
@ -1,9 +0,0 @@
|
||||
function isTruthy(val) { |
||||
if (typeof val === 'string') { |
||||
return ['true', '1', 'yes', 'y'].indexOf(val) !== -1; |
||||
} |
||||
|
||||
return val === 1 || val === true; |
||||
} |
||||
|
||||
module.exports = isTruthy; |
@ -0,0 +1,26 @@
|
||||
|
||||
function sanitize( req, sanitizers, cb ){ |
||||
|
||||
// init an object to store clean
|
||||
// (sanitized) input parameters
|
||||
req.clean = {}; |
||||
|
||||
// source of input parameters
|
||||
// (in this case from the GET querystring params)
|
||||
var params = req.query || {}; |
||||
|
||||
for (var s in sanitizers) { |
||||
var sanity = sanitizers[s]( params, req.clean ); |
||||
|
||||
// errors
|
||||
if( sanity.errors.length ){ |
||||
return cb( sanity.errors[0] ); |
||||
} |
||||
} |
||||
|
||||
return cb( undefined, req.clean ); |
||||
|
||||
} |
||||
|
||||
// export function
|
||||
module.exports = sanitize; |
@ -1,53 +0,0 @@
|
||||
var sanitize = require('../../../sanitiser/_details'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.sanitize_details = function(test, common) { |
||||
var invalid_values = [null, -1, 123, NaN, 'abc']; |
||||
invalid_values.forEach(function(detailsValue) { |
||||
test('invalid details param ' + detailsValue, function(t) { |
||||
var req = {query: { details: detailsValue }}; |
||||
sanitize(req); |
||||
t.equal(req.clean.details, false, 'default details set (to false)'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
var valid_values = ['true', true, 1, '1', 'yes', 'y']; |
||||
valid_values.forEach(function(detailsValue) { |
||||
test('valid details param ' + detailsValue, function(t) { |
||||
var req = {query: { details: detailsValue }}; |
||||
sanitize(req); |
||||
t.equal(req.clean.details, true, 'details set to true'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
var valid_false_values = ['false', false, 0, '0', 'no', 'n']; |
||||
valid_false_values.forEach(function(detailsValue) { |
||||
test('test setting false explicitly ' + detailsValue, function(t) { |
||||
var req = {query: { details: detailsValue }}; |
||||
sanitize(req); |
||||
t.equal(req.clean.details, false, 'details set to false'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
test('test default behavior', function(t) { |
||||
var req = {query: {}}; |
||||
sanitize(req); |
||||
t.equal(req.clean.details, true, 'details set to true'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('SANTIZE _details ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,64 @@
|
||||
var sanitizer = require('../../../sanitiser/_flag_bool'); |
||||
var sanitize = sanitizer('dirty_param', true); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.sanitize_private = function(test, common) { |
||||
var invalid_values = [null, -1, 123, NaN, 'abc']; |
||||
invalid_values.forEach(function (value) { |
||||
test('invalid dirty_param ' + value, function (t) { |
||||
var raw = {dirty_param: value}; |
||||
var clean = {}; |
||||
sanitize(raw, clean); |
||||
t.equal(clean.dirty_param, false, 'default clean value set (to false)'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
var valid_values = ['true', true, 1, '1']; |
||||
valid_values.forEach(function (value) { |
||||
test('valid dirty_param ' + value, function (t) { |
||||
var raw = {dirty_param: value}; |
||||
var clean = {}; |
||||
sanitize(raw, clean); |
||||
t.equal(clean.dirty_param, true, 'clean value set to true'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
var valid_false_values = ['false', false, 0, '0']; |
||||
valid_false_values.forEach(function (value) { |
||||
test('test setting false explicitly ' + value, function (t) { |
||||
var raw = {dirty_param: value}; |
||||
var clean = {}; |
||||
sanitize(raw, clean); |
||||
t.equal(clean.dirty_param, false, 'clean value set to false'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.validate_default_behavior = function(test, common) { |
||||
var default_values = [true, false, 'foo']; |
||||
default_values.forEach(function (defaultValue) { |
||||
test('test default behavior: ' + defaultValue, function (t) { |
||||
var sanitize_true = sanitizer('foo_bar', defaultValue); |
||||
var raw = {}; |
||||
var clean = {}; |
||||
sanitize_true(raw, clean); |
||||
t.equal(clean.foo_bar, defaultValue, 'foo_bar set to ' + defaultValue); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('SANTIZE _flag_bool: ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,120 @@
|
||||
|
||||
var sanitize = require('../../../sanitiser/_targets')('layers', require('../../../query/layers')); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.sanitize_layers = function(test, common) { |
||||
test('unspecified', function(t) { |
||||
var messages = sanitize({ layers: undefined }, {}); |
||||
t.equal(messages.errors.length, 0, 'no errors'); |
||||
t.end(); |
||||
}); |
||||
test('invalid layer', function(t) { |
||||
var raw = { layers: 'test_layer' }; |
||||
var clean = {}; |
||||
|
||||
var messages = sanitize(raw, clean); |
||||
|
||||
var msg = ' is an invalid layers parameter. Valid options: '; |
||||
t.equal(messages.errors.length, 1, 'errors set'); |
||||
t.true(messages.errors[0].match(msg), 'invalid layer requested'); |
||||
t.true(messages.errors[0].length > msg.length, 'invalid error message'); |
||||
t.end(); |
||||
}); |
||||
test('venue (alias) layer', function(t) { |
||||
var venue_layers = ['geoname','osmnode','osmway']; |
||||
var raw = { layers: 'venue' }; |
||||
var clean = {}; |
||||
|
||||
sanitize(raw, clean); |
||||
|
||||
t.deepEqual(clean.types.from_layers, venue_layers, 'venue layers set'); |
||||
t.end(); |
||||
}); |
||||
test('coarse (alias) layer', function(t) { |
||||
var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin']; |
||||
var raw = { layers: 'coarse' }; |
||||
var clean = {}; |
||||
|
||||
sanitize(raw, clean); |
||||
|
||||
t.deepEqual(clean.types.from_layers, admin_layers, 'coarse layers set'); |
||||
t.end(); |
||||
}); |
||||
test('address (alias) layer', function(t) { |
||||
var address_layers = ['osmaddress','openaddresses']; |
||||
var raw = { layers: 'address' }; |
||||
var clean = {}; |
||||
|
||||
sanitize(raw, clean); |
||||
|
||||
t.deepEqual(clean.types.from_layers, address_layers, 'address layers set'); |
||||
t.end(); |
||||
}); |
||||
test('venue alias layer plus regular layers', function(t) { |
||||
var venue_layers = ['geoname','osmnode','osmway']; |
||||
var reg_layers = ['admin0', 'admin1']; |
||||
var raw = { layers: 'venue,country,region' }; |
||||
var clean = {}; |
||||
|
||||
sanitize(raw, clean); |
||||
|
||||
t.deepEqual(clean.types.from_layers, venue_layers.concat(reg_layers), 'venue + regular layers'); |
||||
t.end(); |
||||
}); |
||||
test('coarse alias layer plus regular layers', function(t) { |
||||
var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin']; |
||||
var reg_layers = ['geoname', 'osmnode', 'osmway']; |
||||
|
||||
var raw = { layers: 'coarse,venue,country' }; |
||||
var clean = {}; |
||||
|
||||
sanitize(raw, clean); |
||||
|
||||
t.deepEqual(clean.types.from_layers, admin_layers.concat(reg_layers), 'coarse + regular layers set'); |
||||
t.end(); |
||||
}); |
||||
test('address alias layer plus regular layers', function(t) { |
||||
var address_layers = ['osmaddress','openaddresses']; |
||||
var reg_layers = ['admin0', 'locality']; |
||||
|
||||
var raw = { layers: 'address,country,locality' }; |
||||
var clean = {}; |
||||
|
||||
sanitize(raw, clean); |
||||
|
||||
t.deepEqual(clean.types.from_layers, address_layers.concat(reg_layers), 'address + regular layers set'); |
||||
t.end(); |
||||
}); |
||||
test('alias layer plus regular layers (no duplicates)', function(t) { |
||||
var venue_layers = ['geoname','osmnode','osmway','admin0']; |
||||
var raw = { layers: 'venue,country' }; |
||||
var clean = {}; |
||||
|
||||
sanitize(raw, clean); |
||||
|
||||
t.deepEqual(clean.types.from_layers, venue_layers, 'venue 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']; |
||||
var raw = { layers: 'venue,coarse' }; |
||||
var clean = {}; |
||||
|
||||
sanitize(raw, clean); |
||||
|
||||
t.deepEqual(clean.types.from_layers, alias_layers, 'all layers found (no duplicates)'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('SANTIZE _layers ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,132 @@
|
||||
var sanitize = require( '../../../sanitiser/_targets' )('sources', require('../../../query/sources')); |
||||
|
||||
var success_messages = { error: false }; |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.no_sources = function(test, common) { |
||||
test('sources is not set', function(t) { |
||||
var req = { |
||||
query: { }, |
||||
clean: { } |
||||
}; |
||||
|
||||
var messages = sanitize(req.query, req.clean); |
||||
|
||||
t.deepEqual(req.clean.types, {}, 'clean.types should be empty object'); |
||||
t.deepEqual(messages.errors, [], 'no error returned'); |
||||
t.deepEqual(messages.warnings, [], 'no warnings returned'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('source is empty string', function(t) { |
||||
var req = { |
||||
query: { |
||||
sources: '' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
|
||||
var expected_error = 'sources parameter cannot be an empty string. ' + |
||||
'Valid options: gn,geonames,oa,openaddresses,qs,quattroshapes,osm,openstreetmap'; |
||||
|
||||
var messages = sanitize(req.query, req.clean); |
||||
|
||||
t.deepEqual(req.clean.types, {}, 'clean.types should be empty object'); |
||||
t.deepEqual(messages.errors.length, 1, 'error returned'); |
||||
t.deepEqual(messages.errors[0], expected_error, 'error returned'); |
||||
t.deepEqual(messages.warnings, [], 'no warnings returned'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.valid_sources = function(test, common) { |
||||
test('geonames source', function(t) { |
||||
var req = { |
||||
query: { |
||||
sources: 'geonames' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
|
||||
var messages = sanitize(req.query, req.clean); |
||||
|
||||
t.deepEqual(req.clean.types, { from_sources: ['geoname'] }, 'clean.types should contain from_source entry with geonames'); |
||||
t.deepEqual(messages.errors, [], 'no error returned'); |
||||
t.deepEqual(messages.warnings, [], 'no warnings returned'); |
||||
|
||||
t.end(); |
||||
}); |
||||
|
||||
test('openstreetmap source', function(t) { |
||||
var req = { |
||||
query: { |
||||
sources: 'openstreetmap' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
var expected_types = { |
||||
from_sources: ['osmaddress', 'osmnode', 'osmway'] |
||||
}; |
||||
|
||||
var messages = sanitize(req.query, req.clean); |
||||
|
||||
t.deepEqual(req.clean.types, expected_types, 'clean.types should contain from_source entry with multiple entries for openstreetmap'); |
||||
t.deepEqual(messages.errors, [], 'no error returned'); |
||||
t.deepEqual(messages.warnings, [], 'no warnings returned'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('multiple sources', function(t) { |
||||
var req = { |
||||
query: { |
||||
sources: 'openstreetmap,openaddresses' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
var expected_types = { |
||||
from_sources: ['osmaddress', 'osmnode', 'osmway', 'openaddresses'] |
||||
}; |
||||
|
||||
var messages = sanitize(req.query, req.clean); |
||||
|
||||
t.deepEqual(req.clean.types, expected_types, |
||||
'clean.types should contain from_source entry with multiple entries for openstreetmap and openadresses'); |
||||
t.deepEqual(messages.errors, [], 'no error returned'); |
||||
t.deepEqual(messages.warnings, [], 'no warnings returned'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.invalid_sources = function(test, common) { |
||||
test('geonames source', function(t) { |
||||
var req = { |
||||
query: { |
||||
sources: 'notasource' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
var expected_messages = { |
||||
errors: [ |
||||
'\'notasource\' is an invalid sources parameter. Valid options: gn,geonames,oa,openaddresses,qs,quattroshapes,osm,openstreetmap' |
||||
], |
||||
warnings: [] |
||||
}; |
||||
|
||||
var messages = sanitize(req.query, req.clean); |
||||
|
||||
t.deepEqual(messages, expected_messages, '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 _sources ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue