diff --git a/services/web/test/acceptance/src/helpers/InitApp.js b/services/web/test/acceptance/src/helpers/InitApp.js index f4922c8644..c92a47ff3b 100644 --- a/services/web/test/acceptance/src/helpers/InitApp.js +++ b/services/web/test/acceptance/src/helpers/InitApp.js @@ -9,49 +9,7 @@ const { gracefulShutdown, } = require('../../../../app/src/infrastructure/GracefulShutdown') const { app } = require('../../../../app/src/infrastructure/Server') - -/** - * Inject an endpoint to get the current users session into our app. This - * endpoint should only be available when running in the test environment. - * It is used to retrieve an email confirmation code when registering a - * new user account in acceptance tests. - */ -const addSessionEndpoint = app => { - const stack = app._router.stack - - stack.forEach(layer => { - if (layer.name !== 'router' || !layer.handle || !layer.handle.stack) { - return - } - - // We want to position our /dev/session endpoint next to the /dev/csrf - // endpoint so we check each router for the presence of this path. - const newRouteIndex = layer.handle.stack.findIndex( - route => - route && - route.route && - route.route.path && - route.route.path === '/dev/csrf' - ) - - if (newRouteIndex !== -1) { - // We add our new endpoint to the end of the router stack. - layer.handle.get('/dev/session', (req, res) => { - return res.json(req.session) - }) - - const routeStack = layer.handle.stack - const sessionRoute = routeStack[routeStack.length - 1] - - // Then we reposition it next to the /dev/csrf endpoint. - layer.handle.stack = [ - ...routeStack.slice(0, newRouteIndex), - sessionRoute, - ...routeStack.slice(newRouteIndex, routeStack.length - 1), - ] - } - }) -} +const { injectRouteAfter } = require('./injectRoute') logger.logger.level('error') @@ -62,7 +20,17 @@ MockReCAPTCHAApi.initialize(2222) let server before('start main app', function (done) { - addSessionEndpoint(app) + // We expose a session route in the test environment so that we can + // use it to access email confirmation codes in acceptance tests. + injectRouteAfter( + app, + route => route.path && route.path === '/dev/csrf', + router => { + router.get('/dev/session', (req, res) => { + return res.json(req.session) + }) + } + ) server = App.listen(23000, '127.0.0.1', done) }) diff --git a/services/web/test/acceptance/src/helpers/injectRoute.js b/services/web/test/acceptance/src/helpers/injectRoute.js new file mode 100644 index 0000000000..30a8a19f6a --- /dev/null +++ b/services/web/test/acceptance/src/helpers/injectRoute.js @@ -0,0 +1,39 @@ +/** + * Used to inject an endpoint into our app that should only be available + * when running in the test environment. + * + * @param app - a reference to the app. + * @param searchFilter - a filter function to locate a route to position the new route immediatley after. + * @param addRouteCallback - a callback that takes a router and creates the new route. + */ +function injectRouteAfter(app, searchFilter, addRouteCallback) { + const stack = app._router.stack + + stack.forEach(layer => { + if (layer.name !== 'router' || !layer.handle || !layer.handle.stack) { + return + } + + // Find the route that we want to position out new route after. + const newRouteIndex = layer.handle.stack.findIndex( + route => route && route.route && searchFilter(route.route) + ) + + if (newRouteIndex !== -1) { + // Add our new endpoint to the end of the router stack. + addRouteCallback(layer.handle) + + const routeStack = layer.handle.stack + const sessionRoute = routeStack[routeStack.length - 1] + + // Then we reposition it next to the route we found previously. + layer.handle.stack = [ + ...routeStack.slice(0, newRouteIndex), + sessionRoute, + ...routeStack.slice(newRouteIndex, routeStack.length - 1), + ] + } + }) +} + +module.exports = { injectRouteAfter }