DocumentUpdaterController.test.mjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { expect, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. import MockResponse from '../helpers/MockResponse.js'
  4. const MODULE_PATH =
  5. '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterController.mjs'
  6. describe('DocumentUpdaterController', function () {
  7. beforeEach(async function (ctx) {
  8. ctx.DocumentUpdaterHandler = {
  9. promises: {
  10. getDocument: sinon.stub(),
  11. },
  12. }
  13. ctx.ProjectLocator = {
  14. promises: {
  15. findElement: sinon.stub(),
  16. },
  17. }
  18. vi.doMock('@overleaf/settings', () => ({
  19. default: ctx.settings,
  20. }))
  21. vi.doMock(
  22. '../../../../app/src/Features/Project/ProjectLocator.mjs',
  23. () => ({
  24. default: ctx.ProjectLocator,
  25. })
  26. )
  27. vi.doMock(
  28. '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler.mjs',
  29. () => ({
  30. default: ctx.DocumentUpdaterHandler,
  31. })
  32. )
  33. ctx.controller = (await import(MODULE_PATH)).default
  34. ctx.projectId = '2k3j1lk3j21lk3j'
  35. ctx.fileId = '12321kklj1lk3jk12'
  36. ctx.req = {
  37. params: {
  38. Project_id: ctx.projectId,
  39. Doc_id: ctx.docId,
  40. },
  41. get(key) {
  42. return undefined
  43. },
  44. }
  45. ctx.lines = ['test', '', 'testing']
  46. ctx.res = new MockResponse()
  47. ctx.next = sinon.stub()
  48. ctx.doc = { name: 'myfile.tex' }
  49. })
  50. describe('getDoc', function () {
  51. beforeEach(function (ctx) {
  52. ctx.DocumentUpdaterHandler.promises.getDocument.resolves({
  53. lines: ctx.lines,
  54. })
  55. ctx.ProjectLocator.promises.findElement.resolves({
  56. element: ctx.doc,
  57. })
  58. ctx.res = new MockResponse()
  59. })
  60. it('should call the document updater handler with the project_id and doc_id', async function (ctx) {
  61. await ctx.controller.getDoc(ctx.req, ctx.res, ctx.next)
  62. expect(
  63. ctx.DocumentUpdaterHandler.promises.getDocument
  64. ).to.have.been.calledOnceWith(
  65. ctx.req.params.Project_id,
  66. ctx.req.params.Doc_id,
  67. -1
  68. )
  69. })
  70. it('should return the content', async function (ctx) {
  71. await ctx.controller.getDoc(ctx.req, ctx.res)
  72. expect(ctx.next).to.not.have.been.called
  73. expect(ctx.res.statusCode).to.equal(200)
  74. expect(ctx.res.body).to.equal('test\n\ntesting')
  75. })
  76. it('should find the doc in the project', async function (ctx) {
  77. await ctx.controller.getDoc(ctx.req, ctx.res)
  78. expect(
  79. ctx.ProjectLocator.promises.findElement
  80. ).to.have.been.calledOnceWith({
  81. project_id: ctx.projectId,
  82. element_id: ctx.docId,
  83. type: 'doc',
  84. })
  85. })
  86. it('should set the Content-Disposition header', async function (ctx) {
  87. await ctx.controller.getDoc(ctx.req, ctx.res)
  88. expect(ctx.res.setContentDisposition).to.have.been.calledWith(
  89. 'attachment',
  90. { filename: ctx.doc.name }
  91. )
  92. })
  93. })
  94. })