LocalFileWriterTests.coffee 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. assert = require("chai").assert
  2. sinon = require('sinon')
  3. chai = require('chai')
  4. should = chai.should()
  5. expect = chai.expect
  6. modulePath = "../../../app/js/LocalFileWriter.js"
  7. SandboxedModule = require('sandboxed-module')
  8. describe "LocalFileWriter", ->
  9. beforeEach ->
  10. @writeStream =
  11. on: (type, cb)->
  12. if type == "finish"
  13. cb()
  14. @fs =
  15. createWriteStream : sinon.stub().returns(@writeStream)
  16. unlink: sinon.stub()
  17. @settings =
  18. path:
  19. uploadFolder:"somewhere"
  20. @writer = SandboxedModule.require modulePath, requires:
  21. "fs": @fs
  22. "logger-sharelatex":
  23. log:->
  24. err:->
  25. "settings-sharelatex":@settings
  26. @stubbedFsPath = "something/uploads/eio2k1j3"
  27. describe "writeStrem", ->
  28. beforeEach ->
  29. @writer._getPath = sinon.stub().returns(@stubbedFsPath)
  30. it "write the stream to ./uploads", (done)->
  31. stream =
  32. pipe: (dest)=>
  33. dest.should.equal @writeStream
  34. done()
  35. on: ->
  36. @writer.writeStream stream, null, ()=>
  37. it "should send the path in the callback", (done)->
  38. stream =
  39. pipe: (dest)=>
  40. on: (type, cb)->
  41. if type == "end"
  42. cb()
  43. @writer.writeStream stream, null, (err, fsPath)=>
  44. fsPath.should.equal @stubbedFsPath
  45. done()
  46. describe "delete file", ->
  47. it "should unlink the file", (done)->
  48. error = "my error"
  49. @fs.unlink.callsArgWith(1, error)
  50. @writer.deleteFile @stubbedFsPath, (err)=>
  51. @fs.unlink.calledWith(@stubbedFsPath).should.equal true
  52. err.should.equal error
  53. done()