From 234f9f9738357c9c746b453dfdf74cf56ec7f86d Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Thu, 25 May 2017 11:30:40 -0400 Subject: [PATCH] fixed bug where _geonames_warnings was throwing exception on undefined clean.parsed_text refactored a bit for clarity --- sanitizer/_geonames_warnings.js | 18 +++++++++++------- test/unit/sanitizer/_geonames_warnings.js | 12 ++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/sanitizer/_geonames_warnings.js b/sanitizer/_geonames_warnings.js index e3fdfb1f..eda12131 100644 --- a/sanitizer/_geonames_warnings.js +++ b/sanitizer/_geonames_warnings.js @@ -1,26 +1,30 @@ const _ = require('lodash'); -function isAdminOnly(parsed_text) { - return ['number', 'street', 'query', 'category'].every((prop) => { - return _.isEmpty(parsed_text[prop]); - }); +const non_admin_fields = ['number', 'street', 'query', 'category']; + +function hasAnyNonAdminFields(parsed_text) { + return !_.isEmpty( + _.intersection( + _.keys(parsed_text), + non_admin_fields)); } function sanitize( raw, clean ){ // error & warning messages const messages = { errors: [], warnings: [] }; - // bail early if analysis wasn't admin-only - if (!isAdminOnly(clean.parsed_text)) { + // bail early if analysis isn't admin-only + if (_.isUndefined(clean.parsed_text) || hasAnyNonAdminFields(clean.parsed_text)) { return messages; } + // the analysis is admin-only, so add errors or warnings if geonames was requested if (_.isEqual(clean.sources, ['geonames'])) { // if requested sources is only geonames, return an error messages.errors.push('input contains only administrative area data, ' + 'no results will be returned when sources=geonames'); - } else if (_.indexOf(clean.sources, 'geonames') !== -1) { + } else if (_.includes(clean.sources, 'geonames')) { // if there are other sources besides geonames, return an warning messages.warnings.push('input contains only administrative area data, ' + 'geonames results will not be returned'); diff --git a/test/unit/sanitizer/_geonames_warnings.js b/test/unit/sanitizer/_geonames_warnings.js index ced8ad67..47881781 100644 --- a/test/unit/sanitizer/_geonames_warnings.js +++ b/test/unit/sanitizer/_geonames_warnings.js @@ -8,6 +8,18 @@ const adminProperties = ['neighbourhood', 'borough', 'city', 'county', 'state', module.exports.tests = {}; module.exports.tests.no_errors = (test, common) => { + test('undefined clean.parsed_text should exit early', (t) => { + const clean = { + sources: ['geonames'], + }; + + const messages = geonames_warnings(undefined, clean); + + t.deepEquals(messages, { errors: [], warnings: [] }); + t.end(); + + }); + test('any non-admin analysis field with only geonames sources should exit early', (t) => { adminProperties.forEach((adminProperty) => { nonAdminProperties.forEach((nonAdminProperty) => {