ArchiveManagerTests.coffee 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/Features/Uploads/ArchiveManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. events = require "events"
  7. describe "ArchiveManager", ->
  8. beforeEach ->
  9. @logger =
  10. error: sinon.stub()
  11. log: sinon.stub()
  12. @process = new events.EventEmitter
  13. @process.stdout = new events.EventEmitter
  14. @process.stderr = new events.EventEmitter
  15. @child =
  16. spawn: sinon.stub().returns(@process)
  17. @metrics =
  18. Timer: class Timer
  19. done: sinon.stub()
  20. @ArchiveManager = SandboxedModule.require modulePath, requires:
  21. "child_process": @child
  22. "logger-sharelatex": @logger
  23. "../../infrastructure/Metrics": @metrics
  24. "fs": @fs = {}
  25. describe "extractZipArchive", ->
  26. beforeEach ->
  27. @source = "/path/to/zip/source.zip"
  28. @destination = "/path/to/zip/destination"
  29. @callback = sinon.stub()
  30. describe "successfully", ->
  31. beforeEach (done) ->
  32. @ArchiveManager.extractZipArchive @source, @destination, done
  33. @process.emit "exit"
  34. it "should run unzip", ->
  35. @child.spawn.calledWithExactly("unzip", [@source, "-d", @destination]).should.equal true
  36. it "should time the unzip", ->
  37. @metrics.Timer::done.called.should.equal true
  38. it "should log the unzip", ->
  39. @logger.log.calledWith(sinon.match.any, "unzipping file").should.equal true
  40. describe "with an error on stderr", ->
  41. beforeEach (done) ->
  42. @ArchiveManager.extractZipArchive @source, @destination, (error) =>
  43. @callback(error)
  44. done()
  45. @process.stderr.emit "data", "Something went wrong"
  46. @process.emit "exit"
  47. it "should return the callback with an error", ->
  48. @callback.calledWithExactly(new Error("Something went wrong")).should.equal true
  49. it "should log out the error", ->
  50. @logger.error.called.should.equal true
  51. describe "with an error on the process", ->
  52. beforeEach (done) ->
  53. @ArchiveManager.extractZipArchive @source, @destination, (error) =>
  54. @callback(error)
  55. done()
  56. @process.emit "error", new Error("Something went wrong")
  57. it "should return the callback with an error", ->
  58. @callback.calledWithExactly(new Error("Something went wrong")).should.equal true
  59. it "should log out the error", ->
  60. @logger.error.called.should.equal true
  61. describe "findTopLevelDirectory", ->
  62. beforeEach ->
  63. @fs.readdir = sinon.stub()
  64. @fs.stat = sinon.stub()
  65. @directory = "test/directory"
  66. describe "with multiple files", ->
  67. beforeEach ->
  68. @fs.readdir.callsArgWith(1, null, ["multiple", "files"])
  69. @ArchiveManager.findTopLevelDirectory(@directory, @callback)
  70. it "should find the files in the directory", ->
  71. @fs.readdir
  72. .calledWith(@directory)
  73. .should.equal true
  74. it "should return the original directory", ->
  75. @callback
  76. .calledWith(null, @directory)
  77. .should.equal true
  78. describe "with a single file (not folder)", ->
  79. beforeEach ->
  80. @fs.readdir.callsArgWith(1, null, ["foo.tex"])
  81. @fs.stat.callsArgWith(1, null, { isDirectory: () -> false })
  82. @ArchiveManager.findTopLevelDirectory(@directory, @callback)
  83. it "should check if the file is a directory", ->
  84. @fs.stat
  85. .calledWith(@directory + "/foo.tex")
  86. .should.equal true
  87. it "should return the original directory", ->
  88. @callback
  89. .calledWith(null, @directory)
  90. .should.equal true
  91. describe "with a single top-level folder", ->
  92. beforeEach ->
  93. @fs.readdir.callsArgWith(1, null, ["folder"])
  94. @fs.stat.callsArgWith(1, null, { isDirectory: () -> true })
  95. @ArchiveManager.findTopLevelDirectory(@directory, @callback)
  96. it "should check if the file is a directory", ->
  97. @fs.stat
  98. .calledWith(@directory + "/folder")
  99. .should.equal true
  100. it "should return the child directory", ->
  101. @callback
  102. .calledWith(null, @directory + "/folder")
  103. .should.equal true