DocumentControllerTests.coffee 2.8 KB

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