Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add categories to the places endpoint #1401

Merged
merged 2 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions sanitizer/_categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,11 +39,25 @@ function _sanitize (raw, clean, categories) {
return messages;
}

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 messages;
}

function _expected () {
return [{ name: 'categories' }];
}
// export function
module.exports = () => ({
sanitize: _sanitize,
module.exports = (alwaysBlank) => ({
sanitize: alwaysBlank ? _alwaysBlank : _sanitize,
expected: _expected
});
1 change: 1 addition & 0 deletions sanitizer/place.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')()
};

Expand Down
85 changes: 85 additions & 0 deletions test/unit/sanitizer/_categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,91 @@ 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) {
const req = {
query: {
categories: 'barf'
},
clean: { }
};
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('all garbage categories', function(t) {
const req = {
query: {
categories: 'food'
},
clean: { }
};
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: [] };

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) {
const req = {
query: { },
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, undefined, 'categories should be undefined');
t.end();
});
};

module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANITIZE _categories ' + name, testFunction);
Expand Down