getProjectDocsTests.coffee 4.7 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 - getProjectDocs", ->
  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. @RedisManager.getDocVersion = sinon.stub()
  31. @RedisManager.getDocVersion.withArgs(@doc_ids[0]).callsArgWith(1, null, @doc_versions[0])
  32. @RedisManager.getDocVersion.withArgs(@doc_ids[1]).callsArgWith(1, null, @doc_versions[1])
  33. @RedisManager.getDocVersion.withArgs(@doc_ids[2]).callsArgWith(1, null, @doc_versions[2])
  34. @RedisManager.getDocLines = sinon.stub()
  35. @RedisManager.getDocLines.withArgs(@doc_ids[0]).callsArgWith(1, null, JSON.stringify(@doc_lines[0]))
  36. @RedisManager.getDocLines.withArgs(@doc_ids[1]).callsArgWith(1, null, JSON.stringify(@doc_lines[1]))
  37. @RedisManager.getDocLines.withArgs(@doc_ids[2]).callsArgWith(1, null, JSON.stringify(@doc_lines[2]))
  38. @ProjectManager.getProjectDocs @project_id, @projectStateHash, @excludeVersions, (error, docs) =>
  39. @callback(error, docs)
  40. done()
  41. it "should check the project state", ->
  42. @RedisManager.checkOrSetProjectState
  43. .calledWith(@project_id, @projectStateHash)
  44. .should.equal true
  45. it "should get the doc ids in the project", ->
  46. @RedisManager.getDocIdsInProject
  47. .calledWith(@project_id)
  48. .should.equal true
  49. it "should call the callback without error", ->
  50. @callback.calledWith(null, @docs).should.equal true
  51. it "should time the execution", ->
  52. @Metrics.Timer::done.called.should.equal true
  53. describe "when the state does not match", ->
  54. beforeEach (done) ->
  55. @doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"]
  56. @RedisManager.checkOrSetProjectState = sinon.stub().callsArgWith(2, null, true)
  57. @ProjectManager.getProjectDocs @project_id, @projectStateHash, @excludeVersions, (error, docs) =>
  58. @callback(error, docs)
  59. done()
  60. it "should check the project state", ->
  61. @RedisManager.checkOrSetProjectState
  62. .calledWith(@project_id, @projectStateHash)
  63. .should.equal true
  64. it "should call the callback with an error", ->
  65. @callback.calledWith(new Errors.ProjectStateChangedError("project state changed")).should.equal true
  66. it "should time the execution", ->
  67. @Metrics.Timer::done.called.should.equal true
  68. describe "when a doc errors", ->
  69. beforeEach (done) ->
  70. @doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"]
  71. @RedisManager.checkOrSetProjectState = sinon.stub().callsArgWith(2, null)
  72. @RedisManager.getDocIdsInProject = sinon.stub().callsArgWith(1, null, @doc_ids)
  73. @RedisManager.getDocVersion = sinon.stub().callsArgWith(1, null)
  74. @RedisManager.getDocLines = sinon.stub()
  75. @RedisManager.getDocLines.withArgs("doc-id-1").callsArgWith(1, null, JSON.stringify(["test doc content"]))
  76. @RedisManager.getDocLines.withArgs("doc-id-2").callsArgWith(1, @error = new Error("oops")) # trigger an error
  77. @ProjectManager.getProjectDocs @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")
  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