getDocTests.coffee 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/HttpController.js"
  5. SandboxedModule = require('sandboxed-module')
  6. Errors = require "../../../../app/js/Errors.js"
  7. describe "HttpController.getDoc", ->
  8. beforeEach ->
  9. @HttpController = SandboxedModule.require modulePath, requires:
  10. "./DocumentManager": @DocumentManager = {}
  11. "./ProjectManager": {}
  12. "logger-sharelatex" : @logger = { log: sinon.stub() }
  13. "./Metrics": @Metrics = {}
  14. @Metrics.Timer = class Timer
  15. done: sinon.stub()
  16. @project_id = "project-id-123"
  17. @doc_id = "doc-id-123"
  18. @lines = ["one", "two", "three"]
  19. @ops = ["mock-op-1", "mock-op-2"]
  20. @version = 42
  21. @fromVersion = 42
  22. @ranges = { changes: "mock", comments: "mock" }
  23. @res =
  24. send: sinon.stub()
  25. @req =
  26. params:
  27. project_id: @project_id
  28. doc_id: @doc_id
  29. @next = sinon.stub()
  30. describe "when the document exists and no recent ops are requested", ->
  31. beforeEach ->
  32. @DocumentManager.getDocAndRecentOpsWithLock = sinon.stub().callsArgWith(3, null, @lines, @version, [], @ranges)
  33. @HttpController.getDoc(@req, @res, @next)
  34. it "should get the doc", ->
  35. @DocumentManager.getDocAndRecentOpsWithLock
  36. .calledWith(@project_id, @doc_id, -1)
  37. .should.equal true
  38. it "should return the doc as JSON", ->
  39. @res.send
  40. .calledWith(JSON.stringify({
  41. id: @doc_id
  42. lines: @lines
  43. version: @version
  44. ops: []
  45. ranges: @ranges
  46. }))
  47. .should.equal true
  48. it "should log the request", ->
  49. @logger.log
  50. .calledWith(doc_id: @doc_id, project_id: @project_id, "getting doc via http")
  51. .should.equal true
  52. it "should time the request", ->
  53. @Metrics.Timer::done.called.should.equal true
  54. describe "when recent ops are requested", ->
  55. beforeEach ->
  56. @DocumentManager.getDocAndRecentOpsWithLock = sinon.stub().callsArgWith(3, null, @lines, @version, @ops)
  57. @req.query = fromVersion: "#{@fromVersion}"
  58. @HttpController.getDoc(@req, @res, @next)
  59. it "should get the doc", ->
  60. @DocumentManager.getDocAndRecentOpsWithLock
  61. .calledWith(@project_id, @doc_id, @fromVersion)
  62. .should.equal true
  63. it "should return the doc as JSON", ->
  64. @res.send
  65. .calledWith(JSON.stringify({
  66. id: @doc_id
  67. lines: @lines
  68. version: @version
  69. ops: @ops
  70. }))
  71. .should.equal true
  72. it "should log the request", ->
  73. @logger.log
  74. .calledWith(doc_id: @doc_id, project_id: @project_id, "getting doc via http")
  75. .should.equal true
  76. it "should time the request", ->
  77. @Metrics.Timer::done.called.should.equal true
  78. describe "when the document does not exist", ->
  79. beforeEach ->
  80. @DocumentManager.getDocAndRecentOpsWithLock = sinon.stub().callsArgWith(3, null, null, null)
  81. @HttpController.getDoc(@req, @res, @next)
  82. it "should call next with NotFoundError", ->
  83. @next
  84. .calledWith(new Errors.NotFoundError("not found"))
  85. .should.equal true
  86. describe "when an errors occurs", ->
  87. beforeEach ->
  88. @DocumentManager.getDocAndRecentOpsWithLock = sinon.stub().callsArgWith(3, new Error("oops"), null, null)
  89. @HttpController.getDoc(@req, @res, @next)
  90. it "should call next with the error", ->
  91. @next
  92. .calledWith(new Error("oops"))
  93. .should.equal true