|
|
@ -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); |
|
|
|