getProjectDocsTests.coffee 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/ProjectManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. Errors = require "../../../../app/js/Errors.js"
  7. describe "ProjectManager - getProjectDocsAndFlushIfOld", ->
  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. @callback = sinon.stub()
  20. @doc_versions = [111, 222, 333]
  21. describe "successfully", ->
  22. beforeEach (done) ->
  23. @doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"]
  24. @doc_lines = [["aaa","aaa"],["bbb","bbb"],["ccc","ccc"]]
  25. @docs = [
  26. {_id: @doc_ids[0], lines: @doc_lines[0], v: @doc_versions[0]}
  27. {_id: @doc_ids[1], lines: @doc_lines[1], v: @doc_versions[1]}
  28. {_id: @doc_ids[2], lines: @doc_lines[2], v: @doc_versions[2]}
  29. ]
  30. @RedisManager.checkOrSetProjectState = sinon.stub().callsArgWith(2, null)
  31. @RedisManager.getDocIdsInProject = sinon.stub().callsArgWith(1, null, @doc_ids)
  32. @DocumentManager.getDocAndFlushIfOldWithLock = sinon.stub()
  33. @DocumentManager.getDocAndFlushIfOldWithLock.withArgs(@project_id, @doc_ids[0])
  34. .callsArgWith(2, null, @doc_lines[0], @doc_versions[0])
  35. @DocumentManager.getDocAndFlushIfOldWithLock.withArgs(@project_id, @doc_ids[1])
  36. .callsArgWith(2, null, @doc_lines[1], @doc_versions[1])
  37. @DocumentManager.getDocAndFlushIfOldWithLock.withArgs(@project_id, @doc_ids[2])
  38. .callsArgWith(2, null, @doc_lines[2], @doc_versions[2])
  39. @ProjectManager.getProjectDocsAndFlushIfOld @project_id, @projectStateHash, @excludeVersions, (error, docs) =>
  40. @callback(error, docs)
  41. done()
  42. it "should check the project state", ->
  43. @RedisManager.checkOrSetProjectState
  44. .calledWith(@project_id, @projectStateHash)
  45. .should.equal true
  46. it "should get the doc ids in the project", ->
  47. @RedisManager.getDocIdsInProject
  48. .calledWith(@project_id)
  49. .should.equal true
  50. it "should call the callback without error", ->
  51. @callback.calledWith(null, @docs).should.equal true
  52. it "should time the execution", ->
  53. @Metrics.Timer::done.called.should.equal true
  54. describe "when the state does not match", ->
  55. beforeEach (done) ->
  56. @doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"]
  57. @RedisManager.checkOrSetProjectState = sinon.stub().callsArgWith(2, null, true)
  58. @ProjectManager.getProjectDocsAndFlushIfOld @project_id, @projectStateHash, @excludeVersions, (error, docs) =>
  59. @callback(error, docs)
  60. done()
  61. it "should check the project state", ->
  62. @RedisManager.checkOrSetProjectState
  63. .calledWith(@project_id, @projectStateHash)
  64. .should.equal true
  65. it "should call the callback with an error", ->
  66. @callback.calledWith(new Errors.ProjectStateChangedError("project state changed")).should.equal true
  67. it "should time the execution", ->
  68. @Metrics.Timer::done.called.should.equal true
  69. describe "when a doc errors", ->
  70. beforeEach (done) ->
  71. @doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"]
  72. @RedisManager.checkOrSetProjectState = sinon.stub().callsArgWith(2, null)
  73. @RedisManager.getDocIdsInProject = sinon.stub().callsArgWith(1, null, @doc_ids)
  74. @DocumentManager.getDocAndFlushIfOldWithLock = sinon.stub()
  75. @DocumentManager.getDocAndFlushIfOldWithLock.withArgs(@project_id, "doc-id-1")
  76. .callsArgWith(2, null, ["test doc content"], @doc_versions[1])
  77. @DocumentManager.getDocAndFlushIfOldWithLock.withArgs(@project_id, "doc-id-2")
  78. .callsArgWith(2, @error = new Error("oops")) # trigger an error
  79. @ProjectManager.getProjectDocsAndFlushIfOld @project_id, @projectStateHash, @excludeVersions, (error, docs) =>
  80. @callback(error)
  81. done()
  82. it "should record the error", ->
  83. @logger.error
  84. .calledWith(err: @error, project_id: @project_id, doc_id: "doc-id-2", "error getting project doc lines in getProjectDocsAndFlushIfOld")
  85. .should.equal true
  86. it "should call the callback with an error", ->
  87. @callback.calledWith(new Error("oops")).should.equal true
  88. it "should time the execution", ->
  89. @Metrics.Timer::done.called.should.equal true
  90. describe "clearing the project state with clearProjectState", ->
  91. beforeEach (done) ->
  92. @RedisManager.clearProjectState = sinon.stub().callsArg(1)
  93. @ProjectManager.clearProjectState @project_id, (error) =>
  94. @callback(error)
  95. done()
  96. it "should clear the project state", ->
  97. @RedisManager.clearProjectState
  98. .calledWith(@project_id)
  99. .should.equal true
  100. it "should call the callback", ->
  101. @callback.called.should.equal true