DocumentControllerTests.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/ProjectLocator": @ProjectLocator = {}
  18. "../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
  19. "../Project/ProjectEntityUpdateHandler": @ProjectEntityUpdateHandler = {}
  20. @res = new MockResponse()
  21. @req = new MockRequest()
  22. @next = sinon.stub()
  23. @project_id = "project-id-123"
  24. @doc_id = "doc-id-123"
  25. @doc_lines = ["one", "two", "three"]
  26. @version = 42
  27. @ranges = {"mock": "ranges"}
  28. @pathname = '/a/b/c/file.tex'
  29. @rev = 5
  30. describe "getDocument", ->
  31. beforeEach ->
  32. @req.params =
  33. Project_id: @project_id
  34. doc_id: @doc_id
  35. describe "when the document exists", ->
  36. beforeEach ->
  37. @doc = _id: @doc_id
  38. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, fileSystem: @pathname)
  39. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @doc_lines, @rev, @version, @ranges)
  40. @DocumentController.getDocument(@req, @res, @next)
  41. it "should get the pathname of the document", ->
  42. @ProjectLocator.findElement
  43. .calledWith({project_id: @project_id, element_id: @doc_id, type: 'doc'})
  44. .should.equal true
  45. it "should get the document content", ->
  46. @ProjectEntityHandler.getDoc
  47. .calledWith(@project_id, @doc_id)
  48. .should.equal true
  49. it "should return the document data to the client as JSON", ->
  50. @res.type.should.equal "json"
  51. @res.body.should.equal JSON.stringify
  52. lines: @doc_lines
  53. version: @version
  54. ranges: @ranges
  55. pathname: @pathname
  56. describe "when the document doesn't exist", ->
  57. beforeEach ->
  58. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, new Errors.NotFoundError("not found"))
  59. @DocumentController.getDocument(@req, @res, @next)
  60. it "should call next with the NotFoundError", ->
  61. @next.calledWith(new Errors.NotFoundError("not found"))
  62. .should.equal true
  63. describe "setDocument", ->
  64. beforeEach ->
  65. @req.params =
  66. Project_id: @project_id
  67. doc_id: @doc_id
  68. describe "when the document exists", ->
  69. beforeEach ->
  70. @ProjectEntityUpdateHandler.updateDocLines = sinon.stub().yields()
  71. @req.body =
  72. lines: @doc_lines
  73. version: @version
  74. ranges: @ranges
  75. @DocumentController.setDocument(@req, @res, @next)
  76. it "should update the document in Mongo", ->
  77. @ProjectEntityUpdateHandler.updateDocLines
  78. .calledWith(@project_id, @doc_id, @doc_lines, @version, @ranges)
  79. .should.equal true
  80. it "should return a successful response", ->
  81. @res.success.should.equal true
  82. describe "when the document doesn't exist", ->
  83. beforeEach ->
  84. @ProjectEntityUpdateHandler.updateDocLines = sinon.stub().yields(new Errors.NotFoundError("document does not exist"))
  85. @req.body =
  86. lines: @doc_lines
  87. @DocumentController.setDocument(@req, @res, @next)
  88. it "should call next with the NotFoundError", ->
  89. @next.calledWith(new Errors.NotFoundError("not found"))
  90. .should.equal true