ProjectDownloadsController.test.mjs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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-Accel-Buffering': 'no',
  103. 'X-Content-Type-Options': 'nosniff',
  104. })
  105. })
  106. it('should record the action via Metrics', function (ctx) {
  107. return ctx.metrics.inc.calledWith('zip-downloads').should.equal(true)
  108. })
  109. it('should add an audit log entry', function (ctx) {
  110. return ctx.ProjectAuditLogHandler.addEntryInBackground
  111. .calledWith(
  112. ctx.project_id,
  113. 'project-downloaded',
  114. ctx.req.session.user._id,
  115. ctx.req.ip
  116. )
  117. .should.equal(true)
  118. })
  119. })
  120. describe('downloadMultipleProjects', function () {
  121. beforeEach(function (ctx) {
  122. ctx.stream = { pipe: sinon.stub() }
  123. ctx.ProjectZipStreamManager.createZipStreamForMultipleProjects = sinon
  124. .stub()
  125. .callsArgWith(1, null, ctx.stream)
  126. ctx.project_ids = ['project-1', 'project-2']
  127. ctx.req.query = { project_ids: ctx.project_ids.join(',') }
  128. ctx.req.ip = '192.168.1.1'
  129. ctx.req.session = {
  130. user: {
  131. _id: 'user-id-123',
  132. email: 'user@example.com',
  133. },
  134. }
  135. ctx.DocumentUpdaterHandler.flushMultipleProjectsToMongo = sinon
  136. .stub()
  137. .callsArgWith(1)
  138. ctx.metrics.inc = sinon.stub()
  139. return ctx.ProjectDownloadsController.downloadMultipleProjects(
  140. ctx.req,
  141. ctx.res,
  142. ctx.next
  143. )
  144. })
  145. it('should create a zip from the project', function (ctx) {
  146. return ctx.ProjectZipStreamManager.createZipStreamForMultipleProjects
  147. .calledWith(ctx.project_ids)
  148. .should.equal(true)
  149. })
  150. it('should stream the zip to the request', function (ctx) {
  151. return ctx.stream.pipe.calledWith(ctx.res).should.equal(true)
  152. })
  153. it('should set the correct content type on the request', function (ctx) {
  154. expect(ctx.res.contentType).toHaveBeenCalledWith('application/zip')
  155. })
  156. it('should flush the projects to mongo', function (ctx) {
  157. return ctx.DocumentUpdaterHandler.flushMultipleProjectsToMongo
  158. .calledWith(ctx.project_ids)
  159. .should.equal(true)
  160. })
  161. it('should name the downloaded file after the project', function (ctx) {
  162. ctx.res.headers.should.deep.equal({
  163. 'Content-Disposition':
  164. 'attachment; filename="Overleaf Projects (2 items).zip"',
  165. 'Content-Type': 'application/zip',
  166. 'X-Accel-Buffering': 'no',
  167. 'X-Content-Type-Options': 'nosniff',
  168. })
  169. })
  170. it('should record the action via Metrics', function (ctx) {
  171. return ctx.metrics.inc
  172. .calledWith('zip-downloads-multiple')
  173. .should.equal(true)
  174. })
  175. it('should add an audit log entry for each project', function (ctx) {
  176. ctx.ProjectAuditLogHandler.addEntryInBackground.callCount.should.equal(
  177. ctx.project_ids.length
  178. )
  179. for (const projectId of ctx.project_ids) {
  180. ctx.ProjectAuditLogHandler.addEntryInBackground
  181. .calledWith(
  182. projectId,
  183. 'project-downloaded',
  184. ctx.req.session.user._id,
  185. ctx.req.ip
  186. )
  187. .should.equal(true)
  188. }
  189. })
  190. })
  191. })