From f1e5edb4cfd9852cd0ee895b09109bc8c3beb72c Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Thu, 19 Jan 2017 12:10:12 -0500 Subject: [PATCH] added back logging statement for fallback queries added unit tests to cover conditionals, simplified conditional --- sanitizer/search_fallback.js | 6 ++- test/unit/sanitizer/search_fallback.js | 64 ++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/sanitizer/search_fallback.js b/sanitizer/search_fallback.js index 27f2bc9d..9bdb0c1e 100644 --- a/sanitizer/search_fallback.js +++ b/sanitizer/search_fallback.js @@ -6,19 +6,21 @@ var sanitizeAll = require('../sanitizer/sanitizeAll'), 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 (res && res.hasOwnProperty('data') && res.data.length > 0) { + 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') { - var queryText = logging.isDNT(req) ? '[text removed]' : req.clean.text; + const queryText = logging.isDNT(req) ? '[text removed]' : req.clean.text; + logger.info(`fallback queryText: ${queryText}`); } sanitize( req, function( err, clean ){ diff --git a/test/unit/sanitizer/search_fallback.js b/test/unit/sanitizer/search_fallback.js index 82bad1b0..69e35fcd 100644 --- a/test/unit/sanitizer/search_fallback.js +++ b/test/unit/sanitizer/search_fallback.js @@ -107,6 +107,70 @@ module.exports.tests.sanitize = function(test, common) { }); + 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) {