HistoryControllerTests.coffee 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. @callback = sinon.stub()
  9. @user_id = "user-id-123"
  10. @AuthenticationController =
  11. getLoggedInUserId: sinon.stub().returns(@user_id)
  12. @HistoryController = SandboxedModule.require modulePath, requires:
  13. "request" : @request = sinon.stub()
  14. "settings-sharelatex": @settings = {}
  15. "logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub()}
  16. "../Authentication/AuthenticationController": @AuthenticationController
  17. @settings.apis =
  18. trackchanges:
  19. enabled: false
  20. url: "http://trackchanges.example.com"
  21. project_history:
  22. url: "http://project_history.example.com"
  23. describe "proxyToHistoryApi", ->
  24. beforeEach ->
  25. @req = { url: "/mock/url", method: "POST" }
  26. @res = "mock-res"
  27. @next = sinon.stub()
  28. @proxy =
  29. events: {}
  30. pipe: sinon.stub()
  31. on: (event, handler) -> @events[event] = handler
  32. @request.returns @proxy
  33. describe "successfully", ->
  34. describe "with project history enabled", ->
  35. beforeEach ->
  36. @settings.apis.project_history.enabled = true
  37. @HistoryController.proxyToHistoryApi @req, @res, @next
  38. it "should get the user id", ->
  39. @AuthenticationController.getLoggedInUserId
  40. .calledWith(@req)
  41. .should.equal true
  42. it "should call the project history api", ->
  43. @request
  44. .calledWith({
  45. url: "#{@settings.apis.project_history.url}#{@req.url}"
  46. method: @req.method
  47. headers:
  48. "X-User-Id": @user_id
  49. })
  50. .should.equal true
  51. it "should pipe the response to the client", ->
  52. @proxy.pipe
  53. .calledWith(@res)
  54. .should.equal true
  55. describe "with project history disabled", ->
  56. beforeEach ->
  57. @settings.apis.project_history.enabled = false
  58. @HistoryController.proxyToHistoryApi @req, @res, @next
  59. it "should call the track changes api", ->
  60. @request
  61. .calledWith({
  62. url: "#{@settings.apis.trackchanges.url}#{@req.url}"
  63. method: @req.method
  64. headers:
  65. "X-User-Id": @user_id
  66. })
  67. .should.equal true
  68. describe "with an error", ->
  69. beforeEach ->
  70. @HistoryController.proxyToHistoryApi @req, @res, @next
  71. @proxy.events["error"].call(@proxy, @error = new Error("oops"))
  72. it "should pass the error up the call chain", ->
  73. @next.calledWith(@error).should.equal true
  74. describe "initializeProject", ->
  75. describe "with project history enabled", ->
  76. beforeEach ->
  77. @settings.apis.project_history.enabled = true
  78. describe "project history returns a successful response", ->
  79. beforeEach ->
  80. @overleaf_id = 1234
  81. @res = statusCode: 200
  82. @body = JSON.stringify(project: id: @overleaf_id)
  83. @request.post = sinon.stub().callsArgWith(1, null, @res, @body)
  84. @HistoryController.initializeProject @callback
  85. it "should call the project history api", ->
  86. @request.post.calledWith(
  87. url: "#{@settings.apis.project_history.url}/project"
  88. ).should.equal true
  89. it "should return the callback with the overleaf id", ->
  90. @callback.calledWithExactly(null, { @overleaf_id }).should.equal true
  91. describe "project history returns a response without the project id", ->
  92. beforeEach ->
  93. @res = statusCode: 200
  94. @body = JSON.stringify(project: {})
  95. @request.post = sinon.stub().callsArgWith(1, null, @res, @body)
  96. @HistoryController.initializeProject @callback
  97. it "should return the callback with an error", ->
  98. @callback
  99. .calledWith(sinon.match.has("message", "project-history did not provide an id"))
  100. .should.equal true
  101. describe "project history returns a unsuccessful response", ->
  102. beforeEach ->
  103. @res = statusCode: 404
  104. @request.post = sinon.stub().callsArgWith(1, null, @res)
  105. @HistoryController.initializeProject @callback
  106. it "should return the callback with an error", ->
  107. @callback
  108. .calledWith(sinon.match.has("message", "project-history returned a non-success status code: 404"))
  109. .should.equal true
  110. describe "project history errors", ->
  111. beforeEach ->
  112. @error = sinon.stub()
  113. @request.post = sinon.stub().callsArgWith(1, @error)
  114. @HistoryController.initializeProject @callback
  115. it "should return the callback with the error", ->
  116. @callback.calledWithExactly(@error).should.equal true
  117. describe "with project history disabled", ->
  118. beforeEach ->
  119. @settings.apis.project_history.enabled = false
  120. @HistoryController.initializeProject @callback
  121. it "should return the callback", ->
  122. @callback.calledWithExactly().should.equal true