updateProjectTests.coffee 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "./Metrics": @Metrics =
  13. Timer: class Timer
  14. done: sinon.stub()
  15. @project_id = "project-id-123"
  16. @user_id = "user-id-123"
  17. @callback = sinon.stub()
  18. describe "updateProjectWithLocks", ->
  19. describe "rename operations", ->
  20. beforeEach ->
  21. @firstDocUpdate =
  22. id: 1
  23. pathname: 'foo'
  24. newPathname: 'foo'
  25. @secondDocUpdate =
  26. id: 2
  27. pathname: 'bar'
  28. newPathname: 'bar2'
  29. @docUpdates = [ @firstDocUpdate, @secondDocUpdate ]
  30. @firstFileUpdate =
  31. id: 2
  32. pathname: 'bar'
  33. newPathname: 'bar2'
  34. @fileUpdates = [ @firstFileUpdate ]
  35. @DocumentManager.renameDocWithLock = sinon.stub().yields()
  36. @RedisManager.renameFile = sinon.stub().yields()
  37. describe "successfully", ->
  38. beforeEach ->
  39. @ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @callback
  40. it "should rename the docs in the updates", ->
  41. @DocumentManager.renameDocWithLock
  42. .calledWith(@project_id, @firstDocUpdate.id, @user_id, @firstDocUpdate)
  43. .should.equal true
  44. @DocumentManager.renameDocWithLock
  45. .calledWith(@project_id, @secondDocUpdate.id, @user_id, @secondDocUpdate)
  46. .should.equal true
  47. it "should rename the files in the updates", ->
  48. @RedisManager.renameFile
  49. .calledWith(@project_id, @firstFileUpdate.id, @user_id, @firstFileUpdate)
  50. .should.equal true
  51. it "should call the callback", ->
  52. @callback.called.should.equal true
  53. describe "when renaming a doc fails", ->
  54. beforeEach ->
  55. @error = new Error('error')
  56. @DocumentManager.renameDocWithLock = sinon.stub().yields(@error)
  57. @ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @callback
  58. it "should call the callback with the error", ->
  59. @callback.calledWith(@error).should.equal true
  60. describe "when renaming a file fails", ->
  61. beforeEach ->
  62. @error = new Error('error')
  63. @RedisManager.renameFile = 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 "add operations", ->
  68. beforeEach ->
  69. @firstDocUpdate =
  70. id: 1
  71. docLines: "a\nb"
  72. @secondDocUpdate =
  73. id: 2
  74. docLines: "a\nb"
  75. @docUpdates = [ @firstDocUpdate, @secondDocUpdate ]
  76. @firstFileUpdate =
  77. id: 2
  78. url: 'filestore.example.com/2'
  79. @fileUpdates = [ @firstFileUpdate ]
  80. @RedisManager.addEntity = sinon.stub().yields()
  81. describe "successfully", ->
  82. beforeEach ->
  83. @ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @callback
  84. it "should add the docs in the updates", ->
  85. @RedisManager.addEntity
  86. .calledWith(@project_id, 'doc', @firstDocUpdate.id, @user_id, @firstDocUpdate)
  87. .should.equal true
  88. @RedisManager.addEntity
  89. .calledWith(@project_id, 'doc', @secondDocUpdate.id, @user_id, @secondDocUpdate)
  90. .should.equal true
  91. it "should add the files in the updates", ->
  92. @RedisManager.addEntity
  93. .calledWith(@project_id, 'file', @firstFileUpdate.id, @user_id, @firstFileUpdate)
  94. .should.equal true
  95. it "should call the callback", ->
  96. @callback.called.should.equal true
  97. describe "when adding a doc fails", ->
  98. beforeEach ->
  99. @error = new Error('error')
  100. @RedisManager.addEntity = sinon.stub().yields(@error)
  101. @ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @callback
  102. it "should call the callback with the error", ->
  103. @callback.calledWith(@error).should.equal true
  104. describe "when adding a file fails", ->
  105. beforeEach ->
  106. @error = new Error('error')
  107. @RedisManager.addEntity = sinon.stub().yields(@error)
  108. @ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @callback
  109. it "should call the callback with the error", ->
  110. @callback.calledWith(@error).should.equal true