ProjectDownloadsController.test.mjs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.js'
  11. import MockResponse from '../helpers/MockResponse.js'
  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()
  18. ctx.res = new MockResponse()
  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.js', () => ({
  28. default: (ctx.ProjectGetter = {}),
  29. }))
  30. vi.doMock('@overleaf/metrics', () => ({
  31. default: (ctx.metrics = {}),
  32. }))
  33. vi.doMock(
  34. '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler.js',
  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. return ctx.res.contentType
  72. .calledWith('application/zip')
  73. .should.equal(true)
  74. })
  75. it('should flush the project to mongo', function (ctx) {
  76. return ctx.DocumentUpdaterHandler.flushProjectToMongo
  77. .calledWith(ctx.project_id)
  78. .should.equal(true)
  79. })
  80. it("should look up the project's name", function (ctx) {
  81. return ctx.ProjectGetter.getProject
  82. .calledWith(ctx.project_id, { name: true })
  83. .should.equal(true)
  84. })
  85. it('should name the downloaded file after the project but sanitise special characters', function (ctx) {
  86. ctx.res.headers.should.deep.equal({
  87. 'Content-Disposition': `attachment; filename="project_name_with_accênts_and___special_characters.zip"`,
  88. 'Content-Type': 'application/zip',
  89. 'X-Content-Type-Options': 'nosniff',
  90. })
  91. })
  92. it('should record the action via Metrics', function (ctx) {
  93. return ctx.metrics.inc.calledWith('zip-downloads').should.equal(true)
  94. })
  95. })
  96. describe('downloadMultipleProjects', function () {
  97. beforeEach(function (ctx) {
  98. ctx.stream = { pipe: sinon.stub() }
  99. ctx.ProjectZipStreamManager.createZipStreamForMultipleProjects = sinon
  100. .stub()
  101. .callsArgWith(1, null, ctx.stream)
  102. ctx.project_ids = ['project-1', 'project-2']
  103. ctx.req.query = { project_ids: ctx.project_ids.join(',') }
  104. ctx.DocumentUpdaterHandler.flushMultipleProjectsToMongo = sinon
  105. .stub()
  106. .callsArgWith(1)
  107. ctx.metrics.inc = sinon.stub()
  108. return ctx.ProjectDownloadsController.downloadMultipleProjects(
  109. ctx.req,
  110. ctx.res,
  111. ctx.next
  112. )
  113. })
  114. it('should create a zip from the project', function (ctx) {
  115. return ctx.ProjectZipStreamManager.createZipStreamForMultipleProjects
  116. .calledWith(ctx.project_ids)
  117. .should.equal(true)
  118. })
  119. it('should stream the zip to the request', function (ctx) {
  120. return ctx.stream.pipe.calledWith(ctx.res).should.equal(true)
  121. })
  122. it('should set the correct content type on the request', function (ctx) {
  123. return ctx.res.contentType
  124. .calledWith('application/zip')
  125. .should.equal(true)
  126. })
  127. it('should flush the projects to mongo', function (ctx) {
  128. return ctx.DocumentUpdaterHandler.flushMultipleProjectsToMongo
  129. .calledWith(ctx.project_ids)
  130. .should.equal(true)
  131. })
  132. it('should name the downloaded file after the project', function (ctx) {
  133. ctx.res.headers.should.deep.equal({
  134. 'Content-Disposition':
  135. 'attachment; filename="Overleaf Projects (2 items).zip"',
  136. 'Content-Type': 'application/zip',
  137. 'X-Content-Type-Options': 'nosniff',
  138. })
  139. })
  140. it('should record the action via Metrics', function (ctx) {
  141. return ctx.metrics.inc
  142. .calledWith('zip-downloads-multiple')
  143. .should.equal(true)
  144. })
  145. })
  146. })