InitApp.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 } = req.query
  53. SplitTestHandler.promises
  54. .getAssignment(req, res, splitTestName, {
  55. sync: true,
  56. })
  57. .then(assignment => res.json(assignment))
  58. .catch(error => {
  59. res.status(500).json({ error: JSON.stringify(error) })
  60. })
  61. })
  62. }
  63. )
  64. injectRouteAfter(
  65. app,
  66. route => route.path && route.path === '/dev/csrf',
  67. router => {
  68. router.post('/dev/split_test/session_maintenance', (req, res) => {
  69. SplitTestSessionHandler.promises
  70. .sessionMaintenance(req)
  71. .then(res.sendStatus(200))
  72. .catch(error => {
  73. res.status(500).json({ error: JSON.stringify(error) })
  74. })
  75. })
  76. }
  77. )
  78. injectRouteAfter(
  79. app,
  80. route => route.path && route.path === '/dev/csrf',
  81. router => {
  82. router.csrf.disableDefaultCsrfProtection(
  83. '/dev/no_autostart_post_gateway',
  84. 'POST'
  85. )
  86. router.sessionAutostartMiddleware.disableSessionAutostartForRoute(
  87. '/dev/no_autostart_post_gateway',
  88. 'POST',
  89. (req, res, next) => {
  90. next()
  91. }
  92. )
  93. router.post('/dev/no_autostart_post_gateway', (req, res) => {
  94. res.status(200).json({ message: 'no autostart' })
  95. })
  96. }
  97. )
  98. server = App.listen(23000, '127.0.0.1', done)
  99. })
  100. before('start queue workers', async function () {
  101. QueueWorkers.start()
  102. await Modules.start()
  103. })
  104. after('stop main app', async function () {
  105. if (!server) {
  106. return
  107. }
  108. Settings.gracefulShutdownDelayInMs = 1
  109. await gracefulShutdown(server, 'tests')
  110. })
  111. if (process.env.CI === 'true') {
  112. beforeEach('record error logs in junit', testLogRecorder)
  113. }