LocalFileWriterTests.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. @readStream =
  15. on: ->
  16. @fs =
  17. createWriteStream : sinon.stub().returns(@writeStream)
  18. createReadStream: sinon.stub().returns(@readStream)
  19. unlink: sinon.stub()
  20. @settings =
  21. path:
  22. uploadFolder:"somewhere"
  23. @writer = SandboxedModule.require modulePath, requires:
  24. "fs": @fs
  25. "logger-sharelatex":
  26. log:->
  27. err:->
  28. "settings-sharelatex":@settings
  29. "metrics-sharelatex":
  30. inc:->
  31. Timer:->
  32. done:->
  33. @stubbedFsPath = "something/uploads/eio2k1j3"
  34. describe "writeStrem", ->
  35. beforeEach ->
  36. @writer._getPath = sinon.stub().returns(@stubbedFsPath)
  37. it "write the stream to ./uploads", (done)->
  38. stream =
  39. pipe: (dest)=>
  40. dest.should.equal @writeStream
  41. done()
  42. on: ->
  43. @writer.writeStream stream, null, ()=>
  44. it "should send the path in the callback", (done)->
  45. stream =
  46. pipe: (dest)=>
  47. on: (type, cb)->
  48. if type == "end"
  49. cb()
  50. @writer.writeStream stream, null, (err, fsPath)=>
  51. fsPath.should.equal @stubbedFsPath
  52. done()
  53. describe "getStream", ->
  54. it "should read the stream from the file ", (done)->
  55. @writer.getStream @stubbedFsPath, (err, stream)=>
  56. @fs.createReadStream.calledWith(@stubbedFsPath).should.equal true
  57. done()
  58. it "should send the stream in the callback", (done)->
  59. @writer.getStream @stubbedFsPath, (err, readStream)=>
  60. readStream.should.equal @readStream
  61. done()
  62. describe "delete file", ->
  63. it "should unlink the file", (done)->
  64. error = "my error"
  65. @fs.unlink.callsArgWith(1, error)
  66. @writer.deleteFile @stubbedFsPath, (err)=>
  67. @fs.unlink.calledWith(@stubbedFsPath).should.equal true
  68. err.should.equal error
  69. done()