HistoryControllerTests.coffee 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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:{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 "with project history enabled", ->
  52. beforeEach ->
  53. @settings.apis.project_history.enabled = true
  54. describe "for a project with the project history flag", ->
  55. beforeEach ->
  56. @req.useProjectHistory = true
  57. @HistoryController.proxyToHistoryApi @req, @res, @next
  58. it "should get the user id", ->
  59. @AuthenticationController.getLoggedInUserId
  60. .calledWith(@req)
  61. .should.equal true
  62. it "should call the project history api", ->
  63. @request
  64. .calledWith({
  65. url: "#{@settings.apis.project_history.url}#{@req.url}"
  66. method: @req.method
  67. headers:
  68. "X-User-Id": @user_id
  69. })
  70. .should.equal true
  71. it "should pipe the response to the client", ->
  72. @proxy.pipe
  73. .calledWith(@res)
  74. .should.equal true
  75. describe "for a project without the project history flag", ->
  76. beforeEach ->
  77. @req.useProjectHistory = false
  78. @HistoryController.proxyToHistoryApi @req, @res, @next
  79. it "should get the user id", ->
  80. @AuthenticationController.getLoggedInUserId
  81. .calledWith(@req)
  82. .should.equal true
  83. it "should call the track changes api", ->
  84. @request
  85. .calledWith({
  86. url: "#{@settings.apis.trackchanges.url}#{@req.url}"
  87. method: @req.method
  88. headers:
  89. "X-User-Id": @user_id
  90. })
  91. .should.equal true
  92. it "should pipe the response to the client", ->
  93. @proxy.pipe
  94. .calledWith(@res)
  95. .should.equal true
  96. describe "with project history disabled", ->
  97. beforeEach ->
  98. @settings.apis.project_history.enabled = false
  99. describe "for a project with the project history flag", ->
  100. beforeEach ->
  101. @req.useProjectHistory = true
  102. @HistoryController.proxyToHistoryApi @req, @res, @next
  103. it "should call the track changes api", ->
  104. @request
  105. .calledWith({
  106. url: "#{@settings.apis.trackchanges.url}#{@req.url}"
  107. method: @req.method
  108. headers:
  109. "X-User-Id": @user_id
  110. })
  111. .should.equal true
  112. describe "for a project without the project history flag", ->
  113. beforeEach ->
  114. @req.useProjectHistory = false
  115. @HistoryController.proxyToHistoryApi @req, @res, @next
  116. it "should call the track changes api", ->
  117. @request
  118. .calledWith({
  119. url: "#{@settings.apis.trackchanges.url}#{@req.url}"
  120. method: @req.method
  121. headers:
  122. "X-User-Id": @user_id
  123. })
  124. .should.equal true
  125. describe "with an error", ->
  126. beforeEach ->
  127. @HistoryController.proxyToHistoryApi @req, @res, @next
  128. @proxy.events["error"].call(@proxy, @error = new Error("oops"))
  129. it "should pass the error up the call chain", ->
  130. @next.calledWith(@error).should.equal true
  131. describe "initializeProject", ->
  132. describe "with project history enabled", ->
  133. beforeEach ->
  134. @settings.apis.project_history.enabled = true
  135. describe "project history returns a successful response", ->
  136. beforeEach ->
  137. @overleaf_id = 1234
  138. @res = statusCode: 200
  139. @body = JSON.stringify(project: id: @overleaf_id)
  140. @request.post = sinon.stub().callsArgWith(1, null, @res, @body)
  141. @HistoryController.initializeProject @callback
  142. it "should call the project history api", ->
  143. @request.post.calledWith(
  144. url: "#{@settings.apis.project_history.url}/project"
  145. ).should.equal true
  146. it "should return the callback with the overleaf id", ->
  147. @callback.calledWithExactly(null, { @overleaf_id }).should.equal true
  148. describe "project history returns a response without the project id", ->
  149. beforeEach ->
  150. @res = statusCode: 200
  151. @body = JSON.stringify(project: {})
  152. @request.post = sinon.stub().callsArgWith(1, null, @res, @body)
  153. @HistoryController.initializeProject @callback
  154. it "should return the callback with an error", ->
  155. @callback
  156. .calledWith(sinon.match.has("message", "project-history did not provide an id"))
  157. .should.equal true
  158. describe "project history returns a unsuccessful response", ->
  159. beforeEach ->
  160. @res = statusCode: 404
  161. @request.post = sinon.stub().callsArgWith(1, null, @res)
  162. @HistoryController.initializeProject @callback
  163. it "should return the callback with an error", ->
  164. @callback
  165. .calledWith(sinon.match.has("message", "project-history returned a non-success status code: 404"))
  166. .should.equal true
  167. describe "project history errors", ->
  168. beforeEach ->
  169. @error = sinon.stub()
  170. @request.post = sinon.stub().callsArgWith(1, @error)
  171. @HistoryController.initializeProject @callback
  172. it "should return the callback with the error", ->
  173. @callback.calledWithExactly(@error).should.equal true
  174. describe "with project history disabled", ->
  175. beforeEach ->
  176. @settings.apis.project_history.enabled = false
  177. @HistoryController.initializeProject @callback
  178. it "should return the callback", ->
  179. @callback.calledWithExactly().should.equal true