ProjectDownloadsControllerTests.coffee 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Downloads/ProjectDownloadsController.js"
  6. SandboxedModule = require('sandboxed-module')
  7. MockRequest = require "../helpers/MockRequest"
  8. MockResponse = require "../helpers/MockResponse"
  9. describe "ProjectDownloadsController", ->
  10. beforeEach ->
  11. @project_id = "project-id-123"
  12. @req = new MockRequest()
  13. @res = new MockResponse()
  14. @next = sinon.stub()
  15. @DocumentUpdaterHandler = sinon.stub()
  16. @ProjectDownloadsController = SandboxedModule.require modulePath, requires:
  17. "./ProjectZipStreamManager" : @ProjectZipStreamManager = {}
  18. "../Project/ProjectGetter" : @ProjectGetter = {}
  19. "metrics-sharelatex": @metrics = {}
  20. "logger-sharelatex" : @logger = {log: sinon.stub()}
  21. "../DocumentUpdater/DocumentUpdaterHandler": @DocumentUpdaterHandler
  22. describe "downloadProject", ->
  23. beforeEach ->
  24. @stream =
  25. pipe: sinon.stub()
  26. @ProjectZipStreamManager.createZipStreamForProject =
  27. sinon.stub().callsArgWith(1, null, @stream)
  28. @req.params = Project_id: @project_id
  29. @res.contentType = sinon.stub()
  30. @res.header = sinon.stub()
  31. @project_name = "project name with accênts"
  32. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, name: @project_name)
  33. @DocumentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArgWith(1)
  34. @metrics.inc = sinon.stub()
  35. @ProjectDownloadsController.downloadProject @req, @res, @next
  36. it "should create a zip from the project", ->
  37. @ProjectZipStreamManager.createZipStreamForProject
  38. .calledWith(@project_id)
  39. .should.equal true
  40. it "should stream the zip to the request", ->
  41. @stream.pipe.calledWith(@res)
  42. .should.equal true
  43. it "should set the correct content type on the request", ->
  44. @res.contentType
  45. .calledWith("application/zip")
  46. .should.equal true
  47. it "should flush the project to mongo", ->
  48. @DocumentUpdaterHandler.flushProjectToMongo
  49. .calledWith(@project_id)
  50. .should.equal true
  51. it "should look up the project's name", ->
  52. @ProjectGetter.getProject
  53. .calledWith(@project_id, name: true)
  54. .should.equal(true)
  55. it "should name the downloaded file after the project", ->
  56. @res.setContentDisposition
  57. .calledWith(
  58. 'attachment',
  59. {filename: "#{@project_name}.zip"})
  60. .should.equal true
  61. it "should record the action via Metrics", ->
  62. @metrics.inc.calledWith("zip-downloads").should.equal true
  63. it "should log the action", ->
  64. @logger.log
  65. .calledWith(sinon.match.any, "downloading project")
  66. .should.equal true
  67. describe "downloadMultipleProjects", ->
  68. beforeEach ->
  69. @stream =
  70. pipe: sinon.stub()
  71. @ProjectZipStreamManager.createZipStreamForMultipleProjects =
  72. sinon.stub().callsArgWith(1, null, @stream)
  73. @project_ids = ["project-1", "project-2"]
  74. @req.query = project_ids: @project_ids.join(",")
  75. @res.contentType = sinon.stub()
  76. @res.header = sinon.stub()
  77. @DocumentUpdaterHandler.flushMultipleProjectsToMongo = sinon.stub().callsArgWith(1)
  78. @metrics.inc = sinon.stub()
  79. @ProjectDownloadsController.downloadMultipleProjects @req, @res, @next
  80. it "should create a zip from the project", ->
  81. @ProjectZipStreamManager.createZipStreamForMultipleProjects
  82. .calledWith(@project_ids)
  83. .should.equal true
  84. it "should stream the zip to the request", ->
  85. @stream.pipe.calledWith(@res)
  86. .should.equal true
  87. it "should set the correct content type on the request", ->
  88. @res.contentType
  89. .calledWith("application/zip")
  90. .should.equal true
  91. it "should flush the projects to mongo", ->
  92. @DocumentUpdaterHandler.flushMultipleProjectsToMongo
  93. .calledWith(@project_ids)
  94. .should.equal true
  95. it "should name the downloaded file after the project", ->
  96. @res.setContentDisposition
  97. .calledWith(
  98. 'attachment',
  99. {filename: "ShareLaTeX Projects (2 items).zip"})
  100. .should.equal true
  101. it "should record the action via Metrics", ->
  102. @metrics.inc.calledWith("zip-downloads-multiple").should.equal true
  103. it "should log the action", ->
  104. @logger.log
  105. .calledWith(sinon.match.any, "downloading multiple projects")
  106. .should.equal true