HistoryControllerTests.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const sinon = require('sinon')
  13. const { expect } = require('chai')
  14. const Errors = require('../../../../app/src/Features/Errors/Errors')
  15. const modulePath = '../../../../app/src/Features/History/HistoryController'
  16. const SandboxedModule = require('sandboxed-module')
  17. const { ObjectId } = require('mongodb-legacy')
  18. describe('HistoryController', function () {
  19. beforeEach(function () {
  20. this.callback = sinon.stub()
  21. this.user_id = 'user-id-123'
  22. this.SessionManager = {
  23. getLoggedInUserId: sinon.stub().returns(this.user_id),
  24. }
  25. this.Stream = {
  26. pipeline: sinon.stub(),
  27. }
  28. this.HistoryController = SandboxedModule.require(modulePath, {
  29. requires: {
  30. request: (this.request = sinon.stub()),
  31. '@overleaf/settings': (this.settings = {}),
  32. '@overleaf/fetch-utils': {},
  33. '@overleaf/Metrics': {},
  34. '../../infrastructure/mongodb': { ObjectId },
  35. stream: this.Stream,
  36. '../Authentication/SessionManager': this.SessionManager,
  37. './HistoryManager': (this.HistoryManager = {}),
  38. '../Project/ProjectDetailsHandler': (this.ProjectDetailsHandler = {}),
  39. '../Project/ProjectEntityUpdateHandler':
  40. (this.ProjectEntityUpdateHandler = {}),
  41. '../User/UserGetter': (this.UserGetter = {}),
  42. '../Project/ProjectGetter': (this.ProjectGetter = {}),
  43. './RestoreManager': (this.RestoreManager = {}),
  44. '../../infrastructure/Features': (this.Features = sinon
  45. .stub()
  46. .withArgs('saas')
  47. .returns(true)),
  48. },
  49. })
  50. return (this.settings.apis = {
  51. project_history: {
  52. url: 'http://project_history.example.com',
  53. },
  54. })
  55. })
  56. describe('proxyToHistoryApi', function () {
  57. beforeEach(function () {
  58. this.req = { url: '/mock/url', method: 'POST' }
  59. this.res = 'mock-res'
  60. this.next = sinon.stub()
  61. this.proxy = sinon.stub()
  62. this.request.returns(this.proxy)
  63. })
  64. describe('for a project with the project history flag', function () {
  65. beforeEach(function () {
  66. this.req.useProjectHistory = true
  67. return this.HistoryController.proxyToHistoryApi(
  68. this.req,
  69. this.res,
  70. this.next
  71. )
  72. })
  73. it('should get the user id', function () {
  74. return this.SessionManager.getLoggedInUserId
  75. .calledWith(this.req.session)
  76. .should.equal(true)
  77. })
  78. it('should call the project history api', function () {
  79. return this.request
  80. .calledWith({
  81. url: `${this.settings.apis.project_history.url}${this.req.url}`,
  82. method: this.req.method,
  83. headers: {
  84. 'X-User-Id': this.user_id,
  85. },
  86. })
  87. .should.equal(true)
  88. })
  89. it('should pipe the response to the client', function () {
  90. expect(this.Stream.pipeline).to.have.been.calledWith(
  91. this.proxy,
  92. this.res
  93. )
  94. })
  95. })
  96. describe('for a project without the project history flag', function () {
  97. beforeEach(function () {
  98. this.req.useProjectHistory = false
  99. return this.HistoryController.proxyToHistoryApi(
  100. this.req,
  101. this.res,
  102. this.next
  103. )
  104. })
  105. it('should get the user id', function () {
  106. return this.SessionManager.getLoggedInUserId
  107. .calledWith(this.req.session)
  108. .should.equal(true)
  109. })
  110. it('should pipe the response to the client', function () {
  111. expect(this.Stream.pipeline).to.have.been.calledWith(
  112. this.proxy,
  113. this.res
  114. )
  115. })
  116. })
  117. })
  118. describe('proxyToHistoryApiAndInjectUserDetails', function () {
  119. beforeEach(function () {
  120. this.req = { url: '/mock/url', method: 'POST' }
  121. this.res = { json: sinon.stub() }
  122. this.next = sinon.stub()
  123. this.request.yields(null, { statusCode: 200 }, (this.data = 'mock-data'))
  124. return (this.HistoryManager.injectUserDetails = sinon
  125. .stub()
  126. .yields(null, (this.data_with_users = 'mock-injected-data')))
  127. })
  128. describe('for a project with the project history flag', function () {
  129. beforeEach(function () {
  130. this.req.useProjectHistory = true
  131. return this.HistoryController.proxyToHistoryApiAndInjectUserDetails(
  132. this.req,
  133. this.res,
  134. this.next
  135. )
  136. })
  137. it('should get the user id', function () {
  138. return this.SessionManager.getLoggedInUserId
  139. .calledWith(this.req.session)
  140. .should.equal(true)
  141. })
  142. it('should call the project history api', function () {
  143. return this.request
  144. .calledWith({
  145. url: `${this.settings.apis.project_history.url}${this.req.url}`,
  146. method: this.req.method,
  147. json: true,
  148. headers: {
  149. 'X-User-Id': this.user_id,
  150. },
  151. })
  152. .should.equal(true)
  153. })
  154. it('should inject the user data', function () {
  155. return this.HistoryManager.injectUserDetails
  156. .calledWith(this.data)
  157. .should.equal(true)
  158. })
  159. it('should return the data with users to the client', function () {
  160. return this.res.json.calledWith(this.data_with_users).should.equal(true)
  161. })
  162. })
  163. describe('for a project without the project history flag', function () {
  164. beforeEach(function () {
  165. this.req.useProjectHistory = false
  166. return this.HistoryController.proxyToHistoryApiAndInjectUserDetails(
  167. this.req,
  168. this.res,
  169. this.next
  170. )
  171. })
  172. it('should get the user id', function () {
  173. return this.SessionManager.getLoggedInUserId
  174. .calledWith(this.req.session)
  175. .should.equal(true)
  176. })
  177. it('should inject the user data', function () {
  178. return this.HistoryManager.injectUserDetails
  179. .calledWith(this.data)
  180. .should.equal(true)
  181. })
  182. it('should return the data with users to the client', function () {
  183. return this.res.json.calledWith(this.data_with_users).should.equal(true)
  184. })
  185. })
  186. })
  187. describe('proxyToHistoryApiAndInjectUserDetails (with the history API failing)', function () {
  188. beforeEach(function () {
  189. this.req = { url: '/mock/url', method: 'POST', useProjectHistory: true }
  190. this.res = { json: sinon.stub() }
  191. this.next = sinon.stub()
  192. this.request.yields(null, { statusCode: 500 }, (this.data = 'mock-data'))
  193. this.HistoryManager.injectUserDetails = sinon
  194. .stub()
  195. .yields(null, (this.data_with_users = 'mock-injected-data'))
  196. return this.HistoryController.proxyToHistoryApiAndInjectUserDetails(
  197. this.req,
  198. this.res,
  199. this.next
  200. )
  201. })
  202. it('should not inject the user data', function () {
  203. return this.HistoryManager.injectUserDetails
  204. .calledWith(this.data)
  205. .should.equal(false)
  206. })
  207. it('should not return the data with users to the client', function () {
  208. return this.res.json.calledWith(this.data_with_users).should.equal(false)
  209. })
  210. })
  211. describe('resyncProjectHistory', function () {
  212. describe('for a project without project-history enabled', function () {
  213. beforeEach(function () {
  214. this.project_id = 'mock-project-id'
  215. this.req = { params: { Project_id: this.project_id }, body: {} }
  216. this.res = { setTimeout: sinon.stub(), sendStatus: sinon.stub() }
  217. this.next = sinon.stub()
  218. this.error = new Errors.ProjectHistoryDisabledError()
  219. this.ProjectEntityUpdateHandler.resyncProjectHistory = sinon
  220. .stub()
  221. .yields(this.error)
  222. return this.HistoryController.resyncProjectHistory(
  223. this.req,
  224. this.res,
  225. this.next
  226. )
  227. })
  228. it('response with a 404', function () {
  229. return this.res.sendStatus.calledWith(404).should.equal(true)
  230. })
  231. })
  232. describe('for a project with project-history enabled', function () {
  233. beforeEach(function () {
  234. this.project_id = 'mock-project-id'
  235. this.req = { params: { Project_id: this.project_id }, body: {} }
  236. this.res = { setTimeout: sinon.stub(), sendStatus: sinon.stub() }
  237. this.next = sinon.stub()
  238. this.ProjectEntityUpdateHandler.resyncProjectHistory = sinon
  239. .stub()
  240. .yields()
  241. return this.HistoryController.resyncProjectHistory(
  242. this.req,
  243. this.res,
  244. this.next
  245. )
  246. })
  247. it('sets an extended response timeout', function () {
  248. this.res.setTimeout.should.have.been.calledWith(6 * 60 * 1000)
  249. })
  250. it('resyncs the project', function () {
  251. return this.ProjectEntityUpdateHandler.resyncProjectHistory
  252. .calledWith(this.project_id)
  253. .should.equal(true)
  254. })
  255. it('responds with a 204', function () {
  256. return this.res.sendStatus.calledWith(204).should.equal(true)
  257. })
  258. })
  259. })
  260. })