e2e_test_setup.mjs 4.5 KB

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