DocumentControllerTests.coffee 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Documents/DocumentController.js"
  6. SandboxedModule = require('sandboxed-module')
  7. events = require "events"
  8. MockRequest = require "../helpers/MockRequest"
  9. MockResponse = require "../helpers/MockResponse"
  10. Errors = require "../../../../app/js/Features/Errors/Errors"
  11. describe "DocumentController", ->
  12. beforeEach ->
  13. @DocumentController = SandboxedModule.require modulePath, requires:
  14. "logger-sharelatex":
  15. log:->
  16. err:->
  17. "../Project/ProjectGetter": @ProjectGetter = {}
  18. "../Project/ProjectLocator": @ProjectLocator = {}
  19. "../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
  20. "../Project/ProjectEntityUpdateHandler": @ProjectEntityUpdateHandler = {}
  21. @res = new MockResponse()
  22. @req = new MockRequest()
  23. @next = sinon.stub()
  24. @project_id = "project-id-123"
  25. @doc_id = "doc-id-123"
  26. @doc_lines = ["one", "two", "three"]
  27. @version = 42
  28. @ranges = {"mock": "ranges"}
  29. @pathname = '/a/b/c/file.tex'
  30. @rev = 5
  31. describe "getDocument", ->
  32. beforeEach ->
  33. @req.params =
  34. Project_id: @project_id
  35. doc_id: @doc_id
  36. describe "when the project exists without project history enabled", ->
  37. beforeEach ->
  38. @project = _id: @project_id
  39. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  40. describe "when the document exists", ->
  41. beforeEach ->
  42. @doc = _id: @doc_id
  43. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, fileSystem: @pathname)
  44. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @doc_lines, @rev, @version, @ranges)
  45. @DocumentController.getDocument(@req, @res, @next)
  46. it "should get the project", ->
  47. @ProjectGetter.getProject
  48. .calledWith(@project_id, rootFolder: true, overleaf: true)
  49. .should.equal true
  50. it "should get the pathname of the document", ->
  51. @ProjectLocator.findElement
  52. .calledWith({project: @project, element_id: @doc_id, type: 'doc'})
  53. .should.equal true
  54. it "should get the document content", ->
  55. @ProjectEntityHandler.getDoc
  56. .calledWith(@project_id, @doc_id)
  57. .should.equal true
  58. it "should return the document data to the client as JSON", ->
  59. @res.type.should.equal "json"
  60. @res.body.should.equal JSON.stringify
  61. lines: @doc_lines
  62. version: @version
  63. ranges: @ranges
  64. pathname: @pathname
  65. describe "when the document doesn't exist", ->
  66. beforeEach ->
  67. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, new Errors.NotFoundError("not found"))
  68. @DocumentController.getDocument(@req, @res, @next)
  69. it "should call next with the NotFoundError", ->
  70. @next.calledWith(new Errors.NotFoundError("not found"))
  71. .should.equal true
  72. describe "when project exists with project history enabled", ->
  73. beforeEach ->
  74. @doc = _id: @doc_id
  75. @projectHistoryId = 1234
  76. @project = _id: @project_id, overleaf: history: id: @projectHistoryId
  77. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  78. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, fileSystem: @pathname)
  79. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @doc_lines, @rev, @version, @ranges)
  80. @DocumentController.getDocument(@req, @res, @next)
  81. it "should return the history id to the client as JSON", ->
  82. @res.type.should.equal "json"
  83. @res.body.should.equal JSON.stringify
  84. lines: @doc_lines
  85. version: @version
  86. ranges: @ranges
  87. pathname: @pathname
  88. projectHistoryId: @projectHistoryId
  89. describe "when the project does not exist", ->
  90. beforeEach ->
  91. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, null)
  92. @DocumentController.getDocument(@req, @res, @next)
  93. it "returns a 404", ->
  94. @res.statusCode.should.equal 404
  95. describe "setDocument", ->
  96. beforeEach ->
  97. @req.params =
  98. Project_id: @project_id
  99. doc_id: @doc_id
  100. describe "when the document exists", ->
  101. beforeEach ->
  102. @ProjectEntityUpdateHandler.updateDocLines = sinon.stub().yields()
  103. @req.body =
  104. lines: @doc_lines
  105. version: @version
  106. ranges: @ranges
  107. @DocumentController.setDocument(@req, @res, @next)
  108. it "should update the document in Mongo", ->
  109. @ProjectEntityUpdateHandler.updateDocLines
  110. .calledWith(@project_id, @doc_id, @doc_lines, @version, @ranges)
  111. .should.equal true
  112. it "should return a successful response", ->
  113. @res.success.should.equal true
  114. describe "when the document doesn't exist", ->
  115. beforeEach ->
  116. @ProjectEntityUpdateHandler.updateDocLines = sinon.stub().yields(new Errors.NotFoundError("document does not exist"))
  117. @req.body =
  118. lines: @doc_lines
  119. @DocumentController.setDocument(@req, @res, @next)
  120. it "should call next with the NotFoundError", ->
  121. @next.calledWith(new Errors.NotFoundError("not found"))
  122. .should.equal true