getDocTests.coffee 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. @res =
  23. send: sinon.stub()
  24. @req =
  25. params:
  26. project_id: @project_id
  27. doc_id: @doc_id
  28. @next = sinon.stub()
  29. describe "when the document exists and no recent ops are requested", ->
  30. beforeEach ->
  31. @DocumentManager.getDocAndRecentOpsWithLock = sinon.stub().callsArgWith(3, null, @lines, @version, [])
  32. @HttpController.getDoc(@req, @res, @next)
  33. it "should get the doc", ->
  34. @DocumentManager.getDocAndRecentOpsWithLock
  35. .calledWith(@project_id, @doc_id, -1)
  36. .should.equal true
  37. it "should return the doc as JSON", ->
  38. @res.send
  39. .calledWith(JSON.stringify({
  40. id: @doc_id
  41. lines: @lines
  42. version: @version
  43. ops: []
  44. }))
  45. .should.equal true
  46. it "should log the request", ->
  47. @logger.log
  48. .calledWith(doc_id: @doc_id, project_id: @project_id, "getting doc via http")
  49. .should.equal true
  50. it "should time the request", ->
  51. @Metrics.Timer::done.called.should.equal true
  52. describe "when recent ops are requested", ->
  53. beforeEach ->
  54. @DocumentManager.getDocAndRecentOpsWithLock = sinon.stub().callsArgWith(3, null, @lines, @version, @ops)
  55. @req.query = fromVersion: "#{@fromVersion}"
  56. @HttpController.getDoc(@req, @res, @next)
  57. it "should get the doc", ->
  58. @DocumentManager.getDocAndRecentOpsWithLock
  59. .calledWith(@project_id, @doc_id, @fromVersion)
  60. .should.equal true
  61. it "should return the doc as JSON", ->
  62. @res.send
  63. .calledWith(JSON.stringify({
  64. id: @doc_id
  65. lines: @lines
  66. version: @version
  67. ops: @ops
  68. }))
  69. .should.equal true
  70. it "should log the request", ->
  71. @logger.log
  72. .calledWith(doc_id: @doc_id, project_id: @project_id, "getting doc via http")
  73. .should.equal true
  74. it "should time the request", ->
  75. @Metrics.Timer::done.called.should.equal true
  76. describe "when the document does not exist", ->
  77. beforeEach ->
  78. @DocumentManager.getDocAndRecentOpsWithLock = sinon.stub().callsArgWith(3, null, null, null)
  79. @HttpController.getDoc(@req, @res, @next)
  80. it "should call next with NotFoundError", ->
  81. @next
  82. .calledWith(new Errors.NotFoundError("not found"))
  83. .should.equal true
  84. describe "when an errors occurs", ->
  85. beforeEach ->
  86. @DocumentManager.getDocAndRecentOpsWithLock = sinon.stub().callsArgWith(3, new Error("oops"), null, null)
  87. @HttpController.getDoc(@req, @res, @next)
  88. it "should call next with the error", ->
  89. @next
  90. .calledWith(new Error("oops"))
  91. .should.equal true