DocumentControllerTests.coffee 2.9 KB

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