HistoryControllerTests.coffee 6.8 KB

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