ArchiveManagerTests.coffee 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. describe "extractZipArchive", ->
  25. beforeEach ->
  26. @source = "/path/to/zip/source.zip"
  27. @destination = "/path/to/zip/destination"
  28. @callback = sinon.stub()
  29. describe "successfully", ->
  30. beforeEach (done) ->
  31. @ArchiveManager.extractZipArchive @source, @destination, done
  32. @process.emit "exit"
  33. it "should run unzip", ->
  34. @child.spawn.calledWithExactly("unzip", [@source, "-d", @destination]).should.equal true
  35. it "should time the unzip", ->
  36. @metrics.Timer::done.called.should.equal true
  37. it "should log the unzip", ->
  38. @logger.log.calledWith(sinon.match.any, "unzipping file").should.equal true
  39. describe "with an error on stderr", ->
  40. beforeEach (done) ->
  41. @ArchiveManager.extractZipArchive @source, @destination, (error) =>
  42. @callback(error)
  43. done()
  44. @process.stderr.emit "data", "Something went wrong"
  45. @process.emit "exit"
  46. it "should return the callback with an error", ->
  47. @callback.calledWithExactly(new Error("Something went wrong")).should.equal true
  48. it "should log out the error", ->
  49. @logger.error.called.should.equal true
  50. describe "with an error on the process", ->
  51. beforeEach (done) ->
  52. @ArchiveManager.extractZipArchive @source, @destination, (error) =>
  53. @callback(error)
  54. done()
  55. @process.emit "error", new Error("Something went wrong")
  56. it "should return the callback with an error", ->
  57. @callback.calledWithExactly(new Error("Something went wrong")).should.equal true
  58. it "should log out the error", ->
  59. @logger.error.called.should.equal true