2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-26 21:59:41 +08:00
|
|
|
var assert = require('../../support/assert');
|
2016-03-11 03:45:00 +08:00
|
|
|
var step = require('step');
|
2019-10-07 15:40:50 +08:00
|
|
|
var LayergroupToken = require('../../../lib/models/layergroup-token');
|
2019-10-07 16:44:45 +08:00
|
|
|
var testHelper = require('../../support/test-helper');
|
2019-10-07 15:40:50 +08:00
|
|
|
var CartodbWindshaft = require('../../../lib/server');
|
2019-10-07 16:10:51 +08:00
|
|
|
var serverOptions = require('../../../lib/server-options');
|
2020-04-04 23:34:22 +08:00
|
|
|
const mapnik = require('@carto/mapnik');
|
2016-03-12 01:33:52 +08:00
|
|
|
var IMAGE_TOLERANCE_PER_MIL = 10;
|
2016-03-11 03:45:00 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
describe('turbo-carto for named maps', function () {
|
2018-04-16 22:16:23 +08:00
|
|
|
var server;
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
server = new CartodbWindshaft(serverOptions);
|
|
|
|
});
|
2016-03-11 03:45:00 +08:00
|
|
|
|
|
|
|
var keysToDelete;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
beforeEach(function () {
|
2016-03-11 03:45:00 +08:00
|
|
|
keysToDelete = {};
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
afterEach(function (done) {
|
2016-03-11 03:45:00 +08:00
|
|
|
testHelper.deleteRedisKeys(keysToDelete, done);
|
|
|
|
});
|
|
|
|
|
2016-04-26 21:59:41 +08:00
|
|
|
var templateId = 'turbo-carto-template-1';
|
2016-03-11 03:45:00 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function template (table) {
|
2016-06-21 18:08:40 +08:00
|
|
|
return {
|
|
|
|
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-03-11 03:45:00 +08:00
|
|
|
}
|
2016-06-21 18:08:40 +08:00
|
|
|
},
|
|
|
|
layergroup: {
|
|
|
|
version: '1.0.0',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
options: {
|
|
|
|
sql: [
|
|
|
|
'SELECT ' + table + '.*, _prices.price FROM ' + table + ' JOIN (' +
|
|
|
|
' SELECT 1 AS cartodb_id, 10.00 AS price',
|
2018-07-17 18:43:29 +08:00
|
|
|
' UNION ALL',
|
2016-06-21 18:08:40 +08:00
|
|
|
' SELECT 2, 10.50',
|
2018-07-17 18:43:29 +08:00
|
|
|
' UNION ALL',
|
2016-06-21 18:08:40 +08:00
|
|
|
' SELECT 3, 11.00',
|
2018-07-17 18:43:29 +08:00
|
|
|
' UNION ALL',
|
2016-06-21 18:08:40 +08:00
|
|
|
' SELECT 4, 12.00',
|
2018-07-17 18:43:29 +08:00
|
|
|
' UNION ALL',
|
2016-06-21 18:08:40 +08:00
|
|
|
' SELECT 5, 21.00',
|
|
|
|
') _prices ON _prices.cartodb_id = ' + table + '.cartodb_id'
|
|
|
|
].join('\n'),
|
|
|
|
cartocss: [
|
|
|
|
'#layer {',
|
|
|
|
' marker-fill: ramp([price], colorbrewer(<%= color %>));',
|
|
|
|
' marker-allow-overlap:true;',
|
|
|
|
'}'
|
|
|
|
].join('\n'),
|
|
|
|
cartocss_version: '2.0.2'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-03-11 03:45:00 +08:00
|
|
|
|
2016-03-12 01:28:14 +08:00
|
|
|
var templateParamsReds = { color: 'Reds' };
|
|
|
|
var templateParamsBlues = { color: 'Blues' };
|
2016-03-11 03:45:00 +08:00
|
|
|
|
2016-06-21 18:08:40 +08:00
|
|
|
var scenarios = [
|
|
|
|
{
|
|
|
|
desc: 'with public tables',
|
|
|
|
table: 'test_table'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: 'with private tables',
|
|
|
|
table: 'test_table_private_1'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
scenarios.forEach(function (scenario) {
|
2016-06-21 18:08:40 +08:00
|
|
|
it('should create a template with turbo-carto parsed properly: ' + scenario.desc, function (done) {
|
|
|
|
step(
|
2019-10-22 01:07:24 +08:00
|
|
|
function postTemplate () {
|
2016-06-21 18:08:40 +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(scenario.table))
|
|
|
|
}, {},
|
|
|
|
function (res, err) {
|
|
|
|
next(err, res);
|
|
|
|
});
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function checkTemplate (err, res) {
|
2016-06-21 18:08:40 +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-06-21 18:08:40 +08:00
|
|
|
template_id: templateId
|
|
|
|
});
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function instantiateTemplateWithReds (err) {
|
2016-06-21 18:08:40 +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(templateParamsReds)
|
|
|
|
}, {},
|
2019-10-22 01:07:24 +08:00
|
|
|
function (res, err) {
|
2016-06-21 18:08:40 +08:00
|
|
|
return next(err, res);
|
|
|
|
});
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function checkInstanciationWithReds (err, res) {
|
2016-06-21 18:08:40 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200);
|
2016-06-21 18:08:40 +08:00
|
|
|
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
|
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(parsedBody.layergroupid).token] = 0;
|
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
|
|
|
|
|
|
|
assert.ok(parsedBody.layergroupid);
|
|
|
|
assert.ok(parsedBody.last_updated);
|
|
|
|
|
|
|
|
return parsedBody.layergroupid;
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function requestTileReds (err, layergroupId) {
|
2016-06-21 18:08:40 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/map/' + layergroupId + '/0/0/0.png',
|
|
|
|
method: 'GET',
|
|
|
|
headers: { host: 'localhost' },
|
|
|
|
encoding: 'binary'
|
|
|
|
}, {},
|
2019-10-22 01:07:24 +08:00
|
|
|
function (res, err) {
|
2016-06-21 18:08:40 +08:00
|
|
|
next(err, res);
|
|
|
|
});
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function checkTileReds (err, res) {
|
2016-06-21 18:08:40 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200);
|
|
|
|
assert.strictEqual(res.headers['content-type'], 'image/png');
|
2016-06-21 18:08:40 +08:00
|
|
|
|
|
|
|
var fixturePath = './test/fixtures/turbo-carto-named-maps-reds.png';
|
2019-10-22 02:05:51 +08:00
|
|
|
var image = mapnik.Image.fromBytes(Buffer.from(res.body, 'binary'));
|
2016-06-21 18:08:40 +08:00
|
|
|
|
|
|
|
assert.imageIsSimilarToFile(image, fixturePath, IMAGE_TOLERANCE_PER_MIL, next);
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function instantiateTemplateWithBlues (err) {
|
2016-06-21 18:08:40 +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(templateParamsBlues)
|
|
|
|
}, {},
|
2019-10-22 01:07:24 +08:00
|
|
|
function (res, err) {
|
2016-06-21 18:08:40 +08:00
|
|
|
return next(err, res);
|
|
|
|
});
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function checkInstanciationWithBlues (err, res) {
|
2016-06-21 18:08:40 +08:00
|
|
|
assert.ifError(err);
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200);
|
2016-06-21 18:08:40 +08:00
|
|
|
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
|
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(parsedBody.layergroupid).token] = 0;
|
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
|
|
|
|
|
|
|
assert.ok(parsedBody.layergroupid);
|
|
|
|
assert.ok(parsedBody.last_updated);
|
|
|
|
|
|
|
|
return parsedBody.layergroupid;
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function requestTileBlues (err, layergroupId) {
|
2016-06-21 18:08:40 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/map/' + layergroupId + '/0/0/0.png',
|
|
|
|
method: 'GET',
|
|
|
|
headers: { host: 'localhost' },
|
|
|
|
encoding: 'binary'
|
|
|
|
}, {},
|
2019-10-22 01:07:24 +08:00
|
|
|
function (res, err) {
|
2016-06-21 18:08:40 +08:00
|
|
|
next(err, res);
|
|
|
|
});
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function checkTileBlues (err, res) {
|
2016-06-21 18:08:40 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200);
|
|
|
|
assert.strictEqual(res.headers['content-type'], 'image/png');
|
2016-06-21 18:08:40 +08:00
|
|
|
|
|
|
|
var fixturePath = './test/fixtures/turbo-carto-named-maps-blues.png';
|
2019-10-22 02:05:51 +08:00
|
|
|
var image = mapnik.Image.fromBytes(Buffer.from(res.body, 'binary'));
|
2016-06-21 18:08:40 +08:00
|
|
|
|
|
|
|
assert.imageIsSimilarToFile(image, fixturePath, IMAGE_TOLERANCE_PER_MIL, next);
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function deleteTemplate (err) {
|
2016-06-21 18:08:40 +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-06-21 18:08:40 +08:00
|
|
|
assert.ifError(err);
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(res.statusCode, 204);
|
2016-06-21 18:08:40 +08:00
|
|
|
assert.ok(!res.body);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
function finish (err) {
|
2016-06-21 18:08:40 +08:00
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2016-03-11 03:45:00 +08:00
|
|
|
});
|
|
|
|
});
|