mirror of https://github.com/pelias/api.git
Lily He
8 years ago
committed by
GitHub
25 changed files with 350 additions and 237 deletions
@ -1,13 +1,18 @@
|
||||
const _ = require('lodash'); |
||||
const Debug = require('../../helper/debug'); |
||||
const debugLog = new Debug('controller:predicates:is_admin_only_analysis'); |
||||
|
||||
module.exports = (request, response) => { |
||||
if (!request.clean.hasOwnProperty('parsed_text')) { |
||||
debugLog.push(request, false + '(no parsed_text)'); |
||||
return false; |
||||
} |
||||
|
||||
// return true only if all non-admin properties of parsed_text are empty
|
||||
return ['number', 'street', 'query', 'category', 'postalcode'].every((prop) => { |
||||
const is_admin_only_analysis = ['number', 'street', 'query', 'category', 'postalcode'].every((prop) => { |
||||
return _.isEmpty(request.clean.parsed_text[prop]); |
||||
}); |
||||
|
||||
debugLog.push(request, is_admin_only_analysis); |
||||
return is_admin_only_analysis; |
||||
}; |
||||
|
@ -1,9 +1,13 @@
|
||||
const _ = require('lodash'); |
||||
|
||||
const Debug = require('../../helper/debug'); |
||||
const debugLog = new Debug('controller:predicates:is_coarse_reverse'); |
||||
const non_coarse_layers = ['address', 'street', 'venue']; |
||||
|
||||
module.exports = (req, res) => { |
||||
// returns true if layers is undefined, empty, or contains 'address', 'street', or 'venue'
|
||||
return !_.isEmpty(req.clean.layers) && |
||||
const is_coarse_reverse = !_.isEmpty(req.clean.layers) && |
||||
_.isEmpty(_.intersection(req.clean.layers, non_coarse_layers)); |
||||
|
||||
debugLog.push(req, is_coarse_reverse); |
||||
return is_coarse_reverse; |
||||
}; |
||||
|
@ -0,0 +1,43 @@
|
||||
'use strict'; |
||||
const _ = require('lodash'); |
||||
|
||||
class Debug { |
||||
constructor(moduleName){ |
||||
this.name = moduleName || 'unnamed module'; |
||||
} |
||||
|
||||
push(req, debugMsg){ |
||||
if (!_.isEmpty(req.clean) && req.clean.enableDebug){ |
||||
req.debug = req.debug || []; |
||||
// remove the extra space character
|
||||
req.debug.push({[this.name]: debugMsg}); |
||||
// req.debug.push(`[${this.name}] ${debugMsg}`);
|
||||
} |
||||
} |
||||
// optional debugMsg passed to timer
|
||||
beginTimer(req, debugMsg){ |
||||
if (!_.isEmpty(req.clean) && req.clean.enableDebug){ |
||||
// internal object debugTimers. Doesn't get displayed in geocodeJSON
|
||||
req.debugTimers = req.debugTimers || {}; |
||||
req.debugTimers[this.name] = Date.now(); |
||||
if (debugMsg){ |
||||
this.push(req, `Timer Began: ${debugMsg}`); |
||||
} else { |
||||
this.push(req, `Timer Began`); |
||||
} |
||||
} |
||||
} |
||||
|
||||
stopTimer(req, debugMsg){ |
||||
if (!_.isEmpty(req.clean) && req.clean.enableDebug){ |
||||
let timeElapsed = Date.now() - req.debugTimers[this.name]; |
||||
if (debugMsg){ |
||||
this.push(req, `Timer Stopped: ${timeElapsed} ms: ${debugMsg}`); |
||||
} else { |
||||
this.push(req, `Timer Stopped: ${timeElapsed} ms`); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
module.exports = Debug; |
@ -0,0 +1,23 @@
|
||||
var _ = require('lodash'); |
||||
|
||||
function _sanitize(raw, clean){ |
||||
const messages = {errors: [], warnings: []}; |
||||
|
||||
if(!_.isUndefined(raw.debug) ){ |
||||
clean.enableDebug = (typeof raw.debug === 'string') ? isTruthy(raw.debug.toLowerCase()) : isTruthy( raw.debug ); |
||||
} |
||||
return messages; |
||||
} |
||||
|
||||
function _expected() { |
||||
return [{ name: 'debug' }]; |
||||
} |
||||
|
||||
function isTruthy(val) { |
||||
return _.includes( ['true', '1', 1, true], val ); |
||||
} |
||||
|
||||
module.exports = () => ({ |
||||
sanitize: _sanitize, |
||||
expected: _expected |
||||
}); |
@ -1,29 +0,0 @@
|
||||
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,95 @@
|
||||
const Debug = require('../../../helper/debug'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.debug = function(test, common) { |
||||
test('initialize the debugger with a name', (t) => { |
||||
const debugLog = new Debug('debugger'); |
||||
t.deepEquals(debugLog.name, 'debugger'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('don\'t push debug message if enableDebug is false', (t) => { |
||||
const debugLog = new Debug('debugger'); |
||||
const req = { |
||||
clean: { |
||||
enableDebug: false |
||||
} |
||||
}; |
||||
debugLog.push(req, 'This should not be pushed'); |
||||
t.deepEquals(req.debug, undefined); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('don\'t start timer if enableDebug is false', (t) => { |
||||
const debugLog = new Debug('debugger'); |
||||
const req = { |
||||
clean: { |
||||
enableDebug: false |
||||
} |
||||
}; |
||||
debugLog.beginTimer(req, 'This should not be pushed'); |
||||
t.deepEquals(req.debug, undefined); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('don\'t push debug message if req.clean is empty', (t) => { |
||||
const debugLog = new Debug('debugger'); |
||||
const req = { |
||||
clean: {} |
||||
}; |
||||
debugLog.push('This should not be pushed'); |
||||
t.deepEquals(req.debug, undefined); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('Push messages if enableDebug is true', (t) => { |
||||
const debugLog = new Debug('debugger'); |
||||
const req = { |
||||
clean: { |
||||
enableDebug: true |
||||
} |
||||
}; |
||||
const expected_req = { |
||||
debug: [ |
||||
{ |
||||
debugger: 'This should be pushed' |
||||
}, |
||||
{ |
||||
debugger: 'Timer Began: Timer 1' |
||||
} |
||||
] |
||||
}; |
||||
debugLog.push(req, 'This should be pushed'); |
||||
debugLog.beginTimer(req, 'Timer 1'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('Timer should return positive number of milliseconds', (t) => { |
||||
const debugLog = new Debug('debugger'); |
||||
const req = { |
||||
clean: { |
||||
enableDebug: true |
||||
} |
||||
}; |
||||
debugLog.beginTimer(req); |
||||
setTimeout(() => { |
||||
debugLog.stopTimer(req); |
||||
t.deepEquals(parseInt(req.debug[1].debugger.slice(15, -3)) > 0, true); |
||||
t.end(); |
||||
}, 2); |
||||
|
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('[helper] debug: ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,65 @@
|
||||
const sanitizer = require('../../../sanitizer/_debug')(); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.sanitize_debug = function(test, common) { |
||||
['true', '1', 1, true, 'TRUE', 'TrUe'].forEach((value) => { |
||||
test('debug flag is on', function(t) { |
||||
const raw = { debug: value }; |
||||
const clean = {}; |
||||
const expected_clean = { enableDebug: true }; |
||||
|
||||
const messages = sanitizer.sanitize(raw, clean); |
||||
|
||||
t.deepEquals(clean, expected_clean); |
||||
t.deepEqual(messages.errors, [], 'no error returned'); |
||||
t.deepEqual(messages.warnings, [], 'no warnings returned'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
['false', false, '0', 0, 'value', {}].forEach((value) => { |
||||
test('non-truthy values should set clean.debug to false', function(t) { |
||||
const raw = { debug: value }; |
||||
const clean = {}; |
||||
const expected_clean = { enableDebug: false }; |
||||
|
||||
const messages = sanitizer.sanitize(raw, clean); |
||||
|
||||
t.deepEquals(clean, expected_clean); |
||||
t.deepEqual(messages.errors, [], 'no error returned'); |
||||
t.deepEqual(messages.warnings, [], 'no warnings returned'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
test('undefined default flag should not set clean.debug', function(t) { |
||||
const raw = {}; |
||||
const clean = {}; |
||||
const expected_clean = {}; |
||||
|
||||
const messages = sanitizer.sanitize(raw, clean); |
||||
|
||||
t.deepEquals(clean, expected_clean); |
||||
t.deepEqual(messages.errors, [], 'no error returned'); |
||||
t.deepEqual(messages.warnings, [], 'no warnings returned'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('return an array of expected parameters in object form for validation', (t) => { |
||||
const expected = [{ name: 'debug' }]; |
||||
const validParameters = sanitizer.expected(); |
||||
t.deepEquals(validParameters, expected); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
function test(name, testFunction) { |
||||
return tape('SANITIZE _debug ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -1,197 +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 () { |
||||
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