Browse Source

going from ?id=123&type=geoname to ?id=geoname/123

tests updated
pull/34/head
Harish Krishna 10 years ago
parent
commit
0dd495a378
  1. 21
      sanitiser/_id.js
  2. 49
      test/unit/sanitiser/get.js

21
sanitiser/_id.js

@ -24,18 +24,33 @@ function sanitize( req ){
if('string' !== typeof params.id || !params.id.length){ if('string' !== typeof params.id || !params.id.length){
return errormessage('id'); return errormessage('id');
} }
// id format
if(params.id.indexOf('/') === -1) {
return errormessage('id', 'invalid: must be of the format type/id for ex: \'geoname/4163334\'');
}
req.clean.id = params.id; req.clean.id = params.id;
var param = params.id.split('/');
var param_type = param[0];
var param_id = param[1];
// id text
if('string' !== typeof param_id || !param_id.length){
return errormessage('id');
}
// type text // type text
if('string' !== typeof params.type || !params.type.length){ if('string' !== typeof param_type || !param_type.length){
return errormessage('type'); return errormessage('type');
} }
// type text must be one of the indeces // type text must be one of the indeces
if(indeces.indexOf(params.type) == -1){ if(indeces.indexOf(param_type) == -1){
return errormessage('type', 'type must be one of these values - [' + indeces.join(", ") + ']'); return errormessage('type', 'type must be one of these values - [' + indeces.join(", ") + ']');
} }
req.clean.type = params.type; req.clean.id = param_id;
req.clean.type = param_type;
return { 'error': false }; return { 'error': false };

49
test/unit/sanitiser/get.js

@ -5,6 +5,7 @@ var get = require('../../../sanitiser/get'),
indeces = require('../../../query/indeces'), indeces = require('../../../query/indeces'),
defaultIdError = 'invalid param \'id\': text length, must be >0', defaultIdError = 'invalid param \'id\': text length, must be >0',
defaultTypeError = 'invalid param \'type\': text length, must be >0', defaultTypeError = 'invalid param \'type\': text length, must be >0',
defaultFormatError = 'invalid: must be of the format type/id for ex: \'geoname/4163334\'',
defaultError = defaultIdError, defaultError = defaultIdError,
defaultMissingTypeError = 'type must be one of these values - [' + indeces.join(", ") + ']', defaultMissingTypeError = 'type must be one of these values - [' + indeces.join(", ") + ']',
defaultClean = { id: '123', type: 'geoname' }, defaultClean = { id: '123', type: 'geoname' },
@ -28,31 +29,34 @@ module.exports.tests.interface = function(test, common) {
module.exports.tests.sanitize_id_and_type = function(test, common) { module.exports.tests.sanitize_id_and_type = function(test, common) {
var inputs = { var inputs = {
valid: [ valid: [
{id:'1', type:'geoname'}, 'geoname/1',
{id:'2', type:'osmnode'}, 'osmnode/2',
{id:'3', type:'geoname'}, 'admin0/53',
{id:'4', type:'osmway'}, 'osmway/44',
{id:'5', type:'admin0'} 'geoname/5'
], ],
invalid: [ invalid: [
{id:undefined, type:undefined}, '/',
{id:null, type:null}, '',
{id:'', type:''}, '//',
{id:'4', type:''}, 'geoname/',
{id:'5', type:'gibberish'} '/234',
'gibberish/23'
] ]
}; };
test('invalid id and/or type', function(t) { test('invalid input', function(t) {
inputs.invalid.forEach( function( input ){ inputs.invalid.forEach( function( input ){
sanitize({ id: input.id, type: input.type }, function( err, clean ){ sanitize({ id: input }, function( err, clean ){
switch (err) { switch (err) {
case defaultIdError: case defaultIdError:
t.equal(err, defaultIdError, input.id + ' is invalid (missing id)'); break; t.equal(err, defaultIdError, input + ' is invalid (missing id)'); break;
case defaultTypeError: case defaultTypeError:
t.equal(err, defaultTypeError, input.type + ' is invalid (missing type)'); break; t.equal(err, defaultTypeError, input + ' is invalid (missing type)'); break;
case defaultFormatError:
t.equal(err, defaultFormatError, input + ' is invalid (invalid format)'); break;
case defaultMissingTypeError: case defaultMissingTypeError:
t.equal(err, defaultMissingTypeError, input.type + ' is an unknown type'); break; t.equal(err, defaultMissingTypeError, input + ' is an unknown type'); break;
default: break; default: break;
} }
t.equal(clean, undefined, 'clean not set'); t.equal(clean, undefined, 'clean not set');
@ -61,12 +65,13 @@ module.exports.tests.sanitize_id_and_type = function(test, common) {
t.end(); t.end();
}); });
test('valid id and/or type', function(t) { test('valid input', function(t) {
inputs.valid.forEach( function( input ){ inputs.valid.forEach( function( input ){
var expected = { id: input.id, type: input.type }; var input_parts = input.split('/');
sanitize({ id: input.id, type: input.type, }, function( err, clean ){ var expected = { id: input_parts[1], type: input_parts[0] };
t.equal(err, undefined, 'no error (' + input.id + ', ' + input.type + ')' ); sanitize({ id: input }, function( err, clean ){
t.deepEqual(clean, expected, 'clean set correctly (' + input.id + ', ' + input.type + ')'); t.equal(err, undefined, 'no error (' + input + ')' );
t.deepEqual(clean, expected, 'clean set correctly (' + input + ')');
}); });
}); });
t.end(); t.end();
@ -97,7 +102,7 @@ module.exports.tests.middleware_failure = function(test, common) {
module.exports.tests.middleware_success = function(test, common) { module.exports.tests.middleware_success = function(test, common) {
test('middleware success', function(t) { test('middleware success', function(t) {
var req = { query: { id: '123', type: 'geoname' }}; var req = { query: { id: 'geoname/123' }};
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, defaultClean); t.deepEqual(req.clean, defaultClean);

Loading…
Cancel
Save