ProjectDownloadsController.test.mjs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. vi.doMock(
  40. '../../../../app/src/Features/Project/ProjectAuditLogHandler.mjs',
  41. () => ({
  42. default: (ctx.ProjectAuditLogHandler = {
  43. addEntryInBackground: sinon.stub(),
  44. }),
  45. })
  46. )
  47. ctx.ProjectDownloadsController = (await import(modulePath)).default
  48. })
  49. describe('downloadProject', function () {
  50. beforeEach(function (ctx) {
  51. ctx.stream = { pipe: sinon.stub() }
  52. ctx.ProjectZipStreamManager.createZipStreamForProject = sinon
  53. .stub()
  54. .callsArgWith(1, null, ctx.stream)
  55. ctx.req.params = { Project_id: ctx.project_id }
  56. ctx.req.ip = '192.168.1.1'
  57. ctx.req.session = {
  58. user: {
  59. _id: 'user-id-123',
  60. email: 'user@example.com',
  61. },
  62. }
  63. ctx.project_name = 'project name with accênts and % special characters'
  64. ctx.ProjectGetter.getProject = sinon
  65. .stub()
  66. .callsArgWith(2, null, { name: ctx.project_name })
  67. ctx.DocumentUpdaterHandler.flushProjectToMongo = sinon
  68. .stub()
  69. .callsArgWith(1)
  70. ctx.metrics.inc = sinon.stub()
  71. return ctx.ProjectDownloadsController.downloadProject(
  72. ctx.req,
  73. ctx.res,
  74. ctx.next
  75. )
  76. })
  77. it('should create a zip from the project', function (ctx) {
  78. return ctx.ProjectZipStreamManager.createZipStreamForProject
  79. .calledWith(ctx.project_id)
  80. .should.equal(true)
  81. })
  82. it('should stream the zip to the request', function (ctx) {
  83. return ctx.stream.pipe.calledWith(ctx.res).should.equal(true)
  84. })
  85. it('should set the correct content type on the request', function (ctx) {
  86. expect(ctx.res.contentType).toHaveBeenCalledWith('application/zip')
  87. })
  88. it('should flush the project to mongo', function (ctx) {
  89. return ctx.DocumentUpdaterHandler.flushProjectToMongo
  90. .calledWith(ctx.project_id)
  91. .should.equal(true)
  92. })
  93. it("should look up the project's name", function (ctx) {
  94. return ctx.ProjectGetter.getProject
  95. .calledWith(ctx.project_id, { name: true })
  96. .should.equal(true)
  97. })
  98. it('should name the downloaded file after the project but sanitise special characters', function (ctx) {
  99. ctx.res.headers.should.deep.equal({
  100. 'Content-Disposition': `attachment; filename="project_name_with_accênts_and___special_characters.zip"`,
  101. 'Content-Type': 'application/zip',
  102. 'X-Content-Type-Options': 'nosniff',
  103. })
  104. })
  105. it('should record the action via Metrics', function (ctx) {
  106. return ctx.metrics.inc.calledWith('zip-downloads').should.equal(true)
  107. })
  108. it('should add an audit log entry', function (ctx) {
  109. return ctx.ProjectAuditLogHandler.addEntryInBackground
  110. .calledWith(
  111. ctx.project_id,
  112. 'project-downloaded',
  113. ctx.req.session.user._id,
  114. ctx.req.ip
  115. )
  116. .should.equal(true)
  117. })
  118. })
  119. describe('downloadMultipleProjects', function () {
  120. beforeEach(function (ctx) {
  121. ctx.stream = { pipe: sinon.stub() }
  122. ctx.ProjectZipStreamManager.createZipStreamForMultipleProjects = sinon
  123. .stub()
  124. .callsArgWith(1, null, ctx.stream)
  125. ctx.project_ids = ['project-1', 'project-2']
  126. ctx.req.query = { project_ids: ctx.project_ids.join(',') }
  127. ctx.req.ip = '192.168.1.1'
  128. ctx.req.session = {
  129. user: {
  130. _id: 'user-id-123',
  131. email: 'user@example.com',
  132. },
  133. }
  134. ctx.DocumentUpdaterHandler.flushMultipleProjectsToMongo = sinon
  135. .stub()
  136. .callsArgWith(1)
  137. ctx.metrics.inc = sinon.stub()
  138. return ctx.ProjectDownloadsController.downloadMultipleProjects(
  139. ctx.req,
  140. ctx.res,
  141. ctx.next
  142. )
  143. })
  144. it('should create a zip from the project', function (ctx) {
  145. return ctx.ProjectZipStreamManager.createZipStreamForMultipleProjects
  146. .calledWith(ctx.project_ids)
  147. .should.equal(true)
  148. })
  149. it('should stream the zip to the request', function (ctx) {
  150. return ctx.stream.pipe.calledWith(ctx.res).should.equal(true)
  151. })
  152. it('should set the correct content type on the request', function (ctx) {
  153. expect(ctx.res.contentType).toHaveBeenCalledWith('application/zip')
  154. })
  155. it('should flush the projects to mongo', function (ctx) {
  156. return ctx.DocumentUpdaterHandler.flushMultipleProjectsToMongo
  157. .calledWith(ctx.project_ids)
  158. .should.equal(true)
  159. })
  160. it('should name the downloaded file after the project', function (ctx) {
  161. ctx.res.headers.should.deep.equal({
  162. 'Content-Disposition':
  163. 'attachment; filename="Overleaf Projects (2 items).zip"',
  164. 'Content-Type': 'application/zip',
  165. 'X-Content-Type-Options': 'nosniff',
  166. })
  167. })
  168. it('should record the action via Metrics', function (ctx) {
  169. return ctx.metrics.inc
  170. .calledWith('zip-downloads-multiple')
  171. .should.equal(true)
  172. })
  173. it('should add an audit log entry for each project', function (ctx) {
  174. ctx.ProjectAuditLogHandler.addEntryInBackground.callCount.should.equal(
  175. ctx.project_ids.length
  176. )
  177. for (const projectId of ctx.project_ids) {
  178. ctx.ProjectAuditLogHandler.addEntryInBackground
  179. .calledWith(
  180. projectId,
  181. 'project-downloaded',
  182. ctx.req.session.user._id,
  183. ctx.req.ip
  184. )
  185. .should.equal(true)
  186. }
  187. })
  188. })
  189. })