ProjectZipStreamManagerTests.coffee 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. append: sinon.stub()
  15. @ProjectZipStreamManager = SandboxedModule.require modulePath, requires:
  16. "archiver": @archiver = sinon.stub().returns @archive
  17. "logger-sharelatex": @logger = {error: sinon.stub(), log: sinon.stub()}
  18. "../Project/ProjectEntityHandler" : @ProjectEntityHandler = {}
  19. "../FileStore/FileStoreHandler": @FileStoreHandler = {}
  20. '../Project/ProjectGetter': @ProjectGetter = {}
  21. describe "createZipStreamForMultipleProjects", ->
  22. describe "successfully", ->
  23. beforeEach (done) ->
  24. @project_ids = ["project-1", "project-2"]
  25. @zip_streams =
  26. "project-1": new EventEmitter()
  27. "project-2": new EventEmitter()
  28. @project_names =
  29. "project-1": "Project One Name"
  30. "project-2": "Project Two Name"
  31. @ProjectZipStreamManager.createZipStreamForProject = (project_id, callback) =>
  32. callback null, @zip_streams[project_id]
  33. setTimeout () =>
  34. @zip_streams[project_id].emit "end",
  35. 0
  36. sinon.spy @ProjectZipStreamManager, "createZipStreamForProject"
  37. @ProjectGetter.getProject = (project_id, fields, callback) =>
  38. callback null, name: @project_names[project_id]
  39. sinon.spy @ProjectGetter, "getProject"
  40. @ProjectZipStreamManager.createZipStreamForMultipleProjects @project_ids, (args...) =>
  41. @callback args...
  42. @archive.finalize = () ->
  43. done()
  44. it "should create a zip archive", ->
  45. @archiver.calledWith("zip").should.equal true
  46. it "should return a stream before any processing is done", ->
  47. @callback.calledWith(sinon.match.falsy, @archive).should.equal true
  48. @callback.calledBefore(@ProjectZipStreamManager.createZipStreamForProject).should.equal true
  49. it "should get a zip stream for all of the projects", ->
  50. for project_id in @project_ids
  51. @ProjectZipStreamManager.createZipStreamForProject
  52. .calledWith(project_id)
  53. .should.equal true
  54. it "should get the names of each project", ->
  55. for project_id in @project_ids
  56. @ProjectGetter.getProject
  57. .calledWith(project_id, name: true)
  58. .should.equal true
  59. it "should add all of the projects to the zip", ->
  60. for project_id in @project_ids
  61. @archive.append
  62. .calledWith(@zip_streams[project_id], name: @project_names[project_id] + ".zip")
  63. .should.equal true
  64. describe "createZipStreamForProject", ->
  65. describe "successfully", ->
  66. beforeEach ->
  67. @ProjectZipStreamManager.addAllDocsToArchive = sinon.stub().callsArg(2)
  68. @ProjectZipStreamManager.addAllFilesToArchive = sinon.stub().callsArg(2)
  69. @archive.finalize = sinon.stub()
  70. @ProjectZipStreamManager.createZipStreamForProject @project_id, @callback
  71. it "should create a zip archive", ->
  72. @archiver.calledWith("zip").should.equal true
  73. it "should return a stream before any processing is done", ->
  74. @callback.calledWith(sinon.match.falsy, @archive).should.equal true
  75. @callback.calledBefore(@ProjectZipStreamManager.addAllDocsToArchive).should.equal true
  76. @callback.calledBefore(@ProjectZipStreamManager.addAllFilesToArchive).should.equal true
  77. it "should add all of the project docs to the zip", ->
  78. @ProjectZipStreamManager.addAllDocsToArchive
  79. .calledWith(@project_id, @archive)
  80. .should.equal true
  81. it "should add all of the project files to the zip", ->
  82. @ProjectZipStreamManager.addAllFilesToArchive
  83. .calledWith(@project_id, @archive)
  84. .should.equal true
  85. it "should finalise the stream", ->
  86. @archive.finalize.called.should.equal true
  87. describe "with an error adding docs", ->
  88. beforeEach ->
  89. @ProjectZipStreamManager.addAllDocsToArchive =
  90. sinon.stub().callsArgWith(2, new Error("something went wrong"))
  91. @ProjectZipStreamManager.addAllFilesToArchive = sinon.stub().callsArg(2)
  92. @archive.finalize = sinon.stub()
  93. @ProjectZipStreamManager.createZipStreamForProject @project_id, @callback
  94. it "should log out an error", ->
  95. @logger.error.calledWith(sinon.match.any, "error adding docs to zip stream")
  96. .should.equal true
  97. it "should continue with the process", ->
  98. @ProjectZipStreamManager.addAllDocsToArchive.called.should.equal true
  99. @ProjectZipStreamManager.addAllFilesToArchive.called.should.equal true
  100. @archive.finalize.called.should.equal true
  101. describe "with an error adding files", ->
  102. beforeEach ->
  103. @ProjectZipStreamManager.addAllDocsToArchive = sinon.stub().callsArg(2)
  104. @ProjectZipStreamManager.addAllFilesToArchive =
  105. sinon.stub().callsArgWith(2, new Error("something went wrong"))
  106. @archive.finalize = sinon.stub()
  107. @ProjectZipStreamManager.createZipStreamForProject @project_id, @callback
  108. it "should log out an error", ->
  109. @logger.error.calledWith(sinon.match.any, "error adding files to zip stream")
  110. .should.equal true
  111. it "should continue with the process", ->
  112. @ProjectZipStreamManager.addAllDocsToArchive.called.should.equal true
  113. @ProjectZipStreamManager.addAllFilesToArchive.called.should.equal true
  114. @archive.finalize.called.should.equal true
  115. describe "addAllDocsToArchive", ->
  116. beforeEach (done) ->
  117. @docs =
  118. "/main.tex":
  119. lines: ["\\documentclass{article}", "\\begin{document}", "Hello world", "\\end{document}"]
  120. "/chapters/chapter1.tex":
  121. lines: ["chapter1", "content"]
  122. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  123. @ProjectZipStreamManager.addAllDocsToArchive @project_id, @archive, (error) =>
  124. @callback(error)
  125. done()
  126. it "should get the docs for the project", ->
  127. @ProjectEntityHandler.getAllDocs
  128. .calledWith(@project_id)
  129. .should.equal true
  130. it "should add each doc to the archive", ->
  131. for path, doc of @docs
  132. path = path.slice(1) # remove "/"
  133. @archive.append.calledWith(doc.lines.join("\n"), name: path)
  134. .should.equal true
  135. describe "addAllFilesToArchive", ->
  136. beforeEach ->
  137. @files =
  138. "/image.png":
  139. _id: "file-id-1"
  140. "/folder/picture.png":
  141. _id: "file-id-2"
  142. @streams =
  143. "file-id-1" : new EventEmitter()
  144. "file-id-2" : new EventEmitter()
  145. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  146. @FileStoreHandler.getFileStream = (project_id, file_id, {}, callback) =>
  147. callback null, @streams[file_id]
  148. sinon.spy @FileStoreHandler, "getFileStream"
  149. @ProjectZipStreamManager.addAllFilesToArchive @project_id, @archive, @callback
  150. for path, stream of @streams
  151. stream.emit "end"
  152. it "should get the files for the project", ->
  153. @ProjectEntityHandler.getAllFiles.calledWith(@project_id).should.equal true
  154. it "should get a stream for each file", ->
  155. for path, file of @files
  156. @FileStoreHandler.getFileStream.calledWith(@project_id, file._id).should.equal true
  157. it "should add each file to the archive", ->
  158. for path, file of @files
  159. path = path.slice(1) # remove "/"
  160. @archive.append.calledWith(@streams[file._id], name: path).should.equal true