getProjectDocsTests.coffee 4.6 KB

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