Add test to validate parameters

This commit is contained in:
Daniel García Aubert 2019-12-16 16:12:57 +01:00
parent 6e455a1205
commit 726e1a2268
3 changed files with 179 additions and 10 deletions

View File

@ -18,15 +18,28 @@ function filterQueryTpl ({ sql, column, srid, lng, lat, radiusInMeters } = {}) {
module.exports = class CircleFilter {
constructor (filterDefinition, filterParams) {
const { circle } = filterParams;
let _circle;
if (!circle) {
throw new Error('Circle filter expects to have a "circle" param');
const error = new Error('Circle filter expects to have a "circle" param');
error.type = 'filter';
throw error;
}
const { lng, lat, radius } = JSON.parse(circle);
try {
_circle = JSON.parse(circle);
} catch (err) {
const error = new Error('Invalid circle parameter. Expected a valid JSON');
error.type = 'filter';
throw error;
}
const { lng, lat, radius } = _circle;
if (!Number.isFinite(lng) || !Number.isFinite(lat) || !Number.isFinite(radius)) {
throw new Error('Missing parameter for Circle Filter, expected: "lng", "lat", and "radius"');
const error = new Error('Missing parameter for Circle Filter, expected: "lng", "lat", and "radius"');
error.type = 'filter';
throw error;
}
this.column = filterDefinition.column || 'the_geom_webmercator';

View File

@ -23,17 +23,25 @@ module.exports = class PolygonFilter {
const { polygon } = filterParams;
if (!polygon) {
throw new Error('Polygon filter expects to have a "polygon" param');
const error = new Error('Polygon filter expects to have a "polygon" param');
error.type = 'filter';
throw error;
}
const geojson = JSON.parse(polygon);
let geojson;
try {
geojson = JSON.parse(polygon);
} catch (err) {
const error = new Error('Invalid polygon parameter. Expected a valid GeoJSON');
error.type = 'filter';
throw error;
}
if (geojson.type !== 'Polygon') {
throw new Error('Invalid type of geometry. Valid ones: "Polygon"');
}
if (!Array.isArray(geojson.coordinates)) {
throw new Error('Invalid geometry, it must be an array of coordinates (long/lat)');
const error = new Error('Invalid type of geometry. Valid ones: "Polygon"');
error.type = 'filter';
throw error;
}
try {

View File

@ -130,6 +130,91 @@ describe('spatial filters', function () {
{ category: 'category_1', value: 1, agg: false }
]
}
}, {
dataview: 'categories',
params: {
circle: JSON.stringify({
lng: 0,
radius: 5000
}),
response: {
status: 400
}
},
expected: {
errors: [
'Missing parameter for Circle Filter, expected: "lng", "lat", and "radius"'
],
errors_with_context: [
{
type: 'filter',
message: 'Missing parameter for Circle Filter, expected: "lng", "lat", and "radius"'
}
]
}
}, {
dataview: 'categories',
params: {
circle: JSON.stringify({
lat: 0,
radius: 5000
}),
response: {
status: 400
}
},
expected: {
errors: [
'Missing parameter for Circle Filter, expected: "lng", "lat", and "radius"'
],
errors_with_context: [
{
type: 'filter',
message: 'Missing parameter for Circle Filter, expected: "lng", "lat", and "radius"'
}
]
}
}, {
dataview: 'categories',
params: {
circle: JSON.stringify({
lng: 0,
lat: 0
}),
response: {
status: 400
}
},
expected: {
errors: [
'Missing parameter for Circle Filter, expected: "lng", "lat", and "radius"'
],
errors_with_context: [
{
type: 'filter',
message: 'Missing parameter for Circle Filter, expected: "lng", "lat", and "radius"'
}
]
}
}, {
dataview: 'categories',
params: {
circle: 'wadus',
response: {
status: 400
}
},
expected: {
errors: [
'Invalid circle parameter. Expected a valid JSON'
],
errors_with_context: [
{
type: 'filter',
message: 'Invalid circle parameter. Expected a valid JSON'
}
]
}
},
{
dataview: 'categories',
@ -174,6 +259,69 @@ describe('spatial filters', function () {
{ category: 'category_1', value: 1, agg: false }
]
}
}, {
dataview: 'categories',
params: {
polygon: 'wadus',
response: {
status: 400
}
},
expected: {
errors: [
'Invalid polygon parameter. Expected a valid GeoJSON'
],
errors_with_context: [
{
type: 'filter',
message: 'Invalid polygon parameter. Expected a valid GeoJSON'
}
]
}
}, {
dataview: 'categories',
params: {
polygon: JSON.stringify({
type: 'Point',
coordinates: [30, 10]
}),
response: {
status: 400
}
},
expected: {
errors: [
'Invalid type of geometry. Valid ones: "Polygon"'
],
errors_with_context: [
{
type: 'filter',
message: 'Invalid type of geometry. Valid ones: "Polygon"'
}
]
}
}, {
dataview: 'categories',
params: {
polygon: JSON.stringify({
type: 'Polygon',
coordinates: [[[]]]
}),
response: {
status: 400
}
},
expected: {
errors: [
'Too few ordinates in GeoJSON'
],
errors_with_context: [
{
type: 'unknown',
message: 'Too few ordinates in GeoJSON'
}
]
}
},
{
dataview: 'counter',