|
|
@@ -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)
|
|
|
})
|
|
|
|