HistoryControllerTests.coffee 7.5 KB

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