diff --git a/test/unit/run.js b/test/unit/run.js index 96e9c95a..34b5a92e 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -6,7 +6,8 @@ var tests = [ require('./controller/index'), require('./controller/suggest'), require('./controller/search'), - require('./sanitiser/sanitise'), + require('./sanitiser/suggest'), + require('./sanitiser/get'), require('./query/indeces'), require('./query/suggest'), require('./query/search'), diff --git a/test/unit/sanitiser/get.js b/test/unit/sanitiser/get.js new file mode 100644 index 00000000..d00133c3 --- /dev/null +++ b/test/unit/sanitiser/get.js @@ -0,0 +1,118 @@ + +var get = require('../../../sanitiser/get'), + _sanitize = get.sanitize, + middleware = get.middleware, + indeces = require('../../../query/indeces'), + defaultIdError = 'invalid param \'id\': text length, must be >0', + defaultTypeError = 'invalid param \'type\': text length, must be >0', + defaultError = defaultIdError, + defaultMissingTypeError = 'type must be one of these values - [' + indeces.join(", ") + ']', + defaultClean = { id: '123', type: 'geoname' }, + sanitize = function(query, cb) { _sanitize({'query':query}, cb); } + +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, 'sanitizee has a valid middleware'); + t.end(); + }); +}; + +module.exports.tests.sanitize_id_and_type = function(test, common) { + var inputs = { + valid: [ + {id:'1', type:'geoname'}, + {id:'2', type:'osmnode'}, + {id:'3', type:'geoname'}, + {id:'4', type:'osmway'}, + {id:'5', type:'admin0'} + ], + invalid: [ + {id:undefined, type:undefined}, + {id:null, type:null}, + {id:'', type:''}, + {id:'4', type:''}, + {id:'5', type:'gibberish'} + ] + }; + + inputs.invalid.forEach( function( input ){ + test('invalid id and/or type', function(t) { + sanitize({ id: input.id, type: input.type }, function( err, clean ){ + switch (err) { + case defaultIdError: + t.equal(err, defaultIdError, 'missing id'); break; + case defaultTypeError: + t.equal(err, defaultTypeError, 'missing type'); break; + case defaultMissingTypeError: + t.equal(err, defaultMissingTypeError, 'unknown type'); break; + default: break; + } + t.equal(clean, undefined, 'clean not set'); + t.end(); + }); + }); + }); + inputs.valid.forEach( function( input ){ + test('valid id and/or type', function(t) { + var expected = { id: input.id, type: input.type }; + sanitize({ id: input.id, type: input.type, }, function( err, clean ){ + t.equal(err, undefined, 'no error'); + t.deepEqual(clean, expected, 'clean set correctly'); + t.end(); + }); + }); + }); +}; + +module.exports.tests.invalid_params = function(test, common) { + test('invalid 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) { + test('middleware failure', function(t) { + var res = { status: function( code ){ + t.equal(code, 400, 'status set'); + }}; + var next = function( message ){ + t.equal(message, defaultError); + t.end(); + }; + middleware( {}, res, next ); + }); +}; + +module.exports.tests.middleware_success = function(test, common) { + test('middleware success', function(t) { + var req = { query: { id: '123', type: 'geoname' }}; + var next = function( message ){ + t.equal(message, undefined, 'no error message set'); + t.deepEqual(req.clean, defaultClean); + t.end(); + }; + middleware( req, undefined, next ); + }); +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('SANTIZE /get ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; \ No newline at end of file diff --git a/test/unit/sanitiser/sanitise.js b/test/unit/sanitiser/suggest.js similarity index 99% rename from test/unit/sanitiser/sanitise.js rename to test/unit/sanitiser/suggest.js index cae2d8b1..66055b68 100644 --- a/test/unit/sanitiser/sanitise.js +++ b/test/unit/sanitiser/suggest.js @@ -241,7 +241,7 @@ module.exports.tests.middleware_success = function(test, common) { module.exports.all = function (tape, common) { function test(name, testFunction) { - return tape('SANTIZE /sanitise ' + name, testFunction); + return tape('SANTIZE /suggest ' + name, testFunction); } for( var testCase in module.exports.tests ){