mirror of https://github.com/pelias/api.git
Stephen Hess
8 years ago
12 changed files with 282 additions and 226 deletions
@ -0,0 +1,4 @@
|
||||
const _ = require('lodash'); |
||||
|
||||
// returns true iff req.clean.parser is addressit
|
||||
module.exports = (req, res) => _.get(req, 'clean.parser') === 'addressit'; |
@ -0,0 +1,32 @@
|
||||
const sanitizeAll = require('../sanitizer/sanitizeAll'), |
||||
sanitizers = { |
||||
text: require('../sanitizer/_text_addressit') |
||||
}; |
||||
|
||||
const sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); }; |
||||
const logger = require('pelias-logger').get('api'); |
||||
const logging = require( '../helper/logging' ); |
||||
|
||||
// middleware
|
||||
module.exports = (should_execute) => { |
||||
return 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 (!should_execute(req, res)) { |
||||
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}`); |
||||
} |
||||
|
||||
sanitize( req, function( err, clean ){ |
||||
next(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
}; |
@ -1,30 +0,0 @@
|
||||
var sanitizeAll = require('../sanitizer/sanitizeAll'), |
||||
sanitizers = { |
||||
text: require('../sanitizer/_text_addressit') |
||||
}; |
||||
|
||||
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); }; |
||||
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}`); |
||||
} |
||||
|
||||
sanitize( req, function( err, clean ){ |
||||
next(); |
||||
}); |
||||
|
||||
}; |
@ -0,0 +1,73 @@
|
||||
'use strict'; |
||||
|
||||
const _ = require('lodash'); |
||||
const is_addressit_parse = require('../../../../controller/predicates/is_addressit_parse'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = (test, common) => { |
||||
test('valid interface', t => { |
||||
t.ok(_.isFunction(is_addressit_parse), 'is_addressit_parse is a function'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.true_conditions = (test, common) => { |
||||
test('request.clean.parser=addressit should return true', t => { |
||||
const req = { |
||||
clean: { |
||||
parser: 'addressit' |
||||
} |
||||
}; |
||||
|
||||
t.ok(is_addressit_parse(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.tests.false_conditions = (test, common) => { |
||||
test('undefined request should return false', t => { |
||||
t.notOk(is_addressit_parse(undefined)); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('undefined request.clean should return false', t => { |
||||
const req = {}; |
||||
|
||||
t.notOk(is_addressit_parse(req)); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('undefined request.clean.parser should return false', t => { |
||||
const req = { |
||||
clean: {} |
||||
}; |
||||
|
||||
t.notOk(is_addressit_parse(req)); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('non-\'addressit\' request.clean.parser should return false', t => { |
||||
const req = { |
||||
clean: { |
||||
parser: 'not addressit' |
||||
} |
||||
}; |
||||
|
||||
t.notOk(is_addressit_parse(req)); |
||||
t.end(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = (tape, common) => { |
||||
function test(name, testFunction) { |
||||
return tape(`GET /is_addressit_parse ${name}`, testFunction); |
||||
} |
||||
|
||||
for( const testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,132 @@
|
||||
const proxyquire = require('proxyquire').noCallThru(); |
||||
const mock_logger = require('pelias-mock-logger'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.sanitize = (test, common) => { |
||||
test('verify that no sanitizers were called when should_execute returns false', (t) => { |
||||
t.plan(1); |
||||
|
||||
const logger = mock_logger(); |
||||
|
||||
// rather than re-verify the functionality of all the sanitizers, this test just verifies that they
|
||||
// were all called correctly
|
||||
const defer_to_addressit = proxyquire('../../../sanitizer/defer_to_addressit', { |
||||
'../sanitizer/_text_addressit': () => { |
||||
t.fail('_text_addressit should not have been called'); |
||||
}, |
||||
'pelias-logger': logger |
||||
})(() => false); |
||||
|
||||
defer_to_addressit({}, {}, () => { |
||||
t.equals(logger.getInfoMessages().length, 0); |
||||
t.end(); |
||||
}); |
||||
|
||||
}); |
||||
|
||||
test('verify that _text_addressit sanitizer was called when should_execute returns true', (t) => { |
||||
t.plan(2); |
||||
|
||||
const logger = mock_logger(); |
||||
|
||||
// rather than re-verify the functionality of all the sanitizers, this test just verifies that they
|
||||
// were all called correctly
|
||||
const defer_to_addressit = proxyquire('../../../sanitizer/defer_to_addressit', { |
||||
'../sanitizer/_text_addressit': () => { |
||||
t.pass('_text_addressit should have been called'); |
||||
return { errors: [], warnings: [] }; |
||||
}, |
||||
'pelias-logger': logger, |
||||
'../helper/logging': { |
||||
isDNT: () => false |
||||
} |
||||
})(() => true); |
||||
|
||||
const req = { |
||||
path: '/v1/search', |
||||
clean: { |
||||
text: 'this is the query text' |
||||
} |
||||
}; |
||||
|
||||
defer_to_addressit(req, {}, () => { |
||||
t.deepEquals(logger.getInfoMessages(), ['fallback queryText: this is the query text']); |
||||
t.end(); |
||||
}); |
||||
|
||||
}); |
||||
|
||||
test('query should not be logged if path != \'/v1/search\'', (t) => { |
||||
t.plan(2); |
||||
|
||||
const logger = mock_logger(); |
||||
|
||||
// rather than re-verify the functionality of all the sanitizers, this test just verifies that they
|
||||
// were all called correctly
|
||||
const defer_to_addressit = proxyquire('../../../sanitizer/defer_to_addressit', { |
||||
'../sanitizer/_text_addressit': () => { |
||||
t.pass('_text_addressit should have been called'); |
||||
return { errors: [], warnings: [] }; |
||||
}, |
||||
'pelias-logger': logger |
||||
})(() => true); |
||||
|
||||
const req = { |
||||
path: 'not /v1/search', |
||||
clean: { |
||||
text: 'this is the query text' |
||||
} |
||||
}; |
||||
|
||||
defer_to_addressit(req, {}, () => { |
||||
t.deepEquals(logger.getInfoMessages(), []); |
||||
t.end(); |
||||
}); |
||||
|
||||
}); |
||||
|
||||
test('query should be logged as [text removed] if private', (t) => { |
||||
t.plan(2); |
||||
|
||||
const logger = mock_logger(); |
||||
|
||||
// rather than re-verify the functionality of all the sanitizers, this test just verifies that they
|
||||
// were all called correctly
|
||||
const defer_to_addressit = proxyquire('../../../sanitizer/defer_to_addressit', { |
||||
'../sanitizer/_text_addressit': () => { |
||||
t.pass('_text_addressit should have been called'); |
||||
return { errors: [], warnings: [] }; |
||||
}, |
||||
'pelias-logger': logger, |
||||
'../helper/logging': { |
||||
isDNT: () => true |
||||
} |
||||
})(() => true); |
||||
|
||||
const req = { |
||||
path: '/v1/search', |
||||
clean: { |
||||
text: 'this is the query text' |
||||
} |
||||
}; |
||||
|
||||
defer_to_addressit(req, {}, () => { |
||||
t.deepEquals(logger.getInfoMessages(), ['fallback queryText: [text removed]']); |
||||
t.end(); |
||||
}); |
||||
|
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape(`SANITIZE /defer_to_addressit ${name}`, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -1,185 +0,0 @@
|
||||
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() { |
||||
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() { |
||||
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() { |
||||
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