ProjectZipStreamManagerTests.coffee 5.2 KB

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