InitApp.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import App from '../../../../app.mjs'
  2. import QueueWorkers from '../../../../app/src/infrastructure/QueueWorkers.js'
  3. import MongoHelper from './MongoHelper.mjs'
  4. import RedisHelper from './RedisHelper.mjs'
  5. import Settings from '@overleaf/settings'
  6. import MockReCAPTCHAApi from '../mocks/MockReCaptchaApi.mjs'
  7. import { gracefulShutdown } from '../../../../app/src/infrastructure/GracefulShutdown.js'
  8. import Server from '../../../../app/src/infrastructure/Server.mjs'
  9. import { injectRouteAfter } from './injectRoute.mjs'
  10. import SplitTestHandler from '../../../../app/src/Features/SplitTests/SplitTestHandler.js'
  11. import SplitTestSessionHandler from '../../../../app/src/Features/SplitTests/SplitTestSessionHandler.js'
  12. import Modules from '../../../../app/src/infrastructure/Modules.js'
  13. const app = Server.app
  14. MongoHelper.initialize()
  15. RedisHelper.initialize()
  16. MockReCAPTCHAApi.initialize(2222)
  17. let server
  18. before('start main app', function (done) {
  19. // We expose addition routes in the test environment for acceptance tests.
  20. injectRouteAfter(
  21. app,
  22. route => route.path && route.path === '/dev/csrf',
  23. router => {
  24. router.get('/dev/session', (req, res) => {
  25. // allow changing the session directly for testing, assign any
  26. // properties in the query string to req.session
  27. if (req.query && Object.keys(req.query).length > 0) {
  28. Object.assign(req.session, req.query)
  29. }
  30. return res.json(req.session)
  31. })
  32. }
  33. )
  34. injectRouteAfter(
  35. app,
  36. route => route.path && route.path === '/dev/csrf',
  37. router => {
  38. router.post('/dev/set_in_session', (req, res) => {
  39. for (const [key, value] of Object.entries(req.body)) {
  40. req.session[key] = value
  41. }
  42. return res.sendStatus(200)
  43. })
  44. }
  45. )
  46. injectRouteAfter(
  47. app,
  48. route => route.path && route.path === '/dev/csrf',
  49. router => {
  50. router.get('/dev/split_test/get_assignment', (req, res) => {
  51. const { splitTestName } = req.query
  52. SplitTestHandler.promises
  53. .getAssignment(req, res, splitTestName, {
  54. sync: true,
  55. })
  56. .then(assignment => res.json(assignment))
  57. .catch(error => {
  58. res.status(500).json({ error: JSON.stringify(error) })
  59. })
  60. })
  61. }
  62. )
  63. injectRouteAfter(
  64. app,
  65. route => route.path && route.path === '/dev/csrf',
  66. router => {
  67. router.post('/dev/split_test/session_maintenance', (req, res) => {
  68. SplitTestSessionHandler.promises
  69. .sessionMaintenance(req)
  70. .then(res.sendStatus(200))
  71. .catch(error => {
  72. res.status(500).json({ error: JSON.stringify(error) })
  73. })
  74. })
  75. }
  76. )
  77. injectRouteAfter(
  78. app,
  79. route => route.path && route.path === '/dev/csrf',
  80. router => {
  81. router.csrf.disableDefaultCsrfProtection(
  82. '/dev/no_autostart_post_gateway',
  83. 'POST'
  84. )
  85. router.sessionAutostartMiddleware.disableSessionAutostartForRoute(
  86. '/dev/no_autostart_post_gateway',
  87. 'POST',
  88. (req, res, next) => {
  89. next()
  90. }
  91. )
  92. router.post('/dev/no_autostart_post_gateway', (req, res) => {
  93. res.status(200).json({ message: 'no autostart' })
  94. })
  95. }
  96. )
  97. server = App.listen(23000, '127.0.0.1', done)
  98. })
  99. before('start queue workers', async function () {
  100. QueueWorkers.start()
  101. await Modules.start()
  102. })
  103. after('stop main app', async function () {
  104. if (!server) {
  105. return
  106. }
  107. Settings.gracefulShutdownDelayInMs = 1
  108. await gracefulShutdown(server, 'tests')
  109. })