2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-07 16:44:45 +08:00
|
|
|
require('../support/test-helper');
|
2015-02-04 18:30:36 +08:00
|
|
|
|
2015-01-31 01:57:01 +08:00
|
|
|
var assert = require('assert');
|
|
|
|
var RedisPool = require('redis-mpool');
|
2019-10-07 16:50:14 +08:00
|
|
|
var TemplateMaps = require('../../lib/backends/template-maps');
|
2019-10-07 15:43:40 +08:00
|
|
|
var PgConnection = require('../../lib/backends/pg-connection');
|
2019-10-07 15:40:50 +08:00
|
|
|
var MapConfigNamedLayersAdapter = require('../../lib/models/mapconfig/adapter/mapconfig-named-layers-adapter');
|
2015-01-31 01:57:01 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
describe('mapconfig-named-layers-adapter', function () {
|
2015-01-31 01:57:01 +08:00
|
|
|
// configure redis pool instance to use in tests
|
2015-04-28 00:08:40 +08:00
|
|
|
var redisPool = new RedisPool(global.environment.redis);
|
2015-02-16 18:57:53 +08:00
|
|
|
var pgConnection = new PgConnection(require('cartodb-redis')({ pool: redisPool }));
|
2015-01-31 01:57:01 +08:00
|
|
|
|
|
|
|
var templateMaps = new TemplateMaps(redisPool, {
|
|
|
|
max_user_templates: global.environment.maxUserTemplates
|
|
|
|
});
|
|
|
|
|
2016-05-24 00:03:45 +08:00
|
|
|
var mapConfigNamedLayersAdapter = new MapConfigNamedLayersAdapter(templateMaps, pgConnection);
|
2015-01-31 01:57:01 +08:00
|
|
|
|
|
|
|
var wadusLayer = {
|
|
|
|
type: 'cartodb',
|
|
|
|
options: {
|
|
|
|
sql: 'select 1 cartodb_id, null::geometry the_geom_webmercator',
|
|
|
|
cartocss: '#layer { marker-fill: <%= color %>; }',
|
|
|
|
cartocss_version: '2.3.0'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var wadusMapnikLayer = {
|
|
|
|
type: 'mapnik',
|
|
|
|
options: {
|
|
|
|
sql: 'select 1 cartodb_id, null::geometry the_geom_webmercator',
|
|
|
|
cartocss: '#layer { polygon-fill: <%= polygon_color %>; }',
|
|
|
|
cartocss_version: '2.3.0'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-04 18:30:36 +08:00
|
|
|
var username = 'localhost';
|
2015-01-31 01:57:01 +08:00
|
|
|
|
|
|
|
var templateName = 'valid_template';
|
|
|
|
var template = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: templateName,
|
|
|
|
auth: {
|
|
|
|
method: 'open'
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
placeholders: {
|
|
|
|
color: {
|
|
|
|
type: 'css_color',
|
|
|
|
default: '#cc3300'
|
2015-01-31 01:57:01 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
layergroup: {
|
|
|
|
layers: [
|
|
|
|
wadusLayer
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var tokenAuthTemplateName = 'auth_valid_template';
|
|
|
|
var tokenAuthTemplate = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: tokenAuthTemplateName,
|
|
|
|
auth: {
|
|
|
|
method: 'token',
|
|
|
|
valid_tokens: ['valid1', 'valid2']
|
|
|
|
},
|
|
|
|
layergroup: {
|
|
|
|
layers: [
|
|
|
|
wadusLayer
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var multipleLayersTemplateName = 'multiple_valid_template';
|
|
|
|
var multipleLayersTemplate = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: multipleLayersTemplateName,
|
|
|
|
auth: {
|
|
|
|
method: 'token',
|
|
|
|
valid_tokens: ['valid1', 'valid2']
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
placeholders: {
|
|
|
|
polygon_color: {
|
|
|
|
type: 'css_color',
|
|
|
|
default: 'green'
|
2015-01-31 01:57:01 +08:00
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
color: {
|
|
|
|
type: 'css_color',
|
|
|
|
default: 'red'
|
2015-01-31 01:57:01 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
layergroup: {
|
|
|
|
layers: [
|
|
|
|
wadusMapnikLayer,
|
|
|
|
wadusLayer
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var namedMapLayer = {
|
|
|
|
type: 'named',
|
|
|
|
options: {
|
|
|
|
name: templateName,
|
|
|
|
config: {},
|
|
|
|
auth_tokens: []
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-31 02:11:54 +08:00
|
|
|
var nestedNamedMapTemplateName = 'nested_template';
|
2015-01-31 01:57:01 +08:00
|
|
|
var nestedNamedMapTemplate = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: nestedNamedMapTemplateName,
|
|
|
|
auth: {
|
|
|
|
method: 'open'
|
|
|
|
},
|
|
|
|
layergroup: {
|
|
|
|
layers: [
|
|
|
|
namedMapLayer
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function makeNamedMapLayerConfig (options) {
|
2015-01-31 01:57:01 +08:00
|
|
|
return {
|
|
|
|
version: '1.3.0',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
type: 'named',
|
|
|
|
options: options
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-05-24 00:03:45 +08:00
|
|
|
var params = {};
|
|
|
|
var context = {};
|
2015-01-31 01:57:01 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
beforeEach(function (done) {
|
2015-01-31 01:57:01 +08:00
|
|
|
templateMaps.addTemplate(username, template, done);
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
afterEach(function (done) {
|
2015-09-25 20:08:35 +08:00
|
|
|
templateMaps.delTemplate(username, templateName, done);
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should fail for named map layer with missing name', function (done) {
|
2015-01-31 01:57:01 +08:00
|
|
|
var missingNamedMapLayerConfig = makeNamedMapLayerConfig({
|
|
|
|
config: {}
|
|
|
|
});
|
2016-05-24 00:03:45 +08:00
|
|
|
mapConfigNamedLayersAdapter.getMapConfig(username, missingNamedMapLayerConfig, params, context,
|
2019-10-22 01:07:24 +08:00
|
|
|
function (err, mapConfig) {
|
2015-04-28 00:08:40 +08:00
|
|
|
assert.ok(err);
|
2016-05-23 21:39:11 +08:00
|
|
|
assert.ok(!mapConfig);
|
2016-05-24 01:09:57 +08:00
|
|
|
assert.ok(!context.datasource);
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(err.message, 'Missing Named Map `name` in layer options');
|
2015-02-04 18:30:36 +08:00
|
|
|
|
2015-04-28 00:08:40 +08:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
2015-01-31 01:57:01 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should fail for non-existing template name', function (done) {
|
2015-01-31 01:57:01 +08:00
|
|
|
var missingTemplateName = 'wadus';
|
|
|
|
var nonExistentNamedMapLayerConfig = makeNamedMapLayerConfig({
|
|
|
|
name: missingTemplateName
|
|
|
|
});
|
2016-05-24 00:03:45 +08:00
|
|
|
mapConfigNamedLayersAdapter.getMapConfig(username, nonExistentNamedMapLayerConfig, params, context,
|
2019-10-22 01:07:24 +08:00
|
|
|
function (err, mapConfig) {
|
2015-04-28 00:08:40 +08:00
|
|
|
assert.ok(err);
|
2016-05-23 21:39:11 +08:00
|
|
|
assert.ok(!mapConfig);
|
2016-05-24 01:09:57 +08:00
|
|
|
assert.ok(!context.datasource);
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(
|
2015-04-28 00:08:40 +08:00
|
|
|
err.message, "Template '" + missingTemplateName + "' of user '" + username + "' not found"
|
|
|
|
);
|
2015-02-04 18:30:36 +08:00
|
|
|
|
2015-04-28 00:08:40 +08:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
2015-01-31 01:57:01 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should fail if not properly authorized', function (done) {
|
|
|
|
templateMaps.addTemplate(username, tokenAuthTemplate, function (err) {
|
2015-01-31 01:57:01 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var nonAuthTokensNamedMapLayerConfig = makeNamedMapLayerConfig({
|
|
|
|
name: tokenAuthTemplateName
|
|
|
|
});
|
2016-05-24 00:03:45 +08:00
|
|
|
mapConfigNamedLayersAdapter.getMapConfig(username, nonAuthTokensNamedMapLayerConfig, params, context,
|
2019-10-22 01:07:24 +08:00
|
|
|
function (err, mapConfig) {
|
2015-04-28 00:08:40 +08:00
|
|
|
assert.ok(err);
|
2016-05-23 21:39:11 +08:00
|
|
|
assert.ok(!mapConfig);
|
2016-05-24 01:09:57 +08:00
|
|
|
assert.ok(!context.datasource);
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(err.message, "Unauthorized '" + tokenAuthTemplateName + "' template instantiation");
|
2015-04-28 00:08:40 +08:00
|
|
|
|
|
|
|
templateMaps.delTemplate(username, tokenAuthTemplateName, done);
|
|
|
|
}
|
|
|
|
);
|
2015-01-31 01:57:01 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should fail for nested named map layers', function (done) {
|
|
|
|
templateMaps.addTemplate(username, nestedNamedMapTemplate, function (err) {
|
2015-01-31 01:57:01 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var nestedNamedMapLayerConfig = makeNamedMapLayerConfig({
|
|
|
|
name: nestedNamedMapTemplateName
|
|
|
|
});
|
2016-05-24 00:03:45 +08:00
|
|
|
mapConfigNamedLayersAdapter.getMapConfig(username, nestedNamedMapLayerConfig, params, context,
|
2019-10-22 01:07:24 +08:00
|
|
|
function (err, mapConfig) {
|
2015-04-28 00:08:40 +08:00
|
|
|
assert.ok(err);
|
2016-05-23 21:39:11 +08:00
|
|
|
assert.ok(!mapConfig);
|
2016-05-24 01:09:57 +08:00
|
|
|
assert.ok(!context.datasource);
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(err.message, 'Nested named layers are not allowed');
|
2015-04-28 00:08:40 +08:00
|
|
|
|
|
|
|
templateMaps.delTemplate(username, nestedNamedMapTemplateName, done);
|
|
|
|
}
|
|
|
|
);
|
2015-01-31 01:57:01 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should return an expanded list of layers for a named map layer', function (done) {
|
2015-01-31 01:57:01 +08:00
|
|
|
var validNamedMapMapLayerConfig = makeNamedMapLayerConfig({
|
|
|
|
name: templateName
|
|
|
|
});
|
2016-05-24 00:03:45 +08:00
|
|
|
mapConfigNamedLayersAdapter.getMapConfig(username, validNamedMapMapLayerConfig, params, context,
|
2019-10-22 01:07:24 +08:00
|
|
|
function (err, mapConfig) {
|
2015-04-28 00:08:40 +08:00
|
|
|
assert.ok(!err);
|
2016-05-23 21:39:11 +08:00
|
|
|
var layers = mapConfig.layers;
|
2015-04-28 00:08:40 +08:00
|
|
|
assert.ok(layers.length, 1);
|
|
|
|
assert.ok(layers[0].type, 'cartodb');
|
2019-10-22 02:05:51 +08:00
|
|
|
assert.notStrictEqual(context.datasource.getLayerDatasource(0), undefined);
|
2015-02-04 18:30:36 +08:00
|
|
|
|
2015-04-28 00:08:40 +08:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
2015-01-31 01:57:01 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should return on auth=token with valid tokens provided', function (done) {
|
|
|
|
templateMaps.addTemplate(username, tokenAuthTemplate, function (err) {
|
2015-01-31 01:57:01 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var validAuthTokensNamedMapLayerConfig = makeNamedMapLayerConfig({
|
|
|
|
name: tokenAuthTemplateName,
|
|
|
|
auth_tokens: ['valid1']
|
|
|
|
});
|
2016-05-24 00:03:45 +08:00
|
|
|
mapConfigNamedLayersAdapter.getMapConfig(username, validAuthTokensNamedMapLayerConfig, params, context,
|
2019-10-22 01:07:24 +08:00
|
|
|
function (err, mapConfig) {
|
2015-04-28 00:08:40 +08:00
|
|
|
assert.ok(!err);
|
2016-05-23 21:39:11 +08:00
|
|
|
var layers = mapConfig.layers;
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(layers.length, 1);
|
2019-10-22 02:05:51 +08:00
|
|
|
assert.notStrictEqual(context.datasource.getLayerDatasource(0), undefined);
|
2015-01-31 01:57:01 +08:00
|
|
|
|
2015-04-28 00:08:40 +08:00
|
|
|
templateMaps.delTemplate(username, tokenAuthTemplateName, done);
|
|
|
|
}
|
|
|
|
);
|
2015-01-31 01:57:01 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should return an expanded list of layers for a named map layer, multiple layers version', function (done) {
|
|
|
|
templateMaps.addTemplate(username, multipleLayersTemplate, function (err) {
|
2015-01-31 01:57:01 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var multipleLayersNamedMapLayerConfig = makeNamedMapLayerConfig({
|
|
|
|
name: multipleLayersTemplateName,
|
|
|
|
auth_tokens: ['valid2']
|
|
|
|
});
|
2016-05-24 00:03:45 +08:00
|
|
|
mapConfigNamedLayersAdapter.getMapConfig(username, multipleLayersNamedMapLayerConfig, params, context,
|
2019-10-22 01:07:24 +08:00
|
|
|
function (err, mapConfig) {
|
2015-04-28 00:08:40 +08:00
|
|
|
assert.ok(!err);
|
2016-05-23 21:39:11 +08:00
|
|
|
var layers = mapConfig.layers;
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(layers.length, 2);
|
2015-01-31 01:57:01 +08:00
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(layers[0].type, 'mapnik');
|
|
|
|
assert.strictEqual(layers[0].options.cartocss, '#layer { polygon-fill: green; }');
|
2019-10-22 02:05:51 +08:00
|
|
|
assert.notStrictEqual(context.datasource.getLayerDatasource(0), undefined);
|
2015-01-31 01:57:01 +08:00
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(layers[1].type, 'cartodb');
|
|
|
|
assert.strictEqual(layers[1].options.cartocss, '#layer { marker-fill: red; }');
|
2019-10-22 02:05:51 +08:00
|
|
|
assert.notStrictEqual(context.datasource.getLayerDatasource(1), undefined);
|
2015-01-31 01:57:01 +08:00
|
|
|
|
2015-04-28 00:08:40 +08:00
|
|
|
templateMaps.delTemplate(username, multipleLayersTemplateName, done);
|
|
|
|
}
|
|
|
|
);
|
2015-01-31 01:57:01 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should replace template params with the given config', function (done) {
|
|
|
|
templateMaps.addTemplate(username, multipleLayersTemplate, function (err) {
|
2015-01-31 01:57:01 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
var color = '#cc3300';
|
|
|
|
var polygonColor = '#ff9900';
|
2015-01-31 01:57:01 +08:00
|
|
|
|
|
|
|
var multipleLayersNamedMapLayerConfig = makeNamedMapLayerConfig({
|
|
|
|
name: multipleLayersTemplateName,
|
|
|
|
config: {
|
|
|
|
polygon_color: polygonColor,
|
|
|
|
color: color
|
|
|
|
},
|
|
|
|
auth_tokens: ['valid2']
|
|
|
|
});
|
2016-05-24 00:03:45 +08:00
|
|
|
mapConfigNamedLayersAdapter.getMapConfig(username, multipleLayersNamedMapLayerConfig, params, context,
|
2019-10-22 01:07:24 +08:00
|
|
|
function (err, mapConfig) {
|
2015-04-28 00:08:40 +08:00
|
|
|
assert.ok(!err);
|
2016-05-23 21:39:11 +08:00
|
|
|
var layers = mapConfig.layers;
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(layers.length, 2);
|
2015-01-31 01:57:01 +08:00
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(layers[0].type, 'mapnik');
|
|
|
|
assert.strictEqual(layers[0].options.cartocss, '#layer { polygon-fill: ' + polygonColor + '; }');
|
2019-10-22 02:05:51 +08:00
|
|
|
assert.notStrictEqual(context.datasource.getLayerDatasource(0), undefined);
|
2015-01-31 01:57:01 +08:00
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(layers[1].type, 'cartodb');
|
|
|
|
assert.strictEqual(layers[1].options.cartocss, '#layer { marker-fill: ' + color + '; }');
|
2019-10-22 02:05:51 +08:00
|
|
|
assert.notStrictEqual(context.datasource.getLayerDatasource(1), undefined);
|
2015-01-31 01:57:01 +08:00
|
|
|
|
2015-04-28 00:08:40 +08:00
|
|
|
templateMaps.delTemplate(username, multipleLayersTemplateName, done);
|
|
|
|
}
|
|
|
|
);
|
2015-01-31 01:57:01 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|