HistoryControllerTests.coffee 7.5 KB

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