InitApp.js 3.6 KB

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