mirror of
https://github.com/overleaf/overleaf.git
synced 2025-04-19 22:03:33 +00:00
Merge pull request #17989 from overleaf/dp-test-endpoint-injection
Create an injectRouteAfter test helper method GitOrigin-RevId: e6c7cfd47a0200b47d5074185301f15ae2182c44
This commit is contained in:
parent
0cf17478fe
commit
5ad70690c9
2 changed files with 51 additions and 44 deletions
|
@ -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)
|
||||
})
|
||||
|
||||
|
|
39
services/web/test/acceptance/src/helpers/injectRoute.js
Normal file
39
services/web/test/acceptance/src/helpers/injectRoute.js
Normal file
|
@ -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 }
|
Loading…
Add table
Reference in a new issue