ProjectDownloadsControllerTests.js 5.3 KB

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