@ -1,299 +1,106 @@
var extend = require ( 'extend' ) ,
search = require ( '../../../sanitiser/search' ) ,
text _analyzer = require ( 'pelias-text-analyzer' ) ,
sanitize = search . sanitize ,
middleware = search . middleware ,
defaultError = 'invalid param \'text\': text length, must be >0' ;
// these are the default values you would expect when no input params are specified.
var emptyClean = { private : false , size : 10 } ;
var proxyquire = require ( 'proxyquire' ) . noCallThru ( ) ;
module . exports . tests = { } ;
module . exports . tests . interface = function ( test , common ) {
test ( 'sanitize interface' , function ( t ) {
t . equal ( typeof sanitize , 'function' , 'sanitize is a function' ) ;
t . equal ( sanitize . length , 2 , 'sanitize interface' ) ;
t . end ( ) ;
} ) ;
test ( 'middleware interface' , function ( t ) {
t . equal ( typeof middleware , 'function' , 'middleware is a function' ) ;
t . equal ( middleware . length , 3 , 'sanitize has a valid middleware' ) ;
t . end ( ) ;
} ) ;
} ;
module . exports . tests . sanitisers = function ( test , common ) {
test ( 'check sanitiser list' , function ( t ) {
var expected = [ 'quattroshapes_deprecation' , 'singleScalarParameters' , 'text' , 'size' ,
'layers' , 'sources' , 'sources_and_layers' , 'private' , 'geo_search' , 'boundary_country' , 'categories' ] ;
t . deepEqual ( Object . keys ( search . sanitiser _list ) , expected ) ;
t . end ( ) ;
} ) ;
} ;
module . exports . tests . sanitize _invalid _text = function ( test , common ) {
test ( 'invalid text' , function ( t ) {
var invalid = [ '' , 100 , null , undefined ] ;
invalid . forEach ( function ( text ) {
var req = { query : { text : text } } ;
sanitize ( req , function ( ) {
t . equal ( req . errors [ 0 ] , 'invalid param \'text\': text length, must be >0' , text + ' is an invalid text' ) ;
t . deepEqual ( req . clean , emptyClean , 'clean only has default values set' ) ;
} ) ;
} ) ;
t . end ( ) ;
} ) ;
} ;
module . exports . tests . sanitise _valid _text = function ( test , common ) {
test ( 'valid short text' , function ( t ) {
var req = { query : { text : 'a' } } ;
sanitize ( req , function ( ) {
t . equal ( req . errors [ 0 ] , undefined , 'no error' ) ;
} ) ;
t . end ( ) ;
} ) ;
test ( 'valid not-quite-as-short text' , function ( t ) {
var req = { query : { text : 'aa' } } ;
sanitize ( req , function ( ) {
t . equal ( req . errors [ 0 ] , undefined , 'no error' ) ;
} ) ;
t . end ( ) ;
} ) ;
test ( 'valid longer text' , function ( t ) {
var req = { query : { text : 'aaaaaaaa' } } ;
sanitize ( req , function ( ) {
t . equal ( req . errors [ 0 ] , undefined , 'no error' ) ;
} ) ;
t . end ( ) ;
} ) ;
} ;
module . exports . tests . sanitize _text _with _delim = function ( test , common ) {
var texts = [ 'a,bcd' , '123 main st, region' , ',,,' , ' ' ] ;
test ( 'valid texts with a comma' , function ( t ) {
texts . forEach ( function ( text ) {
var req = { query : { text : text } } ;
sanitize ( req , function ( ) {
var expected _text = text ;
var expected _parsed _text = text _analyzer . parse ( text ) ;
t . equal ( req . errors [ 0 ] , undefined , 'no error' ) ;
t . equal ( req . clean . parsed _text . name , expected _parsed _text . name , 'clean name set correctly' ) ;
t . equal ( req . clean . text , expected _text , 'text should match' ) ;
} ) ;
} ) ;
t . end ( ) ;
} ) ;
} ;
module . exports . tests . sanitize _private _no _value = function ( test , common ) {
test ( 'default private should be set to true' , function ( t ) {
var req = { query : { text : 'test' } } ;
sanitize ( req , function ( ) {
t . equal ( req . 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 ) {
var req = { query : { text : 'test' , private : true } } ;
sanitize ( req , function ( ) {
t . equal ( req . 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 ) {
var req = { query : { text : 'test' , private : false } } ;
sanitize ( req , function ( ) {
t . equal ( req . clean . private , false , 'private set to false' ) ;
} ) ;
t . end ( ) ;
} ) ;
} ;
module . exports . tests . sanitize _lat = function ( test , common ) {
var valid _lats = [ 0 , 45 , 90 , - 0 , '0' , '45' , '90' , - 181 , - 120 , - 91 , 91 , 120 , 181 ] ;
test ( 'valid lat' , function ( t ) {
valid _lats . forEach ( function ( lat ) {
var req = { query : { text : 'test' , 'focus.point.lat' : lat , 'focus.point.lon' : 0 } } ;
sanitize ( req , function ( ) {
var expected _lat = parseFloat ( lat ) ;
t . equal ( req . errors [ 0 ] , undefined , 'no error' ) ;
} ) ;
} ) ;
t . end ( ) ;
} ) ;
} ;
module . exports . tests . sanitize _lon = function ( test , common ) {
var lons = {
valid : [ - 381 , - 181 , - 180 , - 1 , - 0 , 0 , 45 , 90 , '-180' , '0' , '180' , 181 ]
module . exports . tests . sanitize = function ( test , common ) {
test ( 'verify that all sanitisers were called as expected' , function ( t ) {
var called _sanitisers = [ ] ;
// rather than re-verify the functionality of all the sanitisers, this test just verifies that they
// were all called correctly
var search = proxyquire ( '../../../sanitiser/search' , {
'../sanitiser/_deprecate_quattroshapes' : function ( ) {
called _sanitisers . push ( '_deprecate_quattroshapes' ) ;
return { errors : [ ] , warnings : [ ] } ;
} ,
'../sanitiser/_single_scalar_parameters' : function ( ) {
called _sanitisers . push ( '_single_scalar_parameters' ) ;
return { errors : [ ] , warnings : [ ] } ;
} ,
'../sanitiser/_text' : function ( ) {
called _sanitisers . push ( '_text' ) ;
return { errors : [ ] , warnings : [ ] } ;
} ,
'../sanitiser/_size' : function ( ) {
if ( arguments . length === 0 ) {
return function ( ) {
called _sanitisers . push ( '_size' ) ;
return { errors : [ ] , warnings : [ ] } ;
} ;
test ( 'valid lon' , function ( t ) {
lons . valid . forEach ( function ( lon ) {
var req = { query : { text : 'test' , 'focus.point.lat' : 0 , 'focus.point.lon' : lon } } ;
sanitize ( req , function ( ) {
var expected _lon = parseFloat ( lon ) ;
t . equal ( req . errors [ 0 ] , undefined , 'no error' ) ;
} ) ;
} ) ;
t . end ( ) ;
} ) ;
} ;
module . exports . tests . sanitize _optional _geo = function ( test , common ) {
test ( 'no lat/lon' , function ( t ) {
var req = { query : { text : 'test' } } ;
sanitize ( req , function ( ) {
t . equal ( req . errors [ 0 ] , undefined , 'no error' ) ;
t . equal ( req . clean [ 'focus.point.lat' ] , undefined , 'clean set without lat' ) ;
t . equal ( req . clean [ 'focus.point.lon' ] , undefined , 'clean set without lon' ) ;
} ) ;
t . end ( ) ;
} ) ;
test ( 'no lat' , function ( t ) {
var req = { query : { text : 'test' , 'focus.point.lon' : 0 } } ;
sanitize ( req , function ( ) {
var expected _lon = 0 ;
t . equal ( req . errors [ 0 ] , 'parameters focus.point.lat and focus.point.lon must both be specified' ) ;
t . equal ( req . clean [ 'focus.point.lat' ] , undefined ) ;
t . equal ( req . clean [ 'focus.point.lon' ] , undefined ) ;
} ) ;
t . end ( ) ;
} ) ;
test ( 'no lon' , function ( t ) {
var req = { query : { text : 'test' , 'focus.point.lat' : 0 } } ;
sanitize ( req , function ( ) {
var expected _lat = 0 ;
t . equal ( req . errors [ 0 ] , 'parameters focus.point.lat and focus.point.lon must both be specified' ) ;
t . equal ( req . clean [ 'focus.point.lat' ] , undefined ) ;
t . equal ( req . clean [ 'focus.point.lon' ] , undefined ) ;
} ) ;
t . end ( ) ;
} ) ;
} ;
module . exports . tests . sanitize _bounding _rect = function ( test , common ) {
test ( 'valid bounding rect' , function ( t ) {
var req = {
query : {
text : 'test' ,
'boundary.rect.min_lat' : - 40.659 ,
'boundary.rect.max_lat' : - 41.614 ,
'boundary.rect.min_lon' : 174.612 ,
'boundary.rect.max_lon' : 176.333
} else {
throw new Error ( 'should not have passed any parameters to _size' ) ;
}
} ;
sanitize ( req , function ( ) {
t . equal ( req . errors [ 0 ] , undefined , 'no error' ) ;
t . equal ( req . clean [ 'boundary.rect.min_lon' ] , parseFloat ( req . query [ 'boundary.rect.min_lon' ] ) ) ;
t . equal ( req . clean [ 'boundary.rect.max_lat' ] , parseFloat ( req . query [ 'boundary.rect.max_lat' ] ) ) ;
t . equal ( req . clean [ 'boundary.rect.max_lon' ] , parseFloat ( req . query [ 'boundary.rect.max_lon' ] ) ) ;
t . equal ( req . clean [ 'boundary.rect.min_lat' ] , parseFloat ( req . query [ 'boundary.rect.min_lat' ] ) ) ;
t . end ( ) ;
} ) ;
} ) ;
} ;
module . exports . tests . sanitize _size = function ( test , common ) {
test ( 'invalid size value' , function ( t ) {
var req = { query : { size : 'a' , text : 'test' , lat : 0 , lon : 0 } } ;
sanitize ( req , function ( ) {
t . equal ( req . clean . size , 10 , 'default size set' ) ;
t . end ( ) ;
} ) ;
} ) ;
test ( 'below min size value' , function ( t ) {
var req = { query : { size : - 100 , text : 'test' , lat : 0 , lon : 0 } } ;
sanitize ( req , function ( ) {
t . equal ( req . clean . size , 1 , 'min size set' ) ;
t . end ( ) ;
} ) ;
} ) ;
test ( 'above max size value' , function ( t ) {
var req = { query : { size : 9999 , text : 'test' , lat : 0 , lon : 0 } } ;
sanitize ( req , function ( ) {
t . equal ( req . clean . size , 40 , 'max size set' ) ;
t . end ( ) ;
} ) ;
} ) ;
} ;
module . exports . tests . sanitize _private = function ( test , common ) {
var invalid _values = [ null , - 1 , 123 , NaN , 'abc' ] ;
invalid _values . forEach ( function ( value ) {
test ( 'invalid private param ' + value , function ( t ) {
var req = { query : { text : 'test' , lat : 0 , lon : 0 , 'private' : value } } ;
sanitize ( req , function ( ) {
t . equal ( req . clean . private , false , 'default private set (to false)' ) ;
t . end ( ) ;
} ) ;
} ) ;
} ) ;
} ,
'../sanitiser/_targets' : function ( type ) {
if ( [ 'layers' , 'sources' ] . indexOf ( type ) !== - 1 ) {
return function ( ) {
called _sanitisers . push ( '_targets/' + type ) ;
return { errors : [ ] , warnings : [ ] } ;
} ;
var valid _values = [ 'true' , true , 1 , '1' ] ;
valid _values . forEach ( function ( value ) {
test ( 'valid private ' + value , function ( t ) {
var req = { query : { text : 'test' , 'private' : value } } ;
sanitize ( req , function ( ) {
t . equal ( req . clean . private , true , 'private set to true' ) ;
t . end ( ) ;
} ) ;
} ) ;
} ) ;
}
else {
throw new Error ( 'incorrect parameters passed to _targets' ) ;
}
var valid _false _values = [ 'false' , false , 0 , '0' ] ;
valid _false _values . forEach ( function ( value ) {
test ( 'test setting false explicitly ' + value , function ( t ) {
var req = { query : { text : 'test' , 'private' : value } } ;
sanitize ( req , function ( ) {
t . equal ( req . clean . private , false , 'private set to false' ) ;
t . end ( ) ;
} ) ;
} ) ;
} ) ;
} ,
'../sanitiser/_sources_and_layers' : function ( ) {
called _sanitisers . push ( '_sources_and_layers' ) ;
return { errors : [ ] , warnings : [ ] } ;
} ,
'../sanitiser/_flag_bool' : function ( ) {
if ( arguments [ 0 ] === 'private' && arguments [ 1 ] === false ) {
return function ( ) {
called _sanitisers . push ( '_flag_bool' ) ;
return { errors : [ ] , warnings : [ ] } ;
} ;
test ( 'test default behavior' , function ( t ) {
var req = { query : { text : 'test' } } ;
sanitize ( req , function ( ) {
t . equal ( req . clean . private , false , 'private set to false' ) ;
t . end ( ) ;
} ) ;
} ) ;
} ;
}
else {
throw new Error ( 'incorrect parameters passed to _flag_bool' ) ;
}
module . exports . tests . invalid _params = function ( test , common ) {
test ( 'invalid text params' , function ( t ) {
var req = { query : { } } ;
sanitize ( req , function ( ) {
t . equal ( req . errors [ 0 ] , defaultError , 'handle invalid params gracefully' ) ;
} ,
'../sanitiser/_geo_search' : function ( ) {
called _sanitisers . push ( '_geo_search' ) ;
return { errors : [ ] , warnings : [ ] } ;
} ,
'../sanitiser/_boundary_country' : function ( ) {
called _sanitisers . push ( '_boundary_country' ) ;
return { errors : [ ] , warnings : [ ] } ;
} ,
'../sanitiser/_categories' : function ( ) {
called _sanitisers . push ( '_categories' ) ;
return { errors : [ ] , warnings : [ ] } ;
} ,
} ) ;
var expected _sanitisers = [
'_deprecate_quattroshapes' ,
'_single_scalar_parameters' ,
'_text' ,
'_size' ,
'_targets/layers' ,
'_targets/sources' ,
'_sources_and_layers' ,
'_flag_bool' ,
'_geo_search' ,
'_boundary_country' ,
'_categories'
] ;
var req = { } ;
var res = { } ;
search . middleware ( req , res , function ( ) {
t . deepEquals ( called _sanitisers , expected _sanitisers ) ;
t . end ( ) ;
} ) ;
} ) ;
} ;
module . exports . tests . middleware _success = function ( test , common ) {
test ( 'middleware success' , function ( t ) {
var req = { query : { text : 'test' } } ;
var next = function ( message ) {
t . deepEqual ( req . errors , [ ] , 'no error messages set' ) ;
t . end ( ) ;
} ;
middleware ( req , undefined , next ) ;
} ) ;
} ;
module . exports . all = function ( tape , common ) {
function test ( name , testFunction ) {