ProjectDownloadsControllerTests.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 chai = require('chai')
  15. const should = chai.should()
  16. const { expect } = chai
  17. const modulePath =
  18. '../../../../app/src/Features/Downloads/ProjectDownloadsController.js'
  19. const SandboxedModule = require('sandboxed-module')
  20. const MockRequest = require('../helpers/MockRequest')
  21. const MockResponse = require('../helpers/MockResponse')
  22. describe('ProjectDownloadsController', function() {
  23. beforeEach(function() {
  24. this.project_id = 'project-id-123'
  25. this.req = new MockRequest()
  26. this.res = new MockResponse()
  27. this.next = sinon.stub()
  28. this.DocumentUpdaterHandler = sinon.stub()
  29. return (this.ProjectDownloadsController = SandboxedModule.require(
  30. modulePath,
  31. {
  32. requires: {
  33. './ProjectZipStreamManager': (this.ProjectZipStreamManager = {}),
  34. '../Project/ProjectGetter': (this.ProjectGetter = {}),
  35. 'metrics-sharelatex': (this.metrics = {}),
  36. 'logger-sharelatex': (this.logger = { log: sinon.stub() }),
  37. '../DocumentUpdater/DocumentUpdaterHandler': this
  38. .DocumentUpdaterHandler
  39. }
  40. }
  41. ))
  42. })
  43. describe('downloadProject', function() {
  44. beforeEach(function() {
  45. this.stream = { pipe: sinon.stub() }
  46. this.ProjectZipStreamManager.createZipStreamForProject = sinon
  47. .stub()
  48. .callsArgWith(1, null, this.stream)
  49. this.req.params = { Project_id: this.project_id }
  50. this.res.contentType = sinon.stub()
  51. this.res.header = sinon.stub()
  52. this.project_name = 'project name with accênts'
  53. this.ProjectGetter.getProject = sinon
  54. .stub()
  55. .callsArgWith(2, null, { name: this.project_name })
  56. this.DocumentUpdaterHandler.flushProjectToMongo = sinon
  57. .stub()
  58. .callsArgWith(1)
  59. this.metrics.inc = sinon.stub()
  60. return this.ProjectDownloadsController.downloadProject(
  61. this.req,
  62. this.res,
  63. this.next
  64. )
  65. })
  66. it('should create a zip from the project', function() {
  67. return this.ProjectZipStreamManager.createZipStreamForProject
  68. .calledWith(this.project_id)
  69. .should.equal(true)
  70. })
  71. it('should stream the zip to the request', function() {
  72. return this.stream.pipe.calledWith(this.res).should.equal(true)
  73. })
  74. it('should set the correct content type on the request', function() {
  75. return this.res.contentType
  76. .calledWith('application/zip')
  77. .should.equal(true)
  78. })
  79. it('should flush the project to mongo', function() {
  80. return this.DocumentUpdaterHandler.flushProjectToMongo
  81. .calledWith(this.project_id)
  82. .should.equal(true)
  83. })
  84. it("should look up the project's name", function() {
  85. return this.ProjectGetter.getProject
  86. .calledWith(this.project_id, { name: true })
  87. .should.equal(true)
  88. })
  89. it('should name the downloaded file after the project', function() {
  90. return this.res.setContentDisposition
  91. .calledWith('attachment', { filename: `${this.project_name}.zip` })
  92. .should.equal(true)
  93. })
  94. it('should record the action via Metrics', function() {
  95. return this.metrics.inc.calledWith('zip-downloads').should.equal(true)
  96. })
  97. it('should log the action', function() {
  98. return this.logger.log
  99. .calledWith(sinon.match.any, 'downloading project')
  100. .should.equal(true)
  101. })
  102. })
  103. describe('downloadMultipleProjects', function() {
  104. beforeEach(function() {
  105. this.stream = { pipe: sinon.stub() }
  106. this.ProjectZipStreamManager.createZipStreamForMultipleProjects = sinon
  107. .stub()
  108. .callsArgWith(1, null, this.stream)
  109. this.project_ids = ['project-1', 'project-2']
  110. this.req.query = { project_ids: this.project_ids.join(',') }
  111. this.res.contentType = sinon.stub()
  112. this.res.header = sinon.stub()
  113. this.DocumentUpdaterHandler.flushMultipleProjectsToMongo = sinon
  114. .stub()
  115. .callsArgWith(1)
  116. this.metrics.inc = sinon.stub()
  117. return this.ProjectDownloadsController.downloadMultipleProjects(
  118. this.req,
  119. this.res,
  120. this.next
  121. )
  122. })
  123. it('should create a zip from the project', function() {
  124. return this.ProjectZipStreamManager.createZipStreamForMultipleProjects
  125. .calledWith(this.project_ids)
  126. .should.equal(true)
  127. })
  128. it('should stream the zip to the request', function() {
  129. return this.stream.pipe.calledWith(this.res).should.equal(true)
  130. })
  131. it('should set the correct content type on the request', function() {
  132. return this.res.contentType
  133. .calledWith('application/zip')
  134. .should.equal(true)
  135. })
  136. it('should flush the projects to mongo', function() {
  137. return this.DocumentUpdaterHandler.flushMultipleProjectsToMongo
  138. .calledWith(this.project_ids)
  139. .should.equal(true)
  140. })
  141. it('should name the downloaded file after the project', function() {
  142. return this.res.setContentDisposition
  143. .calledWith('attachment', {
  144. filename: 'Overleaf Projects (2 items).zip'
  145. })
  146. .should.equal(true)
  147. })
  148. it('should record the action via Metrics', function() {
  149. return this.metrics.inc
  150. .calledWith('zip-downloads-multiple')
  151. .should.equal(true)
  152. })
  153. it('should log the action', function() {
  154. return this.logger.log
  155. .calledWith(sinon.match.any, 'downloading multiple projects')
  156. .should.equal(true)
  157. })
  158. })
  159. })