HistoryControllerTests.coffee 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. "./HistoryManager": @HistoryManager = {}
  17. "../Authentication/AuthenticationController": @AuthenticationController
  18. "../Project/ProjectDetailsHandler": @ProjectDetailsHandler = {}
  19. @settings.apis =
  20. trackchanges:
  21. enabled: false
  22. url: "http://trackchanges.example.com"
  23. project_history:
  24. url: "http://project_history.example.com"
  25. describe "selectHistoryApi", ->
  26. beforeEach ->
  27. @req = { url: "/mock/url", method: "POST" }
  28. @res = "mock-res"
  29. @next = sinon.stub()
  30. describe "for a project with project history", ->
  31. beforeEach ->
  32. @ProjectDetailsHandler.getDetails = sinon.stub().callsArgWith(1, null, {overleaf:{history:{id: 42, display:true}}})
  33. @HistoryController.selectHistoryApi @req, @res, @next
  34. it "should set the flag for project history to true", ->
  35. @req.useProjectHistory.should.equal true
  36. describe "for any other project ", ->
  37. beforeEach ->
  38. @ProjectDetailsHandler.getDetails = sinon.stub().callsArgWith(1, null, {})
  39. @HistoryController.selectHistoryApi @req, @res, @next
  40. it "should not set the flag for project history to false", ->
  41. @req.useProjectHistory.should.equal false
  42. describe "proxyToHistoryApi", ->
  43. beforeEach ->
  44. @req = { url: "/mock/url", method: "POST" }
  45. @res = "mock-res"
  46. @next = sinon.stub()
  47. @proxy =
  48. events: {}
  49. pipe: sinon.stub()
  50. on: (event, handler) -> @events[event] = handler
  51. @request.returns @proxy
  52. describe "for a project with the project history flag", ->
  53. beforeEach ->
  54. @req.useProjectHistory = true
  55. @HistoryController.proxyToHistoryApi @req, @res, @next
  56. it "should get the user id", ->
  57. @AuthenticationController.getLoggedInUserId
  58. .calledWith(@req)
  59. .should.equal true
  60. it "should call the project history api", ->
  61. @request
  62. .calledWith({
  63. url: "#{@settings.apis.project_history.url}#{@req.url}"
  64. method: @req.method
  65. headers:
  66. "X-User-Id": @user_id
  67. })
  68. .should.equal true
  69. it "should pipe the response to the client", ->
  70. @proxy.pipe
  71. .calledWith(@res)
  72. .should.equal true
  73. describe "for a project without the project history flag", ->
  74. beforeEach ->
  75. @req.useProjectHistory = false
  76. @HistoryController.proxyToHistoryApi @req, @res, @next
  77. it "should get the user id", ->
  78. @AuthenticationController.getLoggedInUserId
  79. .calledWith(@req)
  80. .should.equal true
  81. it "should call the track changes api", ->
  82. @request
  83. .calledWith({
  84. url: "#{@settings.apis.trackchanges.url}#{@req.url}"
  85. method: @req.method
  86. headers:
  87. "X-User-Id": @user_id
  88. })
  89. .should.equal true
  90. it "should pipe the response to the client", ->
  91. @proxy.pipe
  92. .calledWith(@res)
  93. .should.equal true
  94. describe "with an error", ->
  95. beforeEach ->
  96. @HistoryController.proxyToHistoryApi @req, @res, @next
  97. @proxy.events["error"].call(@proxy, @error = new Error("oops"))
  98. it "should pass the error up the call chain", ->
  99. @next.calledWith(@error).should.equal true
  100. describe "proxyToHistoryApiAndInjectUserDetails", ->
  101. beforeEach ->
  102. @req = { url: "/mock/url", method: "POST" }
  103. @res =
  104. json: sinon.stub()
  105. @next = sinon.stub()
  106. @request.yields(null, {statusCode: 200}, @data = "mock-data")
  107. @HistoryManager.injectUserDetails = sinon.stub().yields(null, @data_with_users = "mock-injected-data")
  108. describe "for a project with the project history flag", ->
  109. beforeEach ->
  110. @req.useProjectHistory = true
  111. @HistoryController.proxyToHistoryApiAndInjectUserDetails @req, @res, @next
  112. it "should get the user id", ->
  113. @AuthenticationController.getLoggedInUserId
  114. .calledWith(@req)
  115. .should.equal true
  116. it "should call the project history api", ->
  117. @request
  118. .calledWith({
  119. url: "#{@settings.apis.project_history.url}#{@req.url}"
  120. method: @req.method
  121. json: true
  122. headers:
  123. "X-User-Id": @user_id
  124. })
  125. .should.equal true
  126. it "should inject the user data", ->
  127. @HistoryManager.injectUserDetails
  128. .calledWith(@data)
  129. .should.equal true
  130. it "should return the data with users to the client", ->
  131. @res.json.calledWith(@data_with_users).should.equal true
  132. describe "for a project without the project history flag", ->
  133. beforeEach ->
  134. @req.useProjectHistory = false
  135. @HistoryController.proxyToHistoryApiAndInjectUserDetails @req, @res, @next
  136. it "should get the user id", ->
  137. @AuthenticationController.getLoggedInUserId
  138. .calledWith(@req)
  139. .should.equal true
  140. it "should call the track changes api", ->
  141. @request
  142. .calledWith({
  143. url: "#{@settings.apis.trackchanges.url}#{@req.url}"
  144. method: @req.method
  145. json: true
  146. headers:
  147. "X-User-Id": @user_id
  148. })
  149. .should.equal true
  150. it "should inject the user data", ->
  151. @HistoryManager.injectUserDetails
  152. .calledWith(@data)
  153. .should.equal true
  154. it "should return the data with users to the client", ->
  155. @res.json.calledWith(@data_with_users).should.equal true
  156. describe "proxyToHistoryApiAndInjectUserDetails (with the history API failing)", ->
  157. beforeEach ->
  158. @req = { url: "/mock/url", method: "POST", useProjectHistory: true }
  159. @res = { json: sinon.stub() }
  160. @next = sinon.stub()
  161. @request.yields(null, {statusCode: 500}, @data = "mock-data")
  162. @HistoryManager.injectUserDetails = sinon.stub().yields(null, @data_with_users = "mock-injected-data")
  163. @HistoryController.proxyToHistoryApiAndInjectUserDetails @req, @res, @next
  164. it "should not inject the user data", ->
  165. @HistoryManager.injectUserDetails
  166. .calledWith(@data)
  167. .should.equal false
  168. it "should not return the data with users to the client", ->
  169. @res.json.calledWith(@data_with_users).should.equal false