e2e_test_setup.mjs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. // Delete the subscriptions of the user
  52. await db.subscriptions.deleteMany({ admin_id: user._id })
  53. // Soft delete the user.
  54. await UserDeleter.promises.deleteUser(user._id, {
  55. force: true,
  56. ipAddress: '0.0.0.0',
  57. })
  58. // Hard-delete the users projects.
  59. const projects = await db.deletedProjects
  60. .find(
  61. { deletedProjectOwnerId: user._id },
  62. { projection: { deletedProjectId: 1 } }
  63. )
  64. .toArray()
  65. await promiseMapWithLimit(
  66. 10,
  67. projects.map(p => p.deletedProjectId),
  68. ProjectDeleter.promises.expireDeletedProject
  69. )
  70. // Hard-delete the user.
  71. await UserDeleter.promises.expireDeletedUser(user._id)
  72. }
  73. /**
  74. * @param {string} email
  75. * @return {Promise<void>}
  76. */
  77. async function provisionUser(email) {
  78. if (!email.includes('+')) {
  79. throw new Error(
  80. `email=${email} should include the test suite name, e.g. user+project-sharing@example.com`
  81. )
  82. }
  83. await deleteUser(email)
  84. await createUser(email)
  85. }
  86. async function provisionUsers() {
  87. const emails = Settings.recaptcha.trustedUsers
  88. console.log(`> Provisioning ${emails.length} E2E users.`)
  89. await promiseMapWithLimit(5, emails, provisionUser)
  90. }
  91. async function purgeNewUsers() {
  92. const users = await db.users
  93. .find(
  94. { email: Settings.recaptcha.trustedUsersRegex },
  95. { projection: { email: 1 } }
  96. )
  97. .toArray()
  98. console.log(`> Deleting ${users.length} newly created E2E users.`)
  99. await promiseMapWithLimit(
  100. 5,
  101. users.map(user => user.email),
  102. deleteUser
  103. )
  104. }
  105. async function provisionSplitTests() {
  106. const backup = Path.join(
  107. MONOREPO,
  108. 'backup',
  109. 'split-tests',
  110. new Date().toISOString() + '.json'
  111. )
  112. console.log(
  113. `> 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.`
  114. )
  115. const splitTests = await SplitTestManager.getRuntimeTests()
  116. await fs.promises.mkdir(Path.dirname(backup), { recursive: true })
  117. await fs.promises.writeFile(
  118. backup,
  119. JSON.stringify(splitTests.sort((a, b) => (a.name > b.name ? 1 : -1)))
  120. )
  121. // Imported from production via https://www.overleaf.com/admin/split-test -> "Copy all split tests" -> "Copy for E2E test setup"
  122. const SPLIT_TESTS = JSON.parse(
  123. await fs.promises.readFile(
  124. Path.join(MONOREPO, 'tools/saas-e2e/split-tests.json')
  125. )
  126. )
  127. console.log(`> Importing ${SPLIT_TESTS.length} split-tests from production.`)
  128. await SplitTestManager.replaceSplitTests(SPLIT_TESTS)
  129. }
  130. async function main() {
  131. if (process.env.NODE_ENV !== 'development') {
  132. throw new Error('only available in dev-env')
  133. }
  134. await Promise.all([purgeNewUsers(), provisionUsers(), provisionSplitTests()])
  135. }
  136. await main()
  137. await GracefulShutdown.gracefulShutdown(
  138. {
  139. close(cb) {
  140. cb()
  141. },
  142. },
  143. 'SIGTERM'
  144. )