HttpControllerTests.coffee 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. chai = require('chai')
  4. chai.should()
  5. expect = chai.expect
  6. modulePath = require('path').join __dirname, '../../../app/js/HttpController'
  7. describe "HttpController", ->
  8. beforeEach ->
  9. @HttpController = SandboxedModule.require modulePath, requires:
  10. "./DocManager": @DocManager = {}
  11. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  12. @res = { send: sinon.stub(), json: sinon.stub() }
  13. @req = {}
  14. @next = sinon.stub()
  15. @project_id = "mock-project-id"
  16. @doc_id = "mock-doc-id"
  17. @doc = {
  18. _id: @doc_id
  19. lines: ["mock", "lines"]
  20. }
  21. describe "getDoc", ->
  22. beforeEach ->
  23. @req.params =
  24. project_id: @project_id
  25. doc_id: @doc_id
  26. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc)
  27. @HttpController.getDoc @req, @res, @next
  28. it "should get the document", ->
  29. @DocManager.getDoc
  30. .calledWith(@project_id, @doc_id)
  31. .should.equal true
  32. it "should return the doc as JSON", ->
  33. @res.json
  34. .calledWith(lines: @doc.lines)
  35. .should.equal true
  36. describe "updateDoc", ->
  37. beforeEach ->
  38. @req.params =
  39. project_id: @project_id
  40. doc_id: @doc_id
  41. describe "when the doc lines exist and were updated", ->
  42. beforeEach ->
  43. @req.body =
  44. lines: @lines = ["hello", "world"]
  45. @DocManager.updateDoc = sinon.stub().callsArgWith(3, null, true)
  46. @HttpController.updateDoc @req, @res, @next
  47. it "should update the document", ->
  48. @DocManager.updateDoc
  49. .calledWith(@project_id, @doc_id, @lines)
  50. .should.equal true
  51. it "should return a modified status", ->
  52. @res.json
  53. .calledWith(modified: true)
  54. .should.equal true
  55. describe "when the doc lines exist and were not updated", ->
  56. beforeEach ->
  57. @req.body =
  58. lines: @lines = ["hello", "world"]
  59. @DocManager.updateDoc = sinon.stub().callsArgWith(3, null, false)
  60. @HttpController.updateDoc @req, @res, @next
  61. it "should return a modified status", ->
  62. @res.json
  63. .calledWith(modified: false)
  64. .should.equal true
  65. describe "when the doc lines are not provided", ->
  66. beforeEach ->
  67. @req.body = {}
  68. @DocManager.updateDoc = sinon.stub().callsArgWith(3, null, false)
  69. @HttpController.updateDoc @req, @res, @next
  70. it "should not update the document", ->
  71. @DocManager.updateDoc.called.should.equal false
  72. it "should return a 400 (bad request) response", ->
  73. @res.send
  74. .calledWith(400)
  75. .should.equal true