@ -34,13 +34,10 @@ module.exports.tests.interface = function(test, common) {
} ) ;
} ;
module . exports . tests . sanitize _input = function ( test , common ) {
var inputs = {
invalid : [ '' , 100 , null , undefined , new Date ( ) ] ,
valid : [ 'a' , 'aa' , 'aaaaaaaa' ]
} ;
module . exports . tests . sanitize _invalid _input = function ( test , common ) {
test ( 'invalid input' , function ( t ) {
inputs . invalid . forEach ( function ( input ) {
var invalid = [ '' , 100 , null , undefined , new Date ( ) ] ;
invalid . forEach ( function ( input ) {
sanitize ( { input : input } , function ( err , clean ) {
t . equal ( err , 'invalid param \'input\': text length, must be >0' , input + ' is an invalid input' ) ;
t . equal ( clean , undefined , 'clean not set' ) ;
@ -48,16 +45,26 @@ module.exports.tests.sanitize_input = function(test, common) {
} ) ;
t . end ( ) ;
} ) ;
test ( 'valid input' , function ( t ) {
inputs . valid . forEach ( function ( input ) {
sanitize ( { input : input } , function ( err , clean ) {
var expected = JSON . parse ( JSON . stringify ( defaultClean ) ) ;
expected . input = input ;
expected . parsed _input . target _layer = _input . getTargetLayers ( input ) ;
} ;
module . exports . tests . sanitise _valid _input = function ( test , common ) {
test ( 'valid short input' , function ( t ) {
sanitize ( { input : 'a' } , function ( err , clean ) {
t . equal ( err , undefined , 'no error' ) ;
t . deepEqual ( clean , expected , 'clean set correctly (' + input + ')' ) ;
} ) ;
t . end ( ) ;
} ) ;
test ( 'valid not-quite-as-short input' , function ( t ) {
sanitize ( { input : 'aa' } , function ( err , clean ) {
t . equal ( err , undefined , 'no error' ) ;
} ) ;
t . end ( ) ;
} ) ;
test ( 'valid longer input' , function ( t ) {
sanitize ( { input : 'aaaaaaaa' } , function ( err , clean ) {
t . equal ( err , undefined , 'no error' ) ;
} ) ;
t . end ( ) ;
} ) ;
@ -75,8 +82,6 @@ module.exports.tests.sanitize_input_with_delim = function(test, common) {
expected . parsed _input = parser ( input ) ;
t . equal ( err , undefined , 'no error' ) ;
t . equal ( clean . parsed _input . name , expected . parsed _input . name , 'clean name set correctly' ) ;
t . equal ( clean . parsed _input . admin _parts , expected . parsed _input . admin _parts , 'clean admin_parts set correctly' ) ;
t . deepEqual ( clean , expected , 'clean set correctly (' + input + ')' ) ;
} ) ;
} ) ;
@ -103,10 +108,8 @@ module.exports.tests.sanitize_lat = function(test, common) {
sanitize ( { input : 'test' , lat : lat , lon : 0 } , function ( err , clean ) {
var expected = JSON . parse ( JSON . stringify ( defaultClean ) ) ;
expected . lat = parseFloat ( lat ) ;
expected . lon = 0 ;
t . equal ( err , undefined , 'no error' ) ;
expected . parsed _input = parser ( 'test' ) ;
t . deepEqual ( clean , expected , 'clean set correctly (' + lat + ')' ) ;
t . deepEqual ( clean . lat , expected . lat , 'clean lat set correctly (' + lat + ')' ) ;
} ) ;
} ) ;
t . end ( ) ;
@ -122,10 +125,8 @@ module.exports.tests.sanitize_lon = function(test, common) {
sanitize ( { input : 'test' , lat : 0 , lon : lon } , function ( err , clean ) {
var expected = JSON . parse ( JSON . stringify ( defaultClean ) ) ;
expected . lon = parseFloat ( lon ) ;
expected . lat = 0 ;
t . equal ( err , undefined , 'no error' ) ;
expected . parsed _input = parser ( 'test' ) ;
t . deepEqual ( clean , expected , 'clean set correctly (' + lon + ')' ) ;
t . deepEqual ( clean . lon , expected . lon , 'clean set correctly (' + lon + ')' ) ;
} ) ;
} ) ;
t . end ( ) ;
@ -135,32 +136,25 @@ module.exports.tests.sanitize_lon = function(test, common) {
module . exports . tests . sanitize _optional _geo = function ( test , common ) {
test ( 'no lat/lon' , function ( t ) {
sanitize ( { input : 'test' } , function ( err , clean ) {
var expected = defaultClean ;
t . equal ( err , undefined , 'no error' ) ;
t . equal ( clean . lat , undefined , 'clean set without lat' ) ;
t . equal ( clean . lon , undefined , 'clean set without lon' ) ;
expected . parsed _input = parser ( 'test' ) ;
t . deepEqual ( clean , expected , 'clean set without lat/lon' ) ;
} ) ;
t . end ( ) ;
} ) ;
test ( 'no lat' , function ( t ) {
sanitize ( { input : 'test' , lon : 0 } , function ( err , clean ) {
var expected = JSON . parse ( JSON . stringify ( defaultClean ) ) ;
expected . lon = 0 ;
var expected _lon = 0 ;
t . equal ( err , undefined , 'no error' ) ;
expected . parsed _input = parser ( 'test' ) ;
t . deepEqual ( clean , expected , 'clean set correctly (without any lat)' ) ;
t . deepEqual ( clean . lon , expected _lon , 'clean set correctly (without any lat)' ) ;
} ) ;
t . end ( ) ;
} ) ;
test ( 'no lon' , function ( t ) {
sanitize ( { input : 'test' , lat : 0 } , function ( err , clean ) {
var expected = JSON . parse ( JSON . stringify ( defaultClean ) ) ;
expected . lat = 0 ;
var expected _lat = 0 ;
t . equal ( err , undefined , 'no error' ) ;
expected . parsed _input = parser ( 'test' ) ;
t . deepEqual ( clean , expected , 'clean set correctly (without any lon)' ) ;
t . deepEqual ( clean . lat , expected _lat , 'clean set correctly (without any lon)' ) ;
} ) ;
t . end ( ) ;
} ) ;
@ -168,8 +162,6 @@ module.exports.tests.sanitize_optional_geo = function(test, common) {
module . exports . tests . sanitize _bbox = function ( test , common ) {
var bboxes = {
invalid _coordinates : [
] ,
invalid : [
'91;-181,-91,181' , // invalid - semicolon between coordinates
'these,are,not,numbers' ,
@ -196,22 +188,11 @@ module.exports.tests.sanitize_bbox = function(test, common) {
]
} ;
test ( 'invalid bbox coordinates' , function ( t ) {
bboxes . invalid _coordinates . forEach ( function ( bbox ) {
sanitize ( { input : 'test' , bbox : bbox } , function ( err , clean ) {
t . ok ( err . match ( /Invalid (lat|lon)/ ) , bbox + ' is invalid' ) ;
t . equal ( clean , undefined , 'clean not set' ) ;
} ) ;
} ) ;
t . end ( ) ;
} ) ;
test ( 'invalid bbox' , function ( t ) {
bboxes . invalid . forEach ( function ( bbox ) {
sanitize ( { input : 'test' , bbox : bbox } , function ( err , clean ) {
var expected = JSON . parse ( JSON . stringify ( defaultClean ) ) ;
t . equal ( err , undefined , 'no error' ) ;
expected . parsed _input = parser ( 'test' ) ;
t . deepEqual ( clean , expected , 'falling back on 50km distance from centroid' ) ;
t . equal ( clean . bbox , undefined , 'falling back on 50km distance from centroid' ) ;
} ) ;
} ) ;
t . end ( ) ;
@ -219,19 +200,17 @@ module.exports.tests.sanitize_bbox = function(test, common) {
test ( 'valid bbox' , function ( t ) {
bboxes . valid . forEach ( function ( bbox ) {
sanitize ( { input : 'test' , bbox : bbox } , function ( err , clean ) {
var expected = JSON . parse ( JSON . stringify ( defaultClean ) ) ;
var bboxArray = bbox . split ( ',' ) . map ( function ( i ) {
return parseInt ( i ) ;
} ) ;
expected . bbox = {
var expected _ bbox = {
right : Math . max ( bboxArray [ 0 ] , bboxArray [ 2 ] ) ,
top : Math . max ( bboxArray [ 1 ] , bboxArray [ 3 ] ) ,
left : Math . min ( bboxArray [ 0 ] , bboxArray [ 2 ] ) ,
bottom : Math . min ( bboxArray [ 1 ] , bboxArray [ 3 ] )
} ;
t . equal ( err , undefined , 'no error' ) ;
expected . parsed _input = parser ( 'test' ) ;
t . deepEqual ( clean , expected , 'clean set correctly (' + bbox + ')' ) ;
t . deepEqual ( clean . bbox , expected _bbox , 'clean set correctly (' + bbox + ')' ) ;
} ) ;
} ) ;
t . end ( ) ;
@ -422,8 +401,6 @@ module.exports.tests.middleware_success = function(test, common) {
var req = { query : { input : 'test' } } ;
var next = function ( message ) {
t . equal ( message , undefined , 'no error message set' ) ;
req . clean . parsed _input = parser ( 'test' ) ;
t . deepEqual ( req . clean , defaultClean ) ;
t . end ( ) ;
} ;
middleware ( req , undefined , next ) ;