2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-06 23:43:25 +08:00
|
|
|
var assert = require('../support/assert');
|
|
|
|
var step = require('step');
|
2019-10-07 15:40:50 +08:00
|
|
|
var LayergroupToken = require('../../lib/models/layergroup-token');
|
2019-10-07 16:55:26 +08:00
|
|
|
var testHelper = require('../support/test-helper');
|
2020-06-02 17:49:54 +08:00
|
|
|
var createServer = require('../../lib/server');
|
2019-10-07 16:55:26 +08:00
|
|
|
var serverOptions = require('../../lib/server-options');
|
2016-04-06 23:43:25 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
describe('dynamic styling for named maps', function () {
|
2018-04-16 22:16:23 +08:00
|
|
|
var server;
|
|
|
|
|
|
|
|
before(function () {
|
2020-06-02 17:49:54 +08:00
|
|
|
server = createServer(serverOptions);
|
2018-04-16 22:16:23 +08:00
|
|
|
});
|
2016-04-06 23:43:25 +08:00
|
|
|
|
|
|
|
var keysToDelete;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
beforeEach(function () {
|
2016-04-06 23:43:25 +08:00
|
|
|
keysToDelete = {};
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
afterEach(function (done) {
|
2016-04-06 23:43:25 +08:00
|
|
|
testHelper.deleteRedisKeys(keysToDelete, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
var templateId = 'dynamic-styling-template-1';
|
|
|
|
|
|
|
|
var template = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: templateId,
|
|
|
|
auth: { method: 'open' },
|
|
|
|
placeholders: {
|
|
|
|
color: {
|
2019-10-22 01:07:24 +08:00
|
|
|
type: 'css_color',
|
|
|
|
default: 'Reds'
|
2016-04-06 23:43:25 +08:00
|
|
|
}
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
layergroup: {
|
2016-04-06 23:43:25 +08:00
|
|
|
version: '1.0.0',
|
|
|
|
layers: [{
|
|
|
|
options: {
|
|
|
|
sql: 'SELECT * FROM test_table',
|
|
|
|
cartocss: [
|
|
|
|
'#layer {',
|
|
|
|
' marker-fill: #000;',
|
|
|
|
'}'
|
|
|
|
].join('\n'),
|
|
|
|
cartocss_version: '2.0.2'
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
options: {
|
|
|
|
sql: 'SELECT * FROM test_table',
|
|
|
|
cartocss: [
|
|
|
|
'#layer {',
|
|
|
|
' marker-fill: #000;',
|
|
|
|
'}'
|
|
|
|
].join('\n'),
|
|
|
|
cartocss_version: '2.0.2'
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
options: {
|
|
|
|
sql: 'SELECT * FROM test_table',
|
|
|
|
cartocss: [
|
|
|
|
'#layer {',
|
|
|
|
' marker-fill: #000;',
|
|
|
|
'}'
|
|
|
|
].join('\n'),
|
|
|
|
cartocss_version: '2.0.2'
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var templateParams = {
|
|
|
|
styles: {
|
|
|
|
0: [
|
|
|
|
'#layer {',
|
|
|
|
' marker-fill: #fabada;',
|
|
|
|
'}'
|
|
|
|
].join('\n'),
|
|
|
|
2: [
|
|
|
|
'#layer {',
|
|
|
|
' marker-fill: #cebada;',
|
|
|
|
'}'
|
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
it('should instantiate a template applying cartocss dynamicly', function (done) {
|
|
|
|
step(
|
2019-10-22 01:07:24 +08:00
|
|
|
function postTemplate () {
|
2016-04-06 23:43:25 +08:00
|
|
|
var next = this;
|
|
|
|
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
|
|
|
method: 'POST',
|
|
|
|
headers: { host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template)
|
|
|
|
}, {},
|
|
|
|
function (res, err) {
|
|
|
|
next(err, res);
|
|
|
|
});
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function checkTemplate (err, res) {
|
2016-04-06 23:43:25 +08:00
|
|
|
assert.ifError(err);
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200);
|
2019-10-22 01:52:51 +08:00
|
|
|
assert.deepStrictEqual(JSON.parse(res.body), {
|
2016-04-06 23:43:25 +08:00
|
|
|
template_id: templateId
|
|
|
|
});
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function instantiateTemplate (err) {
|
2016-04-06 23:43:25 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/map/named/' + templateId,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
host: 'localhost',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
data: JSON.stringify(templateParams)
|
|
|
|
}, {},
|
|
|
|
function (res, err) {
|
|
|
|
return next(err, res);
|
|
|
|
});
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function checkInstanciation (err, res) {
|
2016-04-06 23:43:25 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200);
|
2016-04-06 23:43:25 +08:00
|
|
|
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
|
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(parsedBody.layergroupid).token] = 0;
|
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(parsedBody.metadata.layers[0].meta.cartocss, templateParams.styles['0']);
|
|
|
|
assert.strictEqual(
|
2016-04-06 23:43:25 +08:00
|
|
|
parsedBody.metadata.layers[1].meta.cartocss,
|
|
|
|
template.layergroup.layers[1].options.cartocss
|
|
|
|
);
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(parsedBody.metadata.layers[2].meta.cartocss, templateParams.styles['2']);
|
2016-04-06 23:43:25 +08:00
|
|
|
|
|
|
|
return parsedBody.layergroupid;
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function deleteTemplate (err) {
|
2016-04-06 23:43:25 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/map/named/' + templateId + '?api_key=1234',
|
|
|
|
method: 'DELETE',
|
|
|
|
headers: { host: 'localhost' }
|
|
|
|
}, {}, function (res, err) {
|
|
|
|
next(err, res);
|
|
|
|
});
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function checkDeleteTemplate (err, res) {
|
2016-04-06 23:43:25 +08:00
|
|
|
assert.ifError(err);
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(res.statusCode, 204);
|
2016-04-06 23:43:25 +08:00
|
|
|
assert.ok(!res.body);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function finish (err) {
|
2016-04-06 23:43:25 +08:00
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|