DocumentControllerTests.coffee 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. @lastUpdatedAt = (new Date()).getTime()
  31. @lastUpdatedBy = 'fake-last-updater-id'
  32. @rev = 5
  33. describe "getDocument", ->
  34. beforeEach ->
  35. @req.params =
  36. Project_id: @project_id
  37. doc_id: @doc_id
  38. describe "when the project exists without project history enabled", ->
  39. beforeEach ->
  40. @project = _id: @project_id
  41. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  42. describe "when the document exists", ->
  43. beforeEach ->
  44. @doc = _id: @doc_id
  45. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, fileSystem: @pathname)
  46. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @doc_lines, @rev, @version, @ranges)
  47. @DocumentController.getDocument(@req, @res, @next)
  48. it "should get the project", ->
  49. @ProjectGetter.getProject
  50. .calledWith(@project_id, rootFolder: true, overleaf: true)
  51. .should.equal true
  52. it "should get the pathname of the document", ->
  53. @ProjectLocator.findElement
  54. .calledWith({project: @project, element_id: @doc_id, type: 'doc'})
  55. .should.equal true
  56. it "should get the document content", ->
  57. @ProjectEntityHandler.getDoc
  58. .calledWith(@project_id, @doc_id)
  59. .should.equal true
  60. it "should return the document data to the client as JSON", ->
  61. @res.type.should.equal "application/json"
  62. @res.body.should.equal JSON.stringify
  63. lines: @doc_lines
  64. version: @version
  65. ranges: @ranges
  66. pathname: @pathname
  67. describe "when the document doesn't exist", ->
  68. beforeEach ->
  69. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, new Errors.NotFoundError("not found"))
  70. @DocumentController.getDocument(@req, @res, @next)
  71. it "should call next with the NotFoundError", ->
  72. @next.calledWith(new Errors.NotFoundError("not found"))
  73. .should.equal true
  74. describe "when project exists with project history enabled", ->
  75. beforeEach ->
  76. @doc = _id: @doc_id
  77. @projectHistoryId = 1234
  78. @project = _id: @project_id, overleaf: history: id: @projectHistoryId
  79. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  80. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, fileSystem: @pathname)
  81. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @doc_lines, @rev, @version, @ranges)
  82. @DocumentController.getDocument(@req, @res, @next)
  83. it "should return the history id to the client as JSON", ->
  84. @res.type.should.equal "application/json"
  85. @res.body.should.equal JSON.stringify
  86. lines: @doc_lines
  87. version: @version
  88. ranges: @ranges
  89. pathname: @pathname
  90. projectHistoryId: @projectHistoryId
  91. describe "when the project does not exist", ->
  92. beforeEach ->
  93. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, null)
  94. @DocumentController.getDocument(@req, @res, @next)
  95. it "returns a 404", ->
  96. @res.statusCode.should.equal 404
  97. describe "setDocument", ->
  98. beforeEach ->
  99. @req.params =
  100. Project_id: @project_id
  101. doc_id: @doc_id
  102. describe "when the document exists", ->
  103. beforeEach ->
  104. @ProjectEntityUpdateHandler.updateDocLines = sinon.stub().yields()
  105. @req.body =
  106. lines: @doc_lines
  107. version: @version
  108. ranges: @ranges
  109. lastUpdatedAt: @lastUpdatedAt
  110. lastUpdatedBy: @lastUpdatedBy
  111. @DocumentController.setDocument(@req, @res, @next)
  112. it "should update the document in Mongo", ->
  113. sinon.assert.calledWith(
  114. @ProjectEntityUpdateHandler.updateDocLines,
  115. @project_id,
  116. @doc_id,
  117. @doc_lines,
  118. @version,
  119. @ranges,
  120. @lastUpdatedAt,
  121. @lastUpdatedBy
  122. )
  123. it "should return a successful response", ->
  124. @res.success.should.equal true
  125. describe "when the document doesn't exist", ->
  126. beforeEach ->
  127. @ProjectEntityUpdateHandler.updateDocLines = sinon.stub().yields(new Errors.NotFoundError("document does not exist"))
  128. @req.body =
  129. lines: @doc_lines
  130. @DocumentController.setDocument(@req, @res, @next)
  131. it "should call next with the NotFoundError", ->
  132. @next.calledWith(new Errors.NotFoundError("not found"))
  133. .should.equal true