ProjectHistoryHandlerTests.js 5.5 KB

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