Browse Source

Merge branch '245-cleanup-autocomplete-sanitisers'

pull/265/head
Julian Simioni 10 years ago
parent
commit
07f854f565
  1. 3
      routes/v1.js
  2. 19
      sanitiser/_geo_autocomplete.js
  3. 24
      sanitiser/autocomplete.js
  4. 1
      test/unit/run.js
  5. 21
      test/unit/sanitiser/autocomplete.js

3
routes/v1.js

@ -4,6 +4,7 @@ var reverseQuery = require('../query/reverse');
/** ----------------------- sanitisers ----------------------- **/
var sanitisers = {
autocomplete: require('../sanitiser/autocomplete'),
place: require('../sanitiser/place'),
search: require('../sanitiser/search'),
reverse: require('../sanitiser/reverse')
@ -60,7 +61,7 @@ function addRoutes(app, peliasConfig) {
postProc.sendJSON
]),
autocomplete: createRouter([
sanitisers.search.middleware,
sanitisers.autocomplete.middleware,
middleware.types,
controllers.search(null, require('../query/autocomplete')),
postProc.confidenceScores(peliasConfig),

19
sanitiser/_geo_autocomplete.js

@ -0,0 +1,19 @@
var geo_common = require ('./_geo_common');
var LAT_LON_IS_REQUIRED = false;
// validate inputs, convert types and apply defaults
module.exports = function sanitize( raw, clean ){
// error & warning messages
var messages = { errors: [], warnings: [] };
try {
geo_common.sanitize_coord( 'lat', clean, raw['focus.point.lat'], LAT_LON_IS_REQUIRED );
geo_common.sanitize_coord( 'lon', clean, raw['focus.point.lon'], LAT_LON_IS_REQUIRED );
}
catch (err) {
messages.errors.push( err.message );
}
return messages;
};

24
sanitiser/autocomplete.js

@ -0,0 +1,24 @@
var sanitizeAll = require('../sanitiser/sanitizeAll'),
sanitizers = {
text: require('../sanitiser/_text'),
size: require('../sanitiser/_size'),
private: require('../sanitiser/_flag_bool')('private', false),
geo_autocomplete: require('../sanitiser/_geo_autocomplete'),
};
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); };
// export sanitize for testing
module.exports.sanitize = sanitize;
module.exports.sanitiser_list = sanitizers;
// middleware
module.exports.middleware = function( req, res, next ){
sanitize( req, function( err, clean ){
if( err ){
res.status(400); // 400 Bad Request
return next(err);
}
next();
});
};

1
test/unit/run.js

@ -9,6 +9,7 @@ var tests = [
require('./service/mget'),
require('./service/search'),
require('./sanitiser/_flag_bool'),
require('./sanitiser/autocomplete'),
require('./sanitiser/_sources'),
require('./sanitiser/search'),
require('./sanitiser/_layers'),

21
test/unit/sanitiser/autocomplete.js

@ -0,0 +1,21 @@
var autocomplete = require('../../../sanitiser/autocomplete');
module.exports.tests = {};
module.exports.tests.sanitisers = function(test, common) {
test('check sanitiser list', function (t) {
var expected = ['text', 'size', 'private', 'geo_autocomplete' ];
t.deepEqual(Object.keys(autocomplete.sanitiser_list), expected);
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANTIZE /autocomplete ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
Loading…
Cancel
Save