HistoryControllerTests.coffee 5.7 KB

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