ProjectZipStreamManagerTests.coffee 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Downloads/ProjectZipStreamManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. EventEmitter = require("events").EventEmitter
  8. describe "ProjectZipStreamManager", ->
  9. beforeEach ->
  10. @project_id = "project-id-123"
  11. @callback = sinon.stub()
  12. @archive =
  13. on:->
  14. @ProjectZipStreamManager = SandboxedModule.require modulePath, requires:
  15. "archiver": @archiver = sinon.stub().returns @archive
  16. "logger-sharelatex": @logger = {error: sinon.stub(), log: sinon.stub()}
  17. "../Project/ProjectEntityHandler" : @ProjectEntityHandler = {}
  18. "../FileStore/FileStoreHandler": @FileStoreHandler = {}
  19. describe "createZipStreamForProject", ->
  20. describe "successfully", ->
  21. beforeEach ->
  22. @ProjectZipStreamManager.addAllDocsToArchive = sinon.stub().callsArg(2)
  23. @ProjectZipStreamManager.addAllFilesToArchive = sinon.stub().callsArg(2)
  24. @archive.finalize = sinon.stub()
  25. @ProjectZipStreamManager.createZipStreamForProject @project_id, @callback
  26. it "should create a zip archive", ->
  27. @archiver.calledWith("zip").should.equal true
  28. it "should return a stream before any processing is done", ->
  29. @callback.calledWith(sinon.match.falsy, @archive).should.equal true
  30. @callback.calledBefore(@ProjectZipStreamManager.addAllDocsToArchive).should.equal true
  31. @callback.calledBefore(@ProjectZipStreamManager.addAllFilesToArchive).should.equal true
  32. it "should add all of the project docs to the zip", ->
  33. @ProjectZipStreamManager.addAllDocsToArchive
  34. .calledWith(@project_id, @archive)
  35. .should.equal true
  36. it "should add all of the project files to the zip", ->
  37. @ProjectZipStreamManager.addAllFilesToArchive
  38. .calledWith(@project_id, @archive)
  39. .should.equal true
  40. it "should finalise the stream", ->
  41. @archive.finalize.called.should.equal true
  42. describe "with an error adding docs", ->
  43. beforeEach ->
  44. @ProjectZipStreamManager.addAllDocsToArchive =
  45. sinon.stub().callsArgWith(2, new Error("something went wrong"))
  46. @ProjectZipStreamManager.addAllFilesToArchive = sinon.stub().callsArg(2)
  47. @archive.finalize = sinon.stub()
  48. @ProjectZipStreamManager.createZipStreamForProject @project_id, @callback
  49. it "should log out an error", ->
  50. @logger.error.calledWith(sinon.match.any, "error adding docs to zip stream")
  51. .should.equal true
  52. it "should continue with the process", ->
  53. @ProjectZipStreamManager.addAllDocsToArchive.called.should.equal true
  54. @ProjectZipStreamManager.addAllFilesToArchive.called.should.equal true
  55. @archive.finalize.called.should.equal true
  56. describe "with an error adding files", ->
  57. beforeEach ->
  58. @ProjectZipStreamManager.addAllDocsToArchive = sinon.stub().callsArg(2)
  59. @ProjectZipStreamManager.addAllFilesToArchive =
  60. sinon.stub().callsArgWith(2, new Error("something went wrong"))
  61. @archive.finalize = sinon.stub()
  62. @ProjectZipStreamManager.createZipStreamForProject @project_id, @callback
  63. it "should log out an error", ->
  64. @logger.error.calledWith(sinon.match.any, "error adding files to zip stream")
  65. .should.equal true
  66. it "should continue with the process", ->
  67. @ProjectZipStreamManager.addAllDocsToArchive.called.should.equal true
  68. @ProjectZipStreamManager.addAllFilesToArchive.called.should.equal true
  69. @archive.finalize.called.should.equal true
  70. describe "addAllDocsToArchive", ->
  71. beforeEach (done) ->
  72. @docs =
  73. "/main.tex":
  74. lines: ["\\documentclass{article}", "\\begin{document}", "Hello world", "\\end{document}"]
  75. "/chapters/chapter1.tex":
  76. lines: ["chapter1", "content"]
  77. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  78. @archive.append = sinon.stub()
  79. @ProjectZipStreamManager.addAllDocsToArchive @project_id, @archive, (error) =>
  80. @callback(error)
  81. done()
  82. it "should get the docs for the project", ->
  83. @ProjectEntityHandler.getAllDocs
  84. .calledWith(@project_id)
  85. .should.equal true
  86. it "should add each doc to the archive", ->
  87. for path, doc of @docs
  88. path = path.slice(1) # remove "/"
  89. @archive.append.calledWith(doc.lines.join("\n"), name: path)
  90. .should.equal true
  91. describe "addAllFilesToArchive", ->
  92. beforeEach ->
  93. @files =
  94. "/image.png":
  95. _id: "file-id-1"
  96. "/folder/picture.png":
  97. _id: "file-id-2"
  98. @streams =
  99. "file-id-1" : new EventEmitter()
  100. "file-id-2" : new EventEmitter()
  101. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  102. @archive.append = sinon.stub()
  103. @FileStoreHandler.getFileStream = (project_id, file_id, {}, callback) =>
  104. callback null, @streams[file_id]
  105. sinon.spy @FileStoreHandler, "getFileStream"
  106. @ProjectZipStreamManager.addAllFilesToArchive @project_id, @archive, @callback
  107. for path, stream of @streams
  108. stream.emit "end"
  109. it "should get the files for the project", ->
  110. @ProjectEntityHandler.getAllFiles.calledWith(@project_id).should.equal true
  111. it "should get a stream for each file", ->
  112. for path, file of @files
  113. @FileStoreHandler.getFileStream.calledWith(@project_id, file._id).should.equal true
  114. it "should add each file to the archive", ->
  115. for path, file of @files
  116. path = path.slice(1) # remove "/"
  117. @archive.append.calledWith(@streams[file._id], name: path).should.equal true