From aff5c9a614bf5dce1055802da7bbda47fe0dd357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 26 May 2020 16:28:44 +0200 Subject: [PATCH] Add test to check the headers exist while instantiating a map --- lib/api/middlewares/client-header.js | 11 +++ test/acceptance/map-view-headers-test.js | 97 ++++++++++++++++++++++++ test/support/test-client.js | 13 +++- 3 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 lib/api/middlewares/client-header.js create mode 100644 test/acceptance/map-view-headers-test.js diff --git a/lib/api/middlewares/client-header.js b/lib/api/middlewares/client-header.js new file mode 100644 index 00000000..dfcf488a --- /dev/null +++ b/lib/api/middlewares/client-header.js @@ -0,0 +1,11 @@ +'use strict'; + +module.exports = function clientHeader () { + return function clientHeaderMiddleware (req, res, next) { + const { client } = req.query; + + res.set('Carto-Client', client); + + return next(); + }; +}; diff --git a/test/acceptance/map-view-headers-test.js b/test/acceptance/map-view-headers-test.js new file mode 100644 index 00000000..858f7715 --- /dev/null +++ b/test/acceptance/map-view-headers-test.js @@ -0,0 +1,97 @@ +'use strict'; + +require('../support/test-helper'); + +const assert = require('../support/assert'); +const TestClient = require('../support/test-client'); + +const defaultLayers = [{ + type: 'cartodb', + options: { + sql: TestClient.SQL.ONE_POINT + } +}]; +const defaultStatTag = 'wadus'; + +function createMapConfig (layers = defaultLayers, statTag = defaultStatTag) { + return { + version: '1.8.0', + layers: layers, + stat_tag: defaultStatTag + }; +} + +describe('map view headers', function () { + it('anonymous map instantiation should respond with map-view headers', function (done) { + const mapConfig = createMapConfig(); + const testClient = new TestClient(mapConfig); + const params = { client: 'test' }; + + testClient.getLayergroup(params, (err, body, res) => { + if (err) { + return done(err); + } + + assert.strictEqual(res.headers['carto-stat-tag'], defaultStatTag); + assert.strictEqual(res.headers['carto-client'], params.client); + assert.strictEqual(res.headers['carto-user-id'], '1'); + + testClient.drain(done); + }); + }); + + it('named map instantiation should respond with map-view headers', function (done) { + const templateid = `map-view-headers-test-${Date.now()}`; + const template = { + version: '0.0.1', + name: templateid, + layergroup: createMapConfig() + }; + + const testClient = new TestClient(template, 1234); + const params = { client: 'test' }; + + testClient.getLayergroup(params, (err, body, res) => { + if (err) { + return done(err); + } + + assert.strictEqual(res.headers['carto-stat-tag'], defaultStatTag); + assert.strictEqual(res.headers['carto-client'], params.client); + assert.strictEqual(res.headers['carto-user-id'], '1'); + + testClient.drain(done); + }); + }); + + it('preview should respond with map-view headers', function (done) { + const templateid = `map-view-headers-test-${Date.now()}`; + const template = { + version: '0.0.1', + name: templateid, + layergroup: createMapConfig([{ + type: 'cartodb', + options: { + sql: TestClient.SQL.ONE_POINT, + cartocss: TestClient.CARTOCSS.POINTS, + cartocss_version: '2.3.0' + } + }]) + }; + + const testClient = new TestClient(template, 1234); + const params = { client: 'test' }; + + testClient.getPreview(640, 480, params, (err, res) => { + if (err) { + return done(err); + } + + assert.strictEqual(res.headers['carto-stat-tag'], defaultStatTag); + assert.strictEqual(res.headers['carto-client'], params.client); + assert.strictEqual(res.headers['carto-user-id'], '1'); + + testClient.drain(done); + }); + }); +}); diff --git a/test/support/test-client.js b/test/support/test-client.js index 36183a0d..c3204249 100644 --- a/test/support/test-client.js +++ b/test/support/test-client.js @@ -1033,9 +1033,13 @@ TestClient.prototype.getLayergroup = function (params, callback) { queryParams.aggregation = params.aggregation; } + if (params.client !== undefined) { + queryParams.client = params.client; + } + const path = templateId - ? urlNamed + '/' + templateId + '?' + qs.stringify(queryParams) - : url; + ? `${urlNamed}/${templateId}?${qs.stringify(queryParams)}` + : `${url}?${qs.stringify(queryParams)}`; assert.response(self.server, { @@ -1058,12 +1062,15 @@ TestClient.prototype.getLayergroup = function (params, callback) { if (res.statusCode === 200 && self.template && self.template.layergroup && self.template.layergroup.stat_tag) { self.keysToDelete[`user:localhost:mapviews:stat_tag:${self.template.layergroup.stat_tag}`] = 5; } + if (res.statusCode === 200 && self.mapConfig && self.mapConfig.stat_tag) { + self.keysToDelete[`user:localhost:mapviews:stat_tag:${self.mapConfig.stat_tag}`] = 5; + } } if (err) { return callback(err); } - return callback(null, parsedBody); + return callback(null, parsedBody, res); } ); }