updateProjectTests.coffee 5.8 KB

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