Browse Source

Merge branch 'private-param-235' of github.com:pelias/api into private-param-235

pull/241/head
Peter Johnson 9 years ago
parent
commit
2c973b38dc
  1. 24
      sanitiser/_details.js
  2. 31
      sanitiser/_private.js
  3. 9
      sanitiser/_truthy.js
  4. 1
      sanitiser/search.js
  5. 3
      test/unit/run.js
  6. 53
      test/unit/sanitiser/_details.js
  7. 53
      test/unit/sanitiser/_private.js
  8. 31
      test/unit/sanitiser/_truthy.js
  9. 68
      test/unit/sanitiser/search.js

24
sanitiser/_details.js

@ -1,15 +1,16 @@
var isObject = require('is-object');
var isTruthy = require('./_truthy');
// validate inputs, convert types and apply defaults
function sanitize( req, default_value ){
var clean = req.clean || {};
req.clean = req.clean || {};
var params= req.query;
if (default_value === undefined) {
default_value = true;
}
default_value = !!default_value;
// ensure the input params are a valid object
@ -17,25 +18,14 @@ function sanitize( req, default_value ){
params = {};
}
if (params.details !== undefined) {
clean.details = isTruthy(params.details);
if (params.details === undefined) {
req.clean.details = default_value;
} else {
clean.details = default_value;
req.clean.details = isTruthy(params.details);
}
req.clean = clean;
return {'error':false};
}
function isTruthy(val) {
if (typeof val === 'string') {
return ['true', '1', 'yes', 'y'].indexOf(val) !== -1;
}
return val === 1 || val === true;
}
// export function
module.exports = sanitize;

31
sanitiser/_private.js

@ -0,0 +1,31 @@
var isObject = require('is-object');
var isTruthy = require('./_truthy');
// validate inputs, convert types and apply defaults
function sanitize( req, default_value ){
req.clean = req.clean || {};
var params= req.query;
if (default_value === undefined) {
default_value = true;
}
default_value = !!default_value;
// ensure the input params are a valid object
if( !isObject( params ) ){
params = {};
}
if (params.private === undefined) {
req.clean.private = default_value;
} else {
req.clean.private = isTruthy(params.private);
}
return {'error':false};
}
module.exports = sanitize;

9
sanitiser/_truthy.js

@ -0,0 +1,9 @@
function isTruthy(val) {
if (typeof val === 'string') {
return ['true', '1', 'yes', 'y'].indexOf(val) !== -1;
}
return val === 1 || val === true;
}
module.exports = isTruthy;

1
sanitiser/search.js

@ -6,6 +6,7 @@ var _sanitize = require('../sanitiser/_sanitize'),
layers: require('../sanitiser/_layers'),
source: require('../sanitiser/_source'),
details: require('../sanitiser/_details'),
private: require('../sanitiser/_private'),
latlonzoom: require('../sanitiser/_geo_search')
};

3
test/unit/run.js

@ -8,7 +8,10 @@ var tests = [
require('./controller/search'),
require('./service/mget'),
require('./service/search'),
require('./sanitiser/_details'),
require('./sanitiser/_private'),
require('./sanitiser/_source'),
require('./sanitiser/_truthy'),
require('./sanitiser/search'),
require('./sanitiser/reverse'),
require('./sanitiser/place'),

53
test/unit/sanitiser/_details.js

@ -0,0 +1,53 @@
var sanitize = require('../../../sanitiser/_details');
module.exports.tests = {};
module.exports.tests.sanitize_details = function(test, common) {
var invalid_values = [null, -1, 123, NaN, 'abc'];
invalid_values.forEach(function(detailsValue) {
test('invalid details param ' + detailsValue, function(t) {
var req = {query: { details: detailsValue }};
sanitize(req);
t.equal(req.clean.details, false, 'default details set (to false)');
t.end();
});
});
var valid_values = ['true', true, 1, '1', 'yes', 'y'];
valid_values.forEach(function(detailsValue) {
test('valid details param ' + detailsValue, function(t) {
var req = {query: { details: detailsValue }};
sanitize(req);
t.equal(req.clean.details, true, 'details set to true');
t.end();
});
});
var valid_false_values = ['false', false, 0, '0', 'no', 'n'];
valid_false_values.forEach(function(detailsValue) {
test('test setting false explicitly ' + detailsValue, function(t) {
var req = {query: { details: detailsValue }};
sanitize(req);
t.equal(req.clean.details, false, 'details set to false');
t.end();
});
});
test('test default behavior', function(t) {
var req = {query: {}};
sanitize(req);
t.equal(req.clean.details, true, 'details set to true');
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANTIZE _details ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

53
test/unit/sanitiser/_private.js

@ -0,0 +1,53 @@
var sanitize = require('../../../sanitiser/_private');
module.exports.tests = {};
module.exports.tests.sanitize_private = function(test, common) {
var invalid_values = [null, -1, 123, NaN, 'abc'];
invalid_values.forEach(function(privateValue) {
test('invalid private param ' + privateValue, function(t) {
var req = {query: { private: privateValue }};
sanitize(req);
t.equal(req.clean.private, false, 'default private set (to false)');
t.end();
});
});
var valid_values = ['true', true, 1, '1', 'yes', 'y'];
valid_values.forEach(function(privateValue) {
test('valid private param ' + privateValue, function(t) {
var req = {query: { private: privateValue }};
sanitize(req);
t.equal(req.clean.private, true, 'private set to true');
t.end();
});
});
var valid_false_values = ['false', false, 0, '0', 'no', 'n'];
valid_false_values.forEach(function(privateValue) {
test('test setting false explicitly ' + privateValue, function(t) {
var req = {query: { private: privateValue }};
sanitize(req);
t.equal(req.clean.private, false, 'private set to false');
t.end();
});
});
test('test default behavior', function(t) {
var req = {query: {}};
sanitize(req);
t.equal(req.clean.private, true, 'private set to true');
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANTIZE _private ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

31
test/unit/sanitiser/_truthy.js

@ -0,0 +1,31 @@
var isTruthy = require('../../../sanitiser/_truthy');
module.exports.tests = {};
module.exports.tests.sanitize_truthy = function(test, common) {
var valid_values = ['true', true, 1, '1', 'yes', 'y'];
valid_values.forEach(function(value) {
test('truthy value ' + value, function(t) {
t.equal(isTruthy(value), true, 'returns true');
t.end();
});
});
var valid_false_values = ['false', false, 0, '0', 'no', 'n', null, -1, 123, NaN, 'abc'];
valid_false_values.forEach(function(value) {
test('falsey value ' + value, function(t) {
t.equal(isTruthy(value), false, 'returns false');
t.end();
});
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANTIZE _truthy ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

68
test/unit/sanitiser/search.js

@ -70,7 +70,7 @@ module.exports.tests.sanitise_valid_text = function(test, common) {
module.exports.tests.sanitize_text_with_delim = function(test, common) {
var texts = [ 'a,bcd', '123 main st, admin1', ',,,', ' ' ];
test('valid texts with a comma', function(t) {
test('valid texts with a comma', function(t) {
texts.forEach( function( text ){
sanitize({ text: text }, function( err, clean ){
var expected = JSON.parse(JSON.stringify( defaultClean ));
@ -86,6 +86,33 @@ module.exports.tests.sanitize_text_with_delim = function(test, common) {
});
};
module.exports.tests.sanitize_private_no_value = function(test, common) {
test('default private should be set to true', function(t) {
sanitize({ text: 'test' }, function( err, clean ){
t.equal(clean.private, true, 'private set to true');
});
t.end();
});
};
module.exports.tests.sanitize_private_explicit_true_value = function(test, common) {
test('explicit private should be set to true', function(t) {
sanitize({ text: 'test', private: true }, function( err, clean ){
t.equal(clean.private, true, 'private set to true');
});
t.end();
});
};
module.exports.tests.sanitize_private_explicit_false_value = function(test, common) {
test('explicit private should be set to false', function(t) {
sanitize({ text: 'test', private: false }, function( err, clean ){
t.equal(clean.private, false, 'private set to false');
});
t.end();
});
};
module.exports.tests.sanitize_lat = function(test, common) {
var lats = {
invalid: [],
@ -234,45 +261,6 @@ module.exports.tests.sanitize_size = function(test, common) {
});
};
module.exports.tests.sanitize_details = function(test, common) {
var invalid_values = [null, -1, 123, NaN, 'abc'];
invalid_values.forEach(function(details) {
test('invalid details param ' + details, function(t) {
sanitize({ text: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){
t.equal(clean.details, false, 'default details set (to false)');
t.end();
});
});
});
var valid_values = ['true', true, 1, '1', 'yes', 'y'];
valid_values.forEach(function(details) {
test('valid details param ' + details, function(t) {
sanitize({ text: 'test', details: details }, function( err, clean ){
t.equal(clean.details, true, 'details set to true');
t.end();
});
});
});
var valid_false_values = ['false', false, 0, '0', 'no', 'n'];
valid_false_values.forEach(function(details) {
test('test setting false explicitly ' + details, function(t) {
sanitize({ text: 'test', details: details }, function( err, clean ){
t.equal(clean.details, false, 'details set to false');
t.end();
});
});
});
test('test default behavior', function(t) {
sanitize({ text: 'test' }, function( err, clean ){
t.equal(clean.details, true, 'details set to true');
t.end();
});
});
};
module.exports.tests.sanitize_layers = function(test, common) {
test('unspecified', function(t) {
sanitize({ layers: undefined, text: 'test' }, function( err, clean ){

Loading…
Cancel
Save