DocumentControllerTests.coffee 3.1 KB

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