From 769d17da5da1d53745ee82f5aab311b8b28a178c Mon Sep 17 00:00:00 2001 From: Shane Tomlinson Date: Wed, 15 Aug 2018 18:10:29 +0100 Subject: [PATCH] fix(test): Avoid cross test pollution caused by mutating route objects. --- lib/server/web.js | 12 ++++++++---- package.json | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/server/web.js b/lib/server/web.js index 5e9c7c8..5f85485 100644 --- a/lib/server/web.js +++ b/lib/server/web.js @@ -5,6 +5,7 @@ const Hapi = require('hapi'); const Raven = require('raven'); const ScopeSet = require('fxa-shared').oauth.scopes; +const cloneDeep = require('lodash.clonedeep'); const AppError = require('../error'); const config = require('../config').getProperties(); @@ -148,19 +149,22 @@ exports.create = function createServer() { // Expand the scope list on each route to include all super-scopes, // so that Hapi can easily check them via simple string comparison. - routes.forEach(function(route) { + routes = routes.map(function(routeDefinition) { + // create a copy of the route definition to avoid cross-unit test + // contamination since we make local changes to the definition object. + const route = cloneDeep(routeDefinition); var scope = route.config.auth && route.config.auth.scope; if (scope) { route.config.auth.scope = ScopeSet.fromArray(scope).getImplicantValues(); } - }); - - routes.forEach(function(route) { + return route; + }).map(function(route) { if (route.config.cache === undefined) { route.config.cache = { otherwise: 'private, no-cache, no-store, must-revalidate' }; } + return route; }); server.route(routes); diff --git a/package.json b/package.json index 036d685..d4d0fca 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "hapi": "16.6.3", "inert": "4.0.2", "joi": "9.0.4", + "lodash.clonedeep": "4.5.0", "mozlog": "2.2.0", "mysql": "2.16.0", "mysql-patcher": "0.7.0",