ProjectDownloadsController.test.mjs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { vi } from 'vitest'
  2. // TODO: This file was created by bulk-decaffeinate.
  3. // Fix any style issues and re-enable lint.
  4. /*
  5. * decaffeinate suggestions:
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  8. */
  9. import sinon from 'sinon'
  10. import MockRequest from '../helpers/MockRequest.mjs'
  11. import MockResponse from '../helpers/MockResponse.mjs'
  12. const modulePath =
  13. '../../../../app/src/Features/Downloads/ProjectDownloadsController.mjs'
  14. describe('ProjectDownloadsController', function () {
  15. beforeEach(async function (ctx) {
  16. ctx.project_id = 'project-id-123'
  17. ctx.req = new MockRequest(vi)
  18. ctx.res = new MockResponse(vi)
  19. ctx.next = sinon.stub()
  20. ctx.DocumentUpdaterHandler = sinon.stub()
  21. vi.doMock(
  22. '../../../../app/src/Features/Downloads/ProjectZipStreamManager.mjs',
  23. () => ({
  24. default: (ctx.ProjectZipStreamManager = {}),
  25. })
  26. )
  27. vi.doMock('../../../../app/src/Features/Project/ProjectGetter.mjs', () => ({
  28. default: (ctx.ProjectGetter = {}),
  29. }))
  30. vi.doMock('@overleaf/metrics', () => ({
  31. default: (ctx.metrics = {}),
  32. }))
  33. vi.doMock(
  34. '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler.mjs',
  35. () => ({
  36. default: ctx.DocumentUpdaterHandler,
  37. })
  38. )
  39. ctx.ProjectDownloadsController = (await import(modulePath)).default
  40. })
  41. describe('downloadProject', function () {
  42. beforeEach(function (ctx) {
  43. ctx.stream = { pipe: sinon.stub() }
  44. ctx.ProjectZipStreamManager.createZipStreamForProject = sinon
  45. .stub()
  46. .callsArgWith(1, null, ctx.stream)
  47. ctx.req.params = { Project_id: ctx.project_id }
  48. ctx.project_name = 'project name with accênts and % special characters'
  49. ctx.ProjectGetter.getProject = sinon
  50. .stub()
  51. .callsArgWith(2, null, { name: ctx.project_name })
  52. ctx.DocumentUpdaterHandler.flushProjectToMongo = sinon
  53. .stub()
  54. .callsArgWith(1)
  55. ctx.metrics.inc = sinon.stub()
  56. return ctx.ProjectDownloadsController.downloadProject(
  57. ctx.req,
  58. ctx.res,
  59. ctx.next
  60. )
  61. })
  62. it('should create a zip from the project', function (ctx) {
  63. return ctx.ProjectZipStreamManager.createZipStreamForProject
  64. .calledWith(ctx.project_id)
  65. .should.equal(true)
  66. })
  67. it('should stream the zip to the request', function (ctx) {
  68. return ctx.stream.pipe.calledWith(ctx.res).should.equal(true)
  69. })
  70. it('should set the correct content type on the request', function (ctx) {
  71. expect(ctx.res.contentType).toHaveBeenCalledWith('application/zip')
  72. })
  73. it('should flush the project to mongo', function (ctx) {
  74. return ctx.DocumentUpdaterHandler.flushProjectToMongo
  75. .calledWith(ctx.project_id)
  76. .should.equal(true)
  77. })
  78. it("should look up the project's name", function (ctx) {
  79. return ctx.ProjectGetter.getProject
  80. .calledWith(ctx.project_id, { name: true })
  81. .should.equal(true)
  82. })
  83. it('should name the downloaded file after the project but sanitise special characters', function (ctx) {
  84. ctx.res.headers.should.deep.equal({
  85. 'Content-Disposition': `attachment; filename="project_name_with_accênts_and___special_characters.zip"`,
  86. 'Content-Type': 'application/zip',
  87. 'X-Content-Type-Options': 'nosniff',
  88. })
  89. })
  90. it('should record the action via Metrics', function (ctx) {
  91. return ctx.metrics.inc.calledWith('zip-downloads').should.equal(true)
  92. })
  93. })
  94. describe('downloadMultipleProjects', function () {
  95. beforeEach(function (ctx) {
  96. ctx.stream = { pipe: sinon.stub() }
  97. ctx.ProjectZipStreamManager.createZipStreamForMultipleProjects = sinon
  98. .stub()
  99. .callsArgWith(1, null, ctx.stream)
  100. ctx.project_ids = ['project-1', 'project-2']
  101. ctx.req.query = { project_ids: ctx.project_ids.join(',') }
  102. ctx.DocumentUpdaterHandler.flushMultipleProjectsToMongo = sinon
  103. .stub()
  104. .callsArgWith(1)
  105. ctx.metrics.inc = sinon.stub()
  106. return ctx.ProjectDownloadsController.downloadMultipleProjects(
  107. ctx.req,
  108. ctx.res,
  109. ctx.next
  110. )
  111. })
  112. it('should create a zip from the project', function (ctx) {
  113. return ctx.ProjectZipStreamManager.createZipStreamForMultipleProjects
  114. .calledWith(ctx.project_ids)
  115. .should.equal(true)
  116. })
  117. it('should stream the zip to the request', function (ctx) {
  118. return ctx.stream.pipe.calledWith(ctx.res).should.equal(true)
  119. })
  120. it('should set the correct content type on the request', function (ctx) {
  121. expect(ctx.res.contentType).toHaveBeenCalledWith('application/zip')
  122. })
  123. it('should flush the projects to mongo', function (ctx) {
  124. return ctx.DocumentUpdaterHandler.flushMultipleProjectsToMongo
  125. .calledWith(ctx.project_ids)
  126. .should.equal(true)
  127. })
  128. it('should name the downloaded file after the project', function (ctx) {
  129. ctx.res.headers.should.deep.equal({
  130. 'Content-Disposition':
  131. 'attachment; filename="Overleaf Projects (2 items).zip"',
  132. 'Content-Type': 'application/zip',
  133. 'X-Content-Type-Options': 'nosniff',
  134. })
  135. })
  136. it('should record the action via Metrics', function (ctx) {
  137. return ctx.metrics.inc
  138. .calledWith('zip-downloads-multiple')
  139. .should.equal(true)
  140. })
  141. })
  142. })