InitApp.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. logger.logger.level('error')
  16. MongoHelper.initialize()
  17. RedisHelper.initialize()
  18. MockReCAPTCHAApi.initialize(2222)
  19. let server
  20. before('start main app', function (done) {
  21. // We expose addition routes in the test environment for acceptance tests.
  22. injectRouteAfter(
  23. app,
  24. route => route.path && route.path === '/dev/csrf',
  25. router => {
  26. router.get('/dev/session', (req, res) => {
  27. // allow changing the session directly for testing, assign any
  28. // properties in the query string to req.session
  29. if (req.query && Object.keys(req.query).length > 0) {
  30. Object.assign(req.session, req.query)
  31. }
  32. return res.json(req.session)
  33. })
  34. }
  35. )
  36. injectRouteAfter(
  37. app,
  38. route => route.path && route.path === '/dev/csrf',
  39. router => {
  40. router.post('/dev/set_in_session', (req, res) => {
  41. for (const [key, value] of Object.entries(req.body)) {
  42. req.session[key] = value
  43. }
  44. return res.sendStatus(200)
  45. })
  46. }
  47. )
  48. injectRouteAfter(
  49. app,
  50. route => route.path && route.path === '/dev/csrf',
  51. router => {
  52. router.get('/dev/split_test/get_assignment', (req, res) => {
  53. const { splitTestName } = req.query
  54. SplitTestHandler.promises
  55. .getAssignment(req, res, splitTestName, {
  56. sync: 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', function () {
  102. QueueWorkers.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. })