e2e_test_setup.mjs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 { connectionPromise, db } from '../app/src/infrastructure/mongodb.mjs'
  7. import GracefulShutdown from '../app/src/infrastructure/GracefulShutdown.mjs'
  8. import ProjectDeleter from '../app/src/Features/Project/ProjectDeleter.mjs'
  9. import SplitTestManager from '../app/src/Features/SplitTests/SplitTestManager.mjs'
  10. import UserDeleter from '../app/src/Features/User/UserDeleter.mjs'
  11. import UserRegistrationHandler from '../app/src/Features/User/UserRegistrationHandler.mjs'
  12. import HistoryManager from '../app/src/Features/History/HistoryManager.mjs'
  13. import ProjectCreationHandler from '../app/src/Features/Project/ProjectCreationHandler.mjs'
  14. const MONOREPO = Path.dirname(
  15. Path.dirname(Path.dirname(Path.dirname(fileURLToPath(import.meta.url))))
  16. )
  17. /**
  18. * @param {string} email
  19. * @return {Promise<string>}
  20. */
  21. async function createUser(email) {
  22. const user = await UserRegistrationHandler.promises.registerNewUser({
  23. email,
  24. password: process.env.CYPRESS_DEFAULT_PASSWORD,
  25. })
  26. const features = email.startsWith('free+')
  27. ? Settings.defaultFeatures
  28. : Settings.features.professional
  29. await db.users.updateOne(
  30. { _id: user._id },
  31. {
  32. $set: {
  33. // Set admin flag.
  34. isAdmin: email.startsWith('admin+'),
  35. adminRoles: email.startsWith('admin+') ? ['engineering'] : [],
  36. // Disable spell-checking for performance and flakiness reasons.
  37. 'ace.spellCheckLanguage': '',
  38. // Override features.
  39. features,
  40. featuresOverrides: [{ features }],
  41. // disable AI features
  42. 'aiFeatures.enabled': false,
  43. },
  44. }
  45. )
  46. return user._id.toString()
  47. }
  48. /**
  49. * @param {string} email
  50. * @return {Promise<void>}
  51. */
  52. async function deleteUser(email) {
  53. const user = await db.users.findOne({ email })
  54. if (!user) return
  55. // Delete the subscriptions of the user
  56. await db.subscriptions.deleteMany({ admin_id: user._id })
  57. // Soft delete the user.
  58. await UserDeleter.promises.deleteUser(user._id, {
  59. force: true,
  60. ipAddress: '0.0.0.0',
  61. })
  62. // Hard-delete the users projects.
  63. const projects = await db.deletedProjects
  64. .find(
  65. { 'deleterData.deletedProjectOwnerId': user._id },
  66. { projection: { 'deleterData.deletedProjectId': 1 } }
  67. )
  68. .toArray()
  69. await promiseMapWithLimit(
  70. 10,
  71. projects.map(p => p.deleterData.deletedProjectId),
  72. ProjectDeleter.promises.expireDeletedProject
  73. )
  74. // Hard-delete the user.
  75. await UserDeleter.promises.expireDeletedUser(user._id)
  76. }
  77. async function createProjectWithOldHistoryId(userId) {
  78. const projectName = 'old history id'
  79. const historyId = parseInt(
  80. await HistoryManager.promises.initializeProject(),
  81. 10
  82. )
  83. await ProjectCreationHandler.promises.createExampleProject(
  84. userId,
  85. projectName,
  86. { overleaf: { history: { id: historyId } } }
  87. )
  88. }
  89. /**
  90. * @param {string} email
  91. * @return {Promise<void>}
  92. */
  93. async function provisionUser(email) {
  94. if (!email.includes('+')) {
  95. throw new Error(
  96. `email=${email} should include the test suite name, e.g. user+project-sharing@example.com`
  97. )
  98. }
  99. await deleteUser(email)
  100. const userId = await createUser(email)
  101. if (email === 'user+old-history-id@example.com') {
  102. await createProjectWithOldHistoryId(userId)
  103. }
  104. }
  105. async function provisionUsers() {
  106. const emails = Settings.recaptcha.trustedUsers
  107. console.log(`> Provisioning ${emails.length} E2E users.`)
  108. await promiseMapWithLimit(5, emails, provisionUser)
  109. }
  110. async function purgeNewUsers() {
  111. const users = await db.users
  112. .find(
  113. { email: Settings.recaptcha.trustedUsersRegex },
  114. { projection: { email: 1 } }
  115. )
  116. .toArray()
  117. console.log(`> Deleting ${users.length} newly created E2E users.`)
  118. await promiseMapWithLimit(
  119. 5,
  120. users.map(user => user.email),
  121. deleteUser
  122. )
  123. }
  124. async function provisionSplitTests() {
  125. const backup = Path.join(
  126. MONOREPO,
  127. 'backup',
  128. 'split-tests',
  129. new Date().toISOString() + '.json'
  130. )
  131. console.log(
  132. `> 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.`
  133. )
  134. const splitTests = await SplitTestManager.getRuntimeTests()
  135. await fs.promises.mkdir(Path.dirname(backup), { recursive: true })
  136. await fs.promises.writeFile(
  137. backup,
  138. JSON.stringify(splitTests.sort((a, b) => (a.name > b.name ? 1 : -1)))
  139. )
  140. // Imported from production via https://www.overleaf.com/admin/split-test -> "Copy all split tests" -> "Copy for E2E test setup"
  141. const SPLIT_TESTS = JSON.parse(
  142. await fs.promises.readFile(
  143. Path.join(MONOREPO, 'tools/saas-e2e/split-tests.json')
  144. )
  145. )
  146. // Add WIP split test, we can update the JSON blob once this is in production
  147. SPLIT_TESTS.push({
  148. name: 'compile-from-history',
  149. versions: [
  150. {
  151. versionNumber: 1,
  152. createdAt: '2026-02-25T14:55:31.260Z',
  153. active: true,
  154. analyticsEnabled: false,
  155. phase: 'release',
  156. variants: [
  157. {
  158. name: 'enabled',
  159. rolloutPercent: 0,
  160. rolloutStripes: [],
  161. },
  162. ],
  163. },
  164. ],
  165. })
  166. console.log(`> Importing ${SPLIT_TESTS.length} split-tests from production.`)
  167. await SplitTestManager.replaceSplitTests(SPLIT_TESTS)
  168. }
  169. async function checkNoTableScan() {
  170. const client = await connectionPromise
  171. const { notablescan } = await client
  172. .db()
  173. .admin()
  174. .command({ getParameter: 1, notablescan: 1 })
  175. if (!notablescan) {
  176. console.error()
  177. console.error('!!! mongo is running without --notablescan')
  178. console.error()
  179. console.error('To fix this, either')
  180. console.error('- run "internal$ bin/e2e_test_setup"')
  181. console.error(
  182. '- or add MONGO_EXTRA_ARGS=--notablescan in config/local.env and apply with "internal$ bin/up mongo"'
  183. )
  184. console.error()
  185. throw new Error('mongo is running without --notablescan')
  186. }
  187. }
  188. async function main() {
  189. if (process.env.NODE_ENV !== 'development') {
  190. throw new Error('only available in dev-env')
  191. }
  192. await checkNoTableScan()
  193. await Promise.all([purgeNewUsers(), provisionUsers(), provisionSplitTests()])
  194. }
  195. await main()
  196. await GracefulShutdown.gracefulShutdown(
  197. {
  198. close(cb) {
  199. cb()
  200. },
  201. },
  202. 'SIGTERM'
  203. )