Browse Source

Merge pull request #241 from pelias/private-param-235

Private param 235
pull/255/head
Diana Shkolnikov 9 years ago
parent
commit
5274bd64d4
  1. 28
      helper/geojsonify.js
  2. 2
      middleware/geocodeJSON.js
  3. 101
      public/apiDoc.md
  4. 50
      sanitiser/_flag_bool.js
  5. 2
      sanitiser/place.js
  6. 2
      sanitiser/reverse.js
  7. 2
      sanitiser/search.js
  8. 57
      test/unit/controller/place.js
  9. 79
      test/unit/controller/search.js
  10. 89
      test/unit/helper/geojsonify.js
  11. 3
      test/unit/query/search.js
  12. 1
      test/unit/run.js
  13. 64
      test/unit/sanitiser/_flag_bool.js
  14. 53
      test/unit/sanitiser/_private.js
  15. 5
      test/unit/sanitiser/_text.js
  16. 31
      test/unit/sanitiser/_truthy.js
  17. 36
      test/unit/sanitiser/place.js
  18. 46
      test/unit/sanitiser/reverse.js
  19. 60
      test/unit/sanitiser/search.js

28
helper/geojsonify.js

@ -4,7 +4,7 @@ var GeoJSON = require('geojson'),
outputGenerator = require('./outputGenerator'),
logger = require('pelias-logger').get('api');
// Properties to be copied when details=true
// Properties to be copied
var DETAILS_PROPS = [
'housenumber',
'street',
@ -78,14 +78,11 @@ function lookupLayer(src) {
return src._type;
}
function geojsonifyPlaces( docs, params ){
var details = params ? params.details : {};
details = details === true || details === 1;
function geojsonifyPlaces( docs ){
// flatten & expand data for geojson conversion
var geodata = docs
.map(geojsonifyPlace.bind(null, details))
.map(geojsonifyPlace)
.filter( function( doc ){
return !!doc;
});
@ -99,7 +96,7 @@ function geojsonifyPlaces( docs, params ){
return geojson;
}
function geojsonifyPlace(details, place) {
function geojsonifyPlace(place) {
// something went very wrong
if( !place || !place.hasOwnProperty( 'center_point' ) ) {
@ -109,7 +106,7 @@ function geojsonifyPlace(details, place) {
var output = {};
addMetaData(place, output);
addDetails(details, place, output);
addDetails(place, output);
addLabel(place, output);
@ -122,20 +119,17 @@ function geojsonifyPlace(details, place) {
}
/**
* Add details properties when needed
* Add details properties
*
* @param {boolean} details
* @param {object} src
* @param {object} dst
*/
function addDetails(details, src, dst) {
if (details) {
// map name
if( !src.name || !src.name.default ) { return warning(src); }
dst.name = src.name.default;
function addDetails(src, dst) {
// map name
if( !src.name || !src.name.default ) { return warning(src); }
dst.name = src.name.default;
copyProperties(src, DETAILS_PROPS, dst);
}
copyProperties(src, DETAILS_PROPS, dst);
}
/**

2
middleware/geocodeJSON.js

@ -49,7 +49,7 @@ function convertToGeocodeJSON(peliasConfig, req, next) {
req.results.geojson.geocoding.timestamp = new Date().getTime();
// convert docs to geojson and merge with geocoding block
extend(req.results.geojson, geojsonify(req.results.data, req.clean));
extend(req.results.geojson, geojsonify(req.results.data));
next();
}

101
public/apiDoc.md

@ -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!

50
sanitiser/_flag_bool.js

@ -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;

2
sanitiser/place.js

@ -2,7 +2,7 @@
var sanitizeAll = require('../sanitiser/sanitizeAll'),
sanitizers = {
id: require('../sanitiser/_id'),
details: require('../sanitiser/_details')
private: require('../sanitiser/_flag_bool')('private', false)
};
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); };

2
sanitiser/reverse.js

@ -4,7 +4,7 @@ var sanitizeAll = require('../sanitiser/sanitizeAll'),
layers: require('../sanitiser/_targets')('layers', require('../query/layers')),
sources: require('../sanitiser/_targets')('sources', require('../query/sources')),
size: require('../sanitiser/_size'),
details: require('../sanitiser/_details'),
private: require('../sanitiser/_flag_bool')('private', false),
geo_reverse: require('../sanitiser/_geo_reverse'),
categories: require('../sanitiser/_categories')
};

2
sanitiser/search.js

@ -5,7 +5,7 @@ var sanitizeAll = require('../sanitiser/sanitizeAll'),
size: require('../sanitiser/_size'),
layers: require('../sanitiser/_targets')('layers', require( '../query/layers' )),
sources: require('../sanitiser/_targets')('sources', require( '../query/sources' )),
details: require('../sanitiser/_details'),
private: require('../sanitiser/_flag_bool')('private', false),
geo_search: require('../sanitiser/_geo_search'),
categories: require('../sanitiser/_categories')
};

57
test/unit/controller/place.js

@ -63,63 +63,6 @@ module.exports.tests.functional_success = function(test, common) {
};
controller( { clean: { ids: [ {'id' : 123, 'type': 'a' } ] } }, res, next );
});
var detailed_expectation = [{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ -50.5, 100.1 ]
},
properties: {
id: 'myid1',
layer: 'mytype1',
name: 'test name1',
admin0: 'country1',
admin1: 'state1',
admin2: 'city1',
text: 'test name1, city1, state1'
}
}, {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ -51.5, 100.2 ]
},
properties: {
id: 'myid2',
layer: 'mytype2',
name: 'test name2',
admin0: 'country2',
admin1: 'state2',
admin2: 'city2',
text: 'test name2, city2, state2'
}
}];
test('functional success (with details)', function(t) {
var backend = mockBackend( 'client/mget/ok/1', function( cmd ){
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'correct backend command');
});
var controller = setup( backend );
var res = {
status: function( code ){
t.equal(code, 200, 'status set');
return res;
},
json: function( json ){
t.equal(typeof json, 'object', 'returns json');
t.equal(typeof json.date, 'number', 'date set');
t.equal(json.type, 'FeatureCollection', 'valid geojson');
t.true(Array.isArray(json.features), 'features is array');
t.deepEqual(json.features, detailed_expectation, 'values correctly mapped along with details');
}
};
var next = function next() {
t.equal(arguments.length, 0, 'next was called without error');
t.end();
};
controller( { clean: { ids: [ {'id' : 123, 'type': 'a' } ], details: true } }, res, next );
});
};
// functionally test controller (backend failure)

79
test/unit/controller/search.js

@ -21,7 +21,7 @@ module.exports.tests.functional_success = function(test, common) {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ -50.5, 100.1 ]
coordinates: [-50.5, 100.1]
},
properties: {
id: 'myid1',
@ -32,7 +32,7 @@ module.exports.tests.functional_success = function(test, common) {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ -51.5, 100.2 ]
coordinates: [-51.5, 100.2]
},
properties: {
id: 'myid2',
@ -41,17 +41,21 @@ module.exports.tests.functional_success = function(test, common) {
}
}];
test('functional success', function(t) {
var backend = mockBackend( 'client/search/ok/1', function( cmd ){
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command');
test('functional success', function (t) {
var backend = mockBackend('client/search/ok/1', function (cmd) {
t.deepEqual(cmd, {
body: {a: 'b'},
index: 'pelias',
searchType: 'dfs_query_then_fetch'
}, 'correct backend command');
});
var controller = setup( backend, mockQuery() );
var controller = setup(backend, mockQuery());
var res = {
status: function( code ){
status: function (code) {
t.equal(code, 200, 'status set');
return res;
},
json: function( json ){
json: function (json) {
t.equal(typeof json, 'object', 'returns json');
t.equal(typeof json.date, 'number', 'date set');
t.equal(json.type, 'FeatureCollection', 'valid geojson');
@ -63,64 +67,7 @@ module.exports.tests.functional_success = function(test, common) {
t.equal(arguments.length, 0, 'next was called without error');
t.end();
};
controller( { clean: { a: 'b' } }, res, next );
});
var detailed_expectation = [{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ -50.5, 100.1 ]
},
properties: {
id: 'myid1',
layer: 'mytype1',
name: 'test name1',
admin0: 'country1',
admin1: 'state1',
admin2: 'city1',
text: 'test name1, city1, state1'
}
}, {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ -51.5, 100.2 ]
},
properties: {
id: 'myid2',
layer: 'mytype2',
name: 'test name2',
admin0: 'country2',
admin1: 'state2',
admin2: 'city2',
text: 'test name2, city2, state2'
}
}];
test('functional success (with details)', function(t) {
var backend = mockBackend( 'client/search/ok/1', function( cmd ){
t.deepEqual(cmd, { body: { a: 'b', details: true }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command');
});
var controller = setup( backend, mockQuery() );
var res = {
status: function( code ){
t.equal(code, 200, 'status set');
return res;
},
json: function( json ){
t.equal(typeof json, 'object', 'returns json');
t.equal(typeof json.date, 'number', 'date set');
t.equal(json.type, 'FeatureCollection', 'valid geojson');
t.true(Array.isArray(json.features), 'features is array');
t.deepEqual(json.features, detailed_expectation, 'values with details correctly mapped');
}
};
var next = function next() {
t.equal(arguments.length, 0, 'next was called without error');
t.end();
};
controller( { clean: { a: 'b', details: true } }, res, next );
controller({clean: {a: 'b'}}, res, next);
});
};

89
test/unit/helper/geojsonify.js

@ -6,7 +6,7 @@ module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
test('valid interface .search()', function(t) {
t.equal(typeof geojsonify.search, 'function', 'search is a function');
t.equal(geojsonify.search.length, 2, 'accepts x arguments');
t.equal(geojsonify.search.length, 1, 'accepts x arguments');
t.end();
});
};
@ -28,7 +28,7 @@ module.exports.tests.earth = function(test, common) {
test('earth', function(t) {
t.doesNotThrow(function(){
geojsonify.search( earth, { details: true } );
geojsonify.search( earth );
});
t.end();
});
@ -213,92 +213,11 @@ module.exports.tests.search = function(test, common) {
]
};
var truthy_params = [true, 1];
test('geojsonify.search(doc, true) with details', function(t) {
var json = geojsonify.search( input, { details: true } );
t.deepEqual(json, expected, 'all docs (with details) mapped');
t.end();
});
truthy_params.forEach(function(details) {
test('geojsonify.search(doc, '+ details +') with details', function(t) {
var json = geojsonify.search( input, { details: details } );
t.deepEqual(json, expected, 'all docs (with details) mapped');
t.end();
});
});
var no_details_expected = {
'type': 'FeatureCollection',
'bbox': [ -73.985656, 40.748432, -0.101795, 51.5337144 ],
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [
-0.1069716,
51.5337144
]
},
'properties': {
'id': 'id1',
'layer': 'type1',
'source': 'type1',
'label': '\'Round Midnight Jazz and Blues Bar, test3, Angel'
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [
-0.101795,
51.517806
]
},
'properties': {
'id': 'id2',
'layer': 'type2',
'source': 'type2',
'label': 'Blues Cafe, test3, Smithfield'
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [
-73.985656,
40.748432
]
},
'properties': {
'id': '34633854',
'layer': 'venue',
'source': 'osm',
'label': 'Empire State Building, Manhattan, NY'
}
}
]
};
test('geojsonify.search(doc) with no details (default)', function(t) {
test('geojsonify.search(doc)', function(t) {
var json = geojsonify.search( input );
t.deepEqual(json, no_details_expected, 'all docs (with no details) mapped');
t.deepEqual(json, expected, 'all docs mapped');
t.end();
});
var falsy_params = [false, undefined, null, 0, -1, 123, 'abc'];
falsy_params.forEach(function(details) {
test('geojsonify.search(doc, '+ details +') with no details', function(t) {
var json = geojsonify.search( input, { details: details } );
t.deepEqual(json, no_details_expected, 'all docs (with no details) mapped');
t.end();
});
});
};
module.exports.all = function (tape, common) {

3
test/unit/query/search.js

@ -89,7 +89,6 @@ module.exports.tests.query = function(test, common) {
layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
size: 10,
details: true,
parsed_text: parser.get_parsed_address(address),
});
@ -107,7 +106,6 @@ module.exports.tests.query = function(test, common) {
layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
size: 10,
details: true,
parsed_text: parser.get_parsed_address(partial_address),
});
@ -125,7 +123,6 @@ module.exports.tests.query = function(test, common) {
layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
size: 10,
details: true,
parsed_text: parser.get_parsed_address(partial_address),
});

1
test/unit/run.js

@ -8,6 +8,7 @@ var tests = [
require('./controller/search'),
require('./service/mget'),
require('./service/search'),
require('./sanitiser/_flag_bool'),
require('./sanitiser/_sources'),
require('./sanitiser/search'),
require('./sanitiser/_layers'),

64
test/unit/sanitiser/_flag_bool.js

@ -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);
}
};

53
test/unit/sanitiser/_private.js

@ -0,0 +1,53 @@
var sanitize = require('../../../sanitiser/_private');
module.exports.tests = {};
module.exports.tests.sanitize_private = function(test, common) {
var invalid_values = [null, -1, 123, NaN, 'abc'];
invalid_values.forEach(function(privateValue) {
test('invalid private param ' + privateValue, function(t) {
var req = {query: { private: privateValue }};
sanitize(req);
t.equal(req.clean.private, false, 'default private set (to false)');
t.end();
});
});
var valid_values = ['true', true, 1, '1', 'yes', 'y'];
valid_values.forEach(function(privateValue) {
test('valid private param ' + privateValue, function(t) {
var req = {query: { private: privateValue }};
sanitize(req);
t.equal(req.clean.private, true, 'private set to true');
t.end();
});
});
var valid_false_values = ['false', false, 0, '0', 'no', 'n'];
valid_false_values.forEach(function(privateValue) {
test('test setting false explicitly ' + privateValue, function(t) {
var req = {query: { private: privateValue }};
sanitize(req);
t.equal(req.clean.private, false, 'private set to false');
t.end();
});
});
test('test default behavior', function(t) {
var req = {query: {}};
sanitize(req);
t.equal(req.clean.private, true, 'private set to true');
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANTIZE _private ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

5
test/unit/sanitiser/_text.js

@ -1,17 +1,12 @@
var text = require('../../../sanitiser/_text'),
parser = require('../../../helper/query_parser'),
delim = ',',
defaultError = 'invalid param \'text\': text length, must be >0',
allLayers = [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
nonAddressLayers = [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin' ],
defaultParsed= { },
defaultClean = { text: 'test',
layers: allLayers,
size: 10,
details: true,
parsed_text: defaultParsed,
lat:0,
lon:0

31
test/unit/sanitiser/_truthy.js

@ -0,0 +1,31 @@
var isTruthy = require('../../../sanitiser/_truthy');
module.exports.tests = {};
module.exports.tests.sanitize_truthy = function(test, common) {
var valid_values = ['true', true, 1, '1', 'yes', 'y'];
valid_values.forEach(function(value) {
test('truthy value ' + value, function(t) {
t.equal(isTruthy(value), true, 'returns true');
t.end();
});
});
var valid_false_values = ['false', false, 0, '0', 'no', 'n', null, -1, 123, NaN, 'abc'];
valid_false_values.forEach(function(value) {
test('falsey value ' + value, function(t) {
t.equal(isTruthy(value), false, 'returns false');
t.end();
});
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANTIZE _truthy ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

36
test/unit/sanitiser/place.js

@ -10,7 +10,7 @@ var place = require('../../../sanitiser/place'),
defaultMissingTypeError = function(input) {
var type = input.split(delimiter)[0];
return type + ' is invalid. It must be one of these values - [' + types.join(', ') + ']'; },
defaultClean = { ids: [ { id: '123', type: 'geoname' } ], details: true },
defaultClean = { ids: [ { id: '123', type: 'geoname' } ], private: false },
sanitize = function(query, cb) { _sanitize({'query':query}, cb); },
inputs = {
valid: [ 'geoname:1', 'osmnode:2', 'admin0:53', 'osmway:44', 'geoname:5' ],
@ -56,7 +56,7 @@ module.exports.tests.sanitize_id = function(test, common) {
test('valid input', function(t) {
inputs.valid.forEach( function( input ){
var input_parts = input.split(delimiter);
var expected = { ids: [ { id: input_parts[1], type: input_parts[0] } ], details: true };
var expected = { ids: [ { id: input_parts[1], type: input_parts[0] } ], private: false };
sanitize({ id: input }, function( err, clean ){
t.equal(err, undefined, 'no error (' + input + ')' );
t.deepEqual(clean, expected, 'clean set correctly (' + input + ')');
@ -94,7 +94,7 @@ module.exports.tests.sanitize_ids = function(test, common) {
var input_parts = input.split(delimiter);
expected.ids.push({ id: input_parts[1], type: input_parts[0] });
});
expected.details = true;
expected.private = false;
sanitize({ id: inputs.valid }, function( err, clean ){
t.equal(err, undefined, 'no error' );
t.deepEqual(clean, expected, 'clean set correctly');
@ -103,32 +103,32 @@ module.exports.tests.sanitize_ids = function(test, common) {
});
};
module.exports.tests.sanitize_details = function(test, common) {
module.exports.tests.sanitize_private = function(test, common) {
var invalid_values = [null, -1, 123, NaN, 'abc'];
invalid_values.forEach(function(details) {
test('invalid details param ' + details, function(t) {
sanitize({ id:'geoname:123', details: details }, function( err, clean ){
t.equal(clean.details, false, 'default details set (to false)');
invalid_values.forEach(function(value) {
test('invalid private param ' + value, function(t) {
sanitize({ id:'geoname:123', 'private': value}, function( err, clean ){
t.equal(clean.private, false, 'default private set (to false)');
t.end();
});
});
});
var valid_values = ['true', true, 1];
valid_values.forEach(function(details) {
test('valid details param ' + details, function(t) {
sanitize({ id:'geoname:123', details: details }, function( err, clean ){
t.equal(clean.details, true, 'details set to true');
valid_values.forEach(function(value) {
test('valid private param ' + value, function(t) {
sanitize({ id:'geoname:123', 'private': value }, function( err, clean ){
t.equal(clean.private, true, 'private set to true');
t.end();
});
});
});
var valid_false_values = ['false', false, 0];
valid_false_values.forEach(function(details) {
test('test setting false explicitly ' + details, function(t) {
sanitize({ id:'geoname:123', details: details }, function( err, clean ){
t.equal(clean.details, false, 'details set to false');
valid_false_values.forEach(function(value) {
test('test setting false explicitly ' + value, function(t) {
sanitize({ id:'geoname:123', 'private': value }, function( err, clean ){
t.equal(clean.private, false, 'private set to false');
t.end();
});
});
@ -136,14 +136,14 @@ module.exports.tests.sanitize_details = function(test, common) {
test('test default behavior', function(t) {
sanitize({ id:'geoname:123' }, function( err, clean ){
t.equal(clean.details, true, 'details set to true');
t.equal(clean.private, false, 'private set to false');
t.end();
});
});
};
module.exports.tests.de_dupe = function(test, common) {
var expected = { ids: [ { id: '1', type: 'geoname' }, { id: '2', type: 'osmnode' } ], details: true };
var expected = { ids: [ { id: '1', type: 'geoname' }, { id: '2', type: 'osmnode' } ], private: false };
test('duplicate ids', function(t) {
sanitize( { id: ['geoname:1', 'osmnode:2', 'geoname:1'] }, function( err, clean ){
t.equal(err, undefined, 'no error' );

46
test/unit/sanitiser/reverse.js

@ -8,7 +8,7 @@ var reverse = require('../../../sanitiser/reverse'),
},
lon: 0,
size: 10,
details: true,
private: false,
categories: []
},
sanitize = function(query, cb) { _sanitize({'query':query}, cb); };
@ -30,7 +30,7 @@ module.exports.tests.interface = function(test, common) {
module.exports.tests.sanitisers = function(test, common) {
test('check sanitiser list', function (t) {
var expected = ['layers', 'sources', 'size', 'details', 'geo_reverse', 'categories'];
var expected = ['layers', 'sources', 'size', 'private', 'geo_reverse', 'categories'];
t.deepEqual(Object.keys(reverse.sanitiser_list), expected);
t.end();
});
@ -122,41 +122,41 @@ module.exports.tests.sanitize_size = function(test, common) {
});
};
module.exports.tests.sanitize_details = function(test, common) {
module.exports.tests.sanitize_private = function(test, common) {
var invalid_values = [null, -1, 123, NaN, 'abc'];
invalid_values.forEach(function(details) {
test('invalid details param ' + details, function(t) {
sanitize({ 'point.lat': 0, 'point.lon': 0, details: details }, function( err, clean ){
t.equal(clean.details, false, 'details set to false');
invalid_values.forEach(function(value) {
test('invalid private param ' + value, function(t) {
sanitize({ 'point.lat': 0, 'point.lon': 0, 'private': value}, function( err, clean ){
t.equal(clean.private, false, 'default private set (to false)');
t.end();
});
});
});
var valid_values = [true, 'true', 1, '1'];
valid_values.forEach(function(details) {
test('valid details param ' + details, function(t) {
sanitize({ 'point.lat': 0, 'point.lon': 0, details: details }, function( err, clean ){
t.equal(clean.details, true, 'details set to true');
var valid_values = ['true', true, 1, '1'];
valid_values.forEach(function(value) {
test('valid private param ' + value, function(t) {
sanitize({ 'point.lat': 0, 'point.lon': 0, 'private': value }, function( err, clean ){
t.equal(clean.private, true, 'private set to true');
t.end();
});
});
});
test('test default behavior', function(t) {
sanitize({ 'point.lat': 0, 'point.lon': 0 }, function( err, clean ){
t.equal(clean.details, true, 'details set to true');
t.end();
var valid_false_values = ['false', false, 0];
valid_false_values.forEach(function(value) {
test('test setting false explicitly ' + value, function(t) {
sanitize({ 'point.lat': 0, 'point.lon': 0, 'private': value }, function( err, clean ){
t.equal(clean.private, false, 'private set to false');
t.end();
});
});
});
var valid_false_values = ['false', false, 0, '0'];
valid_false_values.forEach(function(details) {
test('test setting false explicitly ' + details, function(t) {
sanitize({ 'point.lat': 0, 'point.lon': 0, details: details }, function( err, clean ){
t.equal(clean.details, false, 'details set to false');
t.end();
});
test('test default behavior', function(t) {
sanitize({ 'point.lat': 0, 'point.lon': 0 }, function( err, clean ){
t.equal(clean.private, false, 'private set to false');
t.end();
});
});
};

60
test/unit/sanitiser/search.js

@ -10,7 +10,6 @@ var search = require('../../../sanitiser/search'),
types: {
},
size: 10,
details: true,
parsed_text: defaultParsed,
},
sanitize = function(query, cb) { _sanitize({'query':query}, cb); };
@ -32,7 +31,7 @@ module.exports.tests.interface = function(test, common) {
module.exports.tests.sanitisers = function(test, common) {
test('check sanitiser list', function (t) {
var expected = ['text', 'size', 'layers', 'sources', 'details', 'geo_search', 'categories' ];
var expected = ['text', 'size', 'layers', 'sources', 'private', 'geo_search', 'categories' ];
t.deepEqual(Object.keys(search.sanitiser_list), expected);
t.end();
});
@ -77,7 +76,7 @@ module.exports.tests.sanitise_valid_text = function(test, common) {
module.exports.tests.sanitize_text_with_delim = function(test, common) {
var texts = [ 'a,bcd', '123 main st, admin1', ',,,', ' ' ];
test('valid texts with a comma', function(t) {
test('valid texts with a comma', function(t) {
texts.forEach( function( text ){
sanitize({ text: text }, function( err, clean ){
var expected = JSON.parse(JSON.stringify( defaultClean ));
@ -93,6 +92,33 @@ module.exports.tests.sanitize_text_with_delim = function(test, common) {
});
};
module.exports.tests.sanitize_private_no_value = function(test, common) {
test('default private should be set to true', function(t) {
sanitize({ text: 'test' }, function( err, clean ){
t.equal(clean.private, false, 'private set to false');
});
t.end();
});
};
module.exports.tests.sanitize_private_explicit_true_value = function(test, common) {
test('explicit private should be set to true', function(t) {
sanitize({ text: 'test', private: true }, function( err, clean ){
t.equal(clean.private, true, 'private set to true');
});
t.end();
});
};
module.exports.tests.sanitize_private_explicit_false_value = function(test, common) {
test('explicit private should be set to false', function(t) {
sanitize({ text: 'test', private: false }, function( err, clean ){
t.equal(clean.private, false, 'private set to false');
});
t.end();
});
};
module.exports.tests.sanitize_lat = function(test, common) {
var lats = {
invalid: [],
@ -241,32 +267,32 @@ module.exports.tests.sanitize_size = function(test, common) {
});
};
module.exports.tests.sanitize_details = function(test, common) {
module.exports.tests.sanitize_private = function(test, common) {
var invalid_values = [null, -1, 123, NaN, 'abc'];
invalid_values.forEach(function(details) {
test('invalid details param ' + details, function(t) {
sanitize({ text: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){
t.equal(clean.details, false, 'default details set (to false)');
invalid_values.forEach(function(value) {
test('invalid private param ' + value, function(t) {
sanitize({ text: 'test', lat: 0, lon: 0, 'private': value }, function( err, clean ){
t.equal(clean.private, false, 'default private set (to false)');
t.end();
});
});
});
var valid_values = ['true', true, 1, '1'];
valid_values.forEach(function(details) {
test('valid details param ' + details, function(t) {
sanitize({ text: 'test', details: details }, function( err, clean ){
t.equal(clean.details, true, 'details set to true');
valid_values.forEach(function(value) {
test('valid private ' + value, function(t) {
sanitize({ text: 'test', 'private': value}, function( err, clean ){
t.equal(clean.private, true, 'private set to true');
t.end();
});
});
});
var valid_false_values = ['false', false, 0, '0'];
valid_false_values.forEach(function(details) {
test('test setting false explicitly ' + details, function(t) {
sanitize({ text: 'test', details: details }, function( err, clean ){
t.equal(clean.details, false, 'details set to false');
valid_false_values.forEach(function(value) {
test('test setting false explicitly ' + value, function(t) {
sanitize({ text: 'test', 'private': value }, function( err, clean ){
t.equal(clean.private, false, 'private set to false');
t.end();
});
});
@ -274,7 +300,7 @@ module.exports.tests.sanitize_details = function(test, common) {
test('test default behavior', function(t) {
sanitize({ text: 'test' }, function( err, clean ){
t.equal(clean.details, true, 'details set to true');
t.equal(clean.private, false, 'private set to false');
t.end();
});
});

Loading…
Cancel
Save