HistoryControllerTests.coffee 7.6 KB

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