RemoveDeletedUsersFromTokenAccessRefsTests.mjs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { db, ObjectId } from '../../../app/src/infrastructure/mongodb.mjs'
  2. import { promisify } from 'node:util'
  3. import { exec } from 'node:child_process'
  4. import logger from '@overleaf/logger'
  5. import { expect } from 'chai'
  6. describe('RemoveDeletedUsersFromTokenAccessRefsTests', function () {
  7. const userId1 = new ObjectId()
  8. const userId2 = new ObjectId()
  9. const userId3 = new ObjectId()
  10. let insertedUsersCount
  11. beforeEach('insert users', async function () {
  12. const users = await db.users.insertMany([
  13. { _id: userId1, email: 'user1@example.com' },
  14. ])
  15. insertedUsersCount = users.insertedCount
  16. })
  17. const projectId1 = new ObjectId('65d726e807c024c8db43be22')
  18. const projectId2 = new ObjectId('65d726e807c024c8db43be23')
  19. const projectId3 = new ObjectId('65d726e807c024c8db43be24')
  20. const projectId4 = new ObjectId('65d726e807c024c8db43be25')
  21. beforeEach('insert projects', async function () {
  22. await db.projects.insertMany([
  23. {
  24. _id: projectId1,
  25. tokenAccessReadAndWrite_refs: [userId1],
  26. tokenAccessReadOnly_refs: [],
  27. },
  28. {
  29. _id: projectId2,
  30. tokenAccessReadAndWrite_refs: [userId2],
  31. tokenAccessReadOnly_refs: [],
  32. },
  33. {
  34. _id: projectId3,
  35. tokenAccessReadAndWrite_refs: [userId3],
  36. },
  37. {
  38. _id: projectId4,
  39. },
  40. ])
  41. })
  42. let stdOut
  43. const runScript = async () => {
  44. let result
  45. try {
  46. result = await promisify(exec)(
  47. 'cd ../../tools/migrations && yarn run migrations migrate -t saas --force 20240220130452_remove_deleted_users_from_token_access_refs'
  48. )
  49. } catch (error) {
  50. // dump details like exit code, stdErr and stdOut
  51. logger.error({ error }, 'script failed')
  52. throw error
  53. }
  54. const { stdout } = result
  55. stdOut = stdout
  56. expect(stdOut).to.match(new RegExp(`User ids count: ${insertedUsersCount}`))
  57. }
  58. describe('dry-run=false', function () {
  59. beforeEach('run script', async function () {
  60. await runScript('--dry-run=false')
  61. expect(stdOut).to.not.match(/dry run/i)
  62. })
  63. it('should show current user id to be removed', function () {
  64. expect(stdOut).to.match(
  65. new RegExp(
  66. `Found deleted user id: ${userId2.toString()} in project: ${projectId2.toString()}`
  67. )
  68. )
  69. expect(stdOut).to.match(
  70. new RegExp(
  71. `Removing deleted ${userId2.toString()} from all projects \\(found in project ${projectId2.toString()}\\)`
  72. )
  73. )
  74. expect(stdOut).to.match(
  75. new RegExp(
  76. `Found deleted user id: ${userId3.toString()} in project: ${projectId3.toString()}`
  77. )
  78. )
  79. expect(stdOut).to.match(
  80. new RegExp(
  81. `Removing deleted ${userId3.toString()} from all projects \\(found in project ${projectId3.toString()}\\)`
  82. )
  83. )
  84. })
  85. it('should show fixed projects with non-existing token access fields', function () {
  86. expect(stdOut)
  87. .to.match(
  88. new RegExp(
  89. `Fixed non-existing token access fields in project ${projectId3.toString()}`
  90. )
  91. )
  92. .and.match(
  93. new RegExp(
  94. `Fixed non-existing token access fields in project ${projectId4.toString()}`
  95. )
  96. )
  97. })
  98. it('should show the deleted user ids (and their count) that were removed', function () {
  99. expect(stdOut).to.match(
  100. new RegExp(
  101. `Deleted user ids \\(2\\)\\n${userId2.toString()}\\n${userId3.toString()}`
  102. )
  103. )
  104. })
  105. it('should show the project ids (and their count) that were fixed', function () {
  106. expect(stdOut).to.match(
  107. new RegExp(
  108. `Projects with deleted user ids \\(2\\)\\n${projectId2.toString()}\\n${projectId3.toString()}`
  109. )
  110. )
  111. })
  112. it('should fix the token access fields of projects', async function () {
  113. const [, ...fixedProjects] = await db.projects
  114. .find({}, { $sort: { _id: 1 } })
  115. .toArray()
  116. expect(fixedProjects).to.deep.equal([
  117. {
  118. _id: projectId2,
  119. tokenAccessReadAndWrite_refs: [],
  120. tokenAccessReadOnly_refs: [],
  121. },
  122. {
  123. _id: projectId3,
  124. tokenAccessReadAndWrite_refs: [],
  125. tokenAccessReadOnly_refs: [],
  126. },
  127. {
  128. _id: projectId4,
  129. tokenAccessReadOnly_refs: [],
  130. tokenAccessReadAndWrite_refs: [],
  131. },
  132. ])
  133. })
  134. })
  135. })