flushAndDeleteProjectTests.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* eslint-disable
  2. camelcase,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS206: Consider reworking classes to avoid initClass
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const sinon = require('sinon')
  16. const chai = require('chai')
  17. const should = chai.should()
  18. const modulePath = '../../../../app/js/ProjectManager.js'
  19. const SandboxedModule = require('sandboxed-module')
  20. describe('ProjectManager - flushAndDeleteProject', function () {
  21. beforeEach(function () {
  22. let Timer
  23. this.ProjectManager = SandboxedModule.require(modulePath, {
  24. requires: {
  25. './RedisManager': (this.RedisManager = {}),
  26. './ProjectHistoryRedisManager': (this.ProjectHistoryRedisManager = {}),
  27. './DocumentManager': (this.DocumentManager = {}),
  28. 'logger-sharelatex': (this.logger = {
  29. log: sinon.stub(),
  30. error: sinon.stub()
  31. }),
  32. './HistoryManager': (this.HistoryManager = {
  33. flushProjectChanges: sinon.stub().callsArg(2)
  34. }),
  35. './Metrics': (this.Metrics = {
  36. Timer: (Timer = (function () {
  37. Timer = class Timer {
  38. static initClass() {
  39. this.prototype.done = sinon.stub()
  40. }
  41. }
  42. Timer.initClass()
  43. return Timer
  44. })())
  45. })
  46. }
  47. })
  48. this.project_id = 'project-id-123'
  49. return (this.callback = sinon.stub())
  50. })
  51. describe('successfully', function () {
  52. beforeEach(function (done) {
  53. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  54. this.RedisManager.getDocIdsInProject = sinon
  55. .stub()
  56. .callsArgWith(1, null, this.doc_ids)
  57. this.DocumentManager.flushAndDeleteDocWithLock = sinon.stub().callsArg(3)
  58. return this.ProjectManager.flushAndDeleteProjectWithLocks(
  59. this.project_id,
  60. {},
  61. (error) => {
  62. this.callback(error)
  63. return done()
  64. }
  65. )
  66. })
  67. it('should get the doc ids in the project', function () {
  68. return this.RedisManager.getDocIdsInProject
  69. .calledWith(this.project_id)
  70. .should.equal(true)
  71. })
  72. it('should delete each doc in the project', function () {
  73. return Array.from(this.doc_ids).map((doc_id) =>
  74. this.DocumentManager.flushAndDeleteDocWithLock
  75. .calledWith(this.project_id, doc_id, {})
  76. .should.equal(true)
  77. )
  78. })
  79. it('should flush project history', function () {
  80. return this.HistoryManager.flushProjectChanges
  81. .calledWith(this.project_id, {})
  82. .should.equal(true)
  83. })
  84. it('should call the callback without error', function () {
  85. return this.callback.calledWith(null).should.equal(true)
  86. })
  87. return it('should time the execution', function () {
  88. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  89. })
  90. })
  91. return describe('when a doc errors', function () {
  92. beforeEach(function (done) {
  93. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  94. this.RedisManager.getDocIdsInProject = sinon
  95. .stub()
  96. .callsArgWith(1, null, this.doc_ids)
  97. this.DocumentManager.flushAndDeleteDocWithLock = sinon.spy(
  98. (project_id, doc_id, options, callback) => {
  99. if (doc_id === 'doc-id-1') {
  100. return callback(
  101. (this.error = new Error('oops, something went wrong'))
  102. )
  103. } else {
  104. return callback()
  105. }
  106. }
  107. )
  108. return this.ProjectManager.flushAndDeleteProjectWithLocks(
  109. this.project_id,
  110. {},
  111. (error) => {
  112. this.callback(error)
  113. return done()
  114. }
  115. )
  116. })
  117. it('should still flush each doc in the project', function () {
  118. return Array.from(this.doc_ids).map((doc_id) =>
  119. this.DocumentManager.flushAndDeleteDocWithLock
  120. .calledWith(this.project_id, doc_id, {})
  121. .should.equal(true)
  122. )
  123. })
  124. it('should still flush project history', function () {
  125. return this.HistoryManager.flushProjectChanges
  126. .calledWith(this.project_id, {})
  127. .should.equal(true)
  128. })
  129. it('should record the error', function () {
  130. return this.logger.error
  131. .calledWith(
  132. { err: this.error, projectId: this.project_id, docId: 'doc-id-1' },
  133. 'error deleting doc'
  134. )
  135. .should.equal(true)
  136. })
  137. it('should call the callback with an error', function () {
  138. return this.callback
  139. .calledWith(sinon.match.instanceOf(Error))
  140. .should.equal(true)
  141. })
  142. return it('should time the execution', function () {
  143. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  144. })
  145. })
  146. })