RemoveDeletedUsersFromTokenAccessRefsTests.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. const { db, ObjectId } = require('../../../app/src/infrastructure/mongodb')
  2. const { promisify } = require('util')
  3. const { exec } = require('child_process')
  4. const logger = require('@overleaf/logger/logging-manager')
  5. const { expect } = require('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. let insertedProjects
  21. beforeEach('insert projects', async function () {
  22. insertedProjects = 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. tokenAccessReadOnly_refs: [],
  37. },
  38. ])
  39. })
  40. let stdOut
  41. const runScript = async (dryRun, projectsList) => {
  42. let result
  43. try {
  44. result = await promisify(exec)(
  45. [
  46. 'VERBOSE_LOGGING=true',
  47. 'node',
  48. 'scripts/remove_deleted_users_from_token_access_refs',
  49. dryRun,
  50. projectsList,
  51. ].join(' ')
  52. )
  53. } catch (error) {
  54. // dump details like exit code, stdErr and stdOut
  55. logger.error({ error }, 'script failed')
  56. throw error
  57. }
  58. const { stdout } = result
  59. stdOut = stdout
  60. expect(stdOut).to.match(new RegExp(`User ids count: ${insertedUsersCount}`))
  61. }
  62. describe('dry-run=true', function () {
  63. beforeEach('run script', async function () {
  64. await runScript('--dry-run=true')
  65. expect(stdOut).to.match(/doing dry run/i)
  66. })
  67. it('should show current user id to be removed', function () {
  68. expect(stdOut).to.match(
  69. new RegExp(
  70. `Found deleted user id: ${userId2.toString()} in project: ${projectId2.toString()}`
  71. )
  72. )
  73. expect(stdOut).to.match(
  74. new RegExp(
  75. `DRY RUN - would remove deleted ${userId2.toString()} from all projects \\(found in project ${projectId2.toString()}\\)`
  76. )
  77. )
  78. expect(stdOut).to.match(
  79. new RegExp(
  80. `Found deleted user id: ${userId3.toString()} in project: ${projectId3.toString()}`
  81. )
  82. )
  83. expect(stdOut).to.match(
  84. new RegExp(
  85. `DRY RUN - would remove deleted ${userId3.toString()} from all projects \\(found in project ${projectId3.toString()}\\)`
  86. )
  87. )
  88. })
  89. it('should show the user ids (and their count) to be deleted', function () {
  90. expect(stdOut).to.match(
  91. new RegExp(
  92. `DRY RUN - would delete user ids \\(2\\)\\n${userId2.toString()}\\n${userId3.toString()}`
  93. )
  94. )
  95. })
  96. it('should show the project ids (and their count) that needs fixing', function () {
  97. expect(stdOut).to.match(
  98. new RegExp(
  99. `Projects with deleted user ids \\(2\\)\\n${projectId2.toString()}\\n${projectId3.toString()}`
  100. )
  101. )
  102. })
  103. it('should not fix the token access fields of projects', async function () {
  104. const projects = await db.projects
  105. .find({}, { $sort: { _id: 1 } })
  106. .toArray()
  107. const users = [userId1, userId2, userId3]
  108. projects.forEach((project, i) => {
  109. expect(project.tokenAccessReadAndWrite_refs[0].toString()).to.eq(
  110. users[i].toString()
  111. )
  112. })
  113. })
  114. })
  115. describe('dry-run=false', function () {
  116. beforeEach('run script', async function () {
  117. await runScript('--dry-run=false')
  118. expect(stdOut).to.not.match(/dry run/i)
  119. })
  120. it('should show current user id to be removed', function () {
  121. expect(stdOut).to.match(
  122. new RegExp(
  123. `Found deleted user id: ${userId2.toString()} in project: ${projectId2.toString()}`
  124. )
  125. )
  126. expect(stdOut).to.match(
  127. new RegExp(
  128. `Removing deleted ${userId2.toString()} from all projects \\(found in project ${projectId2.toString()}\\)`
  129. )
  130. )
  131. expect(stdOut).to.match(
  132. new RegExp(
  133. `Found deleted user id: ${userId3.toString()} in project: ${projectId3.toString()}`
  134. )
  135. )
  136. expect(stdOut).to.match(
  137. new RegExp(
  138. `Removing deleted ${userId3.toString()} from all projects \\(found in project ${projectId3.toString()}\\)`
  139. )
  140. )
  141. })
  142. it('should show the deleted user ids (and their count) that were removed', function () {
  143. expect(stdOut).to.match(
  144. new RegExp(
  145. `Deleted user ids \\(2\\)\\n${userId2.toString()}\\n${userId3.toString()}`
  146. )
  147. )
  148. })
  149. it('should show the project ids (and their count) that were fixed', function () {
  150. expect(stdOut).to.match(
  151. new RegExp(
  152. `Projects with deleted user ids \\(2\\)\\n${projectId2.toString()}\\n${projectId3.toString()}`
  153. )
  154. )
  155. })
  156. it('should fix the token access fields of projects', async function () {
  157. const [, ...fixedProjects] = await db.projects
  158. .find({}, { $sort: { _id: 1 } })
  159. .toArray()
  160. expect(fixedProjects).to.deep.equal([
  161. {
  162. _id: projectId2,
  163. tokenAccessReadAndWrite_refs: [],
  164. tokenAccessReadOnly_refs: [],
  165. },
  166. {
  167. _id: projectId3,
  168. tokenAccessReadAndWrite_refs: [],
  169. tokenAccessReadOnly_refs: [],
  170. },
  171. ])
  172. })
  173. })
  174. describe('projects=projectId2', function () {
  175. beforeEach('run script', async function () {
  176. const projectId2 = insertedProjects.insertedIds[1]
  177. await runScript('--dry-run=false', `--projects=${projectId2.toString()}`)
  178. })
  179. it('should fix only the projects provided', async function () {
  180. const [project1, project2, project3] = await db.projects
  181. .find({}, { $sort: { _id: 1 } })
  182. .toArray()
  183. expect(project1.tokenAccessReadAndWrite_refs.length).to.be.gt(0)
  184. expect(project2.tokenAccessReadAndWrite_refs.length).to.eq(0) // deleted user removed
  185. expect(project3.tokenAccessReadAndWrite_refs.length).to.be.gt(0)
  186. })
  187. })
  188. })