mirror of https://github.com/pelias/api.git
Lily He
7 years ago
committed by
GitHub
59 changed files with 1642 additions and 1016 deletions
@ -1,21 +1,24 @@ |
|||||||
var _ = require('lodash'); |
|
||||||
var sanitizeAll = require('../sanitizer/sanitizeAll'); |
var sanitizeAll = require('../sanitizer/sanitizeAll'); |
||||||
var reverseSanitizers = require('./reverse').sanitizer_list; |
var type_mapping = require('../helper/type_mapping'); |
||||||
|
|
||||||
// add categories to the sanitizer list
|
// add categories to the sanitizer list
|
||||||
var sanitizers = _.merge({}, reverseSanitizers, { |
var sanitizers = { |
||||||
categories: require('../sanitizer/_categories') |
singleScalarParameters: require('../sanitizer/_single_scalar_parameters')(), |
||||||
}); |
quattroshapes_deprecation: require('../sanitizer/_deprecate_quattroshapes')(), |
||||||
|
layers: require('../sanitizer/_targets')('layers', type_mapping.layer_mapping), |
||||||
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); }; |
sources: require('../sanitizer/_targets')('sources', type_mapping.source_mapping), |
||||||
|
// depends on the layers and sources sanitizers, must be run after them
|
||||||
// export sanitize for testing
|
sources_and_layers: require('../sanitizer/_sources_and_layers')(), |
||||||
module.exports.sanitize = sanitize; |
geonames_deprecation: require('../sanitizer/_geonames_deprecation')(), |
||||||
module.exports.sanitizer_list = sanitizers; |
size: require('../sanitizer/_size')(/* use defaults*/), |
||||||
|
private: require('../sanitizer/_flag_bool')('private', false), |
||||||
|
geo_reverse: require('../sanitizer/_geo_reverse')(), |
||||||
|
boundary_country: require('../sanitizer/_boundary_country')(), |
||||||
|
categories: require('../sanitizer/_categories')() |
||||||
|
}; |
||||||
|
|
||||||
// middleware
|
// middleware
|
||||||
module.exports.middleware = function( req, res, next ){ |
module.exports.middleware = function( req, res, next ){ |
||||||
sanitize( req, function( err, clean ){ |
sanitizeAll.runAllChecks(req, sanitizers); |
||||||
next(); |
next(); |
||||||
}); |
|
||||||
}; |
}; |
||||||
|
@ -1,20 +1,13 @@ |
|||||||
|
|
||||||
var sanitizeAll = require('../sanitizer/sanitizeAll'), |
var sanitizeAll = require('../sanitizer/sanitizeAll'), |
||||||
sanitizers = { |
sanitizers = { |
||||||
singleScalarParameters: require('../sanitizer/_single_scalar_parameters'), |
singleScalarParameters: require('../sanitizer/_single_scalar_parameters')(), |
||||||
ids: require('../sanitizer/_ids'), |
ids: require('../sanitizer/_ids')(), |
||||||
private: require('../sanitizer/_flag_bool')('private', false) |
private: require('../sanitizer/_flag_bool')('private', false) |
||||||
}; |
}; |
||||||
|
|
||||||
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); }; |
|
||||||
|
|
||||||
// export sanitize for testing
|
|
||||||
module.exports.sanitize = sanitize; |
|
||||||
module.exports.sanitizer_list = sanitizers; |
|
||||||
|
|
||||||
// middleware
|
// middleware
|
||||||
module.exports.middleware = function( req, res, next ){ |
module.exports.middleware = function(req, res, next){ |
||||||
sanitize( req, function( err, clean ){ |
sanitizeAll.runAllChecks(req, sanitizers); |
||||||
next(); |
next(); |
||||||
}); |
|
||||||
}; |
}; |
||||||
|
@ -0,0 +1,29 @@ |
|||||||
|
var sanitizeAll = require('../sanitizer/sanitizeAll'), |
||||||
|
sanitizers = { |
||||||
|
text: require('../sanitizer/_text_addressit')() |
||||||
|
}; |
||||||
|
|
||||||
|
var logger = require('pelias-logger').get('api'); |
||||||
|
var logging = require( '../helper/logging' ); |
||||||
|
var _ = require('lodash'); |
||||||
|
|
||||||
|
// middleware
|
||||||
|
module.exports.middleware = function( req, res, next ){ |
||||||
|
// if res.data already has results then don't call the _text_autocomplete sanitizer
|
||||||
|
// this has been put into place for when the libpostal integration way of querying
|
||||||
|
// ES doesn't return anything and we want to fallback to the old logic
|
||||||
|
if (_.get(res, 'data', []).length > 0) { |
||||||
|
return next(); |
||||||
|
} |
||||||
|
|
||||||
|
// log the query that caused a fallback since libpostal+new-queries didn't return anything
|
||||||
|
if (req.path === '/v1/search') { |
||||||
|
const queryText = logging.isDNT(req) ? '[text removed]' : req.clean.text; |
||||||
|
logger.info(`fallback queryText: ${queryText}`); |
||||||
|
} |
||||||
|
// calls to sanitize the input
|
||||||
|
// omits check if parameters are valid since it only calls _text_addressit
|
||||||
|
sanitizeAll.sanitize(req, sanitizers); |
||||||
|
next(); |
||||||
|
|
||||||
|
}; |
@ -0,0 +1,197 @@ |
|||||||
|
var proxyquire = require('proxyquire').noCallThru(); |
||||||
|
|
||||||
|
module.exports.tests = {}; |
||||||
|
|
||||||
|
module.exports.tests.sanitize = function(test, common) { |
||||||
|
test('verify that all sanitizers were called as expected when `res` is undefined', 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 search = proxyquire('../../../sanitizer/search_fallback', { |
||||||
|
'../sanitizer/_text_addressit': function () { |
||||||
|
return { |
||||||
|
sanitize: () => { |
||||||
|
called_sanitizers.push('_text_addressit'); |
||||||
|
return { errors: [], warnings: [] }; |
||||||
|
} |
||||||
|
}; |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
var expected_sanitizers = [ |
||||||
|
'_text_addressit' |
||||||
|
]; |
||||||
|
|
||||||
|
var req = {}; |
||||||
|
|
||||||
|
search.middleware(req, undefined, function(){ |
||||||
|
t.deepEquals(called_sanitizers, expected_sanitizers); |
||||||
|
t.end(); |
||||||
|
}); |
||||||
|
|
||||||
|
}); |
||||||
|
|
||||||
|
test('verify that all sanitizers were called as expected when `res` has no `data` property', 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 search = proxyquire('../../../sanitizer/search_fallback', { |
||||||
|
'../sanitizer/_text_addressit': function () { |
||||||
|
return { |
||||||
|
sanitize: () => { |
||||||
|
called_sanitizers.push('_text_addressit'); |
||||||
|
return { errors: [], warnings: [] }; |
||||||
|
} |
||||||
|
}; |
||||||
|
}, |
||||||
|
}); |
||||||
|
|
||||||
|
var expected_sanitizers = [ |
||||||
|
'_text_addressit' |
||||||
|
]; |
||||||
|
|
||||||
|
var req = {}; |
||||||
|
var res = {}; |
||||||
|
|
||||||
|
search.middleware(req, res, function(){ |
||||||
|
t.deepEquals(called_sanitizers, expected_sanitizers); |
||||||
|
t.end(); |
||||||
|
}); |
||||||
|
|
||||||
|
}); |
||||||
|
|
||||||
|
test('verify that all sanitizers were called as expected when res.data is empty', 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 search = proxyquire('../../../sanitizer/search_fallback', { |
||||||
|
'../sanitizer/_text_addressit': function () { |
||||||
|
return { |
||||||
|
sanitize: () => { |
||||||
|
called_sanitizers.push('_text_addressit'); |
||||||
|
return { errors: [], warnings: [] }; |
||||||
|
} |
||||||
|
}; |
||||||
|
}, |
||||||
|
}); |
||||||
|
|
||||||
|
var expected_sanitizers = [ |
||||||
|
'_text_addressit' |
||||||
|
]; |
||||||
|
|
||||||
|
var req = {}; |
||||||
|
var res = { |
||||||
|
data: [] |
||||||
|
}; |
||||||
|
|
||||||
|
search.middleware(req, res, function(){ |
||||||
|
t.deepEquals(called_sanitizers, expected_sanitizers); |
||||||
|
t.end(); |
||||||
|
}); |
||||||
|
|
||||||
|
}); |
||||||
|
|
||||||
|
test('non-empty res.data should not call the _text_autocomplete sanitizer', 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 search = proxyquire('../../../sanitizer/search_fallback', { |
||||||
|
'../sanitizer/_text_autocomplete': function() { |
||||||
|
throw new Error('_text_autocomplete sanitizer should not have been called'); |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
var expected_sanitizers = []; |
||||||
|
|
||||||
|
var req = {}; |
||||||
|
var res = { |
||||||
|
data: [{}] |
||||||
|
}; |
||||||
|
|
||||||
|
search.middleware(req, res, function(){ |
||||||
|
t.deepEquals(called_sanitizers, expected_sanitizers); |
||||||
|
t.end(); |
||||||
|
}); |
||||||
|
|
||||||
|
}); |
||||||
|
|
||||||
|
test('req.clean.text should be logged when isDNT=false', (t) => { |
||||||
|
const infoLog = []; |
||||||
|
|
||||||
|
const search = proxyquire('../../../sanitizer/search_fallback', { |
||||||
|
'pelias-logger': { |
||||||
|
get: () => { |
||||||
|
return { |
||||||
|
info: (msg) => { |
||||||
|
infoLog.push(msg); |
||||||
|
} |
||||||
|
}; |
||||||
|
} |
||||||
|
}, |
||||||
|
'../helper/logging': { |
||||||
|
isDNT: () => { return false; } |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
const req = { |
||||||
|
path: '/v1/search', |
||||||
|
clean: { |
||||||
|
text: 'this is the query text' |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
search.middleware(req, undefined, () => { |
||||||
|
t.deepEquals(infoLog, [`fallback queryText: ${req.clean.text}`]); |
||||||
|
t.end(); |
||||||
|
}); |
||||||
|
|
||||||
|
}); |
||||||
|
|
||||||
|
test('req.clean.text should not be logged when isDNT=true', (t) => { |
||||||
|
const infoLog = []; |
||||||
|
|
||||||
|
const search = proxyquire('../../../sanitizer/search_fallback', { |
||||||
|
'pelias-logger': { |
||||||
|
get: () => { |
||||||
|
return { |
||||||
|
info: (msg) => { |
||||||
|
infoLog.push(msg); |
||||||
|
} |
||||||
|
}; |
||||||
|
} |
||||||
|
}, |
||||||
|
'../helper/logging': { |
||||||
|
isDNT: () => { return true; } |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
const req = { |
||||||
|
path: '/v1/search', |
||||||
|
clean: { |
||||||
|
text: 'this is the query text' |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
search.middleware(req, undefined, () => { |
||||||
|
t.deepEquals(infoLog, ['fallback queryText: [text removed]']); |
||||||
|
t.end(); |
||||||
|
}); |
||||||
|
|
||||||
|
}); |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
module.exports.all = function (tape, common) { |
||||||
|
|
||||||
|
function test(name, testFunction) { |
||||||
|
return tape('SANITIZE /search_fallback ' + name, testFunction); |
||||||
|
} |
||||||
|
|
||||||
|
for( var testCase in module.exports.tests ){ |
||||||
|
module.exports.tests[testCase](test, common); |
||||||
|
} |
||||||
|
}; |
Loading…
Reference in new issue