ProjectHistoryHandlerTests.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* eslint-disable
  2. camelcase,
  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. * DS206: Consider reworking classes to avoid initClass
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const chai = require('chai')
  16. const { assert } = require('chai')
  17. const should = chai.should()
  18. const { expect } = chai
  19. const sinon = require('sinon')
  20. const modulePath = '../../../../app/src/Features/Project/ProjectHistoryHandler'
  21. const SandboxedModule = require('sandboxed-module')
  22. describe('ProjectHistoryHandler', function() {
  23. const project_id = '4eecb1c1bffa66588e0000a1'
  24. const userId = 1234
  25. beforeEach(function() {
  26. let Project
  27. this.ProjectModel = Project = (function() {
  28. Project = class Project {
  29. static initClass() {
  30. this.prototype.rootFolder = [this.rootFolder]
  31. }
  32. constructor(options) {
  33. this._id = project_id
  34. this.name = 'project_name_here'
  35. this.rev = 0
  36. }
  37. }
  38. Project.initClass()
  39. return Project
  40. })()
  41. this.project = new this.ProjectModel()
  42. this.callback = sinon.stub()
  43. return (this.ProjectHistoryHandler = SandboxedModule.require(modulePath, {
  44. globals: {
  45. console: console
  46. },
  47. requires: {
  48. 'logger-sharelatex': (this.logger = {
  49. log: sinon.stub(),
  50. error: sinon.stub(),
  51. err() {}
  52. }),
  53. 'settings-sharelatex': (this.Settings = {}),
  54. '../../models/Project': {
  55. Project: this.ProjectModel
  56. },
  57. './ProjectDetailsHandler': (this.ProjectDetailsHandler = {}),
  58. '../History/HistoryManager': (this.HistoryManager = {}),
  59. './ProjectEntityUpdateHandler': (this.ProjectEntityUpdateHandler = {})
  60. }
  61. }))
  62. })
  63. describe('starting history for an existing project', function() {
  64. beforeEach(function() {
  65. this.newHistoryId = 123456789
  66. this.HistoryManager.initializeProject = sinon
  67. .stub()
  68. .callsArgWith(0, null, { overleaf_id: this.newHistoryId })
  69. this.HistoryManager.flushProject = sinon.stub().callsArg(1)
  70. return (this.ProjectEntityUpdateHandler.resyncProjectHistory = sinon
  71. .stub()
  72. .callsArg(1))
  73. })
  74. describe('when the history does not already exist', function() {
  75. beforeEach(function() {
  76. this.ProjectDetailsHandler.getDetails = sinon
  77. .stub()
  78. .withArgs(project_id)
  79. .callsArgWith(1, null, this.project)
  80. this.ProjectModel.updateOne = sinon
  81. .stub()
  82. .callsArgWith(2, null, { n: 1 })
  83. return this.ProjectHistoryHandler.ensureHistoryExistsForProject(
  84. project_id,
  85. this.callback
  86. )
  87. })
  88. it('should get any existing history id for the project', function() {
  89. return this.ProjectDetailsHandler.getDetails
  90. .calledWith(project_id)
  91. .should.equal(true)
  92. })
  93. it('should initialize a new history in the v1 history service', function() {
  94. return this.HistoryManager.initializeProject.called.should.equal(true)
  95. })
  96. it('should set the new history id on the project', function() {
  97. return this.ProjectModel.updateOne
  98. .calledWith(
  99. { _id: project_id, 'overleaf.history.id': { $exists: false } },
  100. { 'overleaf.history.id': this.newHistoryId }
  101. )
  102. .should.equal(true)
  103. })
  104. it('should resync the project history', function() {
  105. return this.ProjectEntityUpdateHandler.resyncProjectHistory
  106. .calledWith(project_id)
  107. .should.equal(true)
  108. })
  109. it('should flush the project history', function() {
  110. return this.HistoryManager.flushProject
  111. .calledWith(project_id)
  112. .should.equal(true)
  113. })
  114. it('should call the callback without an error', function() {
  115. return this.callback.called.should.equal(true)
  116. })
  117. })
  118. describe('when the history already exists', function() {
  119. beforeEach(function() {
  120. this.project.overleaf = { history: { id: 1234 } }
  121. this.ProjectDetailsHandler.getDetails = sinon
  122. .stub()
  123. .withArgs(project_id)
  124. .callsArgWith(1, null, this.project)
  125. this.ProjectModel.updateOne = sinon.stub()
  126. return this.ProjectHistoryHandler.ensureHistoryExistsForProject(
  127. project_id,
  128. this.callback
  129. )
  130. })
  131. it('should get any existing history id for the project', function() {
  132. return this.ProjectDetailsHandler.getDetails
  133. .calledWith(project_id)
  134. .should.equal(true)
  135. })
  136. it('should not initialize a new history in the v1 history service', function() {
  137. return this.HistoryManager.initializeProject.called.should.equal(false)
  138. })
  139. it('should not set the new history id on the project', function() {
  140. return this.ProjectModel.updateOne.called.should.equal(false)
  141. })
  142. it('should not resync the project history', function() {
  143. return this.ProjectEntityUpdateHandler.resyncProjectHistory.called.should.equal(
  144. false
  145. )
  146. })
  147. it('should not flush the project history', function() {
  148. return this.HistoryManager.flushProject.called.should.equal(false)
  149. })
  150. it('should call the callback', function() {
  151. return this.callback.calledWith().should.equal(true)
  152. })
  153. })
  154. })
  155. })