More exhaustive layergroup validation:

- layers is an array and it's not empty
- layers has at least options
This commit is contained in:
Raul Ochoa 2015-01-26 15:51:10 +01:00
parent 11d9f5dd76
commit e690170689
4 changed files with 160 additions and 16 deletions

View File

@ -138,8 +138,22 @@ function isInvalidLayergroup(layergroup) {
return new Error('Missing layergroup');
}
if (!_.isArray(layergroup.layers)) {
return new Error('Missing layers array from layergroup config');
var layers = layergroup.layers;
if (!_.isArray(layers) || layers.length === 0) {
return new Error('Missing or empty layers array from layergroup config');
}
var invalidLayers = layers
.map(function(layer, layerIndex) {
return layer.options ? null : layerIndex;
})
.filter(function(layerIndex) {
return layerIndex !== null;
});
if (invalidLayers.length) {
return new Error('Missing `options` in layergroup config for layers: ' + invalidLayers.join(', '));
}
return false;

View File

@ -12,12 +12,22 @@ suite('template_maps', function() {
// configure redis pool instance to use in tests
var redis_pool = RedisPool(global.environment.redis);
var wadusLayer = {
options: {
sql: 'select 1 cartodb_id, null::geometry the_geom_webmercator',
cartocss: '#layer { marker-fill:blue; }',
cartocss_version: '2.3.0'
}
};
var validTemplate = {
version:'0.0.1',
name: 'first',
auth: {},
layergroup: {
layers: []
layers: [
wadusLayer
]
}
};
var owner = 'me';
@ -26,7 +36,7 @@ suite('template_maps', function() {
var tmap = new TemplateMaps(redis_pool);
assert.ok(tmap);
var tpl = { version:'6.6.6',
name:'k', auth: {}, layergroup: {layers:[]} };
name:'k', auth: {}, layergroup: {layers:[wadusLayer]} };
Step(
function() {
tmap.addTemplate('me', tpl, this);
@ -46,7 +56,7 @@ suite('template_maps', function() {
var tmap = new TemplateMaps(redis_pool);
assert.ok(tmap);
var tpl = { version:'0.0.1',
auth: {}, layergroup: {layers:[]} };
auth: {}, layergroup: {layers:[wadusLayer]} };
Step(
function() {
tmap.addTemplate('me', tpl, this);
@ -66,7 +76,7 @@ suite('template_maps', function() {
var tmap = new TemplateMaps(redis_pool);
assert.ok(tmap);
var tpl = { version:'0.0.1',
auth: {}, layergroup: {layers:[]} };
auth: {}, layergroup: {layers:[wadusLayer]} };
var invalidnames = [ "ab|", "a b", "a@b", "1ab", "_x", "", " x", "x " ];
var testNext = function() {
if ( ! invalidnames.length ) { done(); return; }
@ -93,7 +103,7 @@ suite('template_maps', function() {
assert.ok(tmap);
var tpl = { version:'0.0.1',
name: "valid", placeholders: {},
auth: {}, layergroup: {layers:[]} };
auth: {}, layergroup: {layers:[wadusLayer]} };
var invalidnames = [ "ab|", "a b", "a@b", "1ab", "_x", "", " x", "x " ];
var testNext = function() {
if ( ! invalidnames.length ) { done(); return; }
@ -121,7 +131,7 @@ suite('template_maps', function() {
assert.ok(tmap);
var tpl = { version:'0.0.1',
name: "valid", placeholders: { v: {} },
auth: {}, layergroup: {layers:[]} };
auth: {}, layergroup: {layers:[wadusLayer]} };
tmap.addTemplate('me', tpl, function(err) {
if ( ! err ) {
done(new Error("Unexpected success with missing placeholder default"));
@ -141,7 +151,7 @@ suite('template_maps', function() {
assert.ok(tmap);
var tpl = { version:'0.0.1',
name: "valid", placeholders: { v: { default:1 } },
auth: {}, layergroup: {layers:[]} };
auth: {}, layergroup: {layers:[wadusLayer]} };
tmap.addTemplate('me', tpl, function(err) {
if ( ! err ) {
done(new Error("Unexpected success with missing placeholder type"));
@ -163,7 +173,7 @@ suite('template_maps', function() {
assert.ok(tmap);
var tpl = { version:'0.0.1',
name: "invalid_auth1", placeholders: { },
auth: { method: 'token' }, layergroup: {layers:[]} };
auth: { method: 'token' }, layergroup: {layers:[wadusLayer]} };
tmap.addTemplate('me', tpl, function(err) {
if ( ! err ) {
done(new Error("Unexpected success with invalid token auth (undefined tokens)"));
@ -184,7 +194,7 @@ suite('template_maps', function() {
var expected_failure = false;
var tpl_id;
var tpl = { version:'0.0.1',
name: 'first', auth: {}, layergroup: {layers:[]} };
name: 'first', auth: {}, layergroup: {layers:[wadusLayer]} };
Step(
function() {
tmap.addTemplate('me', tpl, this);
@ -218,9 +228,9 @@ suite('template_maps', function() {
var tmap = new TemplateMaps(redis_pool);
assert.ok(tmap);
var expected_failure = false;
var tpl1 = { version:'0.0.1', name: 'first', auth: {}, layergroup: {layers:[]} };
var tpl1 = { version:'0.0.1', name: 'first', auth: {}, layergroup: {layers:[wadusLayer]} };
var tpl1_id;
var tpl2 = { version:'0.0.1', name: 'second', auth: {}, layergroup: {layers:[]} };
var tpl2 = { version:'0.0.1', name: 'second', auth: {}, layergroup: {layers:[wadusLayer]} };
var tpl2_id;
Step(
function addTemplate1() {
@ -281,7 +291,7 @@ suite('template_maps', function() {
var tpl = { version:'0.0.1',
name: 'first',
auth: { method: 'open' },
layergroup: {layers:[]}
layergroup: {layers:[wadusLayer]}
};
var tpl_id;
Step(
@ -436,7 +446,7 @@ suite('template_maps', function() {
max_user_templates: 2
});
assert.ok(tmap);
var tpl = { version:'0.0.1', auth: {}, layergroup: {layers:[]} };
var tpl = { version:'0.0.1', auth: {}, layergroup: {layers:[wadusLayer]} };
var expectErr = false;
var idMe = [];
var idYou = [];

View File

@ -17,7 +17,15 @@ suite('template_maps', function() {
version:'0.0.1',
name: templateName,
layergroup: {
layers: []
layers: [
{
options: {
sql: 'select 1 cartodb_id, null::geometry the_geom_webmercator',
cartocss: '#layer { marker-fill:blue; }',
cartocss_version: '2.3.0'
}
}
]
}
};

View File

@ -0,0 +1,112 @@
var assert = require('assert');
var RedisPool = require('redis-mpool');
var TemplateMaps = require('../../../lib/cartodb/template_maps.js');
var test_helper = require('../../support/test_helper');
var _ = require('underscore');
suite('template_maps', function() {
var redisPool = new RedisPool(global.environment.redis),
templateMaps = new TemplateMaps(redisPool);
var owner = 'me';
var templateName = 'wadus';
var defaultTemplate = {
version:'0.0.1',
name: templateName
};
function makeTemplate(layers) {
var layergroup = {
layers: layers
};
return _.extend({}, defaultTemplate, {
layergroup: layergroup
});
}
var layerWithMissingOptions = {},
minimumValidLayer = {
options: {
sql: 'select 1 cartodb_id, null::geometry the_geom_webmercator',
cartocss: '#layer { marker-fill:blue; }',
cartocss_version: '2.3.0'
}
};
var testScenarios = [
{
desc: 'Missing layers array does not validate',
template: makeTemplate(),
expected: {
isValid: false,
message: 'Missing or empty layers array from layergroup config'
}
},
{
desc: 'Empty layers array does not validate',
template: makeTemplate([]),
expected: {
isValid: false,
message: 'Missing or empty layers array from layergroup config'
}
},
{
desc: 'Layer with missing options does not validate',
template: makeTemplate([
layerWithMissingOptions
]),
expected: {
isValid: false,
message: 'Missing `options` in layergroup config for layers: 0'
}
},
{
desc: 'Multiple layers report invalid layer',
template: makeTemplate([
minimumValidLayer,
layerWithMissingOptions
]),
expected: {
isValid: false,
message: 'Missing `options` in layergroup config for layers: 1'
}
},
{
desc: 'default auth but specified placeholders',
template: makeTemplate([
minimumValidLayer
]),
expected: {
isValid: true,
message: ''
}
}
];
testScenarios.forEach(function(testScenario) {
test(testScenario.desc, function(done) {
templateMaps.addTemplate(owner, testScenario.template, function(err) {
if (testScenario.expected.isValid) {
assert.ok(!err);
templateMaps.delTemplate(owner, templateName, done);
} else {
assert.ok(err);
assert.equal(err.message, testScenario.expected.message);
done();
}
});
});
});
});