Skip to content

Commit

Permalink
feat(places-category): Add warning message for non-empty categories
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxit committed Nov 28, 2019
1 parent 9a8e898 commit 7a81375
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
11 changes: 9 additions & 2 deletions sanitizer/_categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 () {
Expand Down
54 changes: 45 additions & 9 deletions test/unit/sanitizer/_categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,45 +186,81 @@ 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');
t.end();
});

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');
t.end();
});

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');
Expand Down

0 comments on commit 7a81375

Please sign in to comment.