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