HistoryController.test.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. import { RequestFailedError } from '@overleaf/fetch-utils'
  4. import Errors from '../../../../app/src/Features/Errors/Errors.js'
  5. import mongodb from 'mongodb-legacy'
  6. const { ObjectId } = mongodb
  7. const modulePath = '../../../../app/src/Features/History/HistoryController.mjs'
  8. describe('HistoryController', function () {
  9. beforeEach(async function (ctx) {
  10. ctx.callback = sinon.stub()
  11. ctx.user_id = 'user-id-123'
  12. ctx.project_id = '000000000000000012345678'
  13. ctx.blobHash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  14. ctx.stream = sinon.stub()
  15. ctx.fetchResponse = {
  16. headers: {
  17. get: sinon.stub(),
  18. },
  19. }
  20. ctx.next = sinon.stub()
  21. ctx.SessionManager = {
  22. getLoggedInUserId: sinon.stub().returns(ctx.user_id),
  23. }
  24. ctx.Stream = {
  25. pipeline: sinon.stub().resolves(),
  26. }
  27. ctx.HistoryManager = {
  28. promises: {
  29. injectUserDetails: sinon.stub(),
  30. requestBlobWithProjectId: sinon.stub(),
  31. },
  32. }
  33. ctx.ProjectEntityUpdateHandler = {
  34. promises: {
  35. resyncProjectHistory: sinon.stub().resolves(),
  36. },
  37. }
  38. ctx.fetchJson = sinon.stub()
  39. ctx.fetchStream = sinon.stub().resolves(ctx.stream)
  40. ctx.fetchStreamWithResponse = sinon
  41. .stub()
  42. .resolves({ stream: ctx.stream, response: ctx.fetchResponse })
  43. ctx.fetchNothing = sinon.stub().resolves()
  44. vi.mock('../../../../app/src/Features/Errors/Errors.js', () =>
  45. vi.importActual('../../../../app/src/Features/Errors/Errors.js')
  46. )
  47. vi.doMock('stream/promises', () => ctx.Stream)
  48. vi.doMock('@overleaf/settings', () => ({
  49. default: (ctx.settings = {}),
  50. }))
  51. vi.doMock('@overleaf/fetch-utils', () => ({
  52. fetchJson: ctx.fetchJson,
  53. fetchStream: ctx.fetchStream,
  54. fetchStreamWithResponse: ctx.fetchStreamWithResponse,
  55. fetchNothing: ctx.fetchNothing,
  56. }))
  57. vi.doMock('@overleaf/Metrics', () => ({
  58. default: {},
  59. }))
  60. vi.doMock('../../../../app/src/infrastructure/mongodb.js', () => ({
  61. default: { ObjectId },
  62. }))
  63. vi.doMock(
  64. '../../../../app/src/Features/Authentication/SessionManager.js',
  65. () => ({
  66. default: ctx.SessionManager,
  67. })
  68. )
  69. vi.doMock('../../../../app/src/Features/History/HistoryManager.js', () => ({
  70. default: ctx.HistoryManager,
  71. }))
  72. vi.doMock(
  73. '../../../../app/src/Features/Project/ProjectDetailsHandler.js',
  74. () => ({
  75. default: (ctx.ProjectDetailsHandler = {}),
  76. })
  77. )
  78. vi.doMock(
  79. '../../../../app/src/Features/Project/ProjectEntityUpdateHandler.js',
  80. () => ({
  81. default: ctx.ProjectEntityUpdateHandler,
  82. })
  83. )
  84. vi.doMock('../../../../app/src/Features/User/UserGetter.js', () => ({
  85. default: (ctx.UserGetter = {}),
  86. }))
  87. vi.doMock('../../../../app/src/Features/Project/ProjectGetter.js', () => ({
  88. default: (ctx.ProjectGetter = {}),
  89. }))
  90. vi.doMock(
  91. '../../../../app/src/Features/History/RestoreManager.mjs',
  92. () => ({
  93. default: (ctx.RestoreManager = {}),
  94. })
  95. )
  96. vi.doMock('../../../../app/src/infrastructure/Features.js', () => ({
  97. default: (ctx.Features = sinon.stub().withArgs('saas').returns(true)),
  98. }))
  99. ctx.HistoryController = (await import(modulePath)).default
  100. ctx.settings.apis = {
  101. project_history: {
  102. url: 'http://project_history.example.com',
  103. },
  104. }
  105. })
  106. describe('proxyToHistoryApi', function () {
  107. beforeEach(async function (ctx) {
  108. ctx.req = { url: '/mock/url', method: 'POST', session: sinon.stub() }
  109. ctx.res = {
  110. set: sinon.stub(),
  111. }
  112. ctx.contentType = 'application/json'
  113. ctx.contentLength = 212
  114. ctx.fetchResponse.headers.get
  115. .withArgs('Content-Type')
  116. .returns(ctx.contentType)
  117. ctx.fetchResponse.headers.get
  118. .withArgs('Content-Length')
  119. .returns(ctx.contentLength)
  120. await ctx.HistoryController.proxyToHistoryApi(ctx.req, ctx.res, ctx.next)
  121. })
  122. it('should get the user id', function (ctx) {
  123. ctx.SessionManager.getLoggedInUserId.should.have.been.calledWith(
  124. ctx.req.session
  125. )
  126. })
  127. it('should call the project history api', function (ctx) {
  128. ctx.fetchStreamWithResponse.should.have.been.calledWith(
  129. `${ctx.settings.apis.project_history.url}${ctx.req.url}`,
  130. {
  131. method: ctx.req.method,
  132. headers: {
  133. 'X-User-Id': ctx.user_id,
  134. },
  135. }
  136. )
  137. })
  138. it('should pipe the response to the client', function (ctx) {
  139. expect(ctx.Stream.pipeline).to.have.been.calledWith(ctx.stream, ctx.res)
  140. })
  141. it('should propagate the appropriate headers', function (ctx) {
  142. expect(ctx.res.set).to.have.been.calledWith(
  143. 'Content-Type',
  144. ctx.contentType
  145. )
  146. expect(ctx.res.set).to.have.been.calledWith(
  147. 'Content-Length',
  148. ctx.contentLength
  149. )
  150. })
  151. })
  152. describe('proxyToHistoryApiAndInjectUserDetails', function () {
  153. beforeEach(async function (ctx) {
  154. ctx.req = { url: '/mock/url', method: 'POST' }
  155. ctx.res = { json: sinon.stub() }
  156. ctx.data = 'mock-data'
  157. ctx.dataWithUsers = 'mock-injected-data'
  158. ctx.fetchJson.resolves(ctx.data)
  159. ctx.HistoryManager.promises.injectUserDetails.resolves(ctx.dataWithUsers)
  160. await ctx.HistoryController.proxyToHistoryApiAndInjectUserDetails(
  161. ctx.req,
  162. ctx.res,
  163. ctx.next
  164. )
  165. })
  166. it('should get the user id', function (ctx) {
  167. ctx.SessionManager.getLoggedInUserId.should.have.been.calledWith(
  168. ctx.req.session
  169. )
  170. })
  171. it('should call the project history api', function (ctx) {
  172. ctx.fetchJson.should.have.been.calledWith(
  173. `${ctx.settings.apis.project_history.url}${ctx.req.url}`,
  174. {
  175. method: ctx.req.method,
  176. headers: {
  177. 'X-User-Id': ctx.user_id,
  178. },
  179. }
  180. )
  181. })
  182. it('should inject the user data', function (ctx) {
  183. ctx.HistoryManager.promises.injectUserDetails.should.have.been.calledWith(
  184. ctx.data
  185. )
  186. })
  187. it('should return the data with users to the client', function (ctx) {
  188. ctx.res.json.should.have.been.calledWith(ctx.dataWithUsers)
  189. })
  190. })
  191. describe('proxyToHistoryApiAndInjectUserDetails (with the history API failing)', function () {
  192. beforeEach(async function (ctx) {
  193. ctx.url = '/mock/url'
  194. ctx.req = { url: ctx.url, method: 'POST' }
  195. ctx.res = { json: sinon.stub() }
  196. ctx.err = new RequestFailedError(ctx.url, {}, { status: 500 })
  197. ctx.fetchJson.rejects(ctx.err)
  198. await ctx.HistoryController.proxyToHistoryApiAndInjectUserDetails(
  199. ctx.req,
  200. ctx.res,
  201. ctx.next
  202. )
  203. })
  204. it('should not inject the user data', function (ctx) {
  205. ctx.HistoryManager.promises.injectUserDetails.should.not.have.been.called
  206. })
  207. it('should not return the data with users to the client', function (ctx) {
  208. ctx.res.json.should.not.have.been.called
  209. })
  210. it('should throw an error', function (ctx) {
  211. ctx.next.should.have.been.calledWith(ctx.err)
  212. })
  213. })
  214. describe('resyncProjectHistory', function () {
  215. describe('for a project without project-history enabled', function () {
  216. beforeEach(async function (ctx) {
  217. ctx.req = { params: { Project_id: ctx.project_id }, body: {} }
  218. ctx.res = { setTimeout: sinon.stub(), sendStatus: sinon.stub() }
  219. ctx.error = new Errors.ProjectHistoryDisabledError()
  220. ctx.ProjectEntityUpdateHandler.promises.resyncProjectHistory.rejects(
  221. ctx.error
  222. )
  223. await ctx.HistoryController.resyncProjectHistory(
  224. ctx.req,
  225. ctx.res,
  226. ctx.next
  227. )
  228. })
  229. it('response with a 404', function (ctx) {
  230. ctx.res.sendStatus.should.have.been.calledWith(404)
  231. })
  232. })
  233. describe('for a project with project-history enabled', function () {
  234. beforeEach(async function (ctx) {
  235. ctx.req = { params: { Project_id: ctx.project_id }, body: {} }
  236. ctx.res = { setTimeout: sinon.stub(), sendStatus: sinon.stub() }
  237. await ctx.HistoryController.resyncProjectHistory(
  238. ctx.req,
  239. ctx.res,
  240. ctx.next
  241. )
  242. })
  243. it('sets an extended response timeout', function (ctx) {
  244. ctx.res.setTimeout.should.have.been.calledWith(6 * 60 * 1000)
  245. })
  246. it('resyncs the project', function (ctx) {
  247. ctx.ProjectEntityUpdateHandler.promises.resyncProjectHistory.should.have.been.calledWith(
  248. ctx.project_id
  249. )
  250. })
  251. it('responds with a 204', function (ctx) {
  252. ctx.res.sendStatus.should.have.been.calledWith(204)
  253. })
  254. })
  255. })
  256. describe('requestBlob', function () {
  257. describe('With Range header', function () {
  258. beforeEach(async function (ctx) {
  259. ctx.req = {
  260. params: {
  261. project_id: ctx.project_id,
  262. hash: ctx.blobHash,
  263. },
  264. query: {},
  265. body: {},
  266. get: sinon.stub(),
  267. }
  268. ctx.req.get.withArgs('Range').returns('bytes=0-42')
  269. ctx.res = { setHeader: sinon.stub(), status: sinon.stub() }
  270. ctx.HistoryManager.promises.requestBlobWithProjectId.resolves({
  271. stream: null,
  272. contentLength: '43',
  273. contentRange: 'bytes 0-42/100',
  274. })
  275. await ctx.HistoryController.getBlob(ctx.req, ctx.res, ctx.next)
  276. })
  277. it('should forward the range request', function (ctx) {
  278. ctx.HistoryManager.promises.requestBlobWithProjectId.should.have.been.calledWith(
  279. sinon.match(val => val.toString() === ctx.project_id),
  280. ctx.blobHash,
  281. 'GET',
  282. 'bytes=0-42'
  283. )
  284. })
  285. it('should forward the Content-Range header', function (ctx) {
  286. ctx.res.setHeader.should.have.been.calledWith(
  287. 'Content-Range',
  288. 'bytes 0-42/100'
  289. )
  290. })
  291. it('should forward the Content-Length header', function (ctx) {
  292. ctx.res.setHeader.should.have.been.calledWith('Content-Length', '43')
  293. })
  294. it('should have status 206', function (ctx) {
  295. ctx.res.status.should.have.been.calledWith(206)
  296. })
  297. })
  298. describe('Without Range header', function () {
  299. beforeEach(async function (ctx) {
  300. ctx.req = {
  301. params: {
  302. project_id: ctx.project_id,
  303. hash: ctx.blobHash,
  304. },
  305. query: {},
  306. body: {},
  307. get: sinon.stub(),
  308. }
  309. ctx.req.get.withArgs('Range').returns(null)
  310. ctx.res = { setHeader: sinon.stub(), status: sinon.stub() }
  311. ctx.HistoryManager.promises.requestBlobWithProjectId.resolves({
  312. stream: null,
  313. contentLength: '100',
  314. range: null,
  315. })
  316. await ctx.HistoryController.getBlob(ctx.req, ctx.res, ctx.next)
  317. })
  318. it('should not have a Content-Range header', function (ctx) {
  319. expect(ctx.res.setHeader).to.not.have.been.calledWith(
  320. 'Content-Range',
  321. sinon.match.string
  322. )
  323. })
  324. it('should forward the Content-Length header', function (ctx) {
  325. ctx.res.setHeader.should.have.been.calledWith('Content-Length', '100')
  326. })
  327. it('should not have status 206', function (ctx) {
  328. ctx.res.status.should.not.have.been.calledWith(206)
  329. })
  330. })
  331. })
  332. })