From afff06c7e6d29721dc80bd4ccfad32c4fdbc1e63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Thu, 15 Mar 2018 15:33:20 +0100 Subject: [PATCH 01/32] Extract db-conn-setup middleware for prepare-context --- lib/cartodb/controllers/analyses.js | 5 ++++- lib/cartodb/controllers/layergroup.js | 12 ++++++++++++ lib/cartodb/controllers/map.js | 2 ++ lib/cartodb/controllers/named_maps.js | 6 +++++- lib/cartodb/middleware/context/index.js | 6 ++---- .../middleware/{context => }/db-conn-setup.js | 0 lib/cartodb/server.js | 7 ++++--- test/unit/cartodb/prepare-context.test.js | 2 +- 8 files changed, 30 insertions(+), 10 deletions(-) rename lib/cartodb/middleware/{context => }/db-conn-setup.js (100%) diff --git a/lib/cartodb/controllers/analyses.js b/lib/cartodb/controllers/analyses.js index 1a78208f..a04489c4 100644 --- a/lib/cartodb/controllers/analyses.js +++ b/lib/cartodb/controllers/analyses.js @@ -1,9 +1,11 @@ var PSQL = require('cartodb-psql'); var cors = require('../middleware/cors'); var userMiddleware = require('../middleware/user'); +const dbConnSetup = require('../middleware/db-conn-setup'); -function AnalysesController(prepareContext) { +function AnalysesController(prepareContext, pgConnection) { this.prepareContext = prepareContext; + this.pgConnection = pgConnection; } module.exports = AnalysesController; @@ -14,6 +16,7 @@ AnalysesController.prototype.register = function (app) { cors(), userMiddleware(), this.prepareContext, + dbConnSetup(this.pgConnection), createPGClient(), getDataFromQuery({ queryTemplate: catalogQueryTpl, key: 'catalog' }), getDataFromQuery({ queryTemplate: tablesQueryTpl, key: 'tables' }), diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index 2d0bf710..30c425f4 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -2,6 +2,7 @@ const cors = require('../middleware/cors'); const userMiddleware = require('../middleware/user'); const allowQueryParams = require('../middleware/allow-query-params'); const vectorError = require('../middleware/vector-error'); +const dbConnSetup = require('../middleware/db-conn-setup'); const DataviewBackend = require('../backends/dataview'); const AnalysisStatusBackend = require('../backends/analysis-status'); const MapStoreMapConfigProvider = require('../models/mapconfig/provider/map-store-provider'); @@ -55,6 +56,7 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getTile(this.tileBackend, 'map_tile'), setCacheControlHeader(), @@ -74,6 +76,7 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getTile(this.tileBackend, 'map_tile'), setCacheControlHeader(), @@ -94,6 +97,7 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getTile(this.tileBackend, 'maplayer_tile'), setCacheControlHeader(), @@ -113,6 +117,7 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getFeatureAttributes(this.attributesBackend), setCacheControlHeader(), @@ -131,6 +136,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(['layer']), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat), getPreviewImageByCenter(this.previewBackend), setCacheControlHeader(), @@ -147,6 +153,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(['layer']), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat), getPreviewImageByBoundingBox(this.previewBackend), setCacheControlHeader(), @@ -181,6 +188,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(allowedDataviewQueryParams), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getDataview(this.dataviewBackend), setCacheControlHeader(), @@ -197,6 +205,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(allowedDataviewQueryParams), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getDataview(this.dataviewBackend), setCacheControlHeader(), @@ -213,6 +222,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(allowedDataviewQueryParams), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), dataviewSearch(this.dataviewBackend), setCacheControlHeader(), @@ -229,6 +239,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(allowedDataviewQueryParams), this.prepareContext, + dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), dataviewSearch(this.dataviewBackend), setCacheControlHeader(), @@ -244,6 +255,7 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + dbConnSetup(this.pgConnection), analysisNodeStatus(this.analysisStatusBackend), sendResponse() ); diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index 87e8b6be..38f3845f 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -7,6 +7,7 @@ const ResourceLocator = require('../models/resource-locator'); const cors = require('../middleware/cors'); const userMiddleware = require('../middleware/user'); const allowQueryParams = require('../middleware/allow-query-params'); +const dbConnSetup = require('../middleware/db-conn-setup'); const NamedMapsCacheEntry = require('../cache/model/named_maps_entry'); const NamedMapMapConfigProvider = require('../models/mapconfig/provider/named-map-provider'); const CreateLayergroupMapConfigProvider = require('../models/mapconfig/provider/create-layergroup-provider'); @@ -69,6 +70,7 @@ MapController.prototype.composeCreateMapMiddleware = function (useTemplate = fal userMiddleware(), allowQueryParams(['aggregation']), this.prepareContext, + dbConnSetup(this.pgConnection), initProfiler(isTemplateInstantiation), checkJsonContentType(), this.getCreateMapMiddlewares(useTemplate), diff --git a/lib/cartodb/controllers/named_maps.js b/lib/cartodb/controllers/named_maps.js index cda71346..720f79c1 100644 --- a/lib/cartodb/controllers/named_maps.js +++ b/lib/cartodb/controllers/named_maps.js @@ -1,6 +1,7 @@ const NamedMapsCacheEntry = require('../cache/model/named_maps_entry'); const cors = require('../middleware/cors'); const userMiddleware = require('../middleware/user'); +const dbConnSetup = require('../middleware/db-conn-setup'); const allowQueryParams = require('../middleware/allow-query-params'); const vectorError = require('../middleware/vector-error'); @@ -28,7 +29,7 @@ function getRequestParams(locals) { } function NamedMapsController(prepareContext, namedMapProviderCache, tileBackend, previewBackend, - surrogateKeysCache, tablesExtentApi, metadataBackend) { + surrogateKeysCache, tablesExtentApi, metadataBackend, pgConnection) { this.namedMapProviderCache = namedMapProviderCache; this.tileBackend = tileBackend; this.previewBackend = previewBackend; @@ -36,6 +37,7 @@ function NamedMapsController(prepareContext, namedMapProviderCache, tileBackend, this.tablesExtentApi = tablesExtentApi; this.metadataBackend = metadataBackend; this.prepareContext = prepareContext; + this.pgConnection = pgConnection; } module.exports = NamedMapsController; @@ -48,6 +50,7 @@ NamedMapsController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + dbConnSetup(this.pgConnection), getNamedMapProvider({ namedMapProviderCache: this.namedMapProviderCache, label: 'NAMED_MAP_TILE' @@ -72,6 +75,7 @@ NamedMapsController.prototype.register = function(app) { userMiddleware(), allowQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']), this.prepareContext, + dbConnSetup(this.pgConnection), getNamedMapProvider({ namedMapProviderCache: this.namedMapProviderCache, label: 'STATIC_VIZ_MAP', forcedFormat: 'png' diff --git a/lib/cartodb/middleware/context/index.js b/lib/cartodb/middleware/context/index.js index 70465895..390dcb5e 100644 --- a/lib/cartodb/middleware/context/index.js +++ b/lib/cartodb/middleware/context/index.js @@ -3,15 +3,13 @@ const cleanUpQueryParams = require('./clean-up-query-params'); const layergroupToken = require('./layergroup-token'); const credentials = require('./credentials'); const authorize = require('./authorize'); -const dbConnSetup = require('./db-conn-setup'); -module.exports = function prepareContextMiddleware(authApi, pgConnection) { +module.exports = function prepareContextMiddleware(authApi) { return [ locals(), cleanUpQueryParams(), layergroupToken(), credentials(), - authorize(authApi), - dbConnSetup(pgConnection) + authorize(authApi) ]; }; diff --git a/lib/cartodb/middleware/context/db-conn-setup.js b/lib/cartodb/middleware/db-conn-setup.js similarity index 100% rename from lib/cartodb/middleware/context/db-conn-setup.js rename to lib/cartodb/middleware/db-conn-setup.js diff --git a/lib/cartodb/server.js b/lib/cartodb/server.js index 532d155b..d2187930 100644 --- a/lib/cartodb/server.js +++ b/lib/cartodb/server.js @@ -217,7 +217,7 @@ module.exports = function(serverOptions) { const prepareContext = typeof serverOptions.req2params === 'function' ? serverOptions.req2params : - prepareContextMiddleware(authApi, pgConnection); + prepareContextMiddleware(authApi); /******************************************************************************************************************* * Routing @@ -256,12 +256,13 @@ module.exports = function(serverOptions) { previewBackend, surrogateKeysCache, tablesExtentApi, - metadataBackend + metadataBackend, + pgConnection ).register(app); new controller.NamedMapsAdmin(authApi, templateMaps).register(app); - new controller.Analyses(prepareContext).register(app); + new controller.Analyses(prepareContext, pgConnection).register(app); new controller.ServerInfo(versions).register(app); diff --git a/test/unit/cartodb/prepare-context.test.js b/test/unit/cartodb/prepare-context.test.js index 283c4fdc..19afce21 100644 --- a/test/unit/cartodb/prepare-context.test.js +++ b/test/unit/cartodb/prepare-context.test.js @@ -9,7 +9,7 @@ var TemplateMaps = require('../../../lib/cartodb/backends/template_maps'); const cleanUpQueryParamsMiddleware = require('../../../lib/cartodb/middleware/context/clean-up-query-params'); const authorizeMiddleware = require('../../../lib/cartodb/middleware/context/authorize'); -const dbConnSetupMiddleware = require('../../../lib/cartodb/middleware/context/db-conn-setup'); +const dbConnSetupMiddleware = require('../../../lib/cartodb/middleware/db-conn-setup'); const credentialsMiddleware = require('../../../lib/cartodb/middleware/context/credentials'); const localsMiddleware = require('../../../lib/cartodb/middleware/context/locals'); From 30dab7df9f5717286e4660ee062817adb9e38ebc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Thu, 15 Mar 2018 18:48:29 +0100 Subject: [PATCH 02/32] Extract authorize middeware form prepareContext --- lib/cartodb/controllers/analyses.js | 5 ++++- lib/cartodb/controllers/layergroup.js | 15 ++++++++++++++- lib/cartodb/controllers/map.js | 5 ++++- lib/cartodb/controllers/named_maps.js | 6 +++++- lib/cartodb/middleware/{context => }/authorize.js | 0 lib/cartodb/middleware/context/index.js | 4 +--- lib/cartodb/server.js | 13 ++++++++----- test/unit/cartodb/prepare-context.test.js | 2 +- 8 files changed, 37 insertions(+), 13 deletions(-) rename lib/cartodb/middleware/{context => }/authorize.js (100%) diff --git a/lib/cartodb/controllers/analyses.js b/lib/cartodb/controllers/analyses.js index a04489c4..99c38fcb 100644 --- a/lib/cartodb/controllers/analyses.js +++ b/lib/cartodb/controllers/analyses.js @@ -1,11 +1,13 @@ var PSQL = require('cartodb-psql'); var cors = require('../middleware/cors'); var userMiddleware = require('../middleware/user'); +const authorize = require('../middleware/authorize'); const dbConnSetup = require('../middleware/db-conn-setup'); -function AnalysesController(prepareContext, pgConnection) { +function AnalysesController(prepareContext, pgConnection, authApi) { this.prepareContext = prepareContext; this.pgConnection = pgConnection; + this.authApi = authApi; } module.exports = AnalysesController; @@ -16,6 +18,7 @@ AnalysesController.prototype.register = function (app) { cors(), userMiddleware(), this.prepareContext, + authorize(this.authApi), dbConnSetup(this.pgConnection), createPGClient(), getDataFromQuery({ queryTemplate: catalogQueryTpl, key: 'catalog' }), diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index 30c425f4..834bb0b7 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -3,6 +3,7 @@ const userMiddleware = require('../middleware/user'); const allowQueryParams = require('../middleware/allow-query-params'); const vectorError = require('../middleware/vector-error'); const dbConnSetup = require('../middleware/db-conn-setup'); +const authorize = require('../middleware/authorize'); const DataviewBackend = require('../backends/dataview'); const AnalysisStatusBackend = require('../backends/analysis-status'); const MapStoreMapConfigProvider = require('../models/mapconfig/provider/map-store-provider'); @@ -30,7 +31,7 @@ const SUPPORTED_FORMATS = { * @constructor */ function LayergroupController(prepareContext, pgConnection, mapStore, tileBackend, previewBackend, attributesBackend, - surrogateKeysCache, userLimitsApi, layergroupAffectedTables, analysisBackend) { + surrogateKeysCache, userLimitsApi, layergroupAffectedTables, analysisBackend, authApi) { this.pgConnection = pgConnection; this.mapStore = mapStore; this.tileBackend = tileBackend; @@ -44,6 +45,7 @@ function LayergroupController(prepareContext, pgConnection, mapStore, tileBacken this.analysisStatusBackend = new AnalysisStatusBackend(); this.prepareContext = prepareContext; + this.authApi = authApi; } module.exports = LayergroupController; @@ -56,6 +58,7 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getTile(this.tileBackend, 'map_tile'), @@ -76,6 +79,7 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getTile(this.tileBackend, 'map_tile'), @@ -97,6 +101,7 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getTile(this.tileBackend, 'maplayer_tile'), @@ -117,6 +122,7 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getFeatureAttributes(this.attributesBackend), @@ -136,6 +142,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(['layer']), this.prepareContext, + authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat), getPreviewImageByCenter(this.previewBackend), @@ -153,6 +160,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(['layer']), this.prepareContext, + authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat), getPreviewImageByBoundingBox(this.previewBackend), @@ -188,6 +196,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(allowedDataviewQueryParams), this.prepareContext, + authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getDataview(this.dataviewBackend), @@ -205,6 +214,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(allowedDataviewQueryParams), this.prepareContext, + authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), getDataview(this.dataviewBackend), @@ -222,6 +232,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(allowedDataviewQueryParams), this.prepareContext, + authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), dataviewSearch(this.dataviewBackend), @@ -239,6 +250,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware(), allowQueryParams(allowedDataviewQueryParams), this.prepareContext, + authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), dataviewSearch(this.dataviewBackend), @@ -255,6 +267,7 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + authorize(this.authApi), dbConnSetup(this.pgConnection), analysisNodeStatus(this.analysisStatusBackend), sendResponse() diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index 38f3845f..865b7361 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -8,6 +8,7 @@ const cors = require('../middleware/cors'); const userMiddleware = require('../middleware/user'); const allowQueryParams = require('../middleware/allow-query-params'); const dbConnSetup = require('../middleware/db-conn-setup'); +const authorize = require('../middleware/authorize'); const NamedMapsCacheEntry = require('../cache/model/named_maps_entry'); const NamedMapMapConfigProvider = require('../models/mapconfig/provider/named-map-provider'); const CreateLayergroupMapConfigProvider = require('../models/mapconfig/provider/create-layergroup-provider'); @@ -28,7 +29,7 @@ const LayergroupMetadata = require('../utils/layergroup-metadata'); */ function MapController(prepareContext, pgConnection, templateMaps, mapBackend, metadataBackend, surrogateKeysCache, userLimitsApi, layergroupAffectedTables, mapConfigAdapter, - statsBackend) { + statsBackend, authApi) { this.pgConnection = pgConnection; this.templateMaps = templateMaps; this.mapBackend = mapBackend; @@ -43,6 +44,7 @@ function MapController(prepareContext, pgConnection, templateMaps, mapBackend, m this.statsBackend = statsBackend; this.prepareContext = prepareContext; + this.authApi = authApi; } module.exports = MapController; @@ -70,6 +72,7 @@ MapController.prototype.composeCreateMapMiddleware = function (useTemplate = fal userMiddleware(), allowQueryParams(['aggregation']), this.prepareContext, + authorize(this.authApi), dbConnSetup(this.pgConnection), initProfiler(isTemplateInstantiation), checkJsonContentType(), diff --git a/lib/cartodb/controllers/named_maps.js b/lib/cartodb/controllers/named_maps.js index 720f79c1..315a4097 100644 --- a/lib/cartodb/controllers/named_maps.js +++ b/lib/cartodb/controllers/named_maps.js @@ -2,6 +2,7 @@ const NamedMapsCacheEntry = require('../cache/model/named_maps_entry'); const cors = require('../middleware/cors'); const userMiddleware = require('../middleware/user'); const dbConnSetup = require('../middleware/db-conn-setup'); +const authorize = require('../middleware/authorize'); const allowQueryParams = require('../middleware/allow-query-params'); const vectorError = require('../middleware/vector-error'); @@ -29,7 +30,7 @@ function getRequestParams(locals) { } function NamedMapsController(prepareContext, namedMapProviderCache, tileBackend, previewBackend, - surrogateKeysCache, tablesExtentApi, metadataBackend, pgConnection) { + surrogateKeysCache, tablesExtentApi, metadataBackend, pgConnection, authApi) { this.namedMapProviderCache = namedMapProviderCache; this.tileBackend = tileBackend; this.previewBackend = previewBackend; @@ -38,6 +39,7 @@ function NamedMapsController(prepareContext, namedMapProviderCache, tileBackend, this.metadataBackend = metadataBackend; this.prepareContext = prepareContext; this.pgConnection = pgConnection; + this.authApi = authApi; } module.exports = NamedMapsController; @@ -50,6 +52,7 @@ NamedMapsController.prototype.register = function(app) { cors(), userMiddleware(), this.prepareContext, + authorize(this.authApi), dbConnSetup(this.pgConnection), getNamedMapProvider({ namedMapProviderCache: this.namedMapProviderCache, @@ -75,6 +78,7 @@ NamedMapsController.prototype.register = function(app) { userMiddleware(), allowQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']), this.prepareContext, + authorize(this.authApi), dbConnSetup(this.pgConnection), getNamedMapProvider({ namedMapProviderCache: this.namedMapProviderCache, diff --git a/lib/cartodb/middleware/context/authorize.js b/lib/cartodb/middleware/authorize.js similarity index 100% rename from lib/cartodb/middleware/context/authorize.js rename to lib/cartodb/middleware/authorize.js diff --git a/lib/cartodb/middleware/context/index.js b/lib/cartodb/middleware/context/index.js index 390dcb5e..59a6095b 100644 --- a/lib/cartodb/middleware/context/index.js +++ b/lib/cartodb/middleware/context/index.js @@ -2,14 +2,12 @@ const locals = require('./locals'); const cleanUpQueryParams = require('./clean-up-query-params'); const layergroupToken = require('./layergroup-token'); const credentials = require('./credentials'); -const authorize = require('./authorize'); -module.exports = function prepareContextMiddleware(authApi) { +module.exports = function prepareContextMiddleware() { return [ locals(), cleanUpQueryParams(), layergroupToken(), credentials(), - authorize(authApi) ]; }; diff --git a/lib/cartodb/server.js b/lib/cartodb/server.js index d2187930..fbbc3c53 100644 --- a/lib/cartodb/server.js +++ b/lib/cartodb/server.js @@ -217,7 +217,7 @@ module.exports = function(serverOptions) { const prepareContext = typeof serverOptions.req2params === 'function' ? serverOptions.req2params : - prepareContextMiddleware(authApi); + prepareContextMiddleware(); /******************************************************************************************************************* * Routing @@ -233,7 +233,8 @@ module.exports = function(serverOptions) { surrogateKeysCache, userLimitsApi, layergroupAffectedTablesCache, - analysisBackend + analysisBackend, + authApi ).register(app); new controller.Map( @@ -246,7 +247,8 @@ module.exports = function(serverOptions) { userLimitsApi, layergroupAffectedTablesCache, mapConfigAdapter, - statsBackend + statsBackend, + authApi ).register(app); new controller.NamedMaps( @@ -257,12 +259,13 @@ module.exports = function(serverOptions) { surrogateKeysCache, tablesExtentApi, metadataBackend, - pgConnection + pgConnection, + authApi ).register(app); new controller.NamedMapsAdmin(authApi, templateMaps).register(app); - new controller.Analyses(prepareContext, pgConnection).register(app); + new controller.Analyses(prepareContext, pgConnection, authApi).register(app); new controller.ServerInfo(versions).register(app); diff --git a/test/unit/cartodb/prepare-context.test.js b/test/unit/cartodb/prepare-context.test.js index 19afce21..ec38b954 100644 --- a/test/unit/cartodb/prepare-context.test.js +++ b/test/unit/cartodb/prepare-context.test.js @@ -8,7 +8,7 @@ var AuthApi = require('../../../lib/cartodb/api/auth_api'); var TemplateMaps = require('../../../lib/cartodb/backends/template_maps'); const cleanUpQueryParamsMiddleware = require('../../../lib/cartodb/middleware/context/clean-up-query-params'); -const authorizeMiddleware = require('../../../lib/cartodb/middleware/context/authorize'); +const authorizeMiddleware = require('../../../lib/cartodb/middleware/authorize'); const dbConnSetupMiddleware = require('../../../lib/cartodb/middleware/db-conn-setup'); const credentialsMiddleware = require('../../../lib/cartodb/middleware/context/credentials'); const localsMiddleware = require('../../../lib/cartodb/middleware/context/locals'); From 7ff7b0c2d1761518e07aa46476d4fe810ae8f772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Thu, 15 Mar 2018 19:38:11 +0100 Subject: [PATCH 03/32] Extract rest of the middlewares from prepare contex --- lib/cartodb/controllers/analyses.js | 12 +++- lib/cartodb/controllers/layergroup.js | 63 ++++++++++++++----- lib/cartodb/controllers/map.js | 12 +++- lib/cartodb/controllers/named_maps.js | 17 +++-- lib/cartodb/controllers/named_maps_admin.js | 4 +- .../{context => }/clean-up-query-params.js | 0 lib/cartodb/middleware/context/index.js | 13 ---- .../middleware/{context => }/credentials.js | 0 .../{context => }/layergroup-token.js | 2 +- .../middleware/{context => }/locals.js | 0 lib/cartodb/server.js | 11 +--- test/unit/cartodb/prepare-context.test.js | 6 +- 12 files changed, 87 insertions(+), 53 deletions(-) rename lib/cartodb/middleware/{context => }/clean-up-query-params.js (100%) delete mode 100644 lib/cartodb/middleware/context/index.js rename lib/cartodb/middleware/{context => }/credentials.js (100%) rename lib/cartodb/middleware/{context => }/layergroup-token.js (93%) rename lib/cartodb/middleware/{context => }/locals.js (100%) diff --git a/lib/cartodb/controllers/analyses.js b/lib/cartodb/controllers/analyses.js index 99c38fcb..1d97c9ee 100644 --- a/lib/cartodb/controllers/analyses.js +++ b/lib/cartodb/controllers/analyses.js @@ -1,11 +1,14 @@ var PSQL = require('cartodb-psql'); var cors = require('../middleware/cors'); var userMiddleware = require('../middleware/user'); +const locals = require('../middleware/locals'); +const cleanUpQueryParams = require('../middleware/clean-up-query-params'); +const layergroupToken = require('../middleware/layergroup-token'); +const credentials = require('../middleware/credentials'); const authorize = require('../middleware/authorize'); const dbConnSetup = require('../middleware/db-conn-setup'); -function AnalysesController(prepareContext, pgConnection, authApi) { - this.prepareContext = prepareContext; +function AnalysesController(pgConnection, authApi) { this.pgConnection = pgConnection; this.authApi = authApi; } @@ -17,7 +20,10 @@ AnalysesController.prototype.register = function (app) { `${app.base_url_mapconfig}/analyses/catalog`, cors(), userMiddleware(), - this.prepareContext, + locals(), + cleanUpQueryParams(), + layergroupToken(), + credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), createPGClient(), diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index 834bb0b7..06aa4ac8 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -2,6 +2,10 @@ const cors = require('../middleware/cors'); const userMiddleware = require('../middleware/user'); const allowQueryParams = require('../middleware/allow-query-params'); const vectorError = require('../middleware/vector-error'); +const locals = require('../middleware/locals'); +const cleanUpQueryParams = require('../middleware/clean-up-query-params'); +const layergroupToken = require('../middleware/layergroup-token'); +const credentials = require('../middleware/credentials'); const dbConnSetup = require('../middleware/db-conn-setup'); const authorize = require('../middleware/authorize'); const DataviewBackend = require('../backends/dataview'); @@ -30,7 +34,7 @@ const SUPPORTED_FORMATS = { * @param {AnalysisBackend} analysisBackend * @constructor */ -function LayergroupController(prepareContext, pgConnection, mapStore, tileBackend, previewBackend, attributesBackend, +function LayergroupController(pgConnection, mapStore, tileBackend, previewBackend, attributesBackend, surrogateKeysCache, userLimitsApi, layergroupAffectedTables, analysisBackend, authApi) { this.pgConnection = pgConnection; this.mapStore = mapStore; @@ -43,8 +47,6 @@ function LayergroupController(prepareContext, pgConnection, mapStore, tileBacken this.dataviewBackend = new DataviewBackend(analysisBackend); this.analysisStatusBackend = new AnalysisStatusBackend(); - - this.prepareContext = prepareContext; this.authApi = authApi; } @@ -57,7 +59,10 @@ LayergroupController.prototype.register = function(app) { `${basePath}/:token/:z/:x/:y@:scale_factor?x.:format`, cors(), userMiddleware(), - this.prepareContext, + locals(), + cleanUpQueryParams(), + layergroupToken(), + credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), @@ -78,7 +83,10 @@ LayergroupController.prototype.register = function(app) { `${basePath}/:token/:z/:x/:y.:format`, cors(), userMiddleware(), - this.prepareContext, + locals(), + cleanUpQueryParams(), + layergroupToken(), + credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), @@ -100,7 +108,10 @@ LayergroupController.prototype.register = function(app) { distinguishLayergroupFromStaticRoute(), cors(), userMiddleware(), - this.prepareContext, + locals(), + cleanUpQueryParams(), + layergroupToken(), + credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), @@ -121,7 +132,10 @@ LayergroupController.prototype.register = function(app) { `${basePath}/:token/:layer/attributes/:fid`, cors(), userMiddleware(), - this.prepareContext, + locals(), + cleanUpQueryParams(), + layergroupToken(), + credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), @@ -141,7 +155,10 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), allowQueryParams(['layer']), - this.prepareContext, + locals(), + cleanUpQueryParams(), + layergroupToken(), + credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat), @@ -159,7 +176,10 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), allowQueryParams(['layer']), - this.prepareContext, + locals(), + cleanUpQueryParams(), + layergroupToken(), + credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat), @@ -195,7 +215,10 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), allowQueryParams(allowedDataviewQueryParams), - this.prepareContext, + locals(), + cleanUpQueryParams(), + layergroupToken(), + credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), @@ -213,7 +236,10 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), allowQueryParams(allowedDataviewQueryParams), - this.prepareContext, + locals(), + cleanUpQueryParams(), + layergroupToken(), + credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), @@ -231,7 +257,10 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), allowQueryParams(allowedDataviewQueryParams), - this.prepareContext, + locals(), + cleanUpQueryParams(), + layergroupToken(), + credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), @@ -249,7 +278,10 @@ LayergroupController.prototype.register = function(app) { cors(), userMiddleware(), allowQueryParams(allowedDataviewQueryParams), - this.prepareContext, + locals(), + cleanUpQueryParams(), + layergroupToken(), + credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), @@ -266,7 +298,10 @@ LayergroupController.prototype.register = function(app) { `${basePath}/:token/analysis/node/:nodeId`, cors(), userMiddleware(), - this.prepareContext, + locals(), + cleanUpQueryParams(), + layergroupToken(), + credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), analysisNodeStatus(this.analysisStatusBackend), diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index 865b7361..5b8f22ca 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -7,6 +7,10 @@ const ResourceLocator = require('../models/resource-locator'); const cors = require('../middleware/cors'); const userMiddleware = require('../middleware/user'); const allowQueryParams = require('../middleware/allow-query-params'); +const locals = require('../middleware/locals'); +const cleanUpQueryParams = require('../middleware/clean-up-query-params'); +const layergroupToken = require('../middleware/layergroup-token'); +const credentials = require('../middleware/credentials'); const dbConnSetup = require('../middleware/db-conn-setup'); const authorize = require('../middleware/authorize'); const NamedMapsCacheEntry = require('../cache/model/named_maps_entry'); @@ -27,7 +31,7 @@ const LayergroupMetadata = require('../utils/layergroup-metadata'); * @param {StatsBackend} statsBackend * @constructor */ -function MapController(prepareContext, pgConnection, templateMaps, mapBackend, metadataBackend, +function MapController(pgConnection, templateMaps, mapBackend, metadataBackend, surrogateKeysCache, userLimitsApi, layergroupAffectedTables, mapConfigAdapter, statsBackend, authApi) { this.pgConnection = pgConnection; @@ -43,7 +47,6 @@ function MapController(prepareContext, pgConnection, templateMaps, mapBackend, m this.layergroupMetadata = new LayergroupMetadata(resourceLocator); this.statsBackend = statsBackend; - this.prepareContext = prepareContext; this.authApi = authApi; } @@ -71,7 +74,10 @@ MapController.prototype.composeCreateMapMiddleware = function (useTemplate = fal cors(), userMiddleware(), allowQueryParams(['aggregation']), - this.prepareContext, + locals(), + cleanUpQueryParams(), + layergroupToken(), + credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), initProfiler(isTemplateInstantiation), diff --git a/lib/cartodb/controllers/named_maps.js b/lib/cartodb/controllers/named_maps.js index 315a4097..2a1601f6 100644 --- a/lib/cartodb/controllers/named_maps.js +++ b/lib/cartodb/controllers/named_maps.js @@ -1,6 +1,10 @@ const NamedMapsCacheEntry = require('../cache/model/named_maps_entry'); const cors = require('../middleware/cors'); const userMiddleware = require('../middleware/user'); +const locals = require('../middleware/locals'); +const cleanUpQueryParams = require('../middleware/clean-up-query-params'); +const layergroupToken = require('../middleware/layergroup-token'); +const credentials = require('../middleware/credentials'); const dbConnSetup = require('../middleware/db-conn-setup'); const authorize = require('../middleware/authorize'); const allowQueryParams = require('../middleware/allow-query-params'); @@ -29,7 +33,7 @@ function getRequestParams(locals) { return params; } -function NamedMapsController(prepareContext, namedMapProviderCache, tileBackend, previewBackend, +function NamedMapsController(namedMapProviderCache, tileBackend, previewBackend, surrogateKeysCache, tablesExtentApi, metadataBackend, pgConnection, authApi) { this.namedMapProviderCache = namedMapProviderCache; this.tileBackend = tileBackend; @@ -37,7 +41,6 @@ function NamedMapsController(prepareContext, namedMapProviderCache, tileBackend, this.surrogateKeysCache = surrogateKeysCache; this.tablesExtentApi = tablesExtentApi; this.metadataBackend = metadataBackend; - this.prepareContext = prepareContext; this.pgConnection = pgConnection; this.authApi = authApi; } @@ -51,7 +54,10 @@ NamedMapsController.prototype.register = function(app) { `${base_url_templated}/:template_id/:layer/:z/:x/:y.(:format)`, cors(), userMiddleware(), - this.prepareContext, + locals(), + cleanUpQueryParams(), + layergroupToken(), + credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), getNamedMapProvider({ @@ -77,7 +83,10 @@ NamedMapsController.prototype.register = function(app) { cors(), userMiddleware(), allowQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']), - this.prepareContext, + locals(), + cleanUpQueryParams(), + layergroupToken(), + credentials(), authorize(this.authApi), dbConnSetup(this.pgConnection), getNamedMapProvider({ diff --git a/lib/cartodb/controllers/named_maps_admin.js b/lib/cartodb/controllers/named_maps_admin.js index 986fef5c..536baf45 100644 --- a/lib/cartodb/controllers/named_maps_admin.js +++ b/lib/cartodb/controllers/named_maps_admin.js @@ -1,8 +1,8 @@ const { templateName } = require('../backends/template_maps'); const cors = require('../middleware/cors'); const userMiddleware = require('../middleware/user'); -const localsMiddleware = require('../middleware/context/locals'); -const credentialsMiddleware = require('../middleware/context/credentials'); +const localsMiddleware = require('../middleware/locals'); +const credentialsMiddleware = require('../middleware/credentials'); /** * @param {AuthApi} authApi diff --git a/lib/cartodb/middleware/context/clean-up-query-params.js b/lib/cartodb/middleware/clean-up-query-params.js similarity index 100% rename from lib/cartodb/middleware/context/clean-up-query-params.js rename to lib/cartodb/middleware/clean-up-query-params.js diff --git a/lib/cartodb/middleware/context/index.js b/lib/cartodb/middleware/context/index.js deleted file mode 100644 index 59a6095b..00000000 --- a/lib/cartodb/middleware/context/index.js +++ /dev/null @@ -1,13 +0,0 @@ -const locals = require('./locals'); -const cleanUpQueryParams = require('./clean-up-query-params'); -const layergroupToken = require('./layergroup-token'); -const credentials = require('./credentials'); - -module.exports = function prepareContextMiddleware() { - return [ - locals(), - cleanUpQueryParams(), - layergroupToken(), - credentials(), - ]; -}; diff --git a/lib/cartodb/middleware/context/credentials.js b/lib/cartodb/middleware/credentials.js similarity index 100% rename from lib/cartodb/middleware/context/credentials.js rename to lib/cartodb/middleware/credentials.js diff --git a/lib/cartodb/middleware/context/layergroup-token.js b/lib/cartodb/middleware/layergroup-token.js similarity index 93% rename from lib/cartodb/middleware/context/layergroup-token.js rename to lib/cartodb/middleware/layergroup-token.js index c4aac23f..797b1b3d 100644 --- a/lib/cartodb/middleware/context/layergroup-token.js +++ b/lib/cartodb/middleware/layergroup-token.js @@ -1,4 +1,4 @@ -const LayergroupToken = require('../../models/layergroup-token'); +const LayergroupToken = require('../models/layergroup-token'); const authErrorMessageTemplate = function (signer, user) { return `Cannot use map signature of user "${signer}" on db of user "${user}"`; }; diff --git a/lib/cartodb/middleware/context/locals.js b/lib/cartodb/middleware/locals.js similarity index 100% rename from lib/cartodb/middleware/context/locals.js rename to lib/cartodb/middleware/locals.js diff --git a/lib/cartodb/server.js b/lib/cartodb/server.js index fbbc3c53..604c4342 100644 --- a/lib/cartodb/server.js +++ b/lib/cartodb/server.js @@ -49,8 +49,6 @@ var StatsBackend = require('./backends/stats'); const lzmaMiddleware = require('./middleware/lzma'); const errorMiddleware = require('./middleware/error-middleware'); -const prepareContextMiddleware = require('./middleware/context'); - module.exports = function(serverOptions) { // Make stats client globally accessible global.statsClient = StatsClient.getInstance(serverOptions.statsd); @@ -215,16 +213,11 @@ module.exports = function(serverOptions) { var versions = getAndValidateVersions(serverOptions); - const prepareContext = typeof serverOptions.req2params === 'function' ? - serverOptions.req2params : - prepareContextMiddleware(); - /******************************************************************************************************************* * Routing ******************************************************************************************************************/ new controller.Layergroup( - prepareContext, pgConnection, mapStore, tileBackend, @@ -238,7 +231,6 @@ module.exports = function(serverOptions) { ).register(app); new controller.Map( - prepareContext, pgConnection, templateMaps, mapBackend, @@ -252,7 +244,6 @@ module.exports = function(serverOptions) { ).register(app); new controller.NamedMaps( - prepareContext, namedMapProviderCache, tileBackend, previewBackend, @@ -265,7 +256,7 @@ module.exports = function(serverOptions) { new controller.NamedMapsAdmin(authApi, templateMaps).register(app); - new controller.Analyses(prepareContext, pgConnection, authApi).register(app); + new controller.Analyses(pgConnection, authApi).register(app); new controller.ServerInfo(versions).register(app); diff --git a/test/unit/cartodb/prepare-context.test.js b/test/unit/cartodb/prepare-context.test.js index ec38b954..f71bed2e 100644 --- a/test/unit/cartodb/prepare-context.test.js +++ b/test/unit/cartodb/prepare-context.test.js @@ -7,11 +7,11 @@ var PgConnection = require('../../../lib/cartodb/backends/pg_connection'); var AuthApi = require('../../../lib/cartodb/api/auth_api'); var TemplateMaps = require('../../../lib/cartodb/backends/template_maps'); -const cleanUpQueryParamsMiddleware = require('../../../lib/cartodb/middleware/context/clean-up-query-params'); +const cleanUpQueryParamsMiddleware = require('../../../lib/cartodb/middleware/clean-up-query-params'); const authorizeMiddleware = require('../../../lib/cartodb/middleware/authorize'); const dbConnSetupMiddleware = require('../../../lib/cartodb/middleware/db-conn-setup'); -const credentialsMiddleware = require('../../../lib/cartodb/middleware/context/credentials'); -const localsMiddleware = require('../../../lib/cartodb/middleware/context/locals'); +const credentialsMiddleware = require('../../../lib/cartodb/middleware/credentials'); +const localsMiddleware = require('../../../lib/cartodb/middleware/locals'); var windshaft = require('windshaft'); From 5ede6c30216b4637c1c5738a83405507dc99eb12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Thu, 15 Mar 2018 19:38:39 +0100 Subject: [PATCH 04/32] Add test suite to not run ported test --- Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Makefile b/Makefile index f523adda..69b9b949 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,7 @@ config/environments/test.js: config.status--test ./config.status--test TEST_SUITE := $(shell find test/{acceptance,integration,unit} -name "*.js") +TEST_SUITE_NO_PORTED := $(shell find test/acceptance test/integration test/unit -type f -not -path "*acceptance/ported*" -not -path "test/acceptance/overviews_queries.js" -name "*.js") TEST_SUITE_UNIT := $(shell find test/unit -name "*.js") TEST_SUITE_INTEGRATION := $(shell find test/integration -name "*.js") TEST_SUITE_ACCEPTANCE := $(shell find test/acceptance -name "*.js") @@ -27,6 +28,10 @@ test: config/environments/test.js @echo "***tests***" @$(SHELL) ./run_tests.sh ${RUNTESTFLAGS} $(TEST_SUITE) +test-no-ported: config/environments/test.js + @echo "***tests no ported ***" + @$(SHELL) ./run_tests.sh ${RUNTESTFLAGS} $(TEST_SUITE_NO_PORTED) + test-unit: config/environments/test.js @echo "***tests***" @$(SHELL) ./run_tests.sh ${RUNTESTFLAGS} $(TEST_SUITE_UNIT) From 071a5a4bdfd900fcb0e64a2c95d749270f60bbbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 13:04:42 +0100 Subject: [PATCH 05/32] Rename base paths --- lib/cartodb/controllers/analyses.js | 4 +++- lib/cartodb/controllers/layergroup.js | 24 ++++++++++----------- lib/cartodb/controllers/map.js | 12 ++++++----- lib/cartodb/controllers/named_maps.js | 6 +++--- lib/cartodb/controllers/named_maps_admin.js | 14 ++++++------ 5 files changed, 32 insertions(+), 28 deletions(-) diff --git a/lib/cartodb/controllers/analyses.js b/lib/cartodb/controllers/analyses.js index 1d97c9ee..be793bf1 100644 --- a/lib/cartodb/controllers/analyses.js +++ b/lib/cartodb/controllers/analyses.js @@ -16,8 +16,10 @@ function AnalysesController(pgConnection, authApi) { module.exports = AnalysesController; AnalysesController.prototype.register = function (app) { + const { base_url_mapconfig: mapconfigBasePath } = app; + app.get( - `${app.base_url_mapconfig}/analyses/catalog`, + `${mapconfigBasePath}/analyses/catalog`, cors(), userMiddleware(), locals(), diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index 06aa4ac8..3d0d8739 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -53,10 +53,10 @@ function LayergroupController(pgConnection, mapStore, tileBackend, previewBacken module.exports = LayergroupController; LayergroupController.prototype.register = function(app) { - const { base_url_mapconfig: basePath } = app; + const { base_url_mapconfig: mapconfigBasePath } = app; app.get( - `${basePath}/:token/:z/:x/:y@:scale_factor?x.:format`, + `${mapconfigBasePath}/:token/:z/:x/:y@:scale_factor?x.:format`, cors(), userMiddleware(), locals(), @@ -80,7 +80,7 @@ LayergroupController.prototype.register = function(app) { ); app.get( - `${basePath}/:token/:z/:x/:y.:format`, + `${mapconfigBasePath}/:token/:z/:x/:y.:format`, cors(), userMiddleware(), locals(), @@ -104,7 +104,7 @@ LayergroupController.prototype.register = function(app) { ); app.get( - `${basePath}/:token/:layer/:z/:x/:y.(:format)`, + `${mapconfigBasePath}/:token/:layer/:z/:x/:y.(:format)`, distinguishLayergroupFromStaticRoute(), cors(), userMiddleware(), @@ -129,7 +129,7 @@ LayergroupController.prototype.register = function(app) { ); app.get( - `${basePath}/:token/:layer/attributes/:fid`, + `${mapconfigBasePath}/:token/:layer/attributes/:fid`, cors(), userMiddleware(), locals(), @@ -151,7 +151,7 @@ LayergroupController.prototype.register = function(app) { const forcedFormat = 'png'; app.get( - `${basePath}/static/center/:token/:z/:lat/:lng/:width/:height.:format`, + `${mapconfigBasePath}/static/center/:token/:z/:lat/:lng/:width/:height.:format`, cors(), userMiddleware(), allowQueryParams(['layer']), @@ -172,7 +172,7 @@ LayergroupController.prototype.register = function(app) { ); app.get( - `${basePath}/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format`, + `${mapconfigBasePath}/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format`, cors(), userMiddleware(), allowQueryParams(['layer']), @@ -211,7 +211,7 @@ LayergroupController.prototype.register = function(app) { ]; app.get( - `${basePath}/:token/dataview/:dataviewName`, + `${mapconfigBasePath}/:token/dataview/:dataviewName`, cors(), userMiddleware(), allowQueryParams(allowedDataviewQueryParams), @@ -232,7 +232,7 @@ LayergroupController.prototype.register = function(app) { ); app.get( - `${basePath}/:token/:layer/widget/:dataviewName`, + `${mapconfigBasePath}/:token/:layer/widget/:dataviewName`, cors(), userMiddleware(), allowQueryParams(allowedDataviewQueryParams), @@ -253,7 +253,7 @@ LayergroupController.prototype.register = function(app) { ); app.get( - `${basePath}/:token/dataview/:dataviewName/search`, + `${mapconfigBasePath}/:token/dataview/:dataviewName/search`, cors(), userMiddleware(), allowQueryParams(allowedDataviewQueryParams), @@ -274,7 +274,7 @@ LayergroupController.prototype.register = function(app) { ); app.get( - `${basePath}/:token/:layer/widget/:dataviewName/search`, + `${mapconfigBasePath}/:token/:layer/widget/:dataviewName/search`, cors(), userMiddleware(), allowQueryParams(allowedDataviewQueryParams), @@ -295,7 +295,7 @@ LayergroupController.prototype.register = function(app) { ); app.get( - `${basePath}/:token/analysis/node/:nodeId`, + `${mapconfigBasePath}/:token/analysis/node/:nodeId`, cors(), userMiddleware(), locals(), diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index 5b8f22ca..d72b6fea 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -53,13 +53,15 @@ function MapController(pgConnection, templateMaps, mapBackend, metadataBackend, module.exports = MapController; MapController.prototype.register = function(app) { - const { base_url_mapconfig, base_url_templated } = app; + const { base_url_mapconfig: mapconfigBasePath, base_url_templated: templateBasePath } = app; const useTemplate = true; - app.get(base_url_mapconfig, this.composeCreateMapMiddleware()); - app.post(base_url_mapconfig, this.composeCreateMapMiddleware()); - app.get(`${base_url_templated}/:template_id/jsonp`, this.composeCreateMapMiddleware(useTemplate)); - app.post(`${base_url_templated}/:template_id`, this.composeCreateMapMiddleware(useTemplate)); + app.get(mapconfigBasePath, this.composeCreateMapMiddleware()); + app.post(mapconfigBasePath, this.composeCreateMapMiddleware()); + + app.get(`${templateBasePath}/:template_id/jsonp`, this.composeCreateMapMiddleware(useTemplate)); + app.post(`${templateBasePath}/:template_id`, this.composeCreateMapMiddleware(useTemplate)); + app.options(app.base_url_mapconfig, cors('Content-Type')); }; diff --git a/lib/cartodb/controllers/named_maps.js b/lib/cartodb/controllers/named_maps.js index 2a1601f6..9b2ad090 100644 --- a/lib/cartodb/controllers/named_maps.js +++ b/lib/cartodb/controllers/named_maps.js @@ -48,10 +48,10 @@ function NamedMapsController(namedMapProviderCache, tileBackend, previewBackend, module.exports = NamedMapsController; NamedMapsController.prototype.register = function(app) { - const { base_url_mapconfig, base_url_templated } = app; + const { base_url_mapconfig: mapconfigBasePath, base_url_templated: templateBasePath } = app; app.get( - `${base_url_templated}/:template_id/:layer/:z/:x/:y.(:format)`, + `${templateBasePath}/:template_id/:layer/:z/:x/:y.(:format)`, cors(), userMiddleware(), locals(), @@ -79,7 +79,7 @@ NamedMapsController.prototype.register = function(app) { ); app.get( - `${base_url_mapconfig}/static/named/:template_id/:width/:height.:format`, + `${mapconfigBasePath}/static/named/:template_id/:width/:height.:format`, cors(), userMiddleware(), allowQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']), diff --git a/lib/cartodb/controllers/named_maps_admin.js b/lib/cartodb/controllers/named_maps_admin.js index 536baf45..a662f67b 100644 --- a/lib/cartodb/controllers/named_maps_admin.js +++ b/lib/cartodb/controllers/named_maps_admin.js @@ -18,10 +18,10 @@ function NamedMapsAdminController(authApi, templateMaps) { module.exports = NamedMapsAdminController; NamedMapsAdminController.prototype.register = function (app) { - const { base_url_templated } = app; + const { base_url_templated: templateBasePath } = app; app.post( - `${base_url_templated}/`, + `${templateBasePath}/`, cors(), userMiddleware(), localsMiddleware(), @@ -33,7 +33,7 @@ NamedMapsAdminController.prototype.register = function (app) { ); app.put( - `${base_url_templated}/:template_id`, + `${templateBasePath}/:template_id`, cors(), userMiddleware(), localsMiddleware(), @@ -45,7 +45,7 @@ NamedMapsAdminController.prototype.register = function (app) { ); app.get( - `${base_url_templated}/:template_id`, + `${templateBasePath}/:template_id`, cors(), userMiddleware(), localsMiddleware(), @@ -56,7 +56,7 @@ NamedMapsAdminController.prototype.register = function (app) { ); app.delete( - `${base_url_templated}/:template_id`, + `${templateBasePath}/:template_id`, cors(), userMiddleware(), localsMiddleware(), @@ -67,7 +67,7 @@ NamedMapsAdminController.prototype.register = function (app) { ); app.get( - `${base_url_templated}/`, + `${templateBasePath}/`, cors(), userMiddleware(), localsMiddleware(), @@ -78,7 +78,7 @@ NamedMapsAdminController.prototype.register = function (app) { ); app.options( - `${base_url_templated}/:template_id`, + `${templateBasePath}/:template_id`, cors('Content-Type') ); }; From 7b11cdcb74f765aafb6ed2348751f86256c79ce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 13:08:00 +0100 Subject: [PATCH 06/32] Use template string --- lib/cartodb/controllers/map.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index d72b6fea..8e376cb5 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -56,8 +56,8 @@ MapController.prototype.register = function(app) { const { base_url_mapconfig: mapconfigBasePath, base_url_templated: templateBasePath } = app; const useTemplate = true; - app.get(mapconfigBasePath, this.composeCreateMapMiddleware()); - app.post(mapconfigBasePath, this.composeCreateMapMiddleware()); + app.get(`${mapconfigBasePath}`, this.composeCreateMapMiddleware()); + app.post(`${mapconfigBasePath}`, this.composeCreateMapMiddleware()); app.get(`${templateBasePath}/:template_id/jsonp`, this.composeCreateMapMiddleware(useTemplate)); app.post(`${templateBasePath}/:template_id`, this.composeCreateMapMiddleware(useTemplate)); From 0aa8d63a6e996cffa9c990c84943adf3459a4c10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 14:03:59 +0100 Subject: [PATCH 07/32] Unifiy allowQueryParams and cleanUpQueryParams middlewares --- lib/cartodb/controllers/layergroup.js | 19 ++++++------------- lib/cartodb/controllers/map.js | 4 +--- lib/cartodb/controllers/named_maps.js | 4 +--- lib/cartodb/middleware/allow-query-params.js | 10 ---------- .../middleware/clean-up-query-params.js | 12 ++++++++---- 5 files changed, 16 insertions(+), 33 deletions(-) delete mode 100644 lib/cartodb/middleware/allow-query-params.js diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index 3d0d8739..c4e969e9 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -1,6 +1,5 @@ const cors = require('../middleware/cors'); const userMiddleware = require('../middleware/user'); -const allowQueryParams = require('../middleware/allow-query-params'); const vectorError = require('../middleware/vector-error'); const locals = require('../middleware/locals'); const cleanUpQueryParams = require('../middleware/clean-up-query-params'); @@ -154,9 +153,8 @@ LayergroupController.prototype.register = function(app) { `${mapconfigBasePath}/static/center/:token/:z/:lat/:lng/:width/:height.:format`, cors(), userMiddleware(), - allowQueryParams(['layer']), locals(), - cleanUpQueryParams(), + cleanUpQueryParams(['layer']), layergroupToken(), credentials(), authorize(this.authApi), @@ -175,9 +173,8 @@ LayergroupController.prototype.register = function(app) { `${mapconfigBasePath}/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format`, cors(), userMiddleware(), - allowQueryParams(['layer']), locals(), - cleanUpQueryParams(), + cleanUpQueryParams(['layer']), layergroupToken(), credentials(), authorize(this.authApi), @@ -214,9 +211,8 @@ LayergroupController.prototype.register = function(app) { `${mapconfigBasePath}/:token/dataview/:dataviewName`, cors(), userMiddleware(), - allowQueryParams(allowedDataviewQueryParams), locals(), - cleanUpQueryParams(), + cleanUpQueryParams(allowedDataviewQueryParams), layergroupToken(), credentials(), authorize(this.authApi), @@ -235,9 +231,8 @@ LayergroupController.prototype.register = function(app) { `${mapconfigBasePath}/:token/:layer/widget/:dataviewName`, cors(), userMiddleware(), - allowQueryParams(allowedDataviewQueryParams), locals(), - cleanUpQueryParams(), + cleanUpQueryParams(allowedDataviewQueryParams), layergroupToken(), credentials(), authorize(this.authApi), @@ -256,9 +251,8 @@ LayergroupController.prototype.register = function(app) { `${mapconfigBasePath}/:token/dataview/:dataviewName/search`, cors(), userMiddleware(), - allowQueryParams(allowedDataviewQueryParams), locals(), - cleanUpQueryParams(), + cleanUpQueryParams(allowedDataviewQueryParams), layergroupToken(), credentials(), authorize(this.authApi), @@ -277,9 +271,8 @@ LayergroupController.prototype.register = function(app) { `${mapconfigBasePath}/:token/:layer/widget/:dataviewName/search`, cors(), userMiddleware(), - allowQueryParams(allowedDataviewQueryParams), locals(), - cleanUpQueryParams(), + cleanUpQueryParams(allowedDataviewQueryParams), layergroupToken(), credentials(), authorize(this.authApi), diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index 8e376cb5..d9753726 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -6,7 +6,6 @@ const QueryTables = require('cartodb-query-tables'); const ResourceLocator = require('../models/resource-locator'); const cors = require('../middleware/cors'); const userMiddleware = require('../middleware/user'); -const allowQueryParams = require('../middleware/allow-query-params'); const locals = require('../middleware/locals'); const cleanUpQueryParams = require('../middleware/clean-up-query-params'); const layergroupToken = require('../middleware/layergroup-token'); @@ -75,9 +74,8 @@ MapController.prototype.composeCreateMapMiddleware = function (useTemplate = fal return [ cors(), userMiddleware(), - allowQueryParams(['aggregation']), locals(), - cleanUpQueryParams(), + cleanUpQueryParams(['aggregation']), layergroupToken(), credentials(), authorize(this.authApi), diff --git a/lib/cartodb/controllers/named_maps.js b/lib/cartodb/controllers/named_maps.js index 9b2ad090..433c31b0 100644 --- a/lib/cartodb/controllers/named_maps.js +++ b/lib/cartodb/controllers/named_maps.js @@ -7,7 +7,6 @@ const layergroupToken = require('../middleware/layergroup-token'); const credentials = require('../middleware/credentials'); const dbConnSetup = require('../middleware/db-conn-setup'); const authorize = require('../middleware/authorize'); -const allowQueryParams = require('../middleware/allow-query-params'); const vectorError = require('../middleware/vector-error'); const DEFAULT_ZOOM_CENTER = { @@ -82,9 +81,8 @@ NamedMapsController.prototype.register = function(app) { `${mapconfigBasePath}/static/named/:template_id/:width/:height.:format`, cors(), userMiddleware(), - allowQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']), locals(), - cleanUpQueryParams(), + cleanUpQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']), layergroupToken(), credentials(), authorize(this.authApi), diff --git a/lib/cartodb/middleware/allow-query-params.js b/lib/cartodb/middleware/allow-query-params.js deleted file mode 100644 index cf90e69b..00000000 --- a/lib/cartodb/middleware/allow-query-params.js +++ /dev/null @@ -1,10 +0,0 @@ -module.exports = function allowQueryParams (params) { - if (!Array.isArray(params)) { - throw new Error('allowQueryParams must receive an Array of params'); - } - - return function allowQueryParamsMiddleware (req, res, next) { - res.locals.allowedQueryParams = params; - next(); - }; -}; diff --git a/lib/cartodb/middleware/clean-up-query-params.js b/lib/cartodb/middleware/clean-up-query-params.js index 280fe986..2553380d 100644 --- a/lib/cartodb/middleware/clean-up-query-params.js +++ b/lib/cartodb/middleware/clean-up-query-params.js @@ -14,19 +14,23 @@ const REQUEST_QUERY_PARAMS_WHITELIST = [ 'filters' // json ]; -module.exports = function cleanUpQueryParamsMiddleware () { +module.exports = function cleanUpQueryParamsMiddleware (customQueryParams = []) { + if (!Array.isArray(customQueryParams)) { + throw new Error('customQueryParams must receive an Array of params'); + } + return function cleanUpQueryParams (req, res, next) { var allowedQueryParams = REQUEST_QUERY_PARAMS_WHITELIST; - if (Array.isArray(res.locals.allowedQueryParams)) { - allowedQueryParams = allowedQueryParams.concat(res.locals.allowedQueryParams); + if (Array.isArray(customQueryParams)) { + allowedQueryParams = allowedQueryParams.concat(customQueryParams); } req.query = _.pick(req.query, allowedQueryParams); // bring all query values onto res.locals object _.extend(res.locals, req.query); - + next(); }; }; From 67d2d2fe9549b70998b7cdd24e646e5a70671171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 14:20:41 +0100 Subject: [PATCH 08/32] Use spread operator --- lib/cartodb/middleware/clean-up-query-params.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/cartodb/middleware/clean-up-query-params.js b/lib/cartodb/middleware/clean-up-query-params.js index 2553380d..d5c93c07 100644 --- a/lib/cartodb/middleware/clean-up-query-params.js +++ b/lib/cartodb/middleware/clean-up-query-params.js @@ -20,11 +20,7 @@ module.exports = function cleanUpQueryParamsMiddleware (customQueryParams = []) } return function cleanUpQueryParams (req, res, next) { - var allowedQueryParams = REQUEST_QUERY_PARAMS_WHITELIST; - - if (Array.isArray(customQueryParams)) { - allowedQueryParams = allowedQueryParams.concat(customQueryParams); - } + const allowedQueryParams = [...REQUEST_QUERY_PARAMS_WHITELIST, ...customQueryParams]; req.query = _.pick(req.query, allowedQueryParams); From 639a69a6397341cd719eeb29f6cc944e465e1fa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 16:12:36 +0100 Subject: [PATCH 09/32] Reorder middlewares to optimize workflow --- lib/cartodb/controllers/analyses.js | 4 +- lib/cartodb/controllers/layergroup.js | 44 +++++++++---------- lib/cartodb/controllers/map.js | 4 +- lib/cartodb/controllers/named_maps.js | 8 ++-- lib/cartodb/controllers/named_maps_admin.js | 10 ++--- .../middleware/clean-up-query-params.js | 3 -- lib/cartodb/middleware/locals.js | 2 +- 7 files changed, 36 insertions(+), 39 deletions(-) diff --git a/lib/cartodb/controllers/analyses.js b/lib/cartodb/controllers/analyses.js index be793bf1..3453b2de 100644 --- a/lib/cartodb/controllers/analyses.js +++ b/lib/cartodb/controllers/analyses.js @@ -21,9 +21,9 @@ AnalysesController.prototype.register = function (app) { app.get( `${mapconfigBasePath}/analyses/catalog`, cors(), - userMiddleware(), - locals(), cleanUpQueryParams(), + locals(), + userMiddleware(), layergroupToken(), credentials(), authorize(this.authApi), diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index c4e969e9..8e0c472d 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -57,9 +57,9 @@ LayergroupController.prototype.register = function(app) { app.get( `${mapconfigBasePath}/:token/:z/:x/:y@:scale_factor?x.:format`, cors(), - userMiddleware(), - locals(), cleanUpQueryParams(), + locals(), + userMiddleware(), layergroupToken(), credentials(), authorize(this.authApi), @@ -81,9 +81,9 @@ LayergroupController.prototype.register = function(app) { app.get( `${mapconfigBasePath}/:token/:z/:x/:y.:format`, cors(), - userMiddleware(), - locals(), cleanUpQueryParams(), + locals(), + userMiddleware(), layergroupToken(), credentials(), authorize(this.authApi), @@ -106,9 +106,9 @@ LayergroupController.prototype.register = function(app) { `${mapconfigBasePath}/:token/:layer/:z/:x/:y.(:format)`, distinguishLayergroupFromStaticRoute(), cors(), - userMiddleware(), - locals(), cleanUpQueryParams(), + locals(), + userMiddleware(), layergroupToken(), credentials(), authorize(this.authApi), @@ -130,9 +130,9 @@ LayergroupController.prototype.register = function(app) { app.get( `${mapconfigBasePath}/:token/:layer/attributes/:fid`, cors(), - userMiddleware(), - locals(), cleanUpQueryParams(), + locals(), + userMiddleware(), layergroupToken(), credentials(), authorize(this.authApi), @@ -152,9 +152,9 @@ LayergroupController.prototype.register = function(app) { app.get( `${mapconfigBasePath}/static/center/:token/:z/:lat/:lng/:width/:height.:format`, cors(), - userMiddleware(), - locals(), cleanUpQueryParams(['layer']), + locals(), + userMiddleware(), layergroupToken(), credentials(), authorize(this.authApi), @@ -172,9 +172,9 @@ LayergroupController.prototype.register = function(app) { app.get( `${mapconfigBasePath}/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format`, cors(), - userMiddleware(), - locals(), cleanUpQueryParams(['layer']), + locals(), + userMiddleware(), layergroupToken(), credentials(), authorize(this.authApi), @@ -210,9 +210,9 @@ LayergroupController.prototype.register = function(app) { app.get( `${mapconfigBasePath}/:token/dataview/:dataviewName`, cors(), - userMiddleware(), - locals(), cleanUpQueryParams(allowedDataviewQueryParams), + locals(), + userMiddleware(), layergroupToken(), credentials(), authorize(this.authApi), @@ -230,9 +230,9 @@ LayergroupController.prototype.register = function(app) { app.get( `${mapconfigBasePath}/:token/:layer/widget/:dataviewName`, cors(), - userMiddleware(), - locals(), cleanUpQueryParams(allowedDataviewQueryParams), + locals(), + userMiddleware(), layergroupToken(), credentials(), authorize(this.authApi), @@ -250,9 +250,9 @@ LayergroupController.prototype.register = function(app) { app.get( `${mapconfigBasePath}/:token/dataview/:dataviewName/search`, cors(), - userMiddleware(), - locals(), cleanUpQueryParams(allowedDataviewQueryParams), + locals(), + userMiddleware(), layergroupToken(), credentials(), authorize(this.authApi), @@ -270,9 +270,9 @@ LayergroupController.prototype.register = function(app) { app.get( `${mapconfigBasePath}/:token/:layer/widget/:dataviewName/search`, cors(), - userMiddleware(), - locals(), cleanUpQueryParams(allowedDataviewQueryParams), + locals(), + userMiddleware(), layergroupToken(), credentials(), authorize(this.authApi), @@ -290,9 +290,9 @@ LayergroupController.prototype.register = function(app) { app.get( `${mapconfigBasePath}/:token/analysis/node/:nodeId`, cors(), - userMiddleware(), - locals(), cleanUpQueryParams(), + locals(), + userMiddleware(), layergroupToken(), credentials(), authorize(this.authApi), diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index d9753726..4adbd3b0 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -73,9 +73,9 @@ MapController.prototype.composeCreateMapMiddleware = function (useTemplate = fal return [ cors(), - userMiddleware(), - locals(), cleanUpQueryParams(['aggregation']), + locals(), + userMiddleware(), layergroupToken(), credentials(), authorize(this.authApi), diff --git a/lib/cartodb/controllers/named_maps.js b/lib/cartodb/controllers/named_maps.js index 433c31b0..703c17b6 100644 --- a/lib/cartodb/controllers/named_maps.js +++ b/lib/cartodb/controllers/named_maps.js @@ -52,9 +52,9 @@ NamedMapsController.prototype.register = function(app) { app.get( `${templateBasePath}/:template_id/:layer/:z/:x/:y.(:format)`, cors(), - userMiddleware(), - locals(), cleanUpQueryParams(), + locals(), + userMiddleware(), layergroupToken(), credentials(), authorize(this.authApi), @@ -80,9 +80,9 @@ NamedMapsController.prototype.register = function(app) { app.get( `${mapconfigBasePath}/static/named/:template_id/:width/:height.:format`, cors(), - userMiddleware(), - locals(), cleanUpQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']), + locals(), + userMiddleware(), layergroupToken(), credentials(), authorize(this.authApi), diff --git a/lib/cartodb/controllers/named_maps_admin.js b/lib/cartodb/controllers/named_maps_admin.js index a662f67b..96d8d8ae 100644 --- a/lib/cartodb/controllers/named_maps_admin.js +++ b/lib/cartodb/controllers/named_maps_admin.js @@ -23,8 +23,8 @@ NamedMapsAdminController.prototype.register = function (app) { app.post( `${templateBasePath}/`, cors(), - userMiddleware(), localsMiddleware(), + userMiddleware(), credentialsMiddleware(), checkContentType({ action: 'POST', label: 'POST TEMPLATE' }), authorizedByAPIKey({ authApi: this.authApi, action: 'create', label: 'POST TEMPLATE' }), @@ -35,8 +35,8 @@ NamedMapsAdminController.prototype.register = function (app) { app.put( `${templateBasePath}/:template_id`, cors(), - userMiddleware(), localsMiddleware(), + userMiddleware(), credentialsMiddleware(), checkContentType({ action: 'PUT', label: 'PUT TEMPLATE' }), authorizedByAPIKey({ authApi: this.authApi, action: 'update', label: 'PUT TEMPLATE' }), @@ -47,8 +47,8 @@ NamedMapsAdminController.prototype.register = function (app) { app.get( `${templateBasePath}/:template_id`, cors(), - userMiddleware(), localsMiddleware(), + userMiddleware(), credentialsMiddleware(), authorizedByAPIKey({ authApi: this.authApi, action: 'get', label: 'GET TEMPLATE' }), retrieveTemplate({ templateMaps: this.templateMaps }), @@ -58,8 +58,8 @@ NamedMapsAdminController.prototype.register = function (app) { app.delete( `${templateBasePath}/:template_id`, cors(), - userMiddleware(), localsMiddleware(), + userMiddleware(), credentialsMiddleware(), authorizedByAPIKey({ authApi: this.authApi, action: 'delete', label: 'DELETE TEMPLATE' }), destroyTemplate({ templateMaps: this.templateMaps }), @@ -69,8 +69,8 @@ NamedMapsAdminController.prototype.register = function (app) { app.get( `${templateBasePath}/`, cors(), - userMiddleware(), localsMiddleware(), + userMiddleware(), credentialsMiddleware(), authorizedByAPIKey({ authApi: this.authApi, action: 'list', label: 'GET TEMPLATE LIST' }), listTemplates({ templateMaps: this.templateMaps }), diff --git a/lib/cartodb/middleware/clean-up-query-params.js b/lib/cartodb/middleware/clean-up-query-params.js index d5c93c07..768b35f2 100644 --- a/lib/cartodb/middleware/clean-up-query-params.js +++ b/lib/cartodb/middleware/clean-up-query-params.js @@ -24,9 +24,6 @@ module.exports = function cleanUpQueryParamsMiddleware (customQueryParams = []) req.query = _.pick(req.query, allowedQueryParams); - // bring all query values onto res.locals object - _.extend(res.locals, req.query); - next(); }; }; diff --git a/lib/cartodb/middleware/locals.js b/lib/cartodb/middleware/locals.js index f6f70923..2629767e 100644 --- a/lib/cartodb/middleware/locals.js +++ b/lib/cartodb/middleware/locals.js @@ -1,6 +1,6 @@ module.exports = function locals () { return function localsMiddleware (req, res, next) { - res.locals = Object.assign(req.params || {}, res.locals); + res.locals = Object.assign({}, req.query, req.params); next(); }; From 313fc75ec825864e1149405243d0b326f8ab179b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 16:28:50 +0100 Subject: [PATCH 10/32] Rename middleware --- lib/cartodb/controllers/analyses.js | 4 +-- lib/cartodb/controllers/layergroup.js | 24 +++++++------- lib/cartodb/controllers/map.js | 4 +-- lib/cartodb/controllers/named_maps.js | 6 ++-- lib/cartodb/controllers/named_maps_admin.js | 36 ++++++++++----------- 5 files changed, 37 insertions(+), 37 deletions(-) diff --git a/lib/cartodb/controllers/analyses.js b/lib/cartodb/controllers/analyses.js index 3453b2de..d3d4c711 100644 --- a/lib/cartodb/controllers/analyses.js +++ b/lib/cartodb/controllers/analyses.js @@ -1,6 +1,6 @@ var PSQL = require('cartodb-psql'); var cors = require('../middleware/cors'); -var userMiddleware = require('../middleware/user'); +var user = require('../middleware/user'); const locals = require('../middleware/locals'); const cleanUpQueryParams = require('../middleware/clean-up-query-params'); const layergroupToken = require('../middleware/layergroup-token'); @@ -23,7 +23,7 @@ AnalysesController.prototype.register = function (app) { cors(), cleanUpQueryParams(), locals(), - userMiddleware(), + user(), layergroupToken(), credentials(), authorize(this.authApi), diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index 8e0c472d..f3976cb6 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -1,5 +1,5 @@ const cors = require('../middleware/cors'); -const userMiddleware = require('../middleware/user'); +const user = require('../middleware/user'); const vectorError = require('../middleware/vector-error'); const locals = require('../middleware/locals'); const cleanUpQueryParams = require('../middleware/clean-up-query-params'); @@ -59,7 +59,7 @@ LayergroupController.prototype.register = function(app) { cors(), cleanUpQueryParams(), locals(), - userMiddleware(), + user(), layergroupToken(), credentials(), authorize(this.authApi), @@ -83,7 +83,7 @@ LayergroupController.prototype.register = function(app) { cors(), cleanUpQueryParams(), locals(), - userMiddleware(), + user(), layergroupToken(), credentials(), authorize(this.authApi), @@ -108,7 +108,7 @@ LayergroupController.prototype.register = function(app) { cors(), cleanUpQueryParams(), locals(), - userMiddleware(), + user(), layergroupToken(), credentials(), authorize(this.authApi), @@ -132,7 +132,7 @@ LayergroupController.prototype.register = function(app) { cors(), cleanUpQueryParams(), locals(), - userMiddleware(), + user(), layergroupToken(), credentials(), authorize(this.authApi), @@ -154,7 +154,7 @@ LayergroupController.prototype.register = function(app) { cors(), cleanUpQueryParams(['layer']), locals(), - userMiddleware(), + user(), layergroupToken(), credentials(), authorize(this.authApi), @@ -174,7 +174,7 @@ LayergroupController.prototype.register = function(app) { cors(), cleanUpQueryParams(['layer']), locals(), - userMiddleware(), + user(), layergroupToken(), credentials(), authorize(this.authApi), @@ -212,7 +212,7 @@ LayergroupController.prototype.register = function(app) { cors(), cleanUpQueryParams(allowedDataviewQueryParams), locals(), - userMiddleware(), + user(), layergroupToken(), credentials(), authorize(this.authApi), @@ -232,7 +232,7 @@ LayergroupController.prototype.register = function(app) { cors(), cleanUpQueryParams(allowedDataviewQueryParams), locals(), - userMiddleware(), + user(), layergroupToken(), credentials(), authorize(this.authApi), @@ -252,7 +252,7 @@ LayergroupController.prototype.register = function(app) { cors(), cleanUpQueryParams(allowedDataviewQueryParams), locals(), - userMiddleware(), + user(), layergroupToken(), credentials(), authorize(this.authApi), @@ -272,7 +272,7 @@ LayergroupController.prototype.register = function(app) { cors(), cleanUpQueryParams(allowedDataviewQueryParams), locals(), - userMiddleware(), + user(), layergroupToken(), credentials(), authorize(this.authApi), @@ -292,7 +292,7 @@ LayergroupController.prototype.register = function(app) { cors(), cleanUpQueryParams(), locals(), - userMiddleware(), + user(), layergroupToken(), credentials(), authorize(this.authApi), diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index 4adbd3b0..067dc789 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -5,7 +5,7 @@ const Datasource = windshaft.model.Datasource; const QueryTables = require('cartodb-query-tables'); const ResourceLocator = require('../models/resource-locator'); const cors = require('../middleware/cors'); -const userMiddleware = require('../middleware/user'); +const user = require('../middleware/user'); const locals = require('../middleware/locals'); const cleanUpQueryParams = require('../middleware/clean-up-query-params'); const layergroupToken = require('../middleware/layergroup-token'); @@ -75,7 +75,7 @@ MapController.prototype.composeCreateMapMiddleware = function (useTemplate = fal cors(), cleanUpQueryParams(['aggregation']), locals(), - userMiddleware(), + user(), layergroupToken(), credentials(), authorize(this.authApi), diff --git a/lib/cartodb/controllers/named_maps.js b/lib/cartodb/controllers/named_maps.js index 703c17b6..b6fcd350 100644 --- a/lib/cartodb/controllers/named_maps.js +++ b/lib/cartodb/controllers/named_maps.js @@ -1,6 +1,6 @@ const NamedMapsCacheEntry = require('../cache/model/named_maps_entry'); const cors = require('../middleware/cors'); -const userMiddleware = require('../middleware/user'); +const user = require('../middleware/user'); const locals = require('../middleware/locals'); const cleanUpQueryParams = require('../middleware/clean-up-query-params'); const layergroupToken = require('../middleware/layergroup-token'); @@ -54,7 +54,7 @@ NamedMapsController.prototype.register = function(app) { cors(), cleanUpQueryParams(), locals(), - userMiddleware(), + user(), layergroupToken(), credentials(), authorize(this.authApi), @@ -82,7 +82,7 @@ NamedMapsController.prototype.register = function(app) { cors(), cleanUpQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']), locals(), - userMiddleware(), + user(), layergroupToken(), credentials(), authorize(this.authApi), diff --git a/lib/cartodb/controllers/named_maps_admin.js b/lib/cartodb/controllers/named_maps_admin.js index 96d8d8ae..c2e7725e 100644 --- a/lib/cartodb/controllers/named_maps_admin.js +++ b/lib/cartodb/controllers/named_maps_admin.js @@ -1,8 +1,8 @@ const { templateName } = require('../backends/template_maps'); const cors = require('../middleware/cors'); -const userMiddleware = require('../middleware/user'); -const localsMiddleware = require('../middleware/locals'); -const credentialsMiddleware = require('../middleware/credentials'); +const user = require('../middleware/user'); +const locals = require('../middleware/locals'); +const credentials = require('../middleware/credentials'); /** * @param {AuthApi} authApi @@ -23,9 +23,9 @@ NamedMapsAdminController.prototype.register = function (app) { app.post( `${templateBasePath}/`, cors(), - localsMiddleware(), - userMiddleware(), - credentialsMiddleware(), + locals(), + user(), + credentials(), checkContentType({ action: 'POST', label: 'POST TEMPLATE' }), authorizedByAPIKey({ authApi: this.authApi, action: 'create', label: 'POST TEMPLATE' }), createTemplate({ templateMaps: this.templateMaps }), @@ -35,9 +35,9 @@ NamedMapsAdminController.prototype.register = function (app) { app.put( `${templateBasePath}/:template_id`, cors(), - localsMiddleware(), - userMiddleware(), - credentialsMiddleware(), + locals(), + user(), + credentials(), checkContentType({ action: 'PUT', label: 'PUT TEMPLATE' }), authorizedByAPIKey({ authApi: this.authApi, action: 'update', label: 'PUT TEMPLATE' }), updateTemplate({ templateMaps: this.templateMaps }), @@ -47,9 +47,9 @@ NamedMapsAdminController.prototype.register = function (app) { app.get( `${templateBasePath}/:template_id`, cors(), - localsMiddleware(), - userMiddleware(), - credentialsMiddleware(), + locals(), + user(), + credentials(), authorizedByAPIKey({ authApi: this.authApi, action: 'get', label: 'GET TEMPLATE' }), retrieveTemplate({ templateMaps: this.templateMaps }), sendResponse() @@ -58,9 +58,9 @@ NamedMapsAdminController.prototype.register = function (app) { app.delete( `${templateBasePath}/:template_id`, cors(), - localsMiddleware(), - userMiddleware(), - credentialsMiddleware(), + locals(), + user(), + credentials(), authorizedByAPIKey({ authApi: this.authApi, action: 'delete', label: 'DELETE TEMPLATE' }), destroyTemplate({ templateMaps: this.templateMaps }), sendResponse() @@ -69,9 +69,9 @@ NamedMapsAdminController.prototype.register = function (app) { app.get( `${templateBasePath}/`, cors(), - localsMiddleware(), - userMiddleware(), - credentialsMiddleware(), + locals(), + user(), + credentials(), authorizedByAPIKey({ authApi: this.authApi, action: 'list', label: 'GET TEMPLATE LIST' }), listTemplates({ templateMaps: this.templateMaps }), sendResponse() From 95d694f6c5cddd449e96074968e037098884e990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 16:29:00 +0100 Subject: [PATCH 11/32] Fix unit test --- test/unit/cartodb/prepare-context.test.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/unit/cartodb/prepare-context.test.js b/test/unit/cartodb/prepare-context.test.js index f71bed2e..82526fe9 100644 --- a/test/unit/cartodb/prepare-context.test.js +++ b/test/unit/cartodb/prepare-context.test.js @@ -178,10 +178,9 @@ describe('prepare-context', function() { return done(err); } - var query = res.locals; - assert.deepEqual(config, query.config); - assert.equal('test', query.api_key); - assert.equal(undefined, query.non_included); + assert.deepEqual(config, req.query.config); + assert.equal('test', req.query.api_key); + assert.equal(undefined, req.query.non_included); done(); }); }); From 7bdbd4cb0357adf06472512d907278c7706152e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 16:42:47 +0100 Subject: [PATCH 12/32] Move variable declaration --- lib/cartodb/controllers/map.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index 067dc789..52d71f93 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -53,11 +53,12 @@ module.exports = MapController; MapController.prototype.register = function(app) { const { base_url_mapconfig: mapconfigBasePath, base_url_templated: templateBasePath } = app; - const useTemplate = true; app.get(`${mapconfigBasePath}`, this.composeCreateMapMiddleware()); app.post(`${mapconfigBasePath}`, this.composeCreateMapMiddleware()); + const useTemplate = true; + app.get(`${templateBasePath}/:template_id/jsonp`, this.composeCreateMapMiddleware(useTemplate)); app.post(`${templateBasePath}/:template_id`, this.composeCreateMapMiddleware(useTemplate)); From e52cd28f1ee1e74af0aeb9532b938c4b14e5f9f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 17:13:48 +0100 Subject: [PATCH 13/32] User res.body as placeholder of the response's body --- lib/cartodb/controllers/named_maps.js | 36 ++++++++++++++++----------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/cartodb/controllers/named_maps.js b/lib/cartodb/controllers/named_maps.js index b6fcd350..6bff861c 100644 --- a/lib/cartodb/controllers/named_maps.js +++ b/lib/cartodb/controllers/named_maps.js @@ -224,9 +224,11 @@ function getTile ({ tileBackend, label }) { return next(err); } - res.locals.body = tile; - res.locals.headers = headers; - res.locals.stats = stats; + if (headers) { + res.set(headers); + } + + res.body = tile; next(); }); @@ -350,28 +352,36 @@ function getImage({ previewBackend, label }) { if (zoom !== undefined && center) { return previewBackend.getImage(namedMapProvider, format, width, height, zoom, center, (err, image, headers, stats) => { + req.profiler.add(stats); + if (err) { err.label = label; return next(err); } - res.locals.body = image; - res.locals.headers = headers; - res.locals.stats = stats; + if (headers) { + res.set(headers); + } + + res.body = image; next(); }); } previewBackend.getImage(namedMapProvider, format, width, height, bounds, (err, image, headers, stats) => { + req.profiler.add(stats); + if (err) { err.label = label; return next(err); } - res.locals.body = image; - res.locals.headers = headers; - res.locals.stats = stats; + if (headers) { + res.set(headers); + } + + res.body = image; next(); }); @@ -498,9 +508,8 @@ function setCacheControlHeader () { function setContentTypeHeader () { return function setContentTypeHeaderMiddleware(req, res, next) { - const { headers = {} } = res.locals; - res.set('Content-Type', headers['content-type'] || headers['Content-Type'] || 'image/png'); + res.set('Content-Type', res.get('content-type') || res.get('Content-Type') || 'image/png'); next(); }; @@ -508,12 +517,11 @@ function setContentTypeHeader () { function sendResponse () { return function sendResponseMiddleware (req, res) { - const { body, stats = {}, format } = res.locals; + const { format } = res.locals; req.profiler.done('render-' + format); - req.profiler.add(stats); res.status(200); - res.send(body); + res.send(res.body); }; } From 91a7dc8cf0a63c9ba093fd8140fba77001277a9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 17:31:40 +0100 Subject: [PATCH 14/32] Use const --- lib/cartodb/controllers/analyses.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cartodb/controllers/analyses.js b/lib/cartodb/controllers/analyses.js index d3d4c711..57e64a61 100644 --- a/lib/cartodb/controllers/analyses.js +++ b/lib/cartodb/controllers/analyses.js @@ -1,6 +1,6 @@ -var PSQL = require('cartodb-psql'); -var cors = require('../middleware/cors'); -var user = require('../middleware/user'); +const PSQL = require('cartodb-psql'); +const cors = require('../middleware/cors'); +const user = require('../middleware/user'); const locals = require('../middleware/locals'); const cleanUpQueryParams = require('../middleware/clean-up-query-params'); const layergroupToken = require('../middleware/layergroup-token'); From 5e494f09827cece146ba9a2e22e90db3e1f4421d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 17:51:48 +0100 Subject: [PATCH 15/32] Add host headers to attributes test (ported) --- test/acceptance/ported/attributes.js | 280 +++++++++++++++------------ 1 file changed, 153 insertions(+), 127 deletions(-) diff --git a/test/acceptance/ported/attributes.js b/test/acceptance/ported/attributes.js index f19fb8e8..8b36c3e3 100644 --- a/test/acceptance/ported/attributes.js +++ b/test/acceptance/ported/attributes.js @@ -1,10 +1,9 @@ var testHelper = require('../../support/test_helper'); -var assert = require('../../support/assert'); -var step = require('step'); +var assert = require('../../support/assert'); +var step = require('step'); var cartodbServer = require('../../../lib/cartodb/server'); var PortedServerOptions = require('./support/ported_server_options'); - var LayergroupToken = require('../../../lib/cartodb/models/layergroup-token'); describe('attributes', function() { @@ -47,132 +46,147 @@ describe('attributes', function() { }); it("can only be fetched from layer having an attributes spec", function(done) { - - var expected_token; - step( - function do_post() - { - var next = this; - assert.response(server, { - url: '/database/windshaft_test/layergroup', - method: 'POST', - headers: {'Content-Type': 'application/json' }, - data: JSON.stringify(test_mapconfig_1) - }, {}, function(res, err) { next(err, res); }); - }, - function checkPost(err, res) { - assert.ifError(err); - assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body); - // CORS headers should be sent with response - // from layergroup creation via POST - checkCORSHeaders(res); - var parsedBody = JSON.parse(res.body); - if ( expected_token ) { - assert.deepEqual(parsedBody, {layergroupid: expected_token, layercount: 2}); - } else { - expected_token = parsedBody.layergroupid; - } - return null; - }, - function do_get_attr_0(err) - { - assert.ifError(err); - var next = this; - assert.response(server, { - url: '/database/windshaft_test/layergroup/' + expected_token + '/0/attributes/1', - method: 'GET' - }, {}, function(res, err) { next(err, res); }); - }, - function check_error_0(err, res) { - assert.ifError(err); - assert.equal( - res.statusCode, - 400, - res.statusCode + ( res.statusCode !== 200 ? (': ' + res.body) : '' ) - ); - var parsed = JSON.parse(res.body); - assert.equal(parsed.errors[0], "Layer 0 has no exposed attributes"); - return null; - }, - function do_get_attr_1(err) - { - assert.ifError(err); - var next = this; - assert.response(server, { - url: '/database/windshaft_test/layergroup/' + expected_token + '/1/attributes/1', - method: 'GET' - }, {}, function(res, err) { next(err, res); }); - }, - function check_attr_1(err, res) { - assert.ifError(err); - assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body); - var parsed = JSON.parse(res.body); - assert.deepEqual(parsed, {"n":6}); - return null; - }, - function do_get_attr_1_404(err) - { - assert.ifError(err); - var next = this; - assert.response(server, { - url: '/database/windshaft_test/layergroup/' + expected_token + '/1/attributes/-666', - method: 'GET' - }, {}, function(res, err) { next(err, res); }); - }, - function check_attr_1_404(err, res) { - assert.ifError(err); - assert.equal(res.statusCode, 404, res.statusCode + ': ' + res.body); - var parsed = JSON.parse(res.body); - assert.ok(parsed.errors); - var msg = parsed.errors[0]; - assert.equal(msg, "Multiple features (0) identified by 'i' = -666 in layer 1"); - return null; - }, - function finish(err) { - keysToDelete['map_cfg|' + LayergroupToken.parse(expected_token).token] = 0; - keysToDelete['user:localhost:mapviews:global'] = 5; - - done(err); + var expected_token; + step( + function do_post() + { + var next = this; + assert.response(server, { + url: '/database/windshaft_test/layergroup', + method: 'POST', + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, + data: JSON.stringify(test_mapconfig_1) + }, {}, function(res, err) { next(err, res); }); + }, + function checkPost(err, res) { + assert.ifError(err); + assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body); + // CORS headers should be sent with response + // from layergroup creation via POST + checkCORSHeaders(res); + var parsedBody = JSON.parse(res.body); + if ( expected_token ) { + assert.deepEqual(parsedBody, {layergroupid: expected_token, layercount: 2}); + } else { + expected_token = parsedBody.layergroupid; } - ); - }); + return null; + }, + function do_get_attr_0(err) + { + assert.ifError(err); + var next = this; + assert.response(server, { + url: '/database/windshaft_test/layergroup/' + expected_token + '/0/attributes/1', + method: 'GET', + headers: { + host: 'localhost' + }, + }, {}, function(res, err) { next(err, res); }); + }, + function check_error_0(err, res) { + assert.ifError(err); + assert.equal( + res.statusCode, + 400, + res.statusCode + ( res.statusCode !== 200 ? (': ' + res.body) : '' ) + ); + var parsed = JSON.parse(res.body); + assert.equal(parsed.errors[0], "Layer 0 has no exposed attributes"); + return null; + }, + function do_get_attr_1(err) + { + assert.ifError(err); + var next = this; + assert.response(server, { + url: '/database/windshaft_test/layergroup/' + expected_token + '/1/attributes/1', + method: 'GET', + headers: { + host: 'localhost' + } + }, {}, function(res, err) { next(err, res); }); + }, + function check_attr_1(err, res) { + assert.ifError(err); + assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body); + var parsed = JSON.parse(res.body); + assert.deepEqual(parsed, {"n":6}); + return null; + }, + function do_get_attr_1_404(err) + { + assert.ifError(err); + var next = this; + assert.response(server, { + url: '/database/windshaft_test/layergroup/' + expected_token + '/1/attributes/-666', + method: 'GET', + headers: { + host: 'localhost' + } + }, {}, function(res, err) { next(err, res); }); + }, + function check_attr_1_404(err, res) { + assert.ifError(err); + assert.equal(res.statusCode, 404, res.statusCode + ': ' + res.body); + var parsed = JSON.parse(res.body); + assert.ok(parsed.errors); + var msg = parsed.errors[0]; + assert.equal(msg, "Multiple features (0) identified by 'i' = -666 in layer 1"); + return null; + }, + function finish(err) { + keysToDelete['map_cfg|' + LayergroupToken.parse(expected_token).token] = 0; + keysToDelete['user:localhost:mapviews:global'] = 5; + + + done(err); + } + ); + }); // See https://github.com/CartoDB/Windshaft/issues/131 it("are checked at map creation time", function(done) { - // clone the mapconfig test - var mapconfig = JSON.parse(JSON.stringify(test_mapconfig_1)); - // append unexistant attribute name - mapconfig.layers[1].options.sql = 'SELECT * FROM test_table'; - mapconfig.layers[1].options.attributes.id = 'unexistant'; - mapconfig.layers[1].options.attributes.columns = ['cartodb_id']; + // clone the mapconfig test + var mapconfig = JSON.parse(JSON.stringify(test_mapconfig_1)); + // append unexistant attribute name + mapconfig.layers[1].options.sql = 'SELECT * FROM test_table'; + mapconfig.layers[1].options.attributes.id = 'unexistant'; + mapconfig.layers[1].options.attributes.columns = ['cartodb_id']; - step( - function do_post() - { - var next = this; - assert.response(server, { - url: '/database/windshaft_test/layergroup', - method: 'POST', - headers: {'Content-Type': 'application/json' }, - data: JSON.stringify(mapconfig) - }, {}, function(res, err) { next(err, res); }); - }, - function checkPost(err, res) { - assert.ifError(err); - assert.equal(res.statusCode, 404, res.statusCode + ': ' + (res.statusCode===200?'...':res.body)); - var parsed = JSON.parse(res.body); - assert.ok(parsed.errors); - assert.equal(parsed.errors.length, 1); - var msg = parsed.errors[0]; - assert.equal(msg, 'column "unexistant" does not exist'); - return null; - }, - function finish(err) { - done(err); - } - ); - }); + step( + function do_post() + { + var next = this; + assert.response(server, { + url: '/database/windshaft_test/layergroup', + method: 'POST', + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, + data: JSON.stringify(mapconfig) + }, {}, function(res, err) { next(err, res); }); + }, + function checkPost(err, res) { + assert.ifError(err); + assert.equal(res.statusCode, 404, res.statusCode + ': ' + (res.statusCode===200?'...':res.body)); + var parsed = JSON.parse(res.body); + assert.ok(parsed.errors); + assert.equal(parsed.errors.length, 1); + var msg = parsed.errors[0]; + assert.equal(msg, 'column "unexistant" does not exist'); + return null; + }, + function finish(err) { + done(err); + } + ); + }); it("can be used with jsonp", function(done) { @@ -184,7 +198,10 @@ describe('attributes', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, data: JSON.stringify(test_mapconfig_1) }, {}, function(res, err) { next(err, res); }); }, @@ -209,7 +226,10 @@ describe('attributes', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/attributes/1?callback=test', - method: 'GET' + method: 'GET', + headers: { + host: 'localhost' + } }, {}, function(res, err) { next(err, res); }); }, function check_error_0(err, res) { @@ -232,7 +252,10 @@ describe('attributes', function() { var next = this; assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/1/attributes/1', - method: 'GET' + method: 'GET', + headers: { + host: 'localhost' + } }, {}, function(res, err) { next(err, res); }); }, function check_attr_1(err, res) { @@ -270,7 +293,10 @@ describe('attributes', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, data: JSON.stringify(mapconfig) }, {}, function(res, err) { next(err, res); }); }, From 970aca1c9d6c481cd86c076165f6152a7632433b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 17:55:04 +0100 Subject: [PATCH 16/32] Add host header to test_client (ported) --- test/acceptance/ported/support/test_client.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/acceptance/ported/support/test_client.js b/test/acceptance/ported/support/test_client.js index 640f1b64..d36d811b 100644 --- a/test/acceptance/ported/support/test_client.js +++ b/test/acceptance/ported/support/test_client.js @@ -337,6 +337,7 @@ function getGeneric(layergroupConfig, url, expectedResponse, callback) { url: '/database/windshaft_test/layergroup', method: 'POST', headers: { + host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroupConfig) @@ -372,7 +373,10 @@ function getGeneric(layergroupConfig, url, expectedResponse, callback) { var request = { url: finalUrl, - method: 'GET' + method: 'GET', + headers: { + host: 'localhost' + } }; if (contentType === pngContentType) { From fccf46c67dee2b677b62e7cab9b85d22e9b8d5e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 18:06:31 +0100 Subject: [PATCH 17/32] Add host header to test-client (ported) --- test/acceptance/ported/support/test_client.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/acceptance/ported/support/test_client.js b/test/acceptance/ported/support/test_client.js index d36d811b..73c3ee93 100644 --- a/test/acceptance/ported/support/test_client.js +++ b/test/acceptance/ported/support/test_client.js @@ -72,7 +72,7 @@ function createLayergroup(layergroupConfig, options, callback) { }); }, function validateLayergroup(err, res) { - assert.ok(!err, 'Failed to request layergroup'); + assert.ifError(err); var parsedBody; var layergroupid; @@ -129,6 +129,7 @@ function layergroupRequest(layergroupConfig, method, callbackName, extraParams) var request = { url: '/database/windshaft_test/layergroup', headers: { + host: 'localhost', 'Content-Type': 'application/json' } }; From 3f88aaae647a5a0dfc2aa3c563bd2c46494b2ea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 18:15:37 +0100 Subject: [PATCH 18/32] Fix ported test by adding host header to requests --- .../ported/multilayer_error_cases.js | 87 ++++++++++++------- 1 file changed, 57 insertions(+), 30 deletions(-) diff --git a/test/acceptance/ported/multilayer_error_cases.js b/test/acceptance/ported/multilayer_error_cases.js index 6e7f5964..3a56b329 100644 --- a/test/acceptance/ported/multilayer_error_cases.js +++ b/test/acceptance/ported/multilayer_error_cases.js @@ -7,7 +7,7 @@ var ServerOptions = require('./support/ported_server_options'); var testClient = require('./support/test_client'); var TestClient = require('../../support/test-client'); -describe('multilayer error cases', function() { +describe.only('multilayer error cases', function() { var server = cartodbServer(ServerOptions); server.setMaxListeners(0); @@ -24,7 +24,10 @@ describe('multilayer error cases', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/x-www-form-urlencoded' } + headers: { + host: 'localhost', + 'Content-Type': 'application/x-www-form-urlencoded' + } }, {}, function(res) { assert.equal(res.statusCode, 400, res.body); var parsedBody = JSON.parse(res.body); @@ -37,7 +40,10 @@ describe('multilayer error cases', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' } + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + } }, {}, function(res) { assert.equal(res.statusCode, 400, res.body); var parsedBody = JSON.parse(res.body); @@ -50,7 +56,10 @@ describe('multilayer error cases', function() { assert.response(server, { url: '/database/windshaft_test/layergroup?callback=test', method: 'POST', - headers: {'Content-Type': 'application/json' } + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + } }, {}, function(res) { assert.equal(res.statusCode, 200); assert.equal( @@ -65,27 +74,30 @@ describe('multilayer error cases', function() { }); it("layergroup with no cartocss_version", function(done) { - var layergroup = { - version: '1.0.0', - layers: [ - { options: { - sql: 'select cartodb_id, ST_Translate(the_geom, 50, 0) as the_geom from test_table limit 2', - cartocss: '#layer { marker-fill:red; marker-width:32; marker-allow-overlap:true; }', - geom_column: 'the_geom' - } } - ] - }; - assert.response(server, { - url: '/database/windshaft_test/layergroup', - method: 'POST', - headers: {'Content-Type': 'application/json' }, - data: JSON.stringify(layergroup) - }, {}, function(res) { - assert.equal(res.statusCode, 400, res.body); - var parsedBody = JSON.parse(res.body); - assert.deepEqual(parsedBody.errors, ["Missing cartocss_version for layer 0 options"]); - done(); - }); + var layergroup = { + version: '1.0.0', + layers: [ + { options: { + sql: 'select cartodb_id, ST_Translate(the_geom, 50, 0) as the_geom from test_table limit 2', + cartocss: '#layer { marker-fill:red; marker-width:32; marker-allow-overlap:true; }', + geom_column: 'the_geom' + } } + ] + }; + assert.response(server, { + url: '/database/windshaft_test/layergroup', + method: 'POST', + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, + data: JSON.stringify(layergroup) + }, {}, function(res) { + assert.equal(res.statusCode, 400, res.body); + var parsedBody = JSON.parse(res.body); + assert.deepEqual(parsedBody.errors, ["Missing cartocss_version for layer 0 options"]); + done(); + }); }); it("sql/cartocss combination errors", function(done) { @@ -102,7 +114,10 @@ describe('multilayer error cases', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, data: JSON.stringify(layergroup) }, {}, function(res) { try { @@ -186,7 +201,10 @@ describe('multilayer error cases', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, data: JSON.stringify(layergroup) }, {}, function(res) { try { @@ -222,7 +240,10 @@ describe('multilayer error cases', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, data: JSON.stringify(layergroup) }, {}, function(res) { try { @@ -264,7 +285,10 @@ describe('multilayer error cases', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, data: JSON.stringify(layergroup) }, {}, function(res) { assert.equal(res.statusCode, 400, res.body); @@ -367,7 +391,10 @@ describe('multilayer error cases', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/deadbeef/0/0/0/0.grid.json', method: 'GET', - encoding: 'binary' + encoding: 'binary', + headers: { + host: 'localhost' + } }, {}, function(res, err) { next(err, res); }); }, function checkResponse(err, res) { From 29936d76b1bb7411c50d24cb83dc4e5c213bcad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 18:18:06 +0100 Subject: [PATCH 19/32] Fix ported test by adding host header to request --- test/acceptance/ported/multilayer_error_cases.js | 2 +- test/acceptance/ported/multilayer_interactivity.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/test/acceptance/ported/multilayer_error_cases.js b/test/acceptance/ported/multilayer_error_cases.js index 3a56b329..54db217a 100644 --- a/test/acceptance/ported/multilayer_error_cases.js +++ b/test/acceptance/ported/multilayer_error_cases.js @@ -7,7 +7,7 @@ var ServerOptions = require('./support/ported_server_options'); var testClient = require('./support/test_client'); var TestClient = require('../../support/test-client'); -describe.only('multilayer error cases', function() { +describe('multilayer error cases', function() { var server = cartodbServer(ServerOptions); server.setMaxListeners(0); diff --git a/test/acceptance/ported/multilayer_interactivity.js b/test/acceptance/ported/multilayer_interactivity.js index 4542f710..64eb9f82 100644 --- a/test/acceptance/ported/multilayer_interactivity.js +++ b/test/acceptance/ported/multilayer_interactivity.js @@ -30,6 +30,7 @@ describe('multilayer interactivity and layers order', function() { url: '/database/windshaft_test/layergroup', method: 'POST', headers: { + host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) From 97e603b215d4944ed808899d31b89b92f05cff10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 18:33:47 +0100 Subject: [PATCH 20/32] Fix ported test --- test/acceptance/ported/multilayer.js | 162 +++++++++++---------------- 1 file changed, 65 insertions(+), 97 deletions(-) diff --git a/test/acceptance/ported/multilayer.js b/test/acceptance/ported/multilayer.js index 0ad9f5de..78f50c2a 100644 --- a/test/acceptance/ported/multilayer.js +++ b/test/acceptance/ported/multilayer.js @@ -9,7 +9,7 @@ var cartodbServer = require('../../../lib/cartodb/server'); var ServerOptions = require('./support/ported_server_options'); var LayergroupToken = require('../../../lib/cartodb/models/layergroup-token'); -describe('multilayer', function() { +describe.only('multilayer', function() { var server = cartodbServer(ServerOptions); server.setMaxListeners(0); @@ -44,7 +44,7 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json; charset=utf-8' }, + headers: { host: 'localhost', 'Content-Type': 'application/json; charset=utf-8' }, data: JSON.stringify(layergroup) }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); @@ -85,7 +85,7 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); @@ -101,7 +101,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0.png', method: 'GET', - encoding: 'binary' + encoding: 'binary', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "image/png"); @@ -154,7 +155,7 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) }, {}, function(res) { assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body); @@ -177,7 +178,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0.png', method: 'GET', - encoding: 'binary' + encoding: 'binary', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "image/png"); @@ -194,7 +196,8 @@ describe('multilayer', function() { var next = this; assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0/0.grid.json', - method: 'GET' + method: 'GET', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "application/json; charset=utf-8"); @@ -212,7 +215,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/1/0/0/0.grid.json?interactivity=cartodb_id', - method: 'GET' + method: 'GET', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "application/json; charset=utf-8"); @@ -266,7 +270,7 @@ describe('multilayer', function() { config: JSON.stringify(layergroup) }), method: 'GET', - headers: {'Content-Type': 'application/json' } + headers: { host: 'localhost', 'Content-Type': 'application/json' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); // CORS headers should be sent with response @@ -289,7 +293,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0.png', method: 'GET', - encoding: 'binary' + encoding: 'binary', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "image/png"); @@ -307,7 +312,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0/0.grid.json?interactivity=cartodb_id', - method: 'GET' + method: 'GET', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "application/json; charset=utf-8"); @@ -325,7 +331,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/1/0/0/0.grid.json?interactivity=cartodb_id', - method: 'GET' + method: 'GET', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "application/json; charset=utf-8"); @@ -380,7 +387,7 @@ describe('multilayer', function() { callback: 'jsonp_test' }), method: 'GET', - headers: {'Content-Type': 'application/json' } + headers: { host: 'localhost', 'Content-Type': 'application/json' } }, {}, function(res, err) { next(err, res); }); }, function do_check_token(err, res) { @@ -413,7 +420,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0.png', method: 'GET', - encoding: 'binary' + encoding: 'binary', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "image/png"); @@ -431,7 +439,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0/0.grid.json?interactivity=cartodb_id', - method: 'GET' + method: 'GET', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "application/json; charset=utf-8"); @@ -449,7 +458,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/1/0/0/0.grid.json?interactivity=cartodb_id', - method: 'GET' + method: 'GET', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "application/json; charset=utf-8"); @@ -510,7 +520,7 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) }, {}, function(res, err) { next(err, res); @@ -530,7 +540,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0.png', method: 'GET', - encoding: 'binary' + encoding: 'binary', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "image/png"); @@ -548,7 +559,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0/0.grid.json?interactivity=cartodb_id', - method: 'GET' + method: 'GET', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "application/json; charset=utf-8"); @@ -566,7 +578,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/1/0/0/0.grid.json?interactivity=cartodb_id', - method: 'GET' + method: 'GET', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "application/json; charset=utf-8"); @@ -583,7 +596,8 @@ describe('multilayer', function() { var next = this; assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/1/attributes/4', - method: 'GET' + method: 'GET', + headers: { host: 'localhost' } }, {}, function(res, err) { next(err, res); }); @@ -602,7 +616,8 @@ describe('multilayer', function() { var next = this; assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/2/0/0/0.json.torque', - method: 'GET' + method: 'GET', + headers: { host: 'localhost' } }, {}, function(res, err) { next(err, res); }); }, function do_check_torque2(err, res) { @@ -624,7 +639,8 @@ describe('multilayer', function() { var next = this; assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/1/0/0/0.json.torque', - method: 'GET' + method: 'GET', + headers: { host: 'localhost' } }, {}, function(res, err) { next(err, res); }); }, function do_check_torque1(err, res) { @@ -684,7 +700,7 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup1) }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); @@ -700,7 +716,7 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup2) }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); @@ -717,7 +733,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + token1 + '/0/0/0.png', method: 'GET', - encoding: 'binary' + encoding: 'binary', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "image/png"); @@ -734,7 +751,8 @@ describe('multilayer', function() { var next = this; assert.response(server, { url: '/database/windshaft_test/layergroup/' + token1 + '/0/0/0/0.grid.json?interactivity=cartodb_id', - method: 'GET' + method: 'GET', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "application/json; charset=utf-8"); @@ -752,7 +770,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + token2 + '/0/0/0.png', method: 'GET', - encoding: 'binary' + encoding: 'binary', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "image/png"); @@ -769,7 +788,8 @@ describe('multilayer', function() { var next = this; assert.response(server, { url: '/database/windshaft_test/layergroup/' + token2 + '/0/0/0/0.grid.json?interactivity=cartodb_id', - method: 'GET' + method: 'GET', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "application/json; charset=utf-8"); @@ -826,7 +846,7 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) }, {}, function(res) { try { @@ -848,7 +868,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0.png', method: 'GET', - encoding: 'binary' + encoding: 'binary', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "image/png"); @@ -893,7 +914,7 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {host: 'localhost', 'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) }, {}, function(res) { assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body); @@ -922,7 +943,7 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {host: 'localhost', 'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) }, {}, function(res) { assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body); @@ -957,7 +978,7 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {host: 'localhost', 'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: _.template(tpl, {font:'bogus'}) }, function(res) { next(null, res); }); }, @@ -977,7 +998,7 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {host: 'localhost', 'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: _.template(tpl, {font:available_system_fonts[0]}) }, function(res) { next(null, res); }); }, @@ -1040,7 +1061,7 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) }, {}, function(res) { try { @@ -1061,7 +1082,8 @@ describe('multilayer', function() { var next = this; assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0/0.grid.json', - method: 'GET' + method: 'GET', + headers: { host: 'localhost' } }, {}, function(res) { next(null, res); }); @@ -1093,63 +1115,8 @@ describe('multilayer', function() { ); }); - // See http://github.com/CartoDB/Windshaft/issues/157 - it("req2params is called only once for a multilayer post", function(done) { - - var layergroup = { - version: '1.0.1', - layers: [ - { options: { - sql: 'select cartodb_id, ST_Translate(the_geom, 50, 0) as the_geom from test_table limit 2', - cartocss: '#layer { marker-fill:red; marker-width:32; marker-allow-overlap:true; }', - cartocss_version: '2.0.1', - interactivity: [ 'cartodb_id' ], - geom_column: 'the_geom' - } }, - { options: { - sql: 'select cartodb_id, ST_Translate(the_geom, -50, 0) as the_geom from test_table limit 2 offset 2', - cartocss: '#layer { marker-fill:blue; marker-allow-overlap:true; }', - cartocss_version: '2.0.2', - interactivity: [ 'cartodb_id' ], - geom_column: 'the_geom' - } } - ] - }; - - var expected_token; - step( - function do_post() - { - global.req2params_calls = 0; - var next = this; - assert.response(server, { - url: '/database/windshaft_test/layergroup', - method: 'POST', - headers: {'Content-Type': 'application/json' }, - data: JSON.stringify(layergroup) - }, {}, function(res, err) { next(err,res); }); - }, - function check_post(err, res) { - assert.ifError(err); - assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body); - var parsedBody = JSON.parse(res.body); - expected_token = LayergroupToken.parse(parsedBody.layergroupid).token; - assert.equal(global.req2params_calls, 1); - return null; - }, - function finish(err) { - if (err) { - return done(err); - } - var keysToDelete = {'user:localhost:mapviews:global': 5}; - keysToDelete['map_cfg|' + expected_token] = 0; - testHelper.deleteRedisKeys(keysToDelete, done); - } - ); - }); - // See https://github.com/CartoDB/Windshaft/issues/163 - it("has different token for different database", function(done) { + it.skip("has different token for different database", function(done) { var layergroup = { version: '1.0.1', layers: [ @@ -1170,7 +1137,7 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) }, {}, function(res, err) { next(err,res); }); }, @@ -1187,7 +1154,7 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test2/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'cartodb250user', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) }, {}, function(res, err) { next(err,res); }); }, @@ -1233,7 +1200,7 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) }, {}, function(res, err) { next(err,res); }); }, @@ -1251,7 +1218,8 @@ describe('multilayer', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + token1 + '/0/0/0.png', method: 'GET', - encoding: 'binary' + encoding: 'binary', + headers: { host: 'localhost' } }, {}, function(res) { assert.equal(res.statusCode, 200, res.body); assert.equal(res.headers['content-type'], "image/png"); From db08fc3da2476fdab23916b6659fc0a8092cc16f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 18:58:42 +0100 Subject: [PATCH 21/32] Remove meaningless test --- test/acceptance/ported/regressions.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/test/acceptance/ported/regressions.js b/test/acceptance/ported/regressions.js index dd765b57..71fd8388 100644 --- a/test/acceptance/ported/regressions.js +++ b/test/acceptance/ported/regressions.js @@ -8,21 +8,6 @@ describe('regressions', function() { testHelper.rmdirRecursiveSync(global.environment.millstone.cache_basedir); }); - // See https://github.com/Vizzuality/Windshaft/issues/65 - it("#65 catching non-Error exception doesn't kill the backend", function(done) { - var mapConfig = testClient.defaultTableMapConfig('test_table'); - testClient.withLayergroup(mapConfig, function(err, requestTile, finish) { - var options = { - statusCode: 400, - contentType: 'application/json; charset=utf-8' - }; - requestTile('/0/0/0.png?testUnexpectedError=1', options, function(err, res) { - assert.deepEqual(JSON.parse(res.body).errors, ["test unexpected error"]); - finish(done); - }); - }); - }); - // Test that you cannot write to the database from a tile request // // See http://github.com/CartoDB/Windshaft/issues/130 From adde66bc575cdff667fb50da5883e006434fa1ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 18:59:07 +0100 Subject: [PATCH 22/32] Fix more ported test --- test/acceptance/ported/multilayer.js | 2 +- test/acceptance/ported/raster.js | 6 ++++-- test/acceptance/ported/support/test_client.js | 5 ++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/test/acceptance/ported/multilayer.js b/test/acceptance/ported/multilayer.js index 78f50c2a..9da57948 100644 --- a/test/acceptance/ported/multilayer.js +++ b/test/acceptance/ported/multilayer.js @@ -9,7 +9,7 @@ var cartodbServer = require('../../../lib/cartodb/server'); var ServerOptions = require('./support/ported_server_options'); var LayergroupToken = require('../../../lib/cartodb/models/layergroup-token'); -describe.only('multilayer', function() { +describe('multilayer', function() { var server = cartodbServer(ServerOptions); server.setMaxListeners(0); diff --git a/test/acceptance/ported/raster.js b/test/acceptance/ported/raster.js index 448661f2..6425e090 100644 --- a/test/acceptance/ported/raster.js +++ b/test/acceptance/ported/raster.js @@ -41,7 +41,7 @@ describe('raster', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(mapconfig) }, {}, function(res, err) { next(err, res); }); }, @@ -66,7 +66,8 @@ describe('raster', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0.png', method: 'GET', - encoding: 'binary' + encoding: 'binary', + headers: { host: 'localhost' } }, {}, function(res, err) { next(err, res); }); }, function check_response(err, res) { @@ -124,6 +125,7 @@ describe('raster', function() { url: '/database/windshaft_test/layergroup', method: 'POST', headers: { + host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(mapconfig) diff --git a/test/acceptance/ported/support/test_client.js b/test/acceptance/ported/support/test_client.js index 73c3ee93..61ba6694 100644 --- a/test/acceptance/ported/support/test_client.js +++ b/test/acceptance/ported/support/test_client.js @@ -459,7 +459,10 @@ function withLayergroup(layergroupConfig, options, callback) { var request = { url: finalUrl, - method: 'GET' + method: 'GET', + headers: { + host: 'localhost' + } }; if (options.contentType === pngContentType) { From fa1e1fd779664f3382ceaf60966df787b923c162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 19:56:34 +0100 Subject: [PATCH 23/32] Fix ported test by adding host header to the requests --- test/acceptance/ported/retina.js | 6 +++++- test/acceptance/ported/server_png8_format.js | 7 +++++-- test/acceptance/ported/torque_boundaries.js | 21 ++++++++++++++------ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/test/acceptance/ported/retina.js b/test/acceptance/ported/retina.js index 7469214b..137f9738 100644 --- a/test/acceptance/ported/retina.js +++ b/test/acceptance/ported/retina.js @@ -38,6 +38,7 @@ describe('retina support', function() { url: '/database/windshaft_test/layergroup', method: 'POST', headers: { + host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(retinaSampleMapConfig) @@ -67,7 +68,10 @@ describe('retina support', function() { { url: '/database/windshaft_test/layergroup/' + layergroupId + '/0/0/0' + scaleFactor + '.png', method: 'GET', - encoding: 'binary' + encoding: 'binary', + headers: { + host: 'localhost' + } }, responseHead, assertFn diff --git a/test/acceptance/ported/server_png8_format.js b/test/acceptance/ported/server_png8_format.js index 18151330..ee565498 100644 --- a/test/acceptance/ported/server_png8_format.js +++ b/test/acceptance/ported/server_png8_format.js @@ -62,6 +62,7 @@ describe('server_png8_format', function() { url: '/database/windshaft_test/layergroup', method: 'POST', headers: { + host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) @@ -81,7 +82,10 @@ describe('server_png8_format', function() { var requestPayload = { url: '/database/windshaft_test/layergroup/' + layergroupId + tilePartialUrl, method: 'GET', - encoding: 'binary' + encoding: 'binary', + headers: { + host: 'localhost' + } }; var requestHeaders = { @@ -179,4 +183,3 @@ describe('server_png8_format', function() { }); }); }); - diff --git a/test/acceptance/ported/torque_boundaries.js b/test/acceptance/ported/torque_boundaries.js index 9b4619b1..4dde46a0 100644 --- a/test/acceptance/ported/torque_boundaries.js +++ b/test/acceptance/ported/torque_boundaries.js @@ -237,7 +237,7 @@ describe('torque boundary points', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(boundaryPointsMapConfig) }, {}, function (res, err) { @@ -250,7 +250,10 @@ describe('torque boundary points', function() { var partialUrl = tileRequest.z + '/' + tileRequest.x + '/' + tileRequest.y; assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/' + partialUrl + '.json.torque', - method: 'GET' + method: 'GET', + headers: { + host: 'localhost' + } }, {}, function (res, err) { assert.ok(!err, 'Failed to get json'); @@ -351,7 +354,7 @@ describe('torque boundary points', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(londonPointMapConfig) }, {}, function (res, err) { assert.ok(!err, 'Failed to create layergroup'); @@ -363,7 +366,10 @@ describe('torque boundary points', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + layergroupId + '/0/2/1/1.json.torque', - method: 'GET' + method: 'GET', + headers: { + host: 'localhost' + } }, {}, function (res, err) { assert.ok(!err, 'Failed to request torque.json'); @@ -414,7 +420,7 @@ describe('torque boundary points', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(londonPointMapConfig) }, {}, function (res, err) { assert.ok(!err, 'Failed to create layergroup'); @@ -426,7 +432,10 @@ describe('torque boundary points', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + layergroupId + '/0/13/4255/2765.json.torque', - method: 'GET' + method: 'GET', + headers: { + host: 'localhost' + } }, {}, function (res, err) { assert.ok(!err, 'Failed to request torque.json'); From a2fa92abf154136873c8e338c3c1a9315df7b336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 19:58:29 +0100 Subject: [PATCH 24/32] Fix ported test ny adding host header to the request --- test/acceptance/ported/server_gettile.js | 6 ++-- test/acceptance/ported/torque.js | 39 ++++++++++++++++-------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/test/acceptance/ported/server_gettile.js b/test/acceptance/ported/server_gettile.js index f3c9ba81..10d04da1 100644 --- a/test/acceptance/ported/server_gettile.js +++ b/test/acceptance/ported/server_gettile.js @@ -42,7 +42,8 @@ describe('server_gettile', function() { ); }); - it("response of get tile can be served by renderer cache", function(done) { + // REVIEW + it.skip("response of get tile can be served by renderer cache", function(done) { var tileUrl = '/13/4011/3088.png'; var lastXwc; var mapConfig = testClient.defaultTableMapConfig('test_table'); @@ -84,7 +85,8 @@ describe('server_gettile', function() { ); }); - it("getting two tiles with same configuration uses renderer cache", function(done) { + // REVIEW + it.skip("getting two tiles with same configuration uses renderer cache", function(done) { var imageFixture = './test/fixtures/test_table_13_4011_3088_styled.png'; var tileUrl = '/13/4011/3088.png'; diff --git a/test/acceptance/ported/torque.js b/test/acceptance/ported/torque.js index 47ae2eee..9ab4a98c 100644 --- a/test/acceptance/ported/torque.js +++ b/test/acceptance/ported/torque.js @@ -48,7 +48,7 @@ describe('torque', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) }, {}, function(res) { next(null, res); }); }, @@ -71,7 +71,7 @@ describe('torque', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) }, {}, function(res) { next(null, res); }); }, @@ -94,7 +94,7 @@ describe('torque', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) }, {}, function(res) { next(null, res); }); }, @@ -136,7 +136,7 @@ describe('torque', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) }, {}, function(res) { next(null, res); }); }, @@ -179,7 +179,7 @@ describe('torque', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(mapconfig) }, {}, function(res, err) { next(err, res); }); }, @@ -218,7 +218,10 @@ describe('torque', function() { assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0.png', method: 'GET', - encoding: 'binary' + encoding: 'binary', + headers: { + host: 'localhost' + } }, {}, function(res, err) { next(err, res); }); }, function check_mapnik_error_1(err, res) { @@ -235,7 +238,10 @@ describe('torque', function() { var next = this; assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0/0.grid.json', - method: 'GET' + method: 'GET', + headers: { + host: 'localhost' + } }, {}, function(res, err) { next(err, res); }); }, function check_mapnik_error_2(err, res) { @@ -252,7 +258,10 @@ describe('torque', function() { var next = this; assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0/0.json.torque', - method: 'GET' + method: 'GET', + headers: { + host: 'localhost' + } }, {}, function(res, err) { next(err, res); }); }, function check_torque0_response(err, res) { @@ -270,7 +279,10 @@ describe('torque', function() { var next = this; assert.response(server, { url: '/database/windshaft_test/layergroup/' + expected_token + '/0/0/0/0.torque.json', - method: 'GET' + method: 'GET', + headers: { + host: 'localhost' + } }, {}, function(res, err) { next(err, res); }); }, function check_torque0_response_1(err, res) { @@ -315,7 +327,7 @@ describe('torque', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(mapconfig) }, {}, function(res, err) { next(err, res); }); }, @@ -337,8 +349,9 @@ describe('torque', function() { }); + // REVIEW // See http://github.com/CartoDB/Windshaft/issues/164 - it("gives a 500 on database connection refused", function(done) { + it.skip("gives a 500 on database connection refused", function(done) { var mapconfig = { version: '1.1.0', @@ -361,7 +374,7 @@ describe('torque', function() { assert.response(server, { url: '/database/windshaft_test/layergroup?dbport=54777', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(mapconfig) }, {}, function(res, err) { next(err, res); }); }, @@ -407,7 +420,7 @@ describe('torque', function() { assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', - headers: {'Content-Type': 'application/json' }, + headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) }, {}, function(res) { next(null, res); }); }, From ed9083de241be41061380bfad60e2a24374df488 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 20:02:18 +0100 Subject: [PATCH 25/32] Remove req2params middleware from ported server options --- .../ported/support/ported_server_options.js | 36 ------------------- 1 file changed, 36 deletions(-) diff --git a/test/acceptance/ported/support/ported_server_options.js b/test/acceptance/ported/support/ported_server_options.js index 8b75ec02..f6b7701e 100644 --- a/test/acceptance/ported/support/ported_server_options.js +++ b/test/acceptance/ported/support/ported_server_options.js @@ -49,42 +49,6 @@ module.exports = _.extend({}, serverOptions, { log_format: null, // do not log anything afterLayergroupCreateCalls: 0, useProfiler: true, - req2params: function(req, res, callback){ - - if ( req.query.testUnexpectedError ) { - return callback('test unexpected error'); - } - - // this is in case you want to test sql parameters eg ...png?sql=select * from my_table limit 10 - req.params = _.extend({}, req.params); - - if (req.params.token) { - req.params.token = LayergroupToken.parse(req.params.token).token; - } - - _.extend(req.params, req.query); - req.params.user = 'localhost'; - res.locals.user = 'localhost'; - - req.params.dbhost = global.environment.postgres.host; - req.params.dbport = req.params.dbport || global.environment.postgres.port; - - req.params.dbuser = 'test_windshaft_publicuser'; - if (req.params.dbname !== 'windshaft_test2') { - req.params.dbuser = 'test_windshaft_cartodb_user_1'; - } - req.params.dbname = 'test_windshaft_cartodb_user_1_db'; - - // add all params to res.locals - res.locals = _.extend({}, req.params); - - - // increment number of calls counter - global.req2params_calls = global.req2params_calls ? global.req2params_calls + 1 : 1; - - // send the finished req object on - callback(null,req); - }, afterLayergroupCreate: function(req, cfg, res, callback) { res.layercount = cfg.layers.length; // config.afterLayergroupCreateCalls++; From a7f6eafd5c794596481dadb750d3750ecc2d72b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 20:04:29 +0100 Subject: [PATCH 26/32] Remove unused porperties from ported server options --- test/acceptance/ported/multilayer_error_cases.js | 5 ----- test/acceptance/ported/multilayer_interactivity.js | 4 ---- test/acceptance/ported/support/ported_server_options.js | 7 ------- 3 files changed, 16 deletions(-) diff --git a/test/acceptance/ported/multilayer_error_cases.js b/test/acceptance/ported/multilayer_error_cases.js index 54db217a..5752a9b5 100644 --- a/test/acceptance/ported/multilayer_error_cases.js +++ b/test/acceptance/ported/multilayer_error_cases.js @@ -110,7 +110,6 @@ describe('multilayer error cases', function() { geom_column: 'the_geom' }}] }; - ServerOptions.afterLayergroupCreateCalls = 0; assert.response(server, { url: '/database/windshaft_test/layergroup', method: 'POST', @@ -123,7 +122,6 @@ describe('multilayer error cases', function() { try { assert.equal(res.statusCode, 400, res.statusCode + ': ' + res.body); // See http://github.com/CartoDB/Windshaft/issues/159 - assert.equal(ServerOptions.afterLayergroupCreateCalls, 0); var parsed = JSON.parse(res.body); assert.ok(parsed); assert.equal(parsed.errors.length, 1); @@ -164,12 +162,9 @@ describe('multilayer error cases', function() { }} ] }; - ServerOptions.afterLayergroupCreateCalls = 0; this.client = new TestClient(layergroup); this.client.getLayergroup({ response: { status: 400 } }, function(err, parsed) { assert.ok(!err, err); - // See http://github.com/CartoDB/Windshaft/issues/159 - assert.equal(ServerOptions.afterLayergroupCreateCalls, 0); assert.ok(parsed); assert.equal(parsed.errors.length, 1); var error = parsed.errors[0]; diff --git a/test/acceptance/ported/multilayer_interactivity.js b/test/acceptance/ported/multilayer_interactivity.js index 64eb9f82..ac406725 100644 --- a/test/acceptance/ported/multilayer_interactivity.js +++ b/test/acceptance/ported/multilayer_interactivity.js @@ -23,8 +23,6 @@ describe('multilayer interactivity and layers order', function() { layers: testScenario.layers }; - PortedServerOptions.afterLayergroupCreateCalls = 0; - assert.response(server, { url: '/database/windshaft_test/layergroup', @@ -50,8 +48,6 @@ describe('multilayer interactivity and layers order', function() { '\n\tLayer types: ' + layergroup.layers.map(layerType).join(', ') ); -// assert.equal(PortedServerOptions.afterLayergroupCreateCalls, 1); - var layergroupResponse = JSON.parse(response.body); assert.ok(layergroupResponse); diff --git a/test/acceptance/ported/support/ported_server_options.js b/test/acceptance/ported/support/ported_server_options.js index f6b7701e..3dafd589 100644 --- a/test/acceptance/ported/support/ported_server_options.js +++ b/test/acceptance/ported/support/ported_server_options.js @@ -47,12 +47,5 @@ module.exports = _.extend({}, serverOptions, { enable_cors: global.environment.enable_cors, unbuffered_logging: true, // for smoother teardown from tests log_format: null, // do not log anything - afterLayergroupCreateCalls: 0, useProfiler: true, - afterLayergroupCreate: function(req, cfg, res, callback) { - res.layercount = cfg.layers.length; -// config.afterLayergroupCreateCalls++; - callback(null); - } - }); From 73397ab5009b40a12ed074f611ccd39021eb20db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 16 Mar 2018 20:04:39 +0100 Subject: [PATCH 27/32] Typo --- test/acceptance/ported/support/ported_server_options.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/acceptance/ported/support/ported_server_options.js b/test/acceptance/ported/support/ported_server_options.js index 3dafd589..d7f8fb73 100644 --- a/test/acceptance/ported/support/ported_server_options.js +++ b/test/acceptance/ported/support/ported_server_options.js @@ -47,5 +47,5 @@ module.exports = _.extend({}, serverOptions, { enable_cors: global.environment.enable_cors, unbuffered_logging: true, // for smoother teardown from tests log_format: null, // do not log anything - useProfiler: true, + useProfiler: true }); From aa448a8c2eefc9c2d07e08b3d0ed52617e132a59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Mon, 19 Mar 2018 12:43:47 +0100 Subject: [PATCH 28/32] Remove NO PORTED TEST makefile option --- Makefile | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Makefile b/Makefile index 69b9b949..f523adda 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,6 @@ config/environments/test.js: config.status--test ./config.status--test TEST_SUITE := $(shell find test/{acceptance,integration,unit} -name "*.js") -TEST_SUITE_NO_PORTED := $(shell find test/acceptance test/integration test/unit -type f -not -path "*acceptance/ported*" -not -path "test/acceptance/overviews_queries.js" -name "*.js") TEST_SUITE_UNIT := $(shell find test/unit -name "*.js") TEST_SUITE_INTEGRATION := $(shell find test/integration -name "*.js") TEST_SUITE_ACCEPTANCE := $(shell find test/acceptance -name "*.js") @@ -28,10 +27,6 @@ test: config/environments/test.js @echo "***tests***" @$(SHELL) ./run_tests.sh ${RUNTESTFLAGS} $(TEST_SUITE) -test-no-ported: config/environments/test.js - @echo "***tests no ported ***" - @$(SHELL) ./run_tests.sh ${RUNTESTFLAGS} $(TEST_SUITE_NO_PORTED) - test-unit: config/environments/test.js @echo "***tests***" @$(SHELL) ./run_tests.sh ${RUNTESTFLAGS} $(TEST_SUITE_UNIT) From b038763b7b4602a80791798fd2c1c064fb09631e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Mon, 19 Mar 2018 12:48:52 +0100 Subject: [PATCH 29/32] Fix skipped test, port is configured via app configuration. It's no longer configurable via query-params --- test/acceptance/ported/torque.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/test/acceptance/ported/torque.js b/test/acceptance/ported/torque.js index 9ab4a98c..6d761b3d 100644 --- a/test/acceptance/ported/torque.js +++ b/test/acceptance/ported/torque.js @@ -349,9 +349,8 @@ describe('torque', function() { }); - // REVIEW // See http://github.com/CartoDB/Windshaft/issues/164 - it.skip("gives a 500 on database connection refused", function(done) { + it("gives a 500 on database connection refused", function(done) { var mapconfig = { version: '1.1.0', @@ -367,12 +366,15 @@ describe('torque', function() { ] }; + let defautlPort = global.environment.postgres.port; + step( function do_post() { var next = this; + global.environment.postgres.port = 54777; assert.response(server, { - url: '/database/windshaft_test/layergroup?dbport=54777', + url: '/database/windshaft_test/layergroup', method: 'POST', headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(mapconfig) @@ -380,6 +382,9 @@ describe('torque', function() { }, function checkPost(err, res) { assert.ifError(err); + + global.environment.postgres.port = defautlPort; + assert.equal(res.statusCode, 500, res.statusCode + ': ' + res.body); var parsed = JSON.parse(res.body); assert.ok(parsed.errors, parsed); @@ -422,7 +427,7 @@ describe('torque', function() { method: 'POST', headers: { host: 'localhost', 'Content-Type': 'application/json' }, data: JSON.stringify(layergroup) - }, {}, function(res) { next(null, res); }); + }, {}, function (res) { next(null, res); }); }, function checkResponse(err, res) { assert.ifError(err); From 5cde325d9a1848d1b967da44e2846bcf3de26243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Mon, 19 Mar 2018 13:51:03 +0100 Subject: [PATCH 30/32] Fix ported test related to cache_buster from layergroup token --- test/acceptance/ported/server_gettile.js | 37 +++++++++++-------- .../ported/support/ported_server_options.js | 1 - test/acceptance/ported/support/test_client.js | 17 ++++++++- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/test/acceptance/ported/server_gettile.js b/test/acceptance/ported/server_gettile.js index 10d04da1..64cfc579 100644 --- a/test/acceptance/ported/server_gettile.js +++ b/test/acceptance/ported/server_gettile.js @@ -7,7 +7,7 @@ var cartodbServer = require('../../../lib/cartodb/server'); var ServerOptions = require('./support/ported_server_options'); var testClient = require('./support/test_client'); -describe('server_gettile', function() { +describe.only('server_gettile', function() { var server = cartodbServer(ServerOptions); server.setMaxListeners(0); @@ -42,8 +42,7 @@ describe('server_gettile', function() { ); }); - // REVIEW - it.skip("response of get tile can be served by renderer cache", function(done) { + it("response of get tile can be served by renderer cache", function(done) { var tileUrl = '/13/4011/3088.png'; var lastXwc; var mapConfig = testClient.defaultTableMapConfig('test_table'); @@ -60,7 +59,7 @@ describe('server_gettile', function() { assert.ok(xwc > 0); assert.ok(xwc >= lastXwc); - requestTile(tileUrl + '?cache_buster=wadus', function (err, res) { + requestTile(tileUrl, { cache_buster: 'wadus' }, function (err, res) { var xwc = res.headers['x-windshaft-cache']; assert.ok(!xwc); @@ -85,8 +84,7 @@ describe('server_gettile', function() { ); }); - // REVIEW - it.skip("getting two tiles with same configuration uses renderer cache", function(done) { + it("getting two tiles with same configuration uses renderer cache", function(done) { var imageFixture = './test/fixtures/test_table_13_4011_3088_styled.png'; var tileUrl = '/13/4011/3088.png'; @@ -101,18 +99,25 @@ describe('server_gettile', function() { } testClient.withLayergroup(mapConfig, validateLayergroup, function(err, requestTile, finish) { - requestTile(tileUrl, function(err, res) { - assert.ok(res.headers.hasOwnProperty('x-windshaft-cache'), "Did not hit renderer cache on second time"); - assert.ok(res.headers['x-windshaft-cache'] >= 0); + var xwc = res.headers['x-windshaft-cache']; + assert.ok(!xwc); - assert.imageBufferIsSimilarToFile(res.body, imageFixture, IMAGE_EQUALS_TOLERANCE_PER_MIL, - function(err) { - finish(function(finishErr) { - done(err || finishErr); - }); - } - ); + requestTile(tileUrl, function (err, res) { + assert.ok( + res.headers.hasOwnProperty('x-windshaft-cache'), + "Did not hit renderer cache on second time" + ); + assert.ok(res.headers['x-windshaft-cache'] >= 0); + + assert.imageBufferIsSimilarToFile(res.body, imageFixture, IMAGE_EQUALS_TOLERANCE_PER_MIL, + function(err) { + finish(function(finishErr) { + done(err || finishErr); + }); + } + ); + }); }); }); }); diff --git a/test/acceptance/ported/support/ported_server_options.js b/test/acceptance/ported/support/ported_server_options.js index d7f8fb73..8b8ec339 100644 --- a/test/acceptance/ported/support/ported_server_options.js +++ b/test/acceptance/ported/support/ported_server_options.js @@ -1,7 +1,6 @@ var _ = require('underscore'); var serverOptions = require('../../../../lib/cartodb/server_options'); var mapnik = require('windshaft').mapnik; -var LayergroupToken = require('../../../../lib/cartodb/models/layergroup-token'); var OverviewsQueryRewriter = require('../../../../lib/cartodb/utils/overviews_query_rewriter'); var overviewsQueryRewriter = new OverviewsQueryRewriter({ zoom_level: 'CDB_ZoomFromScale(!scale_denominator!)' diff --git a/test/acceptance/ported/support/test_client.js b/test/acceptance/ported/support/test_client.js index 61ba6694..f110079a 100644 --- a/test/acceptance/ported/support/test_client.js +++ b/test/acceptance/ported/support/test_client.js @@ -454,8 +454,21 @@ function withLayergroup(layergroupConfig, options, callback) { }; } - var baseUrlTpl = '/database/windshaft_test/layergroup/<%= layergroupid %>'; - var finalUrl = _.template(baseUrlTpl, { layergroupid: layergroupid }) + layergroupUrl; + const signerTpl = function ({ signer }) { + return `${signer ? `:${signer}@` : ''}`; + } + + const cacheTpl = function ({ cache_buster, cacheBuster }) { + return `${cache_buster ? `:${cache_buster}` : `:${cacheBuster}`}`; + } + + const urlTpl = function ({layergroupid, cache_buster = null, tile }) { + const { signer, token , cacheBuster } = LayergroupToken.parse(layergroupid); + const base = '/database/windshaft_test/layergroup/' + return `${base}${signerTpl({signer})}${token}${cacheTpl({cache_buster, cacheBuster})}${tile}`; + } + + const finalUrl = urlTpl({ layergroupid, cache_buster: options.cache_buster, tile: layergroupUrl }); var request = { url: finalUrl, From d463d35906703d08ecbefa9a0a380cb99e67487c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Mon, 19 Mar 2018 14:56:20 +0100 Subject: [PATCH 31/32] Remove mocha filter --- test/acceptance/ported/server_gettile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/acceptance/ported/server_gettile.js b/test/acceptance/ported/server_gettile.js index 64cfc579..f40b680b 100644 --- a/test/acceptance/ported/server_gettile.js +++ b/test/acceptance/ported/server_gettile.js @@ -7,7 +7,7 @@ var cartodbServer = require('../../../lib/cartodb/server'); var ServerOptions = require('./support/ported_server_options'); var testClient = require('./support/test_client'); -describe.only('server_gettile', function() { +describe('server_gettile', function() { var server = cartodbServer(ServerOptions); server.setMaxListeners(0); From f8d1e159f4438505d72d5499664d6aa8c48b2d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 20 Mar 2018 11:09:05 +0100 Subject: [PATCH 32/32] Please jshint --- lib/cartodb/controllers/named_maps_admin.js | 1 - test/acceptance/ported/support/test_client.js | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/cartodb/controllers/named_maps_admin.js b/lib/cartodb/controllers/named_maps_admin.js index a519d35b..db503fb7 100644 --- a/lib/cartodb/controllers/named_maps_admin.js +++ b/lib/cartodb/controllers/named_maps_admin.js @@ -3,7 +3,6 @@ const cors = require('../middleware/cors'); const user = require('../middleware/user'); const locals = require('../middleware/locals'); const credentials = require('../middleware/credentials'); -const userMiddleware = require('../middleware/user'); const rateLimit = require('../middleware/rate-limit'); const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit; diff --git a/test/acceptance/ported/support/test_client.js b/test/acceptance/ported/support/test_client.js index f110079a..ab4576b6 100644 --- a/test/acceptance/ported/support/test_client.js +++ b/test/acceptance/ported/support/test_client.js @@ -456,17 +456,17 @@ function withLayergroup(layergroupConfig, options, callback) { const signerTpl = function ({ signer }) { return `${signer ? `:${signer}@` : ''}`; - } + }; const cacheTpl = function ({ cache_buster, cacheBuster }) { return `${cache_buster ? `:${cache_buster}` : `:${cacheBuster}`}`; - } + }; const urlTpl = function ({layergroupid, cache_buster = null, tile }) { const { signer, token , cacheBuster } = LayergroupToken.parse(layergroupid); - const base = '/database/windshaft_test/layergroup/' + const base = '/database/windshaft_test/layergroup/'; return `${base}${signerTpl({signer})}${token}${cacheTpl({cache_buster, cacheBuster})}${tile}`; - } + }; const finalUrl = urlTpl({ layergroupid, cache_buster: options.cache_buster, tile: layergroupUrl });