e2e_test_setup.mjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import fs from 'node:fs'
  2. import Path from 'node:path'
  3. import { fileURLToPath } from 'node:url'
  4. import { promiseMapWithLimit } from '@overleaf/promise-utils'
  5. import Settings from '@overleaf/settings'
  6. import { db } from '../app/src/infrastructure/mongodb.js'
  7. import GracefulShutdown from '../app/src/infrastructure/GracefulShutdown.js'
  8. import ProjectDeleter from '../app/src/Features/Project/ProjectDeleter.js'
  9. import SplitTestManager from '../app/src/Features/SplitTests/SplitTestManager.js'
  10. import UserDeleter from '../app/src/Features/User/UserDeleter.js'
  11. import UserRegistrationHandler from '../app/src/Features/User/UserRegistrationHandler.js'
  12. const MONOREPO = Path.dirname(
  13. Path.dirname(Path.dirname(Path.dirname(fileURLToPath(import.meta.url))))
  14. )
  15. /**
  16. * @param {string} email
  17. * @return {Promise<void>}
  18. */
  19. async function createUser(email) {
  20. const user = await UserRegistrationHandler.promises.registerNewUser({
  21. email,
  22. password: process.env.CYPRESS_DEFAULT_PASSWORD,
  23. })
  24. const features = email.startsWith('free+')
  25. ? Settings.defaultFeatures
  26. : Settings.features.professional
  27. await db.users.updateOne(
  28. { _id: user._id },
  29. {
  30. $set: {
  31. // Set admin flag.
  32. isAdmin: email.startsWith('admin+'),
  33. // Disable spell-checking for performance and flakiness reasons.
  34. 'ace.spellCheckLanguage': '',
  35. // Override features.
  36. features,
  37. featuresOverrides: [{ features }],
  38. // disable Writefull
  39. 'writefull.enabled': false,
  40. },
  41. }
  42. )
  43. }
  44. /**
  45. * @param {string} email
  46. * @return {Promise<void>}
  47. */
  48. async function deleteUser(email) {
  49. const user = await db.users.findOne({ email })
  50. if (!user) return
  51. // Soft delete the user.
  52. await UserDeleter.promises.deleteUser(user._id, {
  53. force: true,
  54. ipAddress: '0.0.0.0',
  55. })
  56. // Hard-delete the users projects.
  57. const projects = await db.deletedProjects
  58. .find(
  59. { deletedProjectOwnerId: user._id },
  60. { projection: { deletedProjectId: 1 } }
  61. )
  62. .toArray()
  63. await promiseMapWithLimit(
  64. 10,
  65. projects.map(p => p.deletedProjectId),
  66. ProjectDeleter.promises.expireDeletedProject
  67. )
  68. // Hard-delete the user.
  69. await UserDeleter.promises.expireDeletedUser(user._id)
  70. }
  71. /**
  72. * @param {string} email
  73. * @return {Promise<void>}
  74. */
  75. async function provisionUser(email) {
  76. if (!email.includes('+')) {
  77. throw new Error(
  78. `email=${email} should include the test suite name, e.g. user+project-sharing@example.com`
  79. )
  80. }
  81. await deleteUser(email)
  82. await createUser(email)
  83. }
  84. async function provisionUsers() {
  85. const emails = Settings.recaptcha.trustedUsers
  86. console.log(`> Provisioning ${emails.length} E2E users.`)
  87. await promiseMapWithLimit(5, emails, provisionUser)
  88. }
  89. async function purgeNewUsers() {
  90. const users = await db.users
  91. .find(
  92. { email: Settings.recaptcha.trustedUsersRegex },
  93. { projection: { email: 1 } }
  94. )
  95. .toArray()
  96. console.log(`> Deleting ${users.length} newly created E2E users.`)
  97. await promiseMapWithLimit(
  98. 5,
  99. users.map(user => user.email),
  100. deleteUser
  101. )
  102. }
  103. async function provisionSplitTests() {
  104. const backup = Path.join(
  105. MONOREPO,
  106. 'backup',
  107. 'split-tests',
  108. new Date().toISOString() + '.json'
  109. )
  110. console.log(
  111. `> Backing up previous split-tests into ${backup}. You can import them again on https://www.dev-overleaf.com/admin/split-test via the [Import] button.`
  112. )
  113. const splitTests = await SplitTestManager.getRuntimeTests()
  114. await fs.promises.mkdir(Path.dirname(backup), { recursive: true })
  115. await fs.promises.writeFile(
  116. backup,
  117. JSON.stringify(splitTests.sort((a, b) => (a.name > b.name ? 1 : -1)))
  118. )
  119. // Imported from production via https://www.overleaf.com/admin/split-test -> "Copy all split tests" -> "Copy for E2E test setup"
  120. const SPLIT_TESTS = JSON.parse(
  121. await fs.promises.readFile(
  122. Path.join(MONOREPO, 'tools/saas-e2e/split-tests.json')
  123. )
  124. )
  125. console.log(`> Importing ${SPLIT_TESTS.length} split-tests from production.`)
  126. await SplitTestManager.replaceSplitTests(SPLIT_TESTS)
  127. }
  128. async function main() {
  129. if (process.env.NODE_ENV !== 'development') {
  130. throw new Error('only available in dev-env')
  131. }
  132. await Promise.all([purgeNewUsers(), provisionUsers(), provisionSplitTests()])
  133. }
  134. await main()
  135. await GracefulShutdown.gracefulShutdown(
  136. {
  137. close(cb) {
  138. cb()
  139. },
  140. },
  141. 'SIGTERM'
  142. )