InitApp.mjs 3.7 KB

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