InactiveProjectManagerTests.js 5.4 KB

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