InactiveProjectManagerTests.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* eslint-disable
  2. n/handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const SandboxedModule = require('sandboxed-module')
  15. const assert = require('assert')
  16. const path = require('path')
  17. const sinon = require('sinon')
  18. const modulePath = path.join(
  19. __dirname,
  20. '../../../../app/src/Features/InactiveData/InactiveProjectManager'
  21. )
  22. const { expect } = require('chai')
  23. const { ObjectId, ReadPreference } = require('mongodb')
  24. describe('InactiveProjectManager', function () {
  25. beforeEach(function () {
  26. this.settings = {}
  27. this.DocstoreManager = {
  28. unarchiveProject: sinon.stub(),
  29. archiveProject: sinon.stub(),
  30. }
  31. this.ProjectUpdateHandler = {
  32. markAsActive: sinon.stub(),
  33. markAsInactive: sinon.stub(),
  34. }
  35. this.ProjectGetter = { getProject: sinon.stub() }
  36. this.InactiveProjectManager = SandboxedModule.require(modulePath, {
  37. requires: {
  38. mongodb: { ObjectId },
  39. '@overleaf/settings': this.settings,
  40. '../Docstore/DocstoreManager': this.DocstoreManager,
  41. '../Project/ProjectUpdateHandler': this.ProjectUpdateHandler,
  42. '../Project/ProjectGetter': this.ProjectGetter,
  43. '../../models/Project': {},
  44. '../../infrastructure/mongodb': {
  45. ObjectId,
  46. READ_PREFERENCE_SECONDARY: ReadPreference.secondaryPreferred.mode,
  47. },
  48. },
  49. })
  50. return (this.project_id = '1234')
  51. })
  52. describe('reactivateProjectIfRequired', function () {
  53. beforeEach(function () {
  54. this.project = { active: false }
  55. this.ProjectGetter.getProject.callsArgWith(2, null, this.project)
  56. return this.ProjectUpdateHandler.markAsActive.callsArgWith(1)
  57. })
  58. it('should call unarchiveProject', function (done) {
  59. this.DocstoreManager.unarchiveProject.callsArgWith(1)
  60. return this.InactiveProjectManager.reactivateProjectIfRequired(
  61. this.project_id,
  62. err => {
  63. this.DocstoreManager.unarchiveProject
  64. .calledWith(this.project_id)
  65. .should.equal(true)
  66. this.ProjectUpdateHandler.markAsActive
  67. .calledWith(this.project_id)
  68. .should.equal(true)
  69. return done()
  70. }
  71. )
  72. })
  73. it('should not mark project as active if error with unarchiving', function (done) {
  74. const error = new Error('error')
  75. this.DocstoreManager.unarchiveProject.callsArgWith(1, error)
  76. return this.InactiveProjectManager.reactivateProjectIfRequired(
  77. this.project_id,
  78. err => {
  79. err.should.equal(error)
  80. this.DocstoreManager.unarchiveProject
  81. .calledWith(this.project_id)
  82. .should.equal(true)
  83. this.ProjectUpdateHandler.markAsActive
  84. .calledWith(this.project_id)
  85. .should.equal(false)
  86. return done()
  87. }
  88. )
  89. })
  90. it('should not call unarchiveProject if it is active', function (done) {
  91. this.project.active = true
  92. this.DocstoreManager.unarchiveProject.callsArgWith(1)
  93. return this.InactiveProjectManager.reactivateProjectIfRequired(
  94. this.project_id,
  95. err => {
  96. this.DocstoreManager.unarchiveProject
  97. .calledWith(this.project_id)
  98. .should.equal(false)
  99. this.ProjectUpdateHandler.markAsActive
  100. .calledWith(this.project_id)
  101. .should.equal(false)
  102. return done()
  103. }
  104. )
  105. })
  106. })
  107. describe('deactivateProject', function () {
  108. it('should call unarchiveProject and markAsInactive', function (done) {
  109. this.DocstoreManager.archiveProject.callsArgWith(1)
  110. this.ProjectUpdateHandler.markAsInactive.callsArgWith(1)
  111. return this.InactiveProjectManager.deactivateProject(
  112. this.project_id,
  113. err => {
  114. this.DocstoreManager.archiveProject
  115. .calledWith(this.project_id)
  116. .should.equal(true)
  117. this.ProjectUpdateHandler.markAsInactive
  118. .calledWith(this.project_id)
  119. .should.equal(true)
  120. return done()
  121. }
  122. )
  123. })
  124. it('should not call markAsInactive if there was a problem archiving in docstore', function (done) {
  125. this.DocstoreManager.archiveProject.callsArgWith(1, 'errorrr')
  126. this.ProjectUpdateHandler.markAsInactive.callsArgWith(1)
  127. return this.InactiveProjectManager.deactivateProject(
  128. this.project_id,
  129. err => {
  130. err.should.equal('errorrr')
  131. this.DocstoreManager.archiveProject
  132. .calledWith(this.project_id)
  133. .should.equal(true)
  134. this.ProjectUpdateHandler.markAsInactive
  135. .calledWith(this.project_id)
  136. .should.equal(false)
  137. return done()
  138. }
  139. )
  140. })
  141. })
  142. })