HistoryControllerTests.coffee 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. chai = require('chai')
  2. chai.should()
  3. sinon = require("sinon")
  4. modulePath = "../../../../app/js/Features/History/HistoryController"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "HistoryController", ->
  7. beforeEach ->
  8. @user_id = "user-id-123"
  9. @AuthenticationController =
  10. getLoggedInUserId: sinon.stub().returns(@user_id)
  11. @HistoryController = SandboxedModule.require modulePath, requires:
  12. "request" : @request = sinon.stub()
  13. "settings-sharelatex": @settings = {}
  14. "logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub()}
  15. "../Authentication/AuthenticationController": @AuthenticationController
  16. describe "proxyToHistoryApi", ->
  17. beforeEach ->
  18. @req = { url: "/mock/url", method: "POST" }
  19. @res = "mock-res"
  20. @next = sinon.stub()
  21. @settings.apis =
  22. trackchanges:
  23. enabled: false
  24. url: "http://trackchanges.example.com"
  25. project_history:
  26. url: "http://project_history.example.com"
  27. @proxy =
  28. events: {}
  29. pipe: sinon.stub()
  30. on: (event, handler) -> @events[event] = handler
  31. @request.returns @proxy
  32. describe "successfully", ->
  33. describe "with project history enabled", ->
  34. beforeEach ->
  35. @settings.apis.project_history.enabled = true
  36. @HistoryController.proxyToHistoryApi @req, @res, @next
  37. it "should get the user id", ->
  38. @AuthenticationController.getLoggedInUserId
  39. .calledWith(@req)
  40. .should.equal true
  41. it "should call the project history api", ->
  42. @request
  43. .calledWith({
  44. url: "#{@settings.apis.project_history.url}#{@req.url}"
  45. method: @req.method
  46. headers:
  47. "X-User-Id": @user_id
  48. })
  49. .should.equal true
  50. it "should pipe the response to the client", ->
  51. @proxy.pipe
  52. .calledWith(@res)
  53. .should.equal true
  54. describe "with project history disabled", ->
  55. beforeEach ->
  56. @settings.apis.project_history.enabled = false
  57. @HistoryController.proxyToHistoryApi @req, @res, @next
  58. it "should call the track changes api", ->
  59. @request
  60. .calledWith({
  61. url: "#{@settings.apis.trackchanges.url}#{@req.url}"
  62. method: @req.method
  63. headers:
  64. "X-User-Id": @user_id
  65. })
  66. .should.equal true
  67. describe "with an error", ->
  68. beforeEach ->
  69. @HistoryController.proxyToHistoryApi @req, @res, @next
  70. @proxy.events["error"].call(@proxy, @error = new Error("oops"))
  71. it "should pass the error up the call chain", ->
  72. @next.calledWith(@error).should.equal true