From 4aa6ffe28c99674c9be861bbae5cdbc0798810c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 4 Oct 2019 17:54:32 +0200 Subject: [PATCH] Add tests to check custom middlewares behavior --- test/acceptance/custom-middlewares.js | 331 ++++++++++++++++++++++++++ 1 file changed, 331 insertions(+) create mode 100644 test/acceptance/custom-middlewares.js diff --git a/test/acceptance/custom-middlewares.js b/test/acceptance/custom-middlewares.js new file mode 100644 index 00000000..38067296 --- /dev/null +++ b/test/acceptance/custom-middlewares.js @@ -0,0 +1,331 @@ +'use strict'; + +const helper = require('../support/test_helper'); +const assert = require('../support/assert'); +const createServer = require('../../lib/cartodb/server'); +const serverOptions = require('../../lib/cartodb/server_options'); + +describe('custom middlewares', function () { + const RESPONSE_OK = { + statusCode: 200 + }; + const RESPONSE_KO_TEAPOT = { + statusCode: 418 + }; + + describe('wired in /api/v1', function () { + before(function () { + const options = Object.assign({}, serverOptions, { + routes: { + api: [{ + paths: [ + '/api/v1', + '/user/:user/api/v1', + ], + middlewares: [ + function teapot () { + return function teapotMiddleware (req, res) { + res.status(418).send('I\'m a teapot'); + }; + } + ], + // Base url for the Detached Maps API + // "/api/v1/map" is the new API, + map: [{ + paths: [ + '/map', + ] + }], + // Base url for the Templated Maps API + // "/api/v1/map/named" is the new API, + template: [{ + paths: [ + '/map/named' + ] + }] + }] + } + }); + + this.server = createServer(options); + }); + + it('POST /api/v1/map/named returns 418: "I\'m a teapot"', function (done) { + const templateid = `custom-middlewares-test-${Date.now()}`; + const template = { + version: '0.0.1', + name: templateid, + layergroup: { + layers: [{ + type: 'cartodb', + options: { + sql: 'select 1 as cartodb_id, null::geometry as the_geom_webmercator' + } + }] + } + }; + const request = { + url: '/api/v1/map/named?api_key=1234', + method: 'POST', + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, + data: JSON.stringify(template) + }; + + assert.response(this.server, request, RESPONSE_KO_TEAPOT, (res, err) => { + if (err) { + return done(err); + } + + assert.equal(res.body, 'I\'m a teapot'); + + done(); + }); + }); + + it('POST /api/v1/map returns 418: "I\'m a teapot"', function (done) { + const mapConfig = { + layers: [{ + type: 'cartodb', + options: { + sql: 'select 1 as cartodb_id, null::geometry as the_geom_webmercator' + } + }] + }; + const request = { + url: '/api/v1/map', + method: 'POST', + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, + data: JSON.stringify(mapConfig) + }; + + assert.response(this.server, request, RESPONSE_KO_TEAPOT, (res, err) => { + if (err) { + return done(err); + } + + assert.equal(res.body, 'I\'m a teapot'); + + done(); + }); + }); + }); + + describe('wired in /api/v1/map', function () { + before(function () { + const options = Object.assign({}, serverOptions, { + routes: { + api: [{ + paths: [ + '/api/v1', + '/user/:user/api/v1', + ], + // Base url for the Detached Maps API + // "/api/v1/map" is the new API, + map: [{ + paths: [ + '/map', + ], + middlewares: [ + function teapot () { + return function teapotMiddleware (req, res, next) { + if (req.path === '/') { + return res.status(418).send('I\'m a teapot'); + } + next(); + }; + } + ] + }], + // Base url for the Templated Maps API + // "/api/v1/map/named" is the new API, + template: [{ + paths: [ + '/map/named' + ] + }] + }] + } + }); + + this.server = createServer(options); + }); + + it('POST /api/v1/map/named returns 200: template created', function (done) { + const templateid = `custom-middlewares-test-${Date.now()}`; + const template = { + version: '0.0.1', + name: templateid, + layergroup: { + layers: [{ + type: 'cartodb', + options: { + sql: 'select 1 as cartodb_id, null::geometry as the_geom_webmercator' + } + }] + } + }; + const request = { + url: '/api/v1/map/named?api_key=1234', + method: 'POST', + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, + data: JSON.stringify(template) + }; + + assert.response(this.server, request, RESPONSE_OK, (res, err) => { + if (err) { + return done(err); + } + + const body = JSON.parse(res.body); + + assert.deepEqual(body, { template_id: templateid }); + + helper.deleteRedisKeys({ ['map_tpl|localhost']: 0 }, done); + }); + }); + + it('POST /api/v1/map returns 418: "I\'m a teapot"', function (done) { + const mapConfig = { + layers: [{ + type: 'cartodb', + options: { + sql: 'select 1 as cartodb_id, null::geometry as the_geom_webmercator' + } + }] + }; + const request = { + url: '/api/v1/map', + method: 'POST', + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, + data: JSON.stringify(mapConfig) + }; + + assert.response(this.server, request, RESPONSE_KO_TEAPOT, (res, err) => { + if (err) { + return done(err); + } + + assert.equal(res.body, 'I\'m a teapot'); + + done(); + }); + }); + }); + + describe('wired in /api/v1/map/named', function () { + before(function () { + const options = Object.assign({}, serverOptions, { + routes: { + api: [{ + paths: [ + '/api/v1', + '/user/:user/api/v1', + ], + // Base url for the Detached Maps API + // "/api/v1/map" is the new API, + map: [{ + paths: [ + '/map', + ] + }], + // Base url for the Templated Maps API + // "/api/v1/map/named" is the new API, + template: [{ + paths: [ + '/map/named' + ], + middlewares: [ + function teapot () { + return function teapotMiddleware (req, res) { + res.status(418).send('I\'m a teapot'); + }; + } + ] + }] + }] + } + }); + + this.server = createServer(options); + }); + + it('POST /api/v1/map/named returns 418: "I\'m a teapot"', function (done) { + const templateid = `custom-middlewares-test-${Date.now()}`; + const template = { + version: '0.0.1', + name: templateid, + layergroup: { + layers: [{ + type: 'cartodb', + options: { + sql: 'select 1 as cartodb_id, null::geometry as the_geom_webmercator' + } + }] + } + }; + const request = { + url: '/api/v1/map/named?api_key=1234', + method: 'POST', + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, + data: JSON.stringify(template) + }; + + assert.response(this.server, request, RESPONSE_KO_TEAPOT, (res, err) => { + if (err) { + return done(err); + } + + assert.equal(res.body, 'I\'m a teapot'); + + done(); + }); + }); + + it('POST /api/v1/map returns 200: anonymous map created', function (done) { + const mapConfig = { + layers: [{ + type: 'cartodb', + options: { + sql: 'select 1 as cartodb_id, null::geometry as the_geom_webmercator' + } + }] + }; + const request = { + url: '/api/v1/map', + method: 'POST', + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, + data: JSON.stringify(mapConfig) + }; + + assert.response(this.server, request, RESPONSE_OK, (res, err) => { + if (err) { + return done(err); + } + + const body = JSON.parse(res.body); + + assert.ok(body.layergroupid); + + helper.deleteRedisKeys({ ['user:localhost:mapviews:global']: 5 }, done); + }); + }); + }); +});