updateProjectTests.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/ProjectManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. _ = require('lodash')
  7. describe "ProjectManager", ->
  8. beforeEach ->
  9. @ProjectManager = SandboxedModule.require modulePath, requires:
  10. "./RedisManager": @RedisManager = {}
  11. "./ProjectHistoryRedisManager": @ProjectHistoryRedisManager = {}
  12. "./DocumentManager": @DocumentManager = {}
  13. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  14. "./HistoryManager": @HistoryManager = {}
  15. "./Metrics": @Metrics =
  16. Timer: class Timer
  17. done: sinon.stub()
  18. @project_id = "project-id-123"
  19. @projectHistoryId = 'history-id-123'
  20. @user_id = "user-id-123"
  21. @version = 1234567
  22. @HistoryManager.shouldFlushHistoryOps = sinon.stub().returns(false)
  23. @HistoryManager.flushProjectChangesAsync = sinon.stub()
  24. @callback = sinon.stub()
  25. describe "updateProjectWithLocks", ->
  26. describe "rename operations", ->
  27. beforeEach ->
  28. @firstDocUpdate =
  29. id: 1
  30. pathname: 'foo'
  31. newPathname: 'foo'
  32. @secondDocUpdate =
  33. id: 2
  34. pathname: 'bar'
  35. newPathname: 'bar2'
  36. @docUpdates = [ @firstDocUpdate, @secondDocUpdate ]
  37. @firstFileUpdate =
  38. id: 2
  39. pathname: 'bar'
  40. newPathname: 'bar2'
  41. @fileUpdates = [ @firstFileUpdate ]
  42. @DocumentManager.renameDocWithLock = sinon.stub().yields()
  43. @ProjectHistoryRedisManager.queueRenameEntity = sinon.stub().yields()
  44. describe "successfully", ->
  45. beforeEach ->
  46. @ProjectManager.updateProjectWithLocks @project_id, @projectHistoryId, @user_id, @docUpdates, @fileUpdates, @version, @callback
  47. it "should rename the docs in the updates", ->
  48. firstDocUpdateWithVersion = _.extend({}, @firstDocUpdate, {version: "#{@version}.0"})
  49. secondDocUpdateWithVersion = _.extend({}, @secondDocUpdate, {version: "#{@version}.1"})
  50. @DocumentManager.renameDocWithLock
  51. .calledWith(@project_id, @firstDocUpdate.id, @user_id, firstDocUpdateWithVersion, @projectHistoryId)
  52. .should.equal true
  53. @DocumentManager.renameDocWithLock
  54. .calledWith(@project_id, @secondDocUpdate.id, @user_id, secondDocUpdateWithVersion, @projectHistoryId)
  55. .should.equal true
  56. it "should rename the files in the updates", ->
  57. firstFileUpdateWithVersion = _.extend({}, @firstFileUpdate, {version: "#{@version}.2"})
  58. @ProjectHistoryRedisManager.queueRenameEntity
  59. .calledWith(@project_id, @projectHistoryId, 'file', @firstFileUpdate.id, @user_id, firstFileUpdateWithVersion)
  60. .should.equal true
  61. it "should not flush the history", ->
  62. @HistoryManager.flushProjectChangesAsync
  63. .calledWith(@project_id)
  64. .should.equal false
  65. it "should call the callback", ->
  66. @callback.called.should.equal true
  67. describe "when renaming a doc fails", ->
  68. beforeEach ->
  69. @error = new Error('error')
  70. @DocumentManager.renameDocWithLock = sinon.stub().yields(@error)
  71. @ProjectManager.updateProjectWithLocks @project_id, @projectHistoryId, @user_id, @docUpdates, @fileUpdates, @version, @callback
  72. it "should call the callback with the error", ->
  73. @callback.calledWith(@error).should.equal true
  74. describe "when renaming a file fails", ->
  75. beforeEach ->
  76. @error = new Error('error')
  77. @ProjectHistoryRedisManager.queueRenameEntity = sinon.stub().yields(@error)
  78. @ProjectManager.updateProjectWithLocks @project_id, @projectHistoryId, @user_id, @docUpdates, @fileUpdates, @version, @callback
  79. it "should call the callback with the error", ->
  80. @callback.calledWith(@error).should.equal true
  81. describe "with enough ops to flush", ->
  82. beforeEach ->
  83. @HistoryManager.shouldFlushHistoryOps = sinon.stub().returns(true)
  84. @ProjectManager.updateProjectWithLocks @project_id, @projectHistoryId, @user_id, @docUpdates, @fileUpdates, @version, @callback
  85. it "should flush the history", ->
  86. @HistoryManager.flushProjectChangesAsync
  87. .calledWith(@project_id)
  88. .should.equal true
  89. describe "add operations", ->
  90. beforeEach ->
  91. @firstDocUpdate =
  92. id: 1
  93. docLines: "a\nb"
  94. @secondDocUpdate =
  95. id: 2
  96. docLines: "a\nb"
  97. @docUpdates = [ @firstDocUpdate, @secondDocUpdate ]
  98. @firstFileUpdate =
  99. id: 3
  100. url: 'filestore.example.com/2'
  101. @secondFileUpdate =
  102. id: 4
  103. url: 'filestore.example.com/3'
  104. @fileUpdates = [ @firstFileUpdate, @secondFileUpdate ]
  105. @ProjectHistoryRedisManager.queueAddEntity = sinon.stub().yields()
  106. describe "successfully", ->
  107. beforeEach ->
  108. @ProjectManager.updateProjectWithLocks @project_id, @projectHistoryId, @user_id, @docUpdates, @fileUpdates, @version, @callback
  109. it "should add the docs in the updates", ->
  110. firstDocUpdateWithVersion = _.extend({}, @firstDocUpdate, {version: "#{@version}.0"})
  111. secondDocUpdateWithVersion = _.extend({}, @secondDocUpdate, {version: "#{@version}.1"})
  112. @ProjectHistoryRedisManager.queueAddEntity.getCall(0)
  113. .calledWith(@project_id, @projectHistoryId, 'doc', @firstDocUpdate.id, @user_id, firstDocUpdateWithVersion)
  114. .should.equal true
  115. @ProjectHistoryRedisManager.queueAddEntity.getCall(1)
  116. .calledWith(@project_id, @projectHistoryId, 'doc', @secondDocUpdate.id, @user_id, secondDocUpdateWithVersion)
  117. .should.equal true
  118. it "should add the files in the updates", ->
  119. firstFileUpdateWithVersion = _.extend({}, @firstFileUpdate, {version: "#{@version}.2"})
  120. secondFileUpdateWithVersion = _.extend({}, @secondFileUpdate, {version: "#{@version}.3"})
  121. @ProjectHistoryRedisManager.queueAddEntity.getCall(2)
  122. .calledWith(@project_id, @projectHistoryId, 'file', @firstFileUpdate.id, @user_id, firstFileUpdateWithVersion)
  123. .should.equal true
  124. @ProjectHistoryRedisManager.queueAddEntity.getCall(3)
  125. .calledWith(@project_id, @projectHistoryId, 'file', @secondFileUpdate.id, @user_id, secondFileUpdateWithVersion)
  126. .should.equal true
  127. it "should not flush the history", ->
  128. @HistoryManager.flushProjectChangesAsync
  129. .calledWith(@project_id)
  130. .should.equal false
  131. it "should call the callback", ->
  132. @callback.called.should.equal true
  133. describe "when adding a doc fails", ->
  134. beforeEach ->
  135. @error = new Error('error')
  136. @ProjectHistoryRedisManager.queueAddEntity = sinon.stub().yields(@error)
  137. @ProjectManager.updateProjectWithLocks @project_id, @projectHistoryId, @user_id, @docUpdates, @fileUpdates, @version, @callback
  138. it "should call the callback with the error", ->
  139. @callback.calledWith(@error).should.equal true
  140. describe "when adding a file fails", ->
  141. beforeEach ->
  142. @error = new Error('error')
  143. @ProjectHistoryRedisManager.queueAddEntity = sinon.stub().yields(@error)
  144. @ProjectManager.updateProjectWithLocks @project_id, @projectHistoryId, @user_id, @docUpdates, @fileUpdates, @version, @callback
  145. it "should call the callback with the error", ->
  146. @callback.calledWith(@error).should.equal true
  147. describe "with enough ops to flush", ->
  148. beforeEach ->
  149. @HistoryManager.shouldFlushHistoryOps = sinon.stub().returns(true)
  150. @ProjectManager.updateProjectWithLocks @project_id, @projectHistoryId, @user_id, @docUpdates, @fileUpdates, @version, @callback
  151. it "should flush the history", ->
  152. @HistoryManager.flushProjectChangesAsync
  153. .calledWith(@project_id)
  154. .should.equal true