|
|
@ -1,196 +1,124 @@ |
|
|
|
|
|
|
|
const _ = require('lodash'), |
|
|
|
// @todo: refactor this test, it's pretty messy, brittle and hard to follow
|
|
|
|
proxyquire = require('proxyquire').noCallThru(); |
|
|
|
|
|
|
|
|
|
|
|
var reverse = require('../../../sanitizer/reverse'), |
|
|
|
|
|
|
|
sanitize = reverse.sanitize, |
|
|
|
|
|
|
|
middleware = reverse.middleware, |
|
|
|
|
|
|
|
sanitizer_list = reverse.sanitizer_list, |
|
|
|
|
|
|
|
defaults = require('../../../query/reverse_defaults'), |
|
|
|
|
|
|
|
defaultError = 'missing param \'lat\'', |
|
|
|
|
|
|
|
defaultClean = { 'point.lat': 0, |
|
|
|
|
|
|
|
'point.lon': 0, |
|
|
|
|
|
|
|
'boundary.circle.lat': 0, |
|
|
|
|
|
|
|
'boundary.circle.lon': 0, |
|
|
|
|
|
|
|
size: 10, |
|
|
|
|
|
|
|
private: false |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// these are the default values you would expect when no input params are specified.
|
|
|
|
|
|
|
|
// @todo: why is this different from $defaultClean?
|
|
|
|
|
|
|
|
var emptyClean = { private: false, size: 10 }; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports.tests = {}; |
|
|
|
module.exports.tests = {}; |
|
|
|
|
|
|
|
|
|
|
|
module.exports.tests.interface = function(test, common) { |
|
|
|
module.exports.tests.sanitize = function(test, common) { |
|
|
|
test('sanitize interface', function(t) { |
|
|
|
test('verify that all sanitizers were called as expected', function(t) { |
|
|
|
t.equal(typeof sanitize, 'function', 'sanitize is a function'); |
|
|
|
var called_sanitizers = []; |
|
|
|
t.equal(sanitize.length, 3, 'sanitize interface takes one param: req'); |
|
|
|
|
|
|
|
t.end(); |
|
|
|
// rather than re-verify the functionality of all the sanitizers, this test just verifies that they
|
|
|
|
}); |
|
|
|
// were all called correctly
|
|
|
|
test('middleware interface', function(t) { |
|
|
|
var reverse = proxyquire('../../../sanitizer/reverse.js', { |
|
|
|
t.equal(typeof middleware, 'function', 'middleware is a function'); |
|
|
|
'../sanitizer/_single_scalar_parameters': function () { |
|
|
|
t.equal(middleware.length, 3, 'sanitizee has a valid middleware'); |
|
|
|
return { |
|
|
|
t.end(); |
|
|
|
sanitize: () => { |
|
|
|
}); |
|
|
|
called_sanitizers.push('_single_scalar_parameters'); |
|
|
|
|
|
|
|
return { errors: [], warnings: [] }; |
|
|
|
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
}, |
|
|
|
module.exports.tests.sanitizers = function(test, common) { |
|
|
|
'../sanitizer/_deprecate_quattroshapes': function () { |
|
|
|
test('check sanitizer list', function (t) { |
|
|
|
return { |
|
|
|
var expected = ['singleScalarParameters', 'quattroshapes_deprecation', 'layers', |
|
|
|
sanitize: () => { |
|
|
|
'sources', 'sources_and_layers', 'geonames_deprecation', 'size', 'private', |
|
|
|
called_sanitizers.push('_deprecate_quattroshapes'); |
|
|
|
'geo_reverse', 'boundary_country']; |
|
|
|
return { errors: [], warnings: [] }; |
|
|
|
t.deepEqual(Object.keys(reverse.sanitizer_list), expected); |
|
|
|
} |
|
|
|
t.end(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
}, |
|
|
|
module.exports.tests.sanitize_lat = function(test, common) { |
|
|
|
'../sanitizer/_targets': function (type) { |
|
|
|
var lats = { |
|
|
|
if (['layers', 'sources'].indexOf(type) !== -1) { |
|
|
|
invalid: [], |
|
|
|
return { |
|
|
|
valid: [ 0, 45, 90, -0, '0', '45', '90', -181, -120, -91, 91, 120, 181 ], |
|
|
|
sanitize: () => { |
|
|
|
missing: ['', undefined, null] |
|
|
|
called_sanitizers.push(`_targets/${type}`); |
|
|
|
|
|
|
|
return { errors: [], warnings: [] }; |
|
|
|
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
test('invalid lat', function(t) { |
|
|
|
} else { |
|
|
|
lats.invalid.forEach( function( lat ){ |
|
|
|
throw new Error('incorrect parameters passed to _targets'); |
|
|
|
var req = { query: { 'point.lat': lat, 'point.lon': 0 } }; |
|
|
|
} |
|
|
|
sanitize(req, sanitizer_list, () => { |
|
|
|
}, |
|
|
|
t.equal(req.errors[0], 'invalid param \'point.lat\': must be >-90 and <90', lat + ' is an invalid latitude'); |
|
|
|
'../sanitizer/_sources_and_layers': function () { |
|
|
|
t.deepEqual(req.clean, emptyClean, 'clean only has default values set'); |
|
|
|
return { |
|
|
|
}); |
|
|
|
sanitize: () => { |
|
|
|
}); |
|
|
|
called_sanitizers.push('_sources_and_layers'); |
|
|
|
t.end(); |
|
|
|
return { errors: [], warnings: [] }; |
|
|
|
}); |
|
|
|
} |
|
|
|
test('valid lat', function(t) { |
|
|
|
|
|
|
|
lats.valid.forEach( function( lat ){ |
|
|
|
|
|
|
|
var req = { query: { 'point.lat': lat, 'point.lon': 0 } }; |
|
|
|
|
|
|
|
sanitize(req, sanitizer_list, () => { |
|
|
|
|
|
|
|
var expected_lat = parseFloat( lat ); |
|
|
|
|
|
|
|
t.deepEqual(req.errors, [], 'no errors'); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
t.end(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
test('missing lat', function(t) { |
|
|
|
|
|
|
|
lats.missing.forEach( function( lat ){ |
|
|
|
|
|
|
|
var req = { query: { 'point.lat': lat, 'point.lon': 0 } }; |
|
|
|
|
|
|
|
sanitize(req, sanitizer_list, () => { |
|
|
|
|
|
|
|
t.equal(req.errors[0], 'missing param \'point.lat\'', 'latitude is a required field'); |
|
|
|
|
|
|
|
t.deepEqual(req.clean, emptyClean, 'clean only has default values set'); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
t.end(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
}, |
|
|
|
module.exports.tests.sanitize_lon = function(test, common) { |
|
|
|
'../sanitizer/_geonames_deprecation': function () { |
|
|
|
var lons = { |
|
|
|
return { |
|
|
|
valid: [ -360, -181, 181, -180, -1, -0, 0, 45, 90, '-180', '0', '180' ], |
|
|
|
sanitize: () => { |
|
|
|
missing: ['', undefined, null] |
|
|
|
called_sanitizers.push('_geonames_deprecations'); |
|
|
|
|
|
|
|
return { errors: [], warnings: [] }; |
|
|
|
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
test('valid lon', function(t) { |
|
|
|
}, |
|
|
|
lons.valid.forEach( function( lon ){ |
|
|
|
'../sanitizer/_size': function () { |
|
|
|
var req = { query: { 'point.lat': 0, 'point.lon': lon } }; |
|
|
|
if (_.isEmpty(arguments)) { |
|
|
|
sanitize(req, sanitizer_list, () => { |
|
|
|
return { |
|
|
|
var expected_lon = parseFloat( lon ); |
|
|
|
sanitize: () => { |
|
|
|
t.deepEqual(req.errors, [], 'no errors'); |
|
|
|
called_sanitizers.push('_size'); |
|
|
|
}); |
|
|
|
return { errors: [], warnings: [] }; |
|
|
|
}); |
|
|
|
} |
|
|
|
t.end(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
test('missing lon', function(t) { |
|
|
|
|
|
|
|
lons.missing.forEach( function( lon ){ |
|
|
|
|
|
|
|
var req = { query: { 'point.lat': 0, 'point.lon': lon } }; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// @todo: why is lat set?
|
|
|
|
|
|
|
|
var expected = { 'point.lat': 0, private: false, size: 10 }; |
|
|
|
|
|
|
|
sanitize(req, sanitizer_list, () => { |
|
|
|
|
|
|
|
t.equal(req.errors[0], 'missing param \'point.lon\'', 'longitude is a required field'); |
|
|
|
|
|
|
|
t.deepEqual(req.clean, expected, 'clean only has default values set'); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
t.end(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
} else { |
|
|
|
module.exports.tests.sanitize_size = function(test, common) { |
|
|
|
throw new Error('should not have passed any parameters to _size'); |
|
|
|
test('invalid size value', function(t) { |
|
|
|
} |
|
|
|
var req = { query: { size: 'a', 'point.lat': 0, 'point.lon': 0 } }; |
|
|
|
}, |
|
|
|
sanitize(req, sanitizer_list, () => { |
|
|
|
'../sanitizer/_flag_bool': function () { |
|
|
|
t.equal(req.clean.size, 10, 'default size set'); |
|
|
|
if (arguments[0] === 'private' && arguments[1] === false) { |
|
|
|
t.end(); |
|
|
|
return { |
|
|
|
}); |
|
|
|
sanitize: () => { |
|
|
|
}); |
|
|
|
called_sanitizers.push('_flag_bool'); |
|
|
|
test('below min size value', function(t) { |
|
|
|
return { errors: [], warnings: [] }; |
|
|
|
var req = { query: { size: -100, 'point.lat': 0, 'point.lon': 0 } }; |
|
|
|
} |
|
|
|
sanitize(req, sanitizer_list, () => { |
|
|
|
|
|
|
|
t.equal(req.clean.size, 1, 'min size set'); |
|
|
|
|
|
|
|
t.end(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
test('above max size value', function(t) { |
|
|
|
|
|
|
|
var req = { query: { size: 9999, 'point.lat': 0, 'point.lon': 0 } }; |
|
|
|
|
|
|
|
sanitize(req, sanitizer_list, () => { |
|
|
|
|
|
|
|
t.equal(req.clean.size, 40, 'max size set'); |
|
|
|
|
|
|
|
t.end(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
} else { |
|
|
|
module.exports.tests.sanitize_private = function(test, common) { |
|
|
|
throw new Error('incorrect parameters passed to _flag_bool'); |
|
|
|
var invalid_values = [null, -1, 123, NaN, 'abc']; |
|
|
|
} |
|
|
|
invalid_values.forEach(function(value) { |
|
|
|
}, |
|
|
|
test('invalid private param ' + value, function(t) { |
|
|
|
'../sanitizer/_geo_reverse': function () { |
|
|
|
var req = { query: { 'point.lat': 0, 'point.lon': 0, 'private': value } }; |
|
|
|
return { |
|
|
|
sanitize(req, sanitizer_list, () => { |
|
|
|
sanitize: () => { |
|
|
|
t.equal(req.clean.private, false, 'default private set (to false)'); |
|
|
|
called_sanitizers.push('_geo_reverse'); |
|
|
|
t.end(); |
|
|
|
return { errors: [], warnings: [] }; |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
'../sanitizer/_boundary_country': function () { |
|
|
|
|
|
|
|
return { |
|
|
|
|
|
|
|
sanitize: () => { |
|
|
|
|
|
|
|
called_sanitizers.push('_boundary_country'); |
|
|
|
|
|
|
|
return { errors: [], warnings: [] }; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
var valid_values = ['true', true, 1, '1']; |
|
|
|
const expected_sanitizers = [ |
|
|
|
valid_values.forEach(function(value) { |
|
|
|
'_single_scalar_parameters', |
|
|
|
test('valid private param ' + value, function(t) { |
|
|
|
'_deprecate_quattroshapes', |
|
|
|
var req = { query: { 'point.lat': 0, 'point.lon': 0, 'private': value } }; |
|
|
|
'_targets/layers', |
|
|
|
sanitize(req, sanitizer_list, () => { |
|
|
|
'_targets/sources', |
|
|
|
t.equal(req.clean.private, true, 'private set to true'); |
|
|
|
'_sources_and_layers', |
|
|
|
t.end(); |
|
|
|
'_geonames_deprecations', |
|
|
|
}); |
|
|
|
'_size', |
|
|
|
}); |
|
|
|
'_flag_bool', |
|
|
|
}); |
|
|
|
'_geo_reverse', |
|
|
|
|
|
|
|
'_boundary_country' |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
var valid_false_values = ['false', false, 0]; |
|
|
|
const req = {}; |
|
|
|
valid_false_values.forEach(function(value) { |
|
|
|
const res = {}; |
|
|
|
test('test setting false explicitly ' + value, function(t) { |
|
|
|
|
|
|
|
var req = { query: { 'point.lat': 0, 'point.lon': 0, 'private': value } }; |
|
|
|
|
|
|
|
sanitize(req, sanitizer_list, () => { |
|
|
|
|
|
|
|
t.equal(req.clean.private, false, 'private set to false'); |
|
|
|
|
|
|
|
t.end(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test('test default behavior', function(t) { |
|
|
|
reverse.middleware(req, res, () => { |
|
|
|
var req = { query: { 'point.lat': 0, 'point.lon': 0 } }; |
|
|
|
t.deepEquals(called_sanitizers, expected_sanitizers); |
|
|
|
sanitize(req, sanitizer_list, () => { |
|
|
|
|
|
|
|
t.equal(req.clean.private, false, 'private set to false'); |
|
|
|
|
|
|
|
t.end(); |
|
|
|
t.end(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
module.exports.tests.middleware_success = function(test, common) { |
|
|
|
|
|
|
|
test('middleware success', function(t) { |
|
|
|
|
|
|
|
var req = { query: { 'point.lat': 0, 'point.lon': 0 }}; |
|
|
|
|
|
|
|
var next = function(){ |
|
|
|
|
|
|
|
t.deepEqual(req.errors, [], 'no error message set'); |
|
|
|
|
|
|
|
t.deepEqual(req.clean, defaultClean); |
|
|
|
|
|
|
|
t.end(); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
middleware( req, undefined, next ); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports.all = function (tape, common) { |
|
|
|
module.exports.all = function (tape, common) { |
|
|
|
|
|
|
|
|
|
|
|
function test(name, testFunction) { |
|
|
|
function test(name, testFunction) { |
|
|
|