HistoryController.test.mjs 11 KB

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