ProjectDownloadsControllerTests.coffee 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "../../models/Project" : Project: @Project = {}
  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. @Project.findById = 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. @Project.findById.calledWith(@project_id, "name").should.equal(true)
  53. it "should name the downloaded file after the project", ->
  54. @res.setContentDisposition
  55. .calledWith(
  56. 'attachment',
  57. {filename: "#{@project_name}.zip"})
  58. .should.equal true
  59. it "should record the action via Metrics", ->
  60. @metrics.inc.calledWith("zip-downloads").should.equal true
  61. it "should log the action", ->
  62. @logger.log
  63. .calledWith(sinon.match.any, "downloading project")
  64. .should.equal true
  65. describe "downloadMultipleProjects", ->
  66. beforeEach ->
  67. @stream =
  68. pipe: sinon.stub()
  69. @ProjectZipStreamManager.createZipStreamForMultipleProjects =
  70. sinon.stub().callsArgWith(1, null, @stream)
  71. @project_ids = ["project-1", "project-2"]
  72. @req.query = project_ids: @project_ids.join(",")
  73. @res.contentType = sinon.stub()
  74. @res.header = sinon.stub()
  75. @DocumentUpdaterHandler.flushMultipleProjectsToMongo = sinon.stub().callsArgWith(1)
  76. @metrics.inc = sinon.stub()
  77. @ProjectDownloadsController.downloadMultipleProjects @req, @res, @next
  78. it "should create a zip from the project", ->
  79. @ProjectZipStreamManager.createZipStreamForMultipleProjects
  80. .calledWith(@project_ids)
  81. .should.equal true
  82. it "should stream the zip to the request", ->
  83. @stream.pipe.calledWith(@res)
  84. .should.equal true
  85. it "should set the correct content type on the request", ->
  86. @res.contentType
  87. .calledWith("application/zip")
  88. .should.equal true
  89. it "should flush the projects to mongo", ->
  90. @DocumentUpdaterHandler.flushMultipleProjectsToMongo
  91. .calledWith(@project_ids)
  92. .should.equal true
  93. it "should name the downloaded file after the project", ->
  94. @res.setContentDisposition
  95. .calledWith(
  96. 'attachment',
  97. {filename: "ShareLaTeX Projects (2 items).zip"})
  98. .should.equal true
  99. it "should record the action via Metrics", ->
  100. @metrics.inc.calledWith("zip-downloads-multiple").should.equal true
  101. it "should log the action", ->
  102. @logger.log
  103. .calledWith(sinon.match.any, "downloading multiple projects")
  104. .should.equal true