updateProjectTests.coffee 5.9 KB

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