Windshaft-cartodb/test/acceptance/named-map-cache-regressions-test.js

237 lines
8.2 KiB
JavaScript
Raw Normal View History

2019-09-16 17:18:50 +08:00
'use strict';
require('../support/test-helper');
2019-09-16 17:18:50 +08:00
const request = require('request');
const assert = require('assert');
2019-10-07 15:40:50 +08:00
const Server = require('../../lib/server');
const serverOptions = require('../../lib/server-options');
const mapnik = require('@carto/mapnik');
const helper = require('../support/test-helper');
2019-09-16 17:18:50 +08:00
2019-09-16 17:35:40 +08:00
const namedTileUrlTemplate = (ctx) => {
return `http://${ctx.address}/api/v1/map/static/named/${ctx.templateId}/256/256.png?api_key=${ctx.apiKey}`;
};
2019-09-16 17:18:50 +08:00
describe('named map cache regressions', function () {
const server = new Server(serverOptions);
const apiKey = 1234;
const template = {
version: '0.0.1',
name: 'named-map-cache-regression-missing-template',
layergroup: {
version: '1.8.0',
layers: [
{
type: 'cartodb',
options: {
source: {
id: 'a1'
},
cartocss: '#layer{marker-placement: point;marker-width: 5;marker-fill: red;}',
cartocss_version: '2.3.0'
}
}
],
analyses: [
{
id: 'a1',
type: 'source',
params: {
query: 'select * from populated_places_simple_reduced'
}
}
]
}
};
const port = 0; // let the OS to choose a free port
const host = '127.0.0.1';
let listener;
let address;
const keysToDelete = {};
before(function (done) {
listener = server.listen(port, host);
listener.on('error', done);
listener.on('listening', () => {
const { address: host, port } = listener.address();
address = `${host}:${port}`;
done();
});
});
after(function (done) {
helper.deleteRedisKeys(keysToDelete, () => listener.close(done));
});
it('should not fail when a template gets recreated', function (done) {
this.timeout(10000);
const createTemplateRequest = {
url: `http://${address}/api/v1/map/named?api_key=${apiKey}`,
method: 'POST',
headers: {
host: 'localhost',
'Content-Type': 'application/json'
},
body: template,
json: true
};
request(createTemplateRequest, (err, res, body) => {
if (err) {
return done(err);
}
assert.strictEqual(res.statusCode, 200);
const templateId = body.template_id;
keysToDelete['map_tpl|localhost'] = 0;
const previewRequest = {
url: `http://${address}/api/v1/map/static/named/${templateId}/256/256.png?api_key=${apiKey}`,
encoding: 'binary',
method: 'GET',
headers: {
host: 'localhost'
}
};
request(previewRequest, (err, res) => {
if (err) {
return done(err);
}
assert.strictEqual(res.statusCode, 200);
const preview = mapnik.Image.fromBytes(Buffer.from(res.body, 'binary'));
assert.strictEqual(preview.width(), 256);
assert.strictEqual(preview.height(), 256);
const templateUpdate = Object.assign({}, template);
2019-09-16 17:35:40 +08:00
const newQuery = 'select * from populated_places_simple_reduced limit 100';
templateUpdate.layergroup.analyses[0].params.query = newQuery;
2019-09-16 17:18:50 +08:00
2019-09-16 19:08:10 +08:00
const updateTemplateRequest = {
2019-09-16 17:18:50 +08:00
url: `http://${address}/api/v1/map/named/${templateId}?api_key=${apiKey}`,
method: 'PUT',
headers: {
host: 'localhost',
'Content-Type': 'application/json'
},
body: templateUpdate,
json: true
};
2019-09-16 19:08:10 +08:00
request(updateTemplateRequest, (err, res) => {
2019-09-16 17:18:50 +08:00
if (err) {
return done(err);
}
assert.strictEqual(res.statusCode, 200);
request(previewRequest, (err, res) => {
if (err) {
return done(err);
}
const preview = mapnik.Image.fromBytes(Buffer.from(res.body, 'binary'));
assert.strictEqual(preview.width(), 256);
assert.strictEqual(preview.height(), 256);
request(previewRequest, (err, res) => {
if (err) {
return done(err);
}
const preview = mapnik.Image.fromBytes(Buffer.from(res.body, 'binary'));
assert.strictEqual(preview.width(), 256);
assert.strictEqual(preview.height(), 256);
const deleteTemplateRequest = {
url: `http://${address}/api/v1/map/named/${templateId}?api_key=${apiKey}`,
method: 'DELETE',
headers: {
2019-10-22 01:07:24 +08:00
host: 'localhost'
2019-09-16 17:18:50 +08:00
}
};
request(deleteTemplateRequest, (err) => {
if (err) {
return done(err);
}
delete keysToDelete['map_tpl|localhost'];
assert.strictEqual(res.statusCode, 200);
request(createTemplateRequest, (err, res, body) => {
if (err) {
return done(err);
}
assert.strictEqual(res.statusCode, 200);
const templateId = body.template_id;
keysToDelete['map_tpl|localhost'] = 0;
const previewRequest = {
2019-09-16 17:35:40 +08:00
url: namedTileUrlTemplate({ address, templateId, apiKey }),
2019-09-16 17:18:50 +08:00
encoding: 'binary',
method: 'GET',
headers: {
host: 'localhost'
}
};
request(previewRequest, (err, res) => {
if (err) {
return done(err);
}
assert.strictEqual(res.statusCode, 200);
const preview = mapnik.Image.fromBytes(Buffer.from(res.body, 'binary'));
assert.strictEqual(preview.width(), 256);
assert.strictEqual(preview.height(), 256);
request(deleteTemplateRequest, (err) => {
if (err) {
return done(err);
}
delete keysToDelete['map_tpl|localhost'];
assert.strictEqual(res.statusCode, 200);
keysToDelete['user:localhost:mapviews:global'] = 0;
keysToDelete['user:localhost:mapviews:global'] = 5;
helper.deleteRedisKeys(keysToDelete, done);
});
});
});
});
});
});
});
});
});
});
});