DocumentControllerTests.coffee 2.7 KB

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