Browse Source

tests

pull/7/head
Peter Johnson 10 years ago
parent
commit
ffda37adff
  1. 6
      sanitiser/suggest.js
  2. 160
      test/unit/sanitiser/suggest.js

6
sanitiser/suggest.js

@ -21,7 +21,7 @@ function sanitize( params, cb ){
// total results // total results
var size = parseInt( params.size, 10 ); var size = parseInt( params.size, 10 );
if( !isNaN( size ) ){ if( !isNaN( size ) ){
clean.size = Math.min( size, 40 ); // max clean.size = Math.min( Math.max( size, 1 ), 40 ); // max
} else { } else {
clean.size = 10; // default clean.size = 10; // default
} }
@ -33,7 +33,7 @@ function sanitize( params, cb ){
}); });
for( var x=0; x<layers.length; x++ ){ for( var x=0; x<layers.length; x++ ){
if( -1 === indeces.indexOf( layers[x] ) ){ if( -1 === indeces.indexOf( layers[x] ) ){
return cb( 'invalid param \'layer\': must be one or more of ' + layers.join(',') ); return cb( 'invalid param \'layer\': must be one or more of ' + indeces.join(',') );
} }
} }
clean.layers = layers; clean.layers = layers;
@ -59,7 +59,7 @@ function sanitize( params, cb ){
// zoom level // zoom level
var zoom = parseInt( params.zoom, 10 ); var zoom = parseInt( params.zoom, 10 );
if( !isNaN( zoom ) ){ if( !isNaN( zoom ) ){
clean.zoom = Math.min( zoom, 18 ); // max clean.zoom = Math.min( Math.max( zoom, 1 ), 18 ); // max
} else { } else {
clean.zoom = 10; // default clean.zoom = 10; // default
} }

160
test/unit/sanitiser/suggest.js

@ -1,5 +1,7 @@
var sanitize = require('../../../sanitiser/suggest'); var sanitize = require('../../../sanitiser/suggest'),
defaultError = 'invalid param \'input\': text length, must be >0',
defaultClean = { input: 'test', lat: 0, layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood' ], lon: 0, size: 10, zoom: 10 };
module.exports.tests = {}; module.exports.tests = {};
@ -16,13 +18,161 @@ 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' ]
};
inputs.invalid.forEach( function( input ){
test('invalid input', function(t) {
sanitize({ input: input, lat: 0, lon: 0 }, function( err, clean ){
t.equal(err, 'invalid param \'input\': text length, must be >0', 'invalid input');
t.equal(clean, undefined, 'clean not set');
t.end();
});
});
});
inputs.valid.forEach( function( input ){
test('valid input', function(t) {
sanitize({ input: input, lat: 0, lon: 0 }, function( err, clean ){
var expected = JSON.parse(JSON.stringify( defaultClean ));
expected.input = input;
t.equal(err, undefined, 'no error');
t.deepEqual(clean, expected, 'clean set correctly');
t.end();
});
});
});
};
module.exports.tests.sanitize_lat = function(test, common) {
var lats = {
invalid: [ -1, -45, -90, 91, 120, 181 ],
valid: [ 0, 45, 90, -0, '0', '45', '90' ]
};
lats.invalid.forEach( function( lat ){
test('invalid lat', function(t) {
sanitize({ input: 'test', lat: lat, lon: 0 }, function( err, clean ){
t.equal(err, 'invalid param \'lat\': must be >0 and <90', 'invalid latitude');
t.equal(clean, undefined, 'clean not set');
t.end();
});
});
});
lats.valid.forEach( function( lat ){
test('valid lat', function(t) {
sanitize({ input: 'test', lat: lat, lon: 0 }, function( err, clean ){
var expected = JSON.parse(JSON.stringify( defaultClean ));
expected.lat = parseFloat( lat );
t.equal(err, undefined, 'no error');
t.deepEqual(clean, expected, 'clean set correctly');
t.end();
});
});
});
};
module.exports.tests.sanitize_lon = function(test, common) {
var lons = {
invalid: [ -360, -181, 181, 360 ],
valid: [ -180, -1, -0, 0, 45, 90, '-180', '0', '180' ]
};
lons.invalid.forEach( function( lon ){
test('invalid lon', function(t) {
sanitize({ input: 'test', lat: 0, lon: lon }, function( err, clean ){
t.equal(err, 'invalid param \'lon\': must be >-180 and <180', 'invalid longitude');
t.equal(clean, undefined, 'clean not set');
t.end();
});
});
});
lons.valid.forEach( function( lon ){
test('valid lon', function(t) {
sanitize({ input: 'test', lat: 0, lon: lon }, function( err, clean ){
var expected = JSON.parse(JSON.stringify( defaultClean ));
expected.lon = parseFloat( lon );
t.equal(err, undefined, 'no error');
t.deepEqual(clean, expected, 'clean set correctly');
t.end();
});
});
});
};
module.exports.tests.sanitize_zoom = function(test, common) {
test('invalid zoom value', function(t) {
sanitize({ zoom: 'a', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.zoom, 10, 'default zoom set');
t.end();
});
});
test('below min zoom value', function(t) {
sanitize({ zoom: -100, input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.zoom, 1, 'min zoom set');
t.end();
});
});
test('above max zoom value', function(t) {
sanitize({ zoom: 9999, input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.zoom, 18, 'max zoom set');
t.end();
});
});
};
module.exports.tests.sanitize_size = function(test, common) {
test('invalid size value', function(t) {
sanitize({ size: 'a', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.size, 10, 'default size set');
t.end();
});
});
test('below min size value', function(t) {
sanitize({ size: -100, input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.size, 1, 'min size set');
t.end();
});
});
test('above max size value', function(t) {
sanitize({ size: 9999, input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.size, 40, 'max size set');
t.end();
});
});
};
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.end();
});
});
test('invalid layer', function(t) {
sanitize({ layers: 'test_layer', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
var msg = 'invalid param \'layer\': must be one or more of geoname,osmnode,osmway,admin0,admin1,admin2,neighborhood';
t.equal(err, msg, 'invalid layer requested');
t.end();
});
});
};
module.exports.tests.invalid_params = function(test, common) {
test('invalid input params', function(t) {
sanitize( undefined, function( err, clean ){
t.equal(err, defaultError, 'handle invalid params gracefully');
t.end();
});
});
};
module.exports.tests.middleware_failure = function(test, common) { module.exports.tests.middleware_failure = function(test, common) {
test('middleware failure', function(t) { test('middleware failure', function(t) {
var res = { status: function( code ){ var res = { status: function( code ){
t.equal(code, 400, 'status set'); t.equal(code, 400, 'status set');
}}; }};
var next = function( message ){ var next = function( message ){
t.equal(message,'invalid param \'input\': text length, must be >0'); t.equal(message, defaultError);
t.end(); t.end();
}; };
sanitize.middleware( {}, res, next ); sanitize.middleware( {}, res, next );
@ -34,11 +184,7 @@ module.exports.tests.middleware_success = function(test, common) {
var req = { query: { input: 'test', lat: 0, lon: 0 }}; var req = { query: { input: 'test', lat: 0, lon: 0 }};
var next = function( message ){ var next = function( message ){
t.equal(message, undefined, 'no error message set'); t.equal(message, undefined, 'no error message set');
t.deepEqual(req.clean, { t.deepEqual(req.clean, defaultClean);
input: 'test', lat: 0, lon: 0,
layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood' ],
size: 10, zoom: 10
});
t.end(); t.end();
}; };
sanitize.middleware( req, undefined, next ); sanitize.middleware( req, undefined, next );

Loading…
Cancel
Save