mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
5.6 KiB
159 lines
5.6 KiB
10 years ago
|
|
||
10 years ago
|
var doc = require('../../../sanitiser/doc'),
|
||
|
_sanitize = doc.sanitize,
|
||
|
middleware = doc.middleware,
|
||
10 years ago
|
indeces = require('../../../query/indeces'),
|
||
10 years ago
|
delimiter = ':',
|
||
|
defaultLengthError = function(input) { return 'invalid param \''+ input + '\': text length, must be >0' },
|
||
|
defaultFormatError = 'invalid: must be of the format type:id for ex: \'geoname:4163334\'',
|
||
|
defaultError = 'invalid param \'id\': text length, must be >0',
|
||
|
defaultMissingTypeError = function(input) {
|
||
|
var type = input.split(delimiter)[0];
|
||
|
return type + ' is invalid. It must be one of these values - [' + indeces.join(", ") + ']'},
|
||
|
defaultClean = { ids: [ { id: '123', type: 'geoname' } ] },
|
||
|
sanitize = function(query, cb) { _sanitize({'query':query}, cb); },
|
||
|
inputs = {
|
||
|
valid: [ 'geoname:1', 'osmnode:2', 'admin0:53', 'osmway:44', 'geoname:5' ],
|
||
|
invalid: [ ':', '', '::', 'geoname:', ':234', 'gibberish:23' ]
|
||
|
};
|
||
10 years ago
|
|
||
|
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();
|
||
|
});
|
||
|
};
|
||
|
|
||
10 years ago
|
module.exports.tests.sanitize_id = function(test, common) {
|
||
10 years ago
|
test('invalid input', function(t) {
|
||
10 years ago
|
inputs.invalid.forEach( function( input ){
|
||
10 years ago
|
sanitize({ id: input }, function( err, clean ){
|
||
10 years ago
|
switch (err) {
|
||
10 years ago
|
case defaultError:
|
||
|
t.equal(err, defaultError, input + ' is invalid input'); break;
|
||
|
case defaultLengthError(input):
|
||
|
t.equal(err, defaultLengthError(input), input + ' is invalid (missing id/type)'); break;
|
||
10 years ago
|
case defaultFormatError:
|
||
|
t.equal(err, defaultFormatError, input + ' is invalid (invalid format)'); break;
|
||
10 years ago
|
case defaultMissingTypeError(input):
|
||
|
t.equal(err, defaultMissingTypeError(input), input + ' is an unknown type'); break;
|
||
10 years ago
|
default: break;
|
||
|
}
|
||
|
t.equal(clean, undefined, 'clean not set');
|
||
|
});
|
||
|
});
|
||
10 years ago
|
t.end();
|
||
10 years ago
|
});
|
||
10 years ago
|
|
||
10 years ago
|
test('valid input', function(t) {
|
||
10 years ago
|
inputs.valid.forEach( function( input ){
|
||
10 years ago
|
var input_parts = input.split(delimiter);
|
||
|
var expected = { ids: [ { id: input_parts[1], type: input_parts[0] } ] };
|
||
10 years ago
|
sanitize({ id: input }, function( err, clean ){
|
||
|
t.equal(err, undefined, 'no error (' + input + ')' );
|
||
|
t.deepEqual(clean, expected, 'clean set correctly (' + input + ')');
|
||
10 years ago
|
});
|
||
|
});
|
||
10 years ago
|
t.end();
|
||
10 years ago
|
});
|
||
|
};
|
||
|
|
||
10 years ago
|
|
||
|
module.exports.tests.sanitize_ids = function(test, common) {
|
||
|
test('invalid input', function(t) {
|
||
|
sanitize({ id: inputs.invalid }, function( err, clean ){
|
||
|
var input = inputs.invalid[0]; // since it breaks on the first invalid element
|
||
|
switch (err) {
|
||
|
case defaultError:
|
||
|
t.equal(err, defaultError, input + ' is invalid input'); break;
|
||
|
case defaultLengthError(input):
|
||
|
t.equal(err, defaultLengthError(input), input + ' is invalid (missing id/type)'); break;
|
||
|
case defaultFormatError:
|
||
|
t.equal(err, defaultFormatError, input + ' is invalid (invalid format)'); break;
|
||
|
case defaultMissingTypeError(input):
|
||
|
t.equal(err, defaultMissingTypeError(input), input + ' is an unknown type'); break;
|
||
|
default: break;
|
||
|
}
|
||
|
t.equal(clean, undefined, 'clean not set');
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('valid input', function(t) {
|
||
|
var expected={};
|
||
|
expected.ids = [];
|
||
|
inputs.valid.forEach( function( input ){
|
||
|
var input_parts = input.split(delimiter);
|
||
|
expected.ids.push({ id: input_parts[1], type: input_parts[0] });
|
||
|
});
|
||
|
sanitize({ id: inputs.valid }, function( err, clean ){
|
||
|
t.equal(err, undefined, 'no error' );
|
||
|
t.deepEqual(clean, expected, 'clean set correctly');
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
10 years ago
|
module.exports.tests.de_dupe = function(test, common) {
|
||
|
var expected = { ids: [ { id: '1', type: 'geoname' }, { id: '2', type: 'osmnode' } ] };
|
||
|
test('duplicate ids', function(t) {
|
||
|
sanitize( { id: ['geoname:1', 'osmnode:2', 'geoname:1'] }, function( err, clean ){
|
||
|
t.equal(err, undefined, 'no error' );
|
||
|
t.deepEqual(clean, expected, 'clean set correctly');
|
||
|
t.end();
|
||
|
});
|
||
|
});
|
||
|
};
|
||
|
|
||
10 years ago
|
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) {
|
||
10 years ago
|
var req = { query: { id: 'geoname' + delimiter + '123' }};
|
||
10 years ago
|
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) {
|
||
10 years ago
|
return tape('SANTIZE /doc ' + name, testFunction);
|
||
10 years ago
|
}
|
||
|
|
||
|
for( var testCase in module.exports.tests ){
|
||
|
module.exports.tests[testCase](test, common);
|
||
|
}
|
||
|
};
|