ProjectHistoryHandlerTests.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. globals: {
  46. console: console
  47. },
  48. requires: {
  49. 'logger-sharelatex': (this.logger = {
  50. log: sinon.stub(),
  51. error: sinon.stub(),
  52. err() {}
  53. }),
  54. 'settings-sharelatex': (this.Settings = {}),
  55. '../../models/Project': {
  56. Project: this.ProjectModel
  57. },
  58. './ProjectDetailsHandler': (this.ProjectDetailsHandler = {}),
  59. '../History/HistoryManager': (this.HistoryManager = {}),
  60. './ProjectEntityUpdateHandler': (this.ProjectEntityUpdateHandler = {})
  61. }
  62. }))
  63. })
  64. describe('starting history for an existing project', function() {
  65. beforeEach(function() {
  66. this.newHistoryId = 123456789
  67. this.HistoryManager.initializeProject = sinon
  68. .stub()
  69. .callsArgWith(0, null, { overleaf_id: this.newHistoryId })
  70. this.HistoryManager.flushProject = sinon.stub().callsArg(1)
  71. return (this.ProjectEntityUpdateHandler.resyncProjectHistory = sinon
  72. .stub()
  73. .callsArg(1))
  74. })
  75. describe('when the history does not already exist', function() {
  76. beforeEach(function() {
  77. this.ProjectDetailsHandler.getDetails = sinon
  78. .stub()
  79. .withArgs(project_id)
  80. .callsArgWith(1, null, this.project)
  81. this.ProjectModel.update = sinon.stub().callsArgWith(2, null, { n: 1 })
  82. return this.ProjectHistoryHandler.ensureHistoryExistsForProject(
  83. project_id,
  84. this.callback
  85. )
  86. })
  87. it('should get any existing history id for the project', function() {
  88. return this.ProjectDetailsHandler.getDetails
  89. .calledWith(project_id)
  90. .should.equal(true)
  91. })
  92. it('should initialize a new history in the v1 history service', function() {
  93. return this.HistoryManager.initializeProject.called.should.equal.true
  94. })
  95. it('should set the new history id on the project', function() {
  96. return this.ProjectModel.update
  97. .calledWith(
  98. { _id: project_id, 'overleaf.history.id': { $exists: false } },
  99. { 'overleaf.history.id': this.newHistoryId }
  100. )
  101. .should.equal(true)
  102. })
  103. it('should resync the project history', function() {
  104. return this.ProjectEntityUpdateHandler.resyncProjectHistory
  105. .calledWith(project_id)
  106. .should.equal(true)
  107. })
  108. it('should flush the project history', function() {
  109. return this.HistoryManager.flushProject
  110. .calledWith(project_id)
  111. .should.equal(true)
  112. })
  113. it('should call the callback without an error', function() {
  114. return this.callback.called.should.equal(true)
  115. })
  116. })
  117. describe('when the history already exists', function() {
  118. beforeEach(function() {
  119. this.project.overleaf = { history: { id: 1234 } }
  120. this.ProjectDetailsHandler.getDetails = sinon
  121. .stub()
  122. .withArgs(project_id)
  123. .callsArgWith(1, null, this.project)
  124. this.ProjectModel.update = sinon.stub()
  125. return this.ProjectHistoryHandler.ensureHistoryExistsForProject(
  126. project_id,
  127. this.callback
  128. )
  129. })
  130. it('should get any existing history id for the project', function() {
  131. return this.ProjectDetailsHandler.getDetails
  132. .calledWith(project_id)
  133. .should.equal(true)
  134. })
  135. it('should not initialize a new history in the v1 history service', function() {
  136. return this.HistoryManager.initializeProject.called.should.equal(false)
  137. })
  138. it('should not set the new history id on the project', function() {
  139. return this.ProjectModel.update.called.should.equal(false)
  140. })
  141. it('should not resync the project history', function() {
  142. return this.ProjectEntityUpdateHandler.resyncProjectHistory.called.should.equal(
  143. false
  144. )
  145. })
  146. it('should not flush the project history', function() {
  147. return this.HistoryManager.flushProject.called.should.equal(false)
  148. })
  149. it('should call the callback', function() {
  150. return this.callback.calledWith().should.equal(true)
  151. })
  152. })
  153. })
  154. })