Browse Source

test reverse, nearby, place sanitizer wrappers w/ proxyquire

pull/942/head
Lily He 7 years ago
parent
commit
07c7e7d483
  1. 74
      test/unit/sanitizer/nearby.js
  2. 137
      test/unit/sanitizer/place.js
  3. 294
      test/unit/sanitizer/reverse.js

74
test/unit/sanitizer/nearby.js

@ -1,52 +1,36 @@
var nearby = require('../../../sanitizer/nearby');
var defaults = require('../../../query/reverse_defaults');
var sanitize = nearby.sanitize;
var middleware = nearby.middleware;
var sanitizer_list = nearby.sanitizer_list;
var defaultClean = { 'point.lat': 0,
'point.lon': 0,
'boundary.circle.lat': 0,
'boundary.circle.lon': 0,
size: 10,
private: false
};
const _ = require('lodash'),
proxyquire = require('proxyquire').noCallThru();
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, 3, 'sanitize interface takes three args');
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.sanitizers = function(test, common) {
test('check sanitizer list', function (t) {
var expected = ['singleScalarParameters', 'quattroshapes_deprecation', 'layers',
'sources', 'sources_and_layers', 'geonames_deprecation', 'size', 'private',
'geo_reverse', 'boundary_country', 'categories'];
t.deepEqual(Object.keys(nearby.sanitizer_list), expected);
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);
module.exports.tests.sanitize = function(test, common) {
test('verify that all sanitizers were called as expected', function(t) {
var called_sanitizers = [];
// rather than re-verify the functionality of all the sanitizers, this test just verifies that they
// were all called correctly
var nearby = proxyquire('../../../sanitizer/nearby.js', {
'../sanitizer/_categories': function () {
return {
sanitize: () => {
called_sanitizers.push('_categories');
return { errors: [], warnings: [] };
}
};
}
});
const expected_sanitizers = [
'_categories'
];
const req = {};
const res = {};
nearby.middleware(req, res, () => {
t.deepEquals(called_sanitizers, expected_sanitizers);
t.end();
};
middleware( req, undefined, next );
});
});
};

137
test/unit/sanitizer/place.js

@ -1,108 +1,61 @@
var place = require('../../../sanitizer/place'),
sanitize = place.sanitize,
middleware = place.middleware,
sanitizer_list = place.sanitizer_list,
defaultClean = { ids: [ { source: 'geonames', layer: 'venue', id: '123' } ], private: false };
const _ = require('lodash'),
proxyquire = require('proxyquire').noCallThru();
// these are the default values you would expect when no input params are specified.
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, 3, 'sanitize interface takes three args');
t.end();
});
test('middleware interface', function(t) {
t.equal(typeof middleware, 'function', 'middleware is a function');
t.equal(middleware.length, 3, 'sanitize has a valid middleware');
t.end();
});
};
module.exports.tests.sanitizers = function(test, common) {
test('check sanitizer list', function (t) {
var expected = ['singleScalarParameters', 'ids', 'private' ];
t.deepEqual(Object.keys(place.sanitizer_list), expected);
t.end();
});
};
module.exports.tests.sanitize = function(test, common) {
test('verify that all sanitizers were called as expected', function(t) {
var called_sanitizers = [];
module.exports.tests.sanitize_private = function(test, common) {
var invalid_values = [null, -1, 123, NaN, 'abc'];
invalid_values.forEach(function(value) {
test('invalid private param ' + value, function(t) {
var req = { query: { ids:'geonames:venue:123', 'private': value } };
sanitize(req, sanitizer_list, () => {
t.deepEqual( req.errors, [], 'no errors' );
t.deepEqual( req.warnings, [], 'no warnings' );
t.equal(req.clean.private, false, 'default private set (to false)');
t.end();
});
// rather than re-verify the functionality of all the sanitizers, this test just verifies that they
// were all called correctly
var place = proxyquire('../../../sanitizer/place.js', {
'../sanitizer/_single_scalar_parameters': function () {
return {
sanitize: () => {
called_sanitizers.push('_single_scalar_parameters');
return { errors: [], warnings: [] };
}
};
},
'../sanitizer/_ids': function () {
return {
sanitize: () => {
called_sanitizers.push('_ids');
return { errors: [], warnings: [] };
}
};
},
'../sanitizer/_flag_bool': function () {
if (arguments[0] === 'private' && arguments[1] === false) {
return {
sanitize: () => {
called_sanitizers.push('_flag_bool');
return { errors: [], warnings: [] };
}
};
} else {
throw new Error('incorrect parameters passed to _flag_bool');
}
}
});
});
var valid_values = ['true', true, 1];
valid_values.forEach(function(value) {
test('valid private param ' + value, function(t) {
var req = { query: { ids:'geonames:venue:123', 'private': value } };
sanitize(req, sanitizer_list, () => {
t.deepEqual( req.errors, [], 'no errors' );
t.deepEqual( req.warnings, [], 'no warnings' );
t.equal(req.clean.private, true, 'private set to true');
t.end();
});
});
});
var valid_false_values = ['false', false, 0];
valid_false_values.forEach(function(value) {
test('test setting false explicitly ' + value, function(t) {
var req = { query: { ids:'geonames:venue:123', 'private': value } };
sanitize(req, sanitizer_list, () => {
t.deepEqual( req.errors, [], 'no errors' );
t.deepEqual( req.warnings, [], 'no warnings' );
t.equal(req.clean.private, false, 'private set to false');
t.end();
});
});
});
const expected_sanitizers = [
'_single_scalar_parameters',
'_ids',
'_flag_bool'
];
test('test default behavior', function(t) {
var req = { query: { ids:'geonames:venue:123' } };
sanitize(req, sanitizer_list, () => {
t.deepEqual( req.errors, [], 'no errors' );
t.deepEqual( req.warnings, [], 'no warnings' );
t.equal(req.clean.private, false, 'private set to false');
t.end();
});
});
};
const req = {};
const res = {};
module.exports.tests.invalid_params = function(test, common) {
test('no params', function(t) {
var req = { query: {} };
sanitize(req, sanitizer_list, () => {
t.equal( req.errors[0], 'invalid param \'ids\': length must be >0', 'error for missing `ids` param');
t.deepEqual( req.warnings, [], 'no warnings' );
place.middleware(req, res, () => {
t.deepEquals(called_sanitizers, expected_sanitizers);
t.end();
});
});
};
module.exports.tests.middleware_success = function(test, common) {
test('middleware success', function(t) {
var req = { query: { ids: 'geonames:venue:123' }};
var next = function(){
t.deepEqual( req.errors, [], 'no errors' );
t.deepEqual( req.warnings, [], 'no warnings' );
t.deepEqual(req.clean, defaultClean);
t.end();
};
middleware( req, undefined, next );
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {

294
test/unit/sanitizer/reverse.js

@ -1,194 +1,122 @@
// @todo: refactor this test, it's pretty messy, brittle and hard to follow
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 };
const _ = require('lodash'),
proxyquire = require('proxyquire').noCallThru();
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, 3, 'sanitize interface takes one param: req');
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.sanitizers = function(test, common) {
test('check sanitizer list', function (t) {
var expected = ['singleScalarParameters', 'quattroshapes_deprecation', 'layers',
'sources', 'sources_and_layers', 'geonames_deprecation', 'size', 'private',
'geo_reverse', 'boundary_country'];
t.deepEqual(Object.keys(reverse.sanitizer_list), expected);
t.end();
});
};
module.exports.tests.sanitize_lat = function(test, common) {
var lats = {
invalid: [],
valid: [ 0, 45, 90, -0, '0', '45', '90', -181, -120, -91, 91, 120, 181 ],
missing: ['', undefined, null]
};
test('invalid lat', function(t) {
lats.invalid.forEach( function( lat ){
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');
t.deepEqual(req.clean, emptyClean, 'clean only has default values set');
});
});
t.end();
});
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) {
var lons = {
valid: [ -360, -181, 181, -180, -1, -0, 0, 45, 90, '-180', '0', '180' ],
missing: ['', undefined, null]
};
test('valid lon', function(t) {
lons.valid.forEach( function( lon ){
var req = { query: { 'point.lat': 0, 'point.lon': lon } };
sanitize(req, sanitizer_list, () => {
var expected_lon = parseFloat( lon );
t.deepEqual(req.errors, [], 'no errors');
});
module.exports.tests.sanitize = function(test, common) {
test('verify that all sanitizers were called as expected', function(t) {
var called_sanitizers = [];
// rather than re-verify the functionality of all the sanitizers, this test just verifies that they
// were all called correctly
var reverse = proxyquire('../../../sanitizer/reverse.js', {
'../sanitizer/_single_scalar_parameters': function () {
return {
sanitize: () => {
called_sanitizers.push('_single_scalar_parameters');
return { errors: [], warnings: [] };
}
};
},
'../sanitizer/_deprecate_quattroshapes': function () {
return {
sanitize: () => {
called_sanitizers.push('_deprecate_quattroshapes');
return { errors: [], warnings: [] };
}
};
},
'../sanitizer/_targets': function (type) {
if (['layers', 'sources'].indexOf(type) !== -1) {
return {
sanitize: () => {
called_sanitizers.push(`_targets/${type}`);
return { errors: [], warnings: [] };
}
};
} else {
throw new Error('incorrect parameters passed to _targets');
}
},
'../sanitizer/_sources_and_layers': function () {
return {
sanitize: () => {
called_sanitizers.push('_sources_and_layers');
return { errors: [], warnings: [] };
}
};
},
'../sanitizer/_geonames_deprecation': function () {
return {
sanitize: () => {
called_sanitizers.push('_geonames_deprecations');
return { errors: [], warnings: [] };
}
};
},
'../sanitizer/_size': function () {
if (_.isEmpty(arguments)) {
return {
sanitize: () => {
called_sanitizers.push('_size');
return { errors: [], warnings: [] };
}
};
} else {
throw new Error('should not have passed any parameters to _size');
}
},
'../sanitizer/_flag_bool': function () {
if (arguments[0] === 'private' && arguments[1] === false) {
return {
sanitize: () => {
called_sanitizers.push('_flag_bool');
return { errors: [], warnings: [] };
}
};
} else {
throw new Error('incorrect parameters passed to _flag_bool');
}
},
'../sanitizer/_geo_reverse': function () {
return {
sanitize: () => {
called_sanitizers.push('_geo_reverse');
return { errors: [], warnings: [] };
}
};
},
'../sanitizer/_boundary_country': function () {
return {
sanitize: () => {
called_sanitizers.push('_boundary_country');
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();
});
};
module.exports.tests.sanitize_size = function(test, common) {
test('invalid size value', function(t) {
var req = { query: { size: 'a', 'point.lat': 0, 'point.lon': 0 } };
sanitize(req, sanitizer_list, () => {
t.equal(req.clean.size, 10, 'default size set');
const expected_sanitizers = [
'_single_scalar_parameters',
'_deprecate_quattroshapes',
'_targets/layers',
'_targets/sources',
'_sources_and_layers',
'_geonames_deprecations',
'_size',
'_flag_bool',
'_geo_reverse',
'_boundary_country'
];
const req = {};
const res = {};
reverse.middleware(req, res, () => {
t.deepEquals(called_sanitizers, expected_sanitizers);
t.end();
});
});
test('below min size value', function(t) {
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();
});
});
};
module.exports.tests.sanitize_private = function(test, common) {
var invalid_values = [null, -1, 123, NaN, 'abc'];
invalid_values.forEach(function(value) {
test('invalid private param ' + value, function(t) {
var req = { query: { 'point.lat': 0, 'point.lon': 0, 'private': value } };
sanitize(req, sanitizer_list, () => {
t.equal(req.clean.private, false, 'default private set (to false)');
t.end();
});
});
});
var valid_values = ['true', true, 1, '1'];
valid_values.forEach(function(value) {
test('valid private param ' + value, function(t) {
var req = { query: { 'point.lat': 0, 'point.lon': 0, 'private': value } };
sanitize(req, sanitizer_list, () => {
t.equal(req.clean.private, true, 'private set to true');
t.end();
});
});
});
var valid_false_values = ['false', false, 0];
valid_false_values.forEach(function(value) {
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) {
var req = { query: { 'point.lat': 0, 'point.lon': 0 } };
sanitize(req, sanitizer_list, () => {
t.equal(req.clean.private, false, 'private set to false');
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) {

Loading…
Cancel
Save