2014-02-26 10:10:55 -05:00
|
|
|
assert = require("chai").assert
|
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should
|
|
|
|
expect = chai.expect
|
|
|
|
modulePath = "../../../app/js/FSPersistorManager.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
fs = require("fs")
|
2014-12-22 08:01:39 -05:00
|
|
|
response = require("response")
|
2014-02-26 10:10:55 -05:00
|
|
|
|
|
|
|
describe "FSPersistorManagerTests", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@Fs =
|
|
|
|
rename:sinon.stub()
|
|
|
|
createReadStream:sinon.stub()
|
|
|
|
createWriteStream:sinon.stub()
|
|
|
|
unlink:sinon.stub()
|
|
|
|
rmdir:sinon.stub()
|
|
|
|
exists:sinon.stub()
|
2016-03-13 19:45:48 -04:00
|
|
|
readdir:sinon.stub()
|
|
|
|
openSync:sinon.stub()
|
|
|
|
fstatSync:sinon.stub()
|
|
|
|
closeSync:sinon.stub()
|
2014-12-22 08:01:39 -05:00
|
|
|
@Rimraf = sinon.stub()
|
2014-02-26 10:10:55 -05:00
|
|
|
@LocalFileWriter =
|
|
|
|
writeStream: sinon.stub()
|
|
|
|
@requires =
|
|
|
|
"./LocalFileWriter":@LocalFileWriter
|
|
|
|
"fs":@Fs
|
|
|
|
"logger-sharelatex":
|
|
|
|
log:->
|
|
|
|
err:->
|
2014-12-22 08:01:39 -05:00
|
|
|
"response":response
|
|
|
|
"rimraf":@Rimraf
|
2015-09-01 07:10:30 -04:00
|
|
|
"./Errors": @Errors =
|
|
|
|
NotFoundError: sinon.stub()
|
2014-02-26 10:10:55 -05:00
|
|
|
@location = "/tmp"
|
2014-03-01 10:10:47 -05:00
|
|
|
@name1 = "530f2407e7ef165704000007/530f838b46d9a9e859000008"
|
|
|
|
@name1Filtered ="530f2407e7ef165704000007_530f838b46d9a9e859000008"
|
2014-02-26 10:10:55 -05:00
|
|
|
@name2 = "second_file"
|
|
|
|
@error = "error_message"
|
|
|
|
@FSPersistorManager = SandboxedModule.require modulePath, requires: @requires
|
|
|
|
|
|
|
|
describe "sendFile", ->
|
|
|
|
it "should put the file", (done) ->
|
|
|
|
@Fs.rename.callsArgWith(2,@error)
|
|
|
|
@FSPersistorManager.sendFile @location, @name1, @name2, (err)=>
|
2014-03-01 10:10:47 -05:00
|
|
|
@Fs.rename.calledWith( @name2, "#{@location}/#{@name1Filtered}" ).should.equal true
|
2014-02-26 10:10:55 -05:00
|
|
|
err.should.equal @error
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "sendStream", ->
|
|
|
|
beforeEach ->
|
|
|
|
@FSPersistorManager.sendFile = sinon.stub().callsArgWith(3)
|
|
|
|
@LocalFileWriter.writeStream.callsArgWith(2, null, @name1)
|
|
|
|
@SourceStream =
|
|
|
|
on:->
|
|
|
|
|
|
|
|
it "should sent stream to LocalFileWriter", (done)->
|
|
|
|
@FSPersistorManager.sendStream @location, @name1, @SourceStream, =>
|
|
|
|
@LocalFileWriter.writeStream.calledWith(@SourceStream).should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return the error from LocalFileWriter", (done)->
|
|
|
|
@LocalFileWriter.writeStream.callsArgWith(2, @error)
|
|
|
|
@FSPersistorManager.sendStream @location, @name1, @SourceStream, (err)=>
|
|
|
|
err.should.equal @error
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should send the file to the filestore", (done)->
|
|
|
|
@LocalFileWriter.writeStream.callsArgWith(2)
|
|
|
|
@FSPersistorManager.sendStream @location, @name1, @SourceStream, (err)=>
|
|
|
|
@FSPersistorManager.sendFile.called.should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "getFileStream", ->
|
2015-08-27 11:12:11 -04:00
|
|
|
beforeEach ->
|
|
|
|
@opts = {}
|
|
|
|
|
2014-02-26 10:10:55 -05:00
|
|
|
it "should use correct file location", (done) ->
|
2015-08-27 11:12:11 -04:00
|
|
|
@Fs.createReadStream.returns({on: ->})
|
|
|
|
@FSPersistorManager.getFileStream @location, @name1, @opts, (err,res) =>
|
2017-06-27 09:53:20 -04:00
|
|
|
@Fs.createReadStream.calledWith("#{@location}/#{@name1Filtered}").should.equal true
|
2014-12-22 08:07:03 -05:00
|
|
|
done()
|
2014-02-26 10:10:55 -05:00
|
|
|
|
2015-08-27 11:12:11 -04:00
|
|
|
describe "with start and end options", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@opts = {start: 0, end: 8}
|
|
|
|
|
|
|
|
it 'should pass the options to createReadStream', (done) ->
|
|
|
|
@Fs.createReadStream.returns({on: ->})
|
|
|
|
@FSPersistorManager.getFileStream @location, @name1, @opts, (err,res)=>
|
|
|
|
@Fs.createReadStream.calledWith("#{@location}/#{@name1Filtered}", @opts).should.equal true
|
|
|
|
done()
|
|
|
|
|
2015-09-01 07:10:30 -04:00
|
|
|
describe "error conditions", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@fakeCode = 'ENOENT'
|
|
|
|
@Fs.createReadStream.returns(
|
|
|
|
on: (key, callback) =>
|
|
|
|
err = new Error()
|
|
|
|
err.code = @fakeCode
|
|
|
|
callback(err, null)
|
|
|
|
)
|
|
|
|
|
|
|
|
describe "when the file does not exist", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@fakeCode = 'ENOENT'
|
|
|
|
|
|
|
|
it "should give a NotFoundError", (done) ->
|
|
|
|
@FSPersistorManager.getFileStream @location, @name1, @opts, (err,res)=>
|
|
|
|
expect(res).to.equal null
|
|
|
|
expect(err).to.not.equal null
|
|
|
|
expect(err instanceof @Errors.NotFoundError).to.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "when some other error happens", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@fakeCode = 'SOMETHINGHORRIBLE'
|
|
|
|
|
|
|
|
it "should give an Error", (done) ->
|
|
|
|
@FSPersistorManager.getFileStream @location, @name1, @opts, (err,res)=>
|
|
|
|
expect(res).to.equal null
|
|
|
|
expect(err).to.not.equal null
|
|
|
|
expect(err instanceof Error).to.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
|
2015-08-27 11:12:11 -04:00
|
|
|
|
2014-02-26 10:10:55 -05:00
|
|
|
describe "copyFile", ->
|
|
|
|
beforeEach ->
|
|
|
|
@ReadStream=
|
|
|
|
on:->
|
|
|
|
pipe:sinon.stub()
|
|
|
|
@WriteStream=
|
|
|
|
on:->
|
|
|
|
@Fs.createReadStream.returns(@ReadStream)
|
|
|
|
@Fs.createWriteStream.returns(@WriteStream)
|
|
|
|
|
|
|
|
it "Should open the source for reading", (done) ->
|
|
|
|
@FSPersistorManager.copyFile @location, @name1, @name2, ->
|
2017-06-27 09:53:20 -04:00
|
|
|
@Fs.createReadStream.calledWith("#{@location}/#{@name1Filtered}").should.equal true
|
2014-02-26 10:10:55 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "Should open the target for writing", (done) ->
|
|
|
|
@FSPersistorManager.copyFile @location, @name1, @name2, ->
|
2017-06-27 09:53:20 -04:00
|
|
|
@Fs.createWriteStream.calledWith("#{@location}/#{@name2}").should.equal true
|
2014-02-26 10:10:55 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "Should pipe the source to the target", (done) ->
|
|
|
|
@FSPersistorManager.copyFile @location, @name1, @name2, ->
|
2017-06-27 09:53:20 -04:00
|
|
|
@ReadStream.pipe.calledWith(@WriteStream).should.equal true
|
2014-02-26 10:10:55 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
describe "deleteFile", ->
|
|
|
|
beforeEach ->
|
|
|
|
@Fs.unlink.callsArgWith(1,@error)
|
|
|
|
|
|
|
|
it "Should call unlink with correct options", (done) ->
|
|
|
|
@FSPersistorManager.deleteFile @location, @name1, (err) =>
|
2017-06-27 09:53:20 -04:00
|
|
|
@Fs.unlink.calledWith("#{@location}/#{@name1Filtered}").should.equal true
|
2014-02-26 10:10:55 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "Should propogate the error", (done) ->
|
|
|
|
@FSPersistorManager.deleteFile @location, @name1, (err) =>
|
|
|
|
err.should.equal @error
|
|
|
|
done()
|
|
|
|
|
|
|
|
|
|
|
|
describe "deleteDirectory", ->
|
|
|
|
beforeEach ->
|
2014-12-22 08:01:39 -05:00
|
|
|
@Rimraf.callsArgWith(1,@error)
|
2014-02-26 10:10:55 -05:00
|
|
|
|
2014-12-22 08:01:39 -05:00
|
|
|
it "Should call rmdir(rimraf) with correct options", (done) ->
|
2014-02-26 10:10:55 -05:00
|
|
|
@FSPersistorManager.deleteDirectory @location, @name1, (err) =>
|
2017-06-27 09:53:20 -04:00
|
|
|
@Rimraf.calledWith("#{@location}/#{@name1Filtered}").should.equal true
|
2014-02-26 10:10:55 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "Should propogate the error", (done) ->
|
|
|
|
@FSPersistorManager.deleteDirectory @location, @name1, (err) =>
|
|
|
|
err.should.equal @error
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "checkIfFileExists", ->
|
|
|
|
beforeEach ->
|
|
|
|
@Fs.exists.callsArgWith(1,true)
|
|
|
|
|
|
|
|
it "Should call exists with correct options", (done) ->
|
|
|
|
@FSPersistorManager.checkIfFileExists @location, @name1, (exists) =>
|
2017-06-27 09:53:20 -04:00
|
|
|
@Fs.exists.calledWith("#{@location}/#{@name1Filtered}").should.equal true
|
2014-02-26 10:10:55 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
# fs.exists simply returns false on any error, so...
|
|
|
|
it "should not return an error", (done) ->
|
|
|
|
@FSPersistorManager.checkIfFileExists @location, @name1, (err,exists) =>
|
|
|
|
expect(err).to.be.null
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "Should return true for existing files", (done) ->
|
|
|
|
@Fs.exists.callsArgWith(1,true)
|
|
|
|
@FSPersistorManager.checkIfFileExists @location, @name1, (err,exists) =>
|
|
|
|
exists.should.be.true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "Should return false for non-existing files", (done) ->
|
|
|
|
@Fs.exists.callsArgWith(1,false)
|
|
|
|
@FSPersistorManager.checkIfFileExists @location, @name1, (err,exists) =>
|
|
|
|
exists.should.be.false
|
|
|
|
done()
|
2016-03-13 15:22:14 -04:00
|
|
|
|
|
|
|
describe "directorySize", ->
|
|
|
|
|
2016-03-13 19:45:48 -04:00
|
|
|
it "should propogate the error", (done) ->
|
|
|
|
@Fs.readdir.callsArgWith(1, @error)
|
|
|
|
@FSPersistorManager.directorySize @location, @name1, (err, totalsize) =>
|
|
|
|
err.should.equal @error
|
|
|
|
done()
|
|
|
|
|
2016-03-13 15:22:14 -04:00
|
|
|
it "should sum directory files size", (done) ->
|
2016-03-13 19:45:48 -04:00
|
|
|
@Fs.readdir.callsArgWith(1, null, [ {'file1'}, {'file2'} ])
|
|
|
|
@Fs.fstatSync.returns({size : 1024})
|
|
|
|
@FSPersistorManager.directorySize @location, @name1, (err, totalsize) =>
|
|
|
|
expect(totalsize).to.equal 2048
|
2017-06-27 09:53:20 -04:00
|
|
|
done()
|