From 5dac9d956cf8a7cd037bed4b74b10dfa6a67a451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Mon, 16 Sep 2019 11:18:50 +0200 Subject: [PATCH] Add test --- .../acceptance/named-map-cache-regressions.js | 231 ++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 test/acceptance/named-map-cache-regressions.js diff --git a/test/acceptance/named-map-cache-regressions.js b/test/acceptance/named-map-cache-regressions.js new file mode 100644 index 00000000..52040cfe --- /dev/null +++ b/test/acceptance/named-map-cache-regressions.js @@ -0,0 +1,231 @@ +'use strict'; + +require('../support/test_helper'); + +const request = require('request'); +const assert = require('assert'); +const Server = require('../../lib/cartodb/server'); +const serverOptions = require('../../lib/cartodb/server_options'); +const { mapnik } = require('windshaft'); +const helper = require('../support/test_helper'); + +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); + + templateUpdate.layergroup.analyses[0].params.query = 'select * from populated_places_simple_reduced limit 100'; + + const upateTemplateRequest = { + url: `http://${address}/api/v1/map/named/${templateId}?api_key=${apiKey}`, + method: 'PUT', + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, + body: templateUpdate, + json: true + }; + + request(upateTemplateRequest, (err, res, body) => { + 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: { + host: 'localhost', + } + }; + + 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 = { + 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); + + 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); + }); + }); + }); + }); + }); + }); + }); + }); + }); + }); +});