mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
4.8 KiB
171 lines
4.8 KiB
8 years ago
|
var sanitize = require( '../../../sanitizer/_categories');
|
||
9 years ago
|
|
||
|
module.exports.tests = {};
|
||
|
|
||
|
module.exports.tests.no_categories = function(test, common) {
|
||
|
test('categories not set', function(t) {
|
||
|
var req = {
|
||
|
query: { },
|
||
|
clean: { }
|
||
|
};
|
||
|
|
||
|
var messages = sanitize(req.query, req.clean);
|
||
|
|
||
|
t.equal(req.clean.categories, undefined, 'no categories should be defined');
|
||
|
t.deepEqual(messages.errors, [], 'no error returned');
|
||
|
t.deepEqual(messages.warnings, [], 'no warnings returned');
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('categories is empty string', function(t) {
|
||
|
var req = {
|
||
|
query: {
|
||
|
categories: ''
|
||
|
},
|
||
|
clean: { }
|
||
|
};
|
||
|
|
||
|
var expected_error = 'Categories parameter cannot be left blank. See documentation of service for valid options.';
|
||
|
|
||
|
var messages = sanitize(req.query, req.clean);
|
||
|
|
||
|
t.equal(req.clean.categories, undefined, 'no categories should be defined');
|
||
|
t.deepEqual(messages.errors.length, 1, 'error returned');
|
||
|
t.deepEqual(messages.errors[0], expected_error, 'error returned');
|
||
|
t.deepEqual(messages.warnings, [], 'no warnings returned');
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('categories is an array of empty strings', function(t) {
|
||
|
var req = {
|
||
|
query: {
|
||
|
categories: ',,'
|
||
|
},
|
||
|
clean: { }
|
||
|
};
|
||
|
|
||
|
var expected_error = 'Invalid categories parameter value(s). See documentation of service for valid options.';
|
||
|
|
||
|
var messages = sanitize(req.query, req.clean);
|
||
|
|
||
|
t.equal(req.clean.categories, undefined, 'no categories should be defined');
|
||
|
t.deepEqual(messages.errors.length, 1, 'error returned');
|
||
|
t.deepEqual(messages.errors[0], expected_error, 'error returned');
|
||
|
t.deepEqual(messages.warnings, [], 'no warnings returned');
|
||
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports.tests.valid_categories = function(test, common) {
|
||
9 years ago
|
var isValidCategoryCalled = 0;
|
||
|
var validCategories = {
|
||
|
isValidCategory: function (cat) {
|
||
|
isValidCategoryCalled++;
|
||
|
return ['food','health','financial','education','government'].indexOf(cat) !== -1; }
|
||
|
};
|
||
9 years ago
|
|
||
|
test('single category', function(t) {
|
||
9 years ago
|
isValidCategoryCalled = 0;
|
||
|
|
||
9 years ago
|
var req = {
|
||
|
query: {
|
||
|
categories: 'food'
|
||
|
},
|
||
|
clean: { }
|
||
|
};
|
||
|
|
||
|
var messages = sanitize(req.query, req.clean, validCategories);
|
||
|
|
||
|
t.deepEqual(req.clean.categories, ['food'], 'categories should contain food');
|
||
|
t.deepEqual(messages.errors, [], 'no error returned');
|
||
|
t.deepEqual(messages.warnings, [], 'no warnings returned');
|
||
|
|
||
9 years ago
|
t.equal(isValidCategoryCalled, 1);
|
||
|
|
||
9 years ago
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('multiple categories', function(t) {
|
||
9 years ago
|
isValidCategoryCalled = 0;
|
||
9 years ago
|
var req = {
|
||
|
query: {
|
||
|
categories: 'food,health'
|
||
|
},
|
||
|
clean: { }
|
||
|
};
|
||
9 years ago
|
var expectedCategories = ['food', 'health'];
|
||
9 years ago
|
|
||
|
var messages = sanitize(req.query, req.clean, validCategories);
|
||
|
|
||
9 years ago
|
t.deepEqual(req.clean.categories, expectedCategories,
|
||
9 years ago
|
'clean.categories should be an array with proper values');
|
||
|
t.deepEqual(messages.errors, [], 'no error returned');
|
||
|
t.deepEqual(messages.warnings, [], 'no warnings returned');
|
||
9 years ago
|
|
||
|
t.equal(isValidCategoryCalled, expectedCategories.length);
|
||
|
|
||
9 years ago
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports.tests.invalid_categories = function(test, common) {
|
||
9 years ago
|
var isValidCategoryCalled = 0;
|
||
|
var validCategories = {
|
||
|
isValidCategory: function (cat) {
|
||
|
isValidCategoryCalled++;
|
||
|
return ['food','health','financial','education','government'].indexOf(cat) !== -1; }
|
||
|
};
|
||
9 years ago
|
|
||
|
test('garbage category', function(t) {
|
||
|
var req = {
|
||
|
query: {
|
||
|
categories: 'barf'
|
||
|
},
|
||
|
clean: { }
|
||
|
};
|
||
|
var expected_messages = {
|
||
|
errors: [
|
||
|
'Invalid categories parameter value(s). See documentation of service for valid options.'
|
||
|
],
|
||
|
warnings: []
|
||
|
};
|
||
|
|
||
|
var messages = sanitize(req.query, req.clean, validCategories);
|
||
|
|
||
|
t.deepEqual(messages, expected_messages, 'error with message returned');
|
||
|
t.equal(req.clean.categories, undefined, 'clean.categories should remain empty');
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('all garbage categories', function(t) {
|
||
|
var req = {
|
||
|
query: {
|
||
|
categories: 'barf,bleh'
|
||
|
},
|
||
|
clean: { }
|
||
|
};
|
||
|
var expected_messages = {
|
||
|
errors: [
|
||
|
'Invalid categories parameter value(s). See documentation of service for valid options.'
|
||
|
],
|
||
|
warnings: []
|
||
|
};
|
||
|
|
||
9 years ago
|
var messages = sanitize(req.query, req.clean, validCategories);
|
||
9 years ago
|
|
||
|
t.deepEqual(messages, expected_messages, 'error with message returned');
|
||
|
t.equal(req.clean.categories, undefined, 'clean.categories should remain empty');
|
||
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports.all = function (tape, common) {
|
||
|
function test(name, testFunction) {
|
||
|
return tape('SANTIZE _categories ' + name, testFunction);
|
||
|
}
|
||
|
|
||
|
for( var testCase in module.exports.tests ){
|
||
|
module.exports.tests[testCase](test, common);
|
||
|
}
|
||
|
};
|