e2e_test_setup.mjs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. },
  39. }
  40. )
  41. }
  42. /**
  43. * @param {string} email
  44. * @return {Promise<void>}
  45. */
  46. async function deleteUser(email) {
  47. const user = await db.users.findOne({ email })
  48. if (!user) return
  49. // Soft delete the user.
  50. await UserDeleter.promises.deleteUser(user._id, {
  51. force: true,
  52. ipAddress: '0.0.0.0',
  53. })
  54. // Hard-delete the users projects.
  55. const projects = await db.deletedProjects
  56. .find(
  57. { deletedProjectOwnerId: user._id },
  58. { projection: { deletedProjectId: 1 } }
  59. )
  60. .toArray()
  61. await promiseMapWithLimit(
  62. 10,
  63. projects.map(p => p.deletedProjectId),
  64. ProjectDeleter.promises.expireDeletedProject
  65. )
  66. // Hard-delete the user.
  67. await UserDeleter.promises.expireDeletedUser(user._id)
  68. }
  69. /**
  70. * @param {string} email
  71. * @return {Promise<void>}
  72. */
  73. async function provisionUser(email) {
  74. await deleteUser(email)
  75. await createUser(email)
  76. }
  77. async function provisionUsers() {
  78. const emails = Settings.recaptcha.trustedUsers
  79. console.log(`> Provisioning ${emails.length} E2E users.`)
  80. await promiseMapWithLimit(3, emails, provisionUser)
  81. }
  82. async function purgeNewUsers() {
  83. const users = await db.users
  84. .find(
  85. { email: Settings.recaptcha.trustedUsersRegex },
  86. { projection: { email: 1 } }
  87. )
  88. .toArray()
  89. console.log(`> Deleting ${users.length} newly created E2E users.`)
  90. await promiseMapWithLimit(
  91. 3,
  92. users.map(user => user.email),
  93. deleteUser
  94. )
  95. }
  96. const SPLIT_TEST_OVERRIDES = [
  97. // disable writefull, oauth registration does not work in dev-env and their banners hide our buttons.
  98. {
  99. name: 'writefull-auto-account-creation',
  100. versions: [
  101. {
  102. versionNumber: 1,
  103. phase: 'release',
  104. active: true,
  105. analyticsEnabled: false,
  106. variants: [{ name: 'enabled', rolloutPercent: 0, rolloutStripes: [] }],
  107. createdAt: new Date(),
  108. },
  109. ],
  110. },
  111. ]
  112. async function provisionSplitTests() {
  113. const backup = Path.join(
  114. MONOREPO,
  115. 'backup',
  116. 'split-tests',
  117. new Date().toISOString() + '.json'
  118. )
  119. console.log(
  120. `> 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.`
  121. )
  122. const splitTests = await SplitTestManager.getRuntimeTests()
  123. await fs.promises.mkdir(Path.dirname(backup), { recursive: true })
  124. await fs.promises.writeFile(
  125. backup,
  126. JSON.stringify(splitTests.sort((a, b) => (a.name > b.name ? 1 : -1)))
  127. )
  128. // Imported from production via https://www.overleaf.com/admin/split-test -> "Copy all split tests" -> "Copy for E2E test setup"
  129. const SPLIT_TESTS = JSON.parse(
  130. await fs.promises.readFile(
  131. Path.join(MONOREPO, 'tools/saas-e2e/split-tests.json')
  132. )
  133. )
  134. console.log(`> Importing ${SPLIT_TESTS.length} split-tests from production.`)
  135. await SplitTestManager.replaceSplitTests(SPLIT_TESTS)
  136. console.log(
  137. `> Importing ${SPLIT_TEST_OVERRIDES.length} split-tests for test compatibility.`
  138. )
  139. await SplitTestManager.mergeSplitTests(SPLIT_TEST_OVERRIDES, true)
  140. }
  141. async function main() {
  142. if (process.env.NODE_ENV !== 'development') {
  143. throw new Error('only available in dev-env')
  144. }
  145. await purgeNewUsers()
  146. await provisionUsers()
  147. await provisionSplitTests()
  148. }
  149. await main()
  150. await GracefulShutdown.gracefulShutdown(
  151. {
  152. close(cb) {
  153. cb()
  154. },
  155. },
  156. 'SIGTERM'
  157. )