2014-02-14 11:39:05 -05:00
|
|
|
assert = require("chai").assert
|
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
modulePath = "../../../app/js/FileHandler.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
|
|
|
|
describe "FileHandler", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@settings =
|
|
|
|
s3:
|
|
|
|
buckets:
|
|
|
|
user_files:"user_files"
|
2014-02-25 11:38:13 -05:00
|
|
|
@PersistorManager =
|
2014-02-25 10:52:30 -05:00
|
|
|
getFileStream: sinon.stub()
|
|
|
|
checkIfFileExists: sinon.stub()
|
|
|
|
deleteFile: sinon.stub()
|
|
|
|
deleteDirectory: sinon.stub()
|
2014-02-25 11:38:13 -05:00
|
|
|
sendStream: sinon.stub()
|
2014-02-25 10:52:30 -05:00
|
|
|
insertFile: sinon.stub()
|
2014-02-14 11:39:05 -05:00
|
|
|
@LocalFileWriter =
|
|
|
|
writeStream: sinon.stub()
|
2014-03-04 08:36:47 -05:00
|
|
|
deleteFile: sinon.stub()
|
2014-02-14 11:39:05 -05:00
|
|
|
@FileConverter =
|
|
|
|
convert: sinon.stub()
|
|
|
|
thumbnail: sinon.stub()
|
|
|
|
preview: sinon.stub()
|
2014-02-25 10:52:30 -05:00
|
|
|
@keyBuilder =
|
2014-02-14 11:39:05 -05:00
|
|
|
addCachingToKey: sinon.stub()
|
|
|
|
getConvertedFolderKey: sinon.stub()
|
|
|
|
@ImageOptimiser =
|
|
|
|
compressPng: sinon.stub()
|
|
|
|
@handler = SandboxedModule.require modulePath, requires:
|
|
|
|
"settings-sharelatex": @settings
|
2014-02-25 11:38:13 -05:00
|
|
|
"./PersistorManager":@PersistorManager
|
2014-02-14 11:39:05 -05:00
|
|
|
"./LocalFileWriter":@LocalFileWriter
|
|
|
|
"./FileConverter":@FileConverter
|
|
|
|
"./KeyBuilder": @keyBuilder
|
|
|
|
"./ImageOptimiser":@ImageOptimiser
|
|
|
|
"logger-sharelatex":
|
|
|
|
log:->
|
|
|
|
err:->
|
|
|
|
@bucket = "my_bucket"
|
|
|
|
@key = "key/here"
|
|
|
|
@stubbedPath = "/var/somewhere/path"
|
|
|
|
@format = "png"
|
|
|
|
@formattedStubbedPath = "#{@stubbedPath}.#{@format}"
|
|
|
|
|
|
|
|
describe "insertFile", ->
|
|
|
|
beforeEach ->
|
|
|
|
@stream = {}
|
2014-02-25 11:38:13 -05:00
|
|
|
@PersistorManager.deleteDirectory.callsArgWith(2)
|
|
|
|
@PersistorManager.sendStream.callsArgWith(3)
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2014-02-25 11:38:13 -05:00
|
|
|
it "should send file to the filestore", (done)->
|
2014-02-14 11:39:05 -05:00
|
|
|
@handler.insertFile @bucket, @key, @stream, =>
|
2014-02-25 11:38:13 -05:00
|
|
|
@PersistorManager.sendStream.calledWith(@bucket, @key, @stream).should.equal true
|
2014-02-14 11:39:05 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should delete the convetedKey folder", (done)->
|
|
|
|
@keyBuilder.getConvertedFolderKey.returns(@stubbedConvetedKey)
|
|
|
|
@handler.insertFile @bucket, @key, @stream, =>
|
2014-02-25 11:38:13 -05:00
|
|
|
@PersistorManager.deleteDirectory.calledWith(@bucket, @stubbedConvetedKey).should.equal true
|
2014-02-14 11:39:05 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
describe "deleteFile", ->
|
|
|
|
beforeEach ->
|
|
|
|
@keyBuilder.getConvertedFolderKey.returns(@stubbedConvetedKey)
|
2014-02-25 11:38:13 -05:00
|
|
|
@PersistorManager.deleteFile.callsArgWith(2)
|
2014-12-22 07:58:11 -05:00
|
|
|
@PersistorManager.deleteDirectory.callsArgWith(2)
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2014-02-25 11:38:13 -05:00
|
|
|
it "should tell the filestore manager to delete the file", (done)->
|
2014-02-14 11:39:05 -05:00
|
|
|
@handler.deleteFile @bucket, @key, =>
|
2014-02-25 11:38:13 -05:00
|
|
|
@PersistorManager.deleteFile.calledWith(@bucket, @key).should.equal true
|
2014-02-14 11:39:05 -05:00
|
|
|
done()
|
|
|
|
|
2014-02-25 11:38:13 -05:00
|
|
|
it "should tell the filestore manager to delete the cached foler", (done)->
|
2014-02-14 11:39:05 -05:00
|
|
|
@handler.deleteFile @bucket, @key, =>
|
2014-12-22 07:58:11 -05:00
|
|
|
@PersistorManager.deleteDirectory.calledWith(@bucket, @stubbedConvetedKey).should.equal true
|
2014-02-14 11:39:05 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
describe "getFile", ->
|
|
|
|
beforeEach ->
|
|
|
|
@handler._getStandardFile = sinon.stub().callsArgWith(3)
|
|
|
|
@handler._getConvertedFile = sinon.stub().callsArgWith(3)
|
|
|
|
|
|
|
|
it "should call _getStandardFile if no format or style are defined", (done)->
|
|
|
|
|
|
|
|
@handler.getFile @bucket, @key, null, =>
|
|
|
|
@handler._getStandardFile.called.should.equal true
|
|
|
|
@handler._getConvertedFile.called.should.equal false
|
|
|
|
done()
|
|
|
|
|
2015-08-28 08:50:40 -04:00
|
|
|
it "should pass options to _getStandardFile", (done) ->
|
|
|
|
options = {start: 0, end: 8}
|
|
|
|
@handler.getFile @bucket, @key, options, =>
|
|
|
|
expect(@handler._getStandardFile.lastCall.args[2].start).to.equal 0
|
|
|
|
expect(@handler._getStandardFile.lastCall.args[2].end).to.equal 8
|
|
|
|
done()
|
|
|
|
|
2014-02-14 11:39:05 -05:00
|
|
|
it "should call _getConvertedFile if a format is defined", (done)->
|
|
|
|
@handler.getFile @bucket, @key, format:"png", =>
|
|
|
|
@handler._getStandardFile.called.should.equal false
|
|
|
|
@handler._getConvertedFile.called.should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
|
|
|
|
describe "_getStandardFile", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@fileStream = {on:->}
|
2015-08-27 10:21:33 -04:00
|
|
|
@PersistorManager.getFileStream.callsArgWith(3, "err", @fileStream)
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2014-02-25 11:38:13 -05:00
|
|
|
it "should get the stream", (done)->
|
2014-02-14 11:39:05 -05:00
|
|
|
@handler.getFile @bucket, @key, null, =>
|
2014-02-25 11:38:13 -05:00
|
|
|
@PersistorManager.getFileStream.calledWith(@bucket, @key).should.equal true
|
2014-02-14 11:39:05 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return the stream and error", (done)->
|
|
|
|
@handler.getFile @bucket, @key, null, (err, stream)=>
|
|
|
|
err.should.equal "err"
|
|
|
|
stream.should.equal @fileStream
|
|
|
|
done()
|
|
|
|
|
2015-08-28 08:50:40 -04:00
|
|
|
it "should pass options to PersistorManager", (done) ->
|
|
|
|
@handler.getFile @bucket, @key, {start: 0, end: 8}, =>
|
|
|
|
expect(@PersistorManager.getFileStream.lastCall.args[2].start).to.equal 0
|
|
|
|
expect(@PersistorManager.getFileStream.lastCall.args[2].end).to.equal 8
|
|
|
|
done()
|
|
|
|
|
|
|
|
|
2014-02-14 11:39:05 -05:00
|
|
|
describe "_getConvertedFile", ->
|
|
|
|
|
|
|
|
it "should getFileStream if it does exists", (done)->
|
2014-02-25 11:38:13 -05:00
|
|
|
@PersistorManager.checkIfFileExists.callsArgWith(2, null, true)
|
2015-09-08 04:23:08 -04:00
|
|
|
@PersistorManager.getFileStream.callsArgWith(3)
|
2014-02-14 11:39:05 -05:00
|
|
|
@handler._getConvertedFile @bucket, @key, {}, =>
|
2014-02-25 11:38:13 -05:00
|
|
|
@PersistorManager.getFileStream.calledWith(@bucket).should.equal true
|
2014-02-14 11:39:05 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should call _getConvertedFileAndCache if it does exists", (done)->
|
2014-02-25 11:38:13 -05:00
|
|
|
@PersistorManager.checkIfFileExists.callsArgWith(2, null, false)
|
2014-02-14 11:39:05 -05:00
|
|
|
@handler._getConvertedFileAndCache = sinon.stub().callsArgWith(4)
|
|
|
|
@handler._getConvertedFile @bucket, @key, {}, =>
|
|
|
|
@handler._getConvertedFileAndCache.calledWith(@bucket, @key).should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "_getConvertedFileAndCache", ->
|
|
|
|
|
|
|
|
it "should _convertFile ", (done)->
|
2014-03-04 08:36:47 -05:00
|
|
|
@stubbedStream = {"something":"here"}
|
2014-02-25 11:38:13 -05:00
|
|
|
@PersistorManager.sendFile = sinon.stub().callsArgWith(3)
|
2015-09-08 04:23:08 -04:00
|
|
|
@PersistorManager.getFileStream = sinon.stub().callsArgWith(3, null, @stubbedStream)
|
2014-02-14 11:39:05 -05:00
|
|
|
@convetedKey = @key+"converted"
|
|
|
|
@handler._convertFile = sinon.stub().callsArgWith(3, null, @stubbedPath)
|
|
|
|
@ImageOptimiser.compressPng = sinon.stub().callsArgWith(1)
|
2014-03-04 08:36:47 -05:00
|
|
|
@handler._getConvertedFileAndCache @bucket, @key, @convetedKey, {}, (err, fsStream)=>
|
2014-02-14 11:39:05 -05:00
|
|
|
@handler._convertFile.called.should.equal true
|
2014-02-25 11:38:13 -05:00
|
|
|
@PersistorManager.sendFile.calledWith(@bucket, @convetedKey, @stubbedPath).should.equal true
|
|
|
|
@PersistorManager.getFileStream.calledWith(@bucket, @convetedKey).should.equal true
|
2014-02-14 11:39:05 -05:00
|
|
|
@ImageOptimiser.compressPng.calledWith(@stubbedPath).should.equal true
|
2014-03-04 08:36:47 -05:00
|
|
|
fsStream.should.equal @stubbedStream
|
2014-02-14 11:39:05 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
describe "_convertFile", ->
|
|
|
|
beforeEach ->
|
|
|
|
@FileConverter.convert.callsArgWith(2, null, @formattedStubbedPath)
|
|
|
|
@FileConverter.thumbnail.callsArgWith(1, null, @formattedStubbedPath)
|
|
|
|
@FileConverter.preview.callsArgWith(1, null, @formattedStubbedPath)
|
2015-09-08 04:23:08 -04:00
|
|
|
@handler._writeS3FileToDisk = sinon.stub().callsArgWith(3, null, @stubbedPath)
|
2014-03-04 08:36:47 -05:00
|
|
|
@LocalFileWriter.deleteFile.callsArgWith(1)
|
2014-02-14 11:39:05 -05:00
|
|
|
|
|
|
|
it "should call thumbnail on the writer path if style was thumbnail was specified", (done)->
|
|
|
|
@handler._convertFile @bucket, @key, style:"thumbnail", (err, path)=>
|
|
|
|
path.should.equal @formattedStubbedPath
|
|
|
|
@FileConverter.thumbnail.calledWith(@stubbedPath).should.equal true
|
2014-03-04 08:36:47 -05:00
|
|
|
@LocalFileWriter.deleteFile.calledWith(@stubbedPath).should.equal true
|
2014-02-14 11:39:05 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should call preview on the writer path if style was preview was specified", (done)->
|
|
|
|
@handler._convertFile @bucket, @key, style:"preview", (err, path)=>
|
|
|
|
path.should.equal @formattedStubbedPath
|
|
|
|
@FileConverter.preview.calledWith(@stubbedPath).should.equal true
|
2014-03-04 08:36:47 -05:00
|
|
|
@LocalFileWriter.deleteFile.calledWith(@stubbedPath).should.equal true
|
2014-02-14 11:39:05 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should call convert on the writer path if a format was specified", (done)->
|
|
|
|
@handler._convertFile @bucket, @key, format:@format, (err, path)=>
|
|
|
|
path.should.equal @formattedStubbedPath
|
|
|
|
@FileConverter.convert.calledWith(@stubbedPath, @format).should.equal true
|
2014-03-04 08:36:47 -05:00
|
|
|
@LocalFileWriter.deleteFile.calledWith(@stubbedPath).should.equal true
|
2014-02-14 11:39:05 -05:00
|
|
|
done()
|