Windshaft-cartodb/test/acceptance/dynamic-styling-named-maps-test.js

176 lines
5.4 KiB
JavaScript
Raw Normal View History

'use strict';
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');
var createServer = require('../../lib/server');
2019-10-07 16:55:26 +08:00
var serverOptions = require('../../lib/server-options');
2019-10-22 01:07:24 +08:00
describe('dynamic styling for named maps', function () {
var server;
before(function () {
server = createServer(serverOptions);
});
var keysToDelete;
2019-10-22 01:07:24 +08:00
beforeEach(function () {
keysToDelete = {};
});
2019-10-22 01:07:24 +08:00
afterEach(function (done) {
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'
}
},
2019-10-22 01:07:24 +08:00
layergroup: {
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 () {
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) {
assert.ifError(err);
assert.strictEqual(res.statusCode, 200);
assert.deepStrictEqual(JSON.parse(res.body), {
template_id: templateId
});
return null;
},
2019-10-22 01:07:24 +08:00
function instantiateTemplate (err) {
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) {
assert.ifError(err);
assert.strictEqual(res.statusCode, 200);
var parsedBody = JSON.parse(res.body);
keysToDelete['map_cfg|' + LayergroupToken.parse(parsedBody.layergroupid).token] = 0;
keysToDelete['user:localhost:mapviews:global'] = 5;
assert.strictEqual(parsedBody.metadata.layers[0].meta.cartocss, templateParams.styles['0']);
assert.strictEqual(
parsedBody.metadata.layers[1].meta.cartocss,
template.layergroup.layers[1].options.cartocss
);
assert.strictEqual(parsedBody.metadata.layers[2].meta.cartocss, templateParams.styles['2']);
return parsedBody.layergroupid;
},
2019-10-22 01:07:24 +08:00
function deleteTemplate (err) {
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) {
assert.ifError(err);
assert.strictEqual(res.statusCode, 204);
assert.ok(!res.body);
return null;
},
2019-10-22 01:07:24 +08:00
function finish (err) {
done(err);
}
);
});
});