DocumentControllerTests.coffee 3.0 KB

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