InactiveProjectManagerTests.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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')
  9. const { expect } = require('chai')
  10. describe('InactiveProjectManager', function () {
  11. beforeEach(function () {
  12. this.settings = {}
  13. this.DocstoreManager = {
  14. promises: {
  15. unarchiveProject: sinon.stub(),
  16. archiveProject: sinon.stub(),
  17. },
  18. }
  19. this.ProjectUpdateHandler = {
  20. promises: {
  21. markAsActive: sinon.stub(),
  22. markAsInactive: sinon.stub(),
  23. },
  24. }
  25. this.ProjectGetter = { promises: { getProject: sinon.stub() } }
  26. this.InactiveProjectManager = SandboxedModule.require(modulePath, {
  27. requires: {
  28. mongodb: { ObjectId },
  29. '@overleaf/settings': this.settings,
  30. '../Docstore/DocstoreManager': this.DocstoreManager,
  31. '../Project/ProjectUpdateHandler': this.ProjectUpdateHandler,
  32. '../Project/ProjectGetter': this.ProjectGetter,
  33. '../../models/Project': {},
  34. '../../infrastructure/mongodb': {
  35. ObjectId,
  36. READ_PREFERENCE_SECONDARY: ReadPreference.secondaryPreferred.mode,
  37. },
  38. },
  39. })
  40. this.project_id = '1234'
  41. })
  42. describe('reactivateProjectIfRequired', function () {
  43. beforeEach(function () {
  44. this.project = { active: false }
  45. this.ProjectGetter.promises.getProject.resolves(this.project)
  46. this.ProjectUpdateHandler.promises.markAsActive.resolves()
  47. })
  48. it('should call unarchiveProject', async function () {
  49. this.DocstoreManager.promises.unarchiveProject.resolves()
  50. await this.InactiveProjectManager.promises.reactivateProjectIfRequired(
  51. this.project_id
  52. )
  53. this.DocstoreManager.promises.unarchiveProject
  54. .calledWith(this.project_id)
  55. .should.equal(true)
  56. this.ProjectUpdateHandler.promises.markAsActive
  57. .calledWith(this.project_id)
  58. .should.equal(true)
  59. })
  60. it('should not mark project as active if error with unarchiving', async function () {
  61. this.DocstoreManager.promises.unarchiveProject.rejects()
  62. await expect(
  63. this.InactiveProjectManager.promises.reactivateProjectIfRequired(
  64. this.project_id
  65. )
  66. ).to.be.rejected
  67. this.DocstoreManager.promises.unarchiveProject
  68. .calledWith(this.project_id)
  69. .should.equal(true)
  70. this.ProjectUpdateHandler.promises.markAsActive
  71. .calledWith(this.project_id)
  72. .should.equal(false)
  73. })
  74. it('should not call unarchiveProject if it is active', async function () {
  75. this.project.active = true
  76. this.DocstoreManager.promises.unarchiveProject.resolves()
  77. await this.InactiveProjectManager.promises.reactivateProjectIfRequired(
  78. this.project_id
  79. )
  80. this.DocstoreManager.promises.unarchiveProject
  81. .calledWith(this.project_id)
  82. .should.equal(false)
  83. this.ProjectUpdateHandler.promises.markAsActive
  84. .calledWith(this.project_id)
  85. .should.equal(false)
  86. })
  87. })
  88. describe('deactivateProject', function () {
  89. it('should call unarchiveProject and markAsInactive', async function () {
  90. this.DocstoreManager.promises.archiveProject.resolves()
  91. this.ProjectUpdateHandler.promises.markAsInactive.resolves()
  92. await this.InactiveProjectManager.promises.deactivateProject(
  93. this.project_id
  94. )
  95. this.DocstoreManager.promises.archiveProject
  96. .calledWith(this.project_id)
  97. .should.equal(true)
  98. this.ProjectUpdateHandler.promises.markAsInactive
  99. .calledWith(this.project_id)
  100. .should.equal(true)
  101. })
  102. it('should not call markAsInactive if there was a problem archiving in docstore', async function () {
  103. this.DocstoreManager.promises.archiveProject.rejects()
  104. this.ProjectUpdateHandler.promises.markAsInactive.resolves()
  105. await expect(
  106. this.InactiveProjectManager.promises.deactivateProject(this.project_id)
  107. ).to.be.rejected
  108. this.DocstoreManager.promises.archiveProject
  109. .calledWith(this.project_id)
  110. .should.equal(true)
  111. this.ProjectUpdateHandler.promises.markAsInactive
  112. .calledWith(this.project_id)
  113. .should.equal(false)
  114. })
  115. })
  116. })