Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bboure committed Sep 22, 2020
1 parent 5f663ea commit 12886fe
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 7 deletions.
4 changes: 3 additions & 1 deletion sanitizer/_sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ function _setup(){

clean.sort = raw.sort;

if( clean.sort && !allowed_values.includes(clean.sort) ){
if (!clean.sort) {
clean.sort = DEFAULT_SORT;
} else if( !allowed_values.includes(clean.sort) ){
messages.warnings.push('invalid \'sort\', using \'distance\'');
clean.sort = DEFAULT_SORT;
}
Expand Down
6 changes: 3 additions & 3 deletions sanitizer/nearby.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ module.exports.middleware = (_api_pelias_config) => {
sources_and_layers: require('../sanitizer/_sources_and_layers')(),
geonames_deprecation: require('../sanitizer/_geonames_deprecation')(),
size: require('../sanitizer/_size')(/* use defaults*/),
sort: require('../sanitizer/_sort')(),
private: require('../sanitizer/_flag_bool')('private', false),
geo_reverse: require('../sanitizer/_geo_reverse')(),
boundary_country: require('../sanitizer/_boundary_country')(),
categories: require('../sanitizer/_categories')(),
request_language: require('../sanitizer/_request_language')()
request_language: require('../sanitizer/_request_language')(),
sort: require('../sanitizer/_sort')()
};

return function( req, res, next ){
sanitizeAll.runAllChecks(req, sanitizers);
next();
};
};
};
24 changes: 23 additions & 1 deletion test/unit/query/reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const views = {
layers: 'layers view',
categories: 'categories view',
sort_distance: 'sort_distance view',
sort_popularity: 'sort_popularity view',
boundary_gid: 'boundary_gid view'
};

Expand Down Expand Up @@ -70,7 +71,8 @@ module.exports.tests.query = (test, common) => {
]);

t.deepEquals(query.body.sort_functions, [
'sort_distance view'
'sort_distance view',
'sort_popularity view'
]);

t.end();
Expand Down Expand Up @@ -98,6 +100,26 @@ module.exports.tests.query = (test, common) => {

});

test('clean.sort should set sort:field parameter', t => {
const clean = {
sort: 'popularity'
};

const query = proxyquire('../../../query/reverse', {
'pelias-query': {
layout: {
FilteredBooleanQuery: MockQuery
},
view: views,
Vars: require('pelias-query').Vars
},
'./reverse_defaults': {}
})(clean);

t.deepEquals(query.body.vs.var('sort:field').toString(), 'popularity');
t.end();

});
};

module.exports.tests.sources = (test, common) => {
Expand Down
1 change: 1 addition & 0 deletions test/unit/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ var tests = [
require('./sanitizer/_tokenizer'),
require('./sanitizer/_categories'),
require('./sanitizer/_boundary_gid'),
require('./sanitizer/_sort'),
require('./sanitizer/nearby'),
require('./sanitizer/autocomplete'),
require('./sanitizer/structured_geocoding'),
Expand Down
56 changes: 56 additions & 0 deletions test/unit/sanitizer/_sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var sanitizer = require('../../../sanitizer/_sort');

module.exports.tests = {};

module.exports.tests.sanitize_sort = function(test, common) {
test('sort=foo', function(t) {
var raw = { sort: 'foo' };
var clean = {};
var res = sanitizer().sanitize(raw, clean);
t.equal(res.errors.length, 0, 'should return no errors');
t.equal(res.warnings.length, 1, 'should return warning');
t.equal(res.warnings[0], 'invalid \'sort\', using \'distance\'');
t.equal(clean.sort, 'distance', 'default to distance');
t.end();
});

test('sort not set', function(t) {
var raw = {};
var clean = {};
var res = sanitizer().sanitize(raw, clean);
t.equal(res.errors.length, 0, 'should return no errors');
t.equal(res.warnings.length, 0, 'should return no warning');
t.equal(clean.sort, 'distance', 'default to distance');
t.end();
});

test('return an array of expected parameters in object form for validation', function(t) {
const expected = [{ name: 'sort' }];
const validParameters = sanitizer().expected();
t.deepEquals(validParameters, expected);
t.end();
});

var valid_sorts = ['popularity', 'distance'];
valid_sorts.forEach(function (sort) {
test('sort=' + sort, function (t) {
var raw = {sort: sort};
var clean = {};
var res = sanitizer().sanitize(raw, clean);
t.equal(res.errors.length, 0, 'should return no errors');
t.equal(res.warnings.length, 0, 'should return warning');
t.equal(clean.sort, sort, 'set to correct value');
t.end();
});
});
};

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

for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
13 changes: 11 additions & 2 deletions test/unit/sanitizer/nearby.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,15 @@ module.exports.tests.sanitize = function(test, common) {
return { errors: [], warnings: [] };
}
};
}
},
'../sanitizer/_sort': function () {
return {
sanitize: () => {
called_sanitizers.push('_sort');
return { errors: [], warnings: [] };
}
};
},
});

const expected_sanitizers = [
Expand All @@ -124,7 +132,8 @@ module.exports.tests.sanitize = function(test, common) {
'_geo_reverse',
'_boundary_country',
'_categories',
'_request_language'
'_request_language',
'_sort',
];

const req = {};
Expand Down

0 comments on commit 12886fe

Please sign in to comment.