From f9ae79dbf97fcad8ae2d8f8f17f29cbc44956041 Mon Sep 17 00:00:00 2001 From: Joxit Date: Mon, 25 Nov 2019 15:47:12 +0100 Subject: [PATCH 1/2] feat(places-category): add categories to the places endpoint Categories are something really useful (and interesting) that's why they should also be available in the Places API. This PR will add categories when we use the endpoint places e.g `/v1/places?categories&ids=...` --- sanitizer/_categories.js | 12 ++++++-- sanitizer/place.js | 1 + test/unit/sanitizer/_categories.js | 49 ++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/sanitizer/_categories.js b/sanitizer/_categories.js index f3620fc20..2077b1e4e 100644 --- a/sanitizer/_categories.js +++ b/sanitizer/_categories.js @@ -38,11 +38,19 @@ function _sanitize (raw, clean, categories) { return messages; } +function _alwaysBlank (raw, clean, categories) { + if (raw.hasOwnProperty('categories')) { + clean.categories = []; + } + + return { errors: [], warnings: [] }; +} + function _expected () { return [{ name: 'categories' }]; } // export function -module.exports = () => ({ - sanitize: _sanitize, +module.exports = (alwaysBlank) => ({ + sanitize: alwaysBlank ? _alwaysBlank : _sanitize, expected: _expected }); diff --git a/sanitizer/place.js b/sanitizer/place.js index 9b7c7fdb7..4395cc887 100644 --- a/sanitizer/place.js +++ b/sanitizer/place.js @@ -4,6 +4,7 @@ var sanitizeAll = require('../sanitizer/sanitizeAll'), debug: require('../sanitizer/_debug')(), ids: require('../sanitizer/_ids')(), private: require('../sanitizer/_flag_bool')('private', false), + categories: require('../sanitizer/_categories')(true), request_language: require('../sanitizer/_request_language')() }; diff --git a/test/unit/sanitizer/_categories.js b/test/unit/sanitizer/_categories.js index 655a7b0e0..d3fb7e651 100644 --- a/test/unit/sanitizer/_categories.js +++ b/test/unit/sanitizer/_categories.js @@ -183,6 +183,55 @@ 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 = { + query: { + categories: 'barf' + }, + clean: { } + }; + var expected_messages = { errors: [], warnings: [] }; + + var 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('all garbage categories', function(t) { + var req = { + query: { + categories: 'food' + }, + clean: { } + }; + var expected_messages = { errors: [], warnings: [] }; + + var 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('not defined categories', function(t) { + var req = { + query: { }, + clean: { } + }; + var expected_messages = { errors: [], warnings: [] }; + + var 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'); + t.end(); + }); +}; + module.exports.all = function (tape, common) { function test(name, testFunction) { return tape('SANITIZE _categories ' + name, testFunction); From 4a69858edd5843be0a4cdc4b76fc9c18cedbe5c7 Mon Sep 17 00:00:00 2001 From: Joxit Date: Thu, 28 Nov 2019 17:33:13 +0100 Subject: [PATCH 2/2] 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 2077b1e4e..291848d3a 100644 --- a/sanitizer/_categories.js +++ b/sanitizer/_categories.js @@ -2,7 +2,8 @@ const _ = require('lodash'); const categoryTaxonomy = require('pelias-categories'); const 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 (_.isString(raw.categories) && !_.isEmpty(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');