From 7a8137564dd86f775fa307c08f60f17a0f3f567e Mon Sep 17 00:00:00 2001 From: Joxit Date: Thu, 28 Nov 2019 17:33:13 +0100 Subject: [PATCH] feat(places-category): Add warning message for non-empty categories --- sanitizer/_categories.js | 11 ++++-- test/unit/sanitizer/_categories.js | 54 +++++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/sanitizer/_categories.js b/sanitizer/_categories.js index 7b618845a..3702a4c1e 100644 --- a/sanitizer/_categories.js +++ b/sanitizer/_categories.js @@ -2,7 +2,8 @@ var check = require('check-types'); var categoryTaxonomy = require('pelias-categories'); var WARNINGS = { - empty: 'Categories parameter left blank, showing results from all categories.' + empty: 'Categories parameter left blank, showing results from all categories.', + notEmpty: 'Categories filtering not supported on this endpoint, showing results from all categories.' }; // validate inputs, convert types and apply defaults @@ -39,11 +40,17 @@ function _sanitize (raw, clean, categories) { } function _alwaysBlank (raw, clean, categories) { + // error & warning messages + const messages = { errors: [], warnings: [] }; + if (raw.hasOwnProperty('categories')) { clean.categories = []; + if (check.nonEmptyString(raw.categories)) { + messages.warnings.push(WARNINGS.notEmpty); + } } - return { errors: [], warnings: [] }; + return messages; } function _expected () { diff --git a/test/unit/sanitizer/_categories.js b/test/unit/sanitizer/_categories.js index d3fb7e651..8593dd8ee 100644 --- a/test/unit/sanitizer/_categories.js +++ b/test/unit/sanitizer/_categories.js @@ -186,15 +186,17 @@ module.exports.tests.invalid_categories = function(test, common) { module.exports.tests.always_blank = function(test, common) { const alwaysBlankSanitizer = require( '../../../sanitizer/_categories')(true); test('garbage category', function(t) { - var req = { + const req = { query: { categories: 'barf' }, clean: { } }; - var expected_messages = { errors: [], warnings: [] }; + const expected_messages = { errors: [], warnings: [ + 'Categories filtering not supported on this endpoint, showing results from all categories.' + ] }; - var messages = alwaysBlankSanitizer.sanitize(req.query, req.clean); + const messages = alwaysBlankSanitizer.sanitize(req.query, req.clean); t.deepEqual(messages, expected_messages, 'error with message returned'); t.deepEqual(req.clean.categories, [], 'should return empty array'); @@ -202,15 +204,49 @@ module.exports.tests.always_blank = function(test, common) { }); test('all garbage categories', function(t) { - var req = { + const req = { query: { categories: 'food' }, clean: { } }; - var expected_messages = { errors: [], warnings: [] }; + const expected_messages = { errors: [], warnings: [ + 'Categories filtering not supported on this endpoint, showing results from all categories.' + ] }; + + const messages = alwaysBlankSanitizer.sanitize(req.query, req.clean); + + t.deepEqual(messages, expected_messages, 'error with message returned'); + t.deepEqual(req.clean.categories, [], 'should return empty array'); + t.end(); + }); + + test('defined categories', function(t) { + const req = { + query: { + categories: undefined + }, + clean: { } + }; + const expected_messages = { errors: [], warnings: [] }; - var messages = alwaysBlankSanitizer.sanitize(req.query, req.clean); + const messages = alwaysBlankSanitizer.sanitize(req.query, req.clean); + + t.deepEqual(messages, expected_messages, 'error with message returned'); + t.deepEqual(req.clean.categories, [], 'should return empty array'); + t.end(); + }); + + test('empty categories', function(t) { + const req = { + query: { + categories: '' + }, + clean: { } + }; + const expected_messages = { errors: [], warnings: [] }; + + const messages = alwaysBlankSanitizer.sanitize(req.query, req.clean); t.deepEqual(messages, expected_messages, 'error with message returned'); t.deepEqual(req.clean.categories, [], 'should return empty array'); @@ -218,13 +254,13 @@ module.exports.tests.always_blank = function(test, common) { }); test('not defined categories', function(t) { - var req = { + const req = { query: { }, clean: { } }; - var expected_messages = { errors: [], warnings: [] }; + const expected_messages = { errors: [], warnings: [] }; - var messages = alwaysBlankSanitizer.sanitize(req.query, req.clean); + const messages = alwaysBlankSanitizer.sanitize(req.query, req.clean); t.deepEqual(messages, expected_messages, 'error with message returned'); t.deepEqual(req.clean.categories, undefined, 'categories should be undefined');