ProjectZipStreamManagerTests.coffee 5.2 KB

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