move test wrapper into fsWrapper where appropriate

This commit is contained in:
Oliver Matthews 2014-02-25 10:54:51 +00:00
parent f68080d56c
commit c43533b5cb
4 changed files with 58 additions and 44 deletions

View file

@ -1,9 +1,19 @@
settings = require("settings-sharelatex") settings = require("settings-sharelatex")
wrappedFs = switch settings.filestoreWrapper logger = require("logger-sharelatex")
when "s3" then require("./s3Wrapper") s3Wrapper = require("./s3Wrapper")
testWrapper = require("./testWrapper")
module.exports =
selectBackend: (backend) ->
wrappedFs = switch backend
when "s3" then s3Wrapper
when "test" then testWrapper
else null else null
if !wrappedFs if !wrappedFs
throw new Error( "Unknown filestore wrapper #{settings.filestoreWrapper}" ) throw new Error( "Unknown filestore wrapper #{backend}" )
module.exports[name] = method for name,method of wrappedFs module.exports[name] = method for name,method of wrappedFs
if settings.fileStoreWrapper?
module.exports.selectBackend(settings.fileStoreWrapper)

View file

@ -0,0 +1,11 @@
sinon = require('sinon')
logger = require("logger-sharelatex")
module.exports =
getFileStream: sinon.stub()
checkIfFileExists: sinon.stub()
deleteFile: sinon.stub()
deleteDirectory: sinon.stub()
sendStreamToS3: sinon.stub()
insertFile: sinon.stub()
copyFile: sinon.stub()

View file

@ -9,13 +9,8 @@ SandboxedModule = require('sandboxed-module')
describe "FileController", -> describe "FileController", ->
beforeEach -> beforeEach ->
@s3Wrapper =
sendStreamToS3: sinon.stub()
getAndPipe: sinon.stub()
copyFile: sinon.stub()
deleteFile:sinon.stub()
@settings = @settings =
filestreamWrapper:"test"
s3: s3:
buckets: buckets:
user_files:"user_files" user_files:"user_files"
@ -27,11 +22,13 @@ describe "FileController", ->
@controller = SandboxedModule.require modulePath, requires: @controller = SandboxedModule.require modulePath, requires:
"./LocalFileWriter":@LocalFileWriter "./LocalFileWriter":@LocalFileWriter
"./FileHandler": @FileHandler "./FileHandler": @FileHandler
"./fsWrapper":@s3Wrapper "./fsWrapper":@FsWrapper
"settings-sharelatex": @settings "settings-sharelatex": @settings
"logger-sharelatex": "logger-sharelatex":
log:-> log:->
err:-> err:->
@FsWrapper = require("../../../app/js/fsWrapper.js")
@FsWrapper.selectBackend("test")
@project_id = "project_id" @project_id = "project_id"
@file_id = "file_id" @file_id = "file_id"
@bucket = "user_files" @bucket = "user_files"
@ -74,7 +71,7 @@ describe "FileController", ->
describe "insertFile", -> describe "insertFile", ->
it "should send bucket name key and res to s3Wrapper", (done)-> it "should send bucket name key and res to FsWrapper", (done)->
@FileHandler.insertFile.callsArgWith(3) @FileHandler.insertFile.callsArgWith(3)
@res.send = => @res.send = =>
@FileHandler.insertFile.calledWith(@bucket, @key, @req).should.equal true @FileHandler.insertFile.calledWith(@bucket, @key, @req).should.equal true
@ -91,17 +88,17 @@ describe "FileController", ->
project_id: @oldProject_id project_id: @oldProject_id
file_id: @oldFile_id file_id: @oldFile_id
it "should send bucket name and both keys to s3Wrapper", (done)-> it "should send bucket name and both keys to FsWrapper", (done)->
@s3Wrapper.copyFile.callsArgWith(3) @FsWrapper.copyFile.callsArgWith(3)
@res.send = (code)=> @res.send = (code)=>
code.should.equal 200 code.should.equal 200
@s3Wrapper.copyFile.calledWith(@bucket, "#{@oldProject_id}/#{@oldFile_id}", @key).should.equal true @FsWrapper.copyFile.calledWith(@bucket, "#{@oldProject_id}/#{@oldFile_id}", @key).should.equal true
done() done()
@controller.copyFile @req, @res @controller.copyFile @req, @res
it "should send a 500 if there was an error", (done)-> it "should send a 500 if there was an error", (done)->
@s3Wrapper.copyFile.callsArgWith(3, "error") @FsWrapper.copyFile.callsArgWith(3, "error")
@res.send = (code)=> @res.send = (code)=>
code.should.equal 500 code.should.equal 500
done() done()

View file

@ -11,16 +11,10 @@ describe "FileHandler", ->
beforeEach -> beforeEach ->
@settings = @settings =
filestreamWrapper:"test"
s3: s3:
buckets: buckets:
user_files:"user_files" user_files:"user_files"
@s3Wrapper =
getFileStream: sinon.stub()
checkIfFileExists: sinon.stub()
deleteFile: sinon.stub()
deleteDirectory: sinon.stub()
sendStreamToS3: sinon.stub()
insertFile: sinon.stub()
@LocalFileWriter = @LocalFileWriter =
writeStream: sinon.stub() writeStream: sinon.stub()
@FileConverter = @FileConverter =
@ -32,9 +26,11 @@ describe "FileHandler", ->
getConvertedFolderKey: sinon.stub() getConvertedFolderKey: sinon.stub()
@ImageOptimiser = @ImageOptimiser =
compressPng: sinon.stub() compressPng: sinon.stub()
@FsWrapper = require("../../../app/js/fsWrapper.js")
@FsWrapper.selectBackend("test")
@handler = SandboxedModule.require modulePath, requires: @handler = SandboxedModule.require modulePath, requires:
"settings-sharelatex": @settings "settings-sharelatex": @settings
"./fsWrapper":@s3Wrapper "./fsWrapper":@FsWrapper
"./LocalFileWriter":@LocalFileWriter "./LocalFileWriter":@LocalFileWriter
"./FileConverter":@FileConverter "./FileConverter":@FileConverter
"./KeyBuilder": @keyBuilder "./KeyBuilder": @keyBuilder
@ -51,33 +47,33 @@ describe "FileHandler", ->
describe "insertFile", -> describe "insertFile", ->
beforeEach -> beforeEach ->
@stream = {} @stream = {}
@s3Wrapper.deleteDirectory.callsArgWith(2) @FsWrapper.deleteDirectory.callsArgWith(2)
@s3Wrapper.sendStreamToS3.callsArgWith(3) @FsWrapper.sendStreamToS3.callsArgWith(3)
it "should send file to s3", (done)-> it "should send file to s3", (done)->
@handler.insertFile @bucket, @key, @stream, => @handler.insertFile @bucket, @key, @stream, =>
@s3Wrapper.sendStreamToS3.calledWith(@bucket, @key, @stream).should.equal true @FsWrapper.sendStreamToS3.calledWith(@bucket, @key, @stream).should.equal true
done() done()
it "should delete the convetedKey folder", (done)-> it "should delete the convetedKey folder", (done)->
@keyBuilder.getConvertedFolderKey.returns(@stubbedConvetedKey) @keyBuilder.getConvertedFolderKey.returns(@stubbedConvetedKey)
@handler.insertFile @bucket, @key, @stream, => @handler.insertFile @bucket, @key, @stream, =>
@s3Wrapper.deleteDirectory.calledWith(@bucket, @stubbedConvetedKey).should.equal true @FsWrapper.deleteDirectory.calledWith(@bucket, @stubbedConvetedKey).should.equal true
done() done()
describe "deleteFile", -> describe "deleteFile", ->
beforeEach -> beforeEach ->
@keyBuilder.getConvertedFolderKey.returns(@stubbedConvetedKey) @keyBuilder.getConvertedFolderKey.returns(@stubbedConvetedKey)
@s3Wrapper.deleteFile.callsArgWith(2) @FsWrapper.deleteFile.callsArgWith(2)
it "should tell the s3 wrapper to delete the file", (done)-> it "should tell the s3 wrapper to delete the file", (done)->
@handler.deleteFile @bucket, @key, => @handler.deleteFile @bucket, @key, =>
@s3Wrapper.deleteFile.calledWith(@bucket, @key).should.equal true @FsWrapper.deleteFile.calledWith(@bucket, @key).should.equal true
done() done()
it "should tell the s3 wrapper to delete the cached foler", (done)-> it "should tell the s3 wrapper to delete the cached foler", (done)->
@handler.deleteFile @bucket, @key, => @handler.deleteFile @bucket, @key, =>
@s3Wrapper.deleteFile.calledWith(@bucket, @stubbedConvetedKey).should.equal true @FsWrapper.deleteFile.calledWith(@bucket, @stubbedConvetedKey).should.equal true
done() done()
describe "getFile", -> describe "getFile", ->
@ -103,11 +99,11 @@ describe "FileHandler", ->
beforeEach -> beforeEach ->
@fileStream = {on:->} @fileStream = {on:->}
@s3Wrapper.getFileStream.callsArgWith(2, "err", @fileStream) @FsWrapper.getFileStream.callsArgWith(2, "err", @fileStream)
it "should get the stream from s3 ", (done)-> it "should get the stream from s3 ", (done)->
@handler.getFile @bucket, @key, null, => @handler.getFile @bucket, @key, null, =>
@s3Wrapper.getFileStream.calledWith(@bucket, @key).should.equal true @FsWrapper.getFileStream.calledWith(@bucket, @key).should.equal true
done() done()
it "should return the stream and error", (done)-> it "should return the stream and error", (done)->
@ -119,14 +115,14 @@ describe "FileHandler", ->
describe "_getConvertedFile", -> describe "_getConvertedFile", ->
it "should getFileStream if it does exists", (done)-> it "should getFileStream if it does exists", (done)->
@s3Wrapper.checkIfFileExists.callsArgWith(2, null, true) @FsWrapper.checkIfFileExists.callsArgWith(2, null, true)
@s3Wrapper.getFileStream.callsArgWith(2) @FsWrapper.getFileStream.callsArgWith(2)
@handler._getConvertedFile @bucket, @key, {}, => @handler._getConvertedFile @bucket, @key, {}, =>
@s3Wrapper.getFileStream.calledWith(@bucket).should.equal true @FsWrapper.getFileStream.calledWith(@bucket).should.equal true
done() done()
it "should call _getConvertedFileAndCache if it does exists", (done)-> it "should call _getConvertedFileAndCache if it does exists", (done)->
@s3Wrapper.checkIfFileExists.callsArgWith(2, null, false) @FsWrapper.checkIfFileExists.callsArgWith(2, null, false)
@handler._getConvertedFileAndCache = sinon.stub().callsArgWith(4) @handler._getConvertedFileAndCache = sinon.stub().callsArgWith(4)
@handler._getConvertedFile @bucket, @key, {}, => @handler._getConvertedFile @bucket, @key, {}, =>
@handler._getConvertedFileAndCache.calledWith(@bucket, @key).should.equal true @handler._getConvertedFileAndCache.calledWith(@bucket, @key).should.equal true
@ -135,15 +131,15 @@ describe "FileHandler", ->
describe "_getConvertedFileAndCache", -> describe "_getConvertedFileAndCache", ->
it "should _convertFile ", (done)-> it "should _convertFile ", (done)->
@s3Wrapper.sendFileToS3 = sinon.stub().callsArgWith(3) @FsWrapper.sendFileToS3 = sinon.stub().callsArgWith(3)
@s3Wrapper.getFileStream = sinon.stub().callsArgWith(2) @FsWrapper.getFileStream = sinon.stub().callsArgWith(2)
@convetedKey = @key+"converted" @convetedKey = @key+"converted"
@handler._convertFile = sinon.stub().callsArgWith(3, null, @stubbedPath) @handler._convertFile = sinon.stub().callsArgWith(3, null, @stubbedPath)
@ImageOptimiser.compressPng = sinon.stub().callsArgWith(1) @ImageOptimiser.compressPng = sinon.stub().callsArgWith(1)
@handler._getConvertedFileAndCache @bucket, @key, @convetedKey, {}, => @handler._getConvertedFileAndCache @bucket, @key, @convetedKey, {}, =>
@handler._convertFile.called.should.equal true @handler._convertFile.called.should.equal true
@s3Wrapper.sendFileToS3.calledWith(@bucket, @convetedKey, @stubbedPath).should.equal true @FsWrapper.sendFileToS3.calledWith(@bucket, @convetedKey, @stubbedPath).should.equal true
@s3Wrapper.getFileStream.calledWith(@bucket, @convetedKey).should.equal true @FsWrapper.getFileStream.calledWith(@bucket, @convetedKey).should.equal true
@ImageOptimiser.compressPng.calledWith(@stubbedPath).should.equal true @ImageOptimiser.compressPng.calledWith(@stubbedPath).should.equal true
done() done()