InactiveProjectManager.test.mjs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. import mongodb, { ReadPreference } from 'mongodb-legacy'
  4. const { ObjectId } = mongodb
  5. const modulePath =
  6. '../../../../app/src/Features/InactiveData/InactiveProjectManager'
  7. describe('InactiveProjectManager', function () {
  8. beforeEach(async function (ctx) {
  9. ctx.settings = {}
  10. ctx.metrics = { inc: sinon.stub() }
  11. ctx.DocstoreManager = {
  12. promises: {
  13. unarchiveProject: sinon.stub(),
  14. archiveProject: sinon.stub(),
  15. },
  16. }
  17. ctx.DocumentUpdaterHandler = {
  18. promises: {
  19. flushProjectToMongoAndDelete: sinon.stub(),
  20. },
  21. }
  22. ctx.ProjectUpdateHandler = {
  23. promises: {
  24. markAsActive: sinon.stub(),
  25. markAsInactive: sinon.stub(),
  26. },
  27. }
  28. ctx.ProjectGetter = { promises: { getProject: sinon.stub() } }
  29. ctx.Modules = { promises: { hooks: { fire: sinon.stub() } } }
  30. vi.doMock('mongodb-legacy', () => ({
  31. default: { ObjectId },
  32. }))
  33. vi.doMock('@overleaf/settings', () => ({
  34. default: ctx.settings,
  35. }))
  36. vi.doMock('@overleaf/metrics', () => ({
  37. default: ctx.metrics,
  38. }))
  39. vi.doMock('../../../../app/src/Features/Docstore/DocstoreManager', () => ({
  40. default: ctx.DocstoreManager,
  41. }))
  42. vi.doMock(
  43. '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler',
  44. () => ({
  45. default: ctx.DocumentUpdaterHandler,
  46. })
  47. )
  48. vi.doMock(
  49. '../../../../app/src/Features/Project/ProjectUpdateHandler',
  50. () => ({
  51. default: ctx.ProjectUpdateHandler,
  52. })
  53. )
  54. vi.doMock('../../../../app/src/Features/Project/ProjectGetter', () => ({
  55. default: ctx.ProjectGetter,
  56. }))
  57. vi.doMock('../../../../app/src/models/Project', () => ({}))
  58. vi.doMock('../../../../app/src/infrastructure/Modules', () => ({
  59. default: ctx.Modules,
  60. }))
  61. vi.doMock('../../../../app/src/infrastructure/mongodb', () => ({
  62. default: {
  63. ObjectId,
  64. READ_PREFERENCE_SECONDARY: ReadPreference.secondaryPreferred.mode,
  65. },
  66. }))
  67. ctx.InactiveProjectManager = (await import(modulePath)).default
  68. ctx.project_id = '1234'
  69. })
  70. describe('reactivateProjectIfRequired', function () {
  71. beforeEach(function (ctx) {
  72. ctx.project = { active: false }
  73. ctx.ProjectGetter.promises.getProject.resolves(ctx.project)
  74. ctx.ProjectUpdateHandler.promises.markAsActive.resolves()
  75. })
  76. it('should call unarchiveProject', async function (ctx) {
  77. ctx.DocstoreManager.promises.unarchiveProject.resolves()
  78. await ctx.InactiveProjectManager.promises.reactivateProjectIfRequired(
  79. ctx.project_id
  80. )
  81. ctx.DocstoreManager.promises.unarchiveProject
  82. .calledWith(ctx.project_id)
  83. .should.equal(true)
  84. ctx.ProjectUpdateHandler.promises.markAsActive
  85. .calledWith(ctx.project_id)
  86. .should.equal(true)
  87. })
  88. it('should not mark project as active if error with unarchiving', async function (ctx) {
  89. ctx.DocstoreManager.promises.unarchiveProject.rejects()
  90. await expect(
  91. ctx.InactiveProjectManager.promises.reactivateProjectIfRequired(
  92. ctx.project_id
  93. )
  94. ).to.be.rejected
  95. ctx.DocstoreManager.promises.unarchiveProject
  96. .calledWith(ctx.project_id)
  97. .should.equal(true)
  98. ctx.ProjectUpdateHandler.promises.markAsActive
  99. .calledWith(ctx.project_id)
  100. .should.equal(false)
  101. })
  102. it('should not call unarchiveProject if it is active', async function (ctx) {
  103. ctx.project.active = true
  104. ctx.DocstoreManager.promises.unarchiveProject.resolves()
  105. await ctx.InactiveProjectManager.promises.reactivateProjectIfRequired(
  106. ctx.project_id
  107. )
  108. ctx.DocstoreManager.promises.unarchiveProject
  109. .calledWith(ctx.project_id)
  110. .should.equal(false)
  111. ctx.ProjectUpdateHandler.promises.markAsActive
  112. .calledWith(ctx.project_id)
  113. .should.equal(false)
  114. })
  115. })
  116. describe('deactivateProject', function () {
  117. it('should call archiveProject and markAsInactive after flushing', async function (ctx) {
  118. ctx.DocstoreManager.promises.archiveProject.resolves()
  119. ctx.DocumentUpdaterHandler.promises.flushProjectToMongoAndDelete.resolves()
  120. ctx.ProjectUpdateHandler.promises.markAsInactive.resolves()
  121. ctx.Modules.promises.hooks.fire.resolves()
  122. await ctx.InactiveProjectManager.promises.deactivateProject(
  123. ctx.project_id
  124. )
  125. ctx.DocumentUpdaterHandler.promises.flushProjectToMongoAndDelete
  126. .calledWith(ctx.project_id)
  127. .should.equal(true)
  128. ctx.Modules.promises.hooks.fire
  129. .calledWith('deactivateProject', ctx.project_id)
  130. .should.equal(true)
  131. ctx.DocstoreManager.promises.archiveProject
  132. .calledWith(ctx.project_id)
  133. .should.equal(true)
  134. ctx.ProjectUpdateHandler.promises.markAsInactive
  135. .calledWith(ctx.project_id)
  136. .should.equal(true)
  137. })
  138. it('should not call markAsInactive if there was a problem archiving in docstore', async function (ctx) {
  139. ctx.DocstoreManager.promises.archiveProject.rejects()
  140. ctx.DocumentUpdaterHandler.promises.flushProjectToMongoAndDelete.resolves()
  141. ctx.ProjectUpdateHandler.promises.markAsInactive.resolves()
  142. ctx.Modules.promises.hooks.fire.resolves()
  143. await expect(
  144. ctx.InactiveProjectManager.promises.deactivateProject(ctx.project_id)
  145. ).to.be.rejected
  146. ctx.DocumentUpdaterHandler.promises.flushProjectToMongoAndDelete
  147. .calledWith(ctx.project_id)
  148. .should.equal(true)
  149. ctx.DocstoreManager.promises.archiveProject
  150. .calledWith(ctx.project_id)
  151. .should.equal(true)
  152. ctx.ProjectUpdateHandler.promises.markAsInactive
  153. .calledWith(ctx.project_id)
  154. .should.equal(false)
  155. })
  156. })
  157. })