add unit tests for fsWrapper

make other unit tests no longer depend on fsWrapper
This commit is contained in:
Oliver Matthews 2014-02-25 15:52:30 +00:00
parent c43533b5cb
commit 5f87dfb3bf
6 changed files with 118 additions and 37 deletions

View file

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

View file

@ -1,11 +0,0 @@
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

@ -7,7 +7,7 @@ module.exports =
# which backend to use # which backend to use
# current options are: # current options are:
# "s3" - Amazon S3 # "s3" - Amazon S3
filestoreWrapper: "s3" filestoreBackend: "s3"
# ShareLaTeX stores binary files like images in S3. # ShareLaTeX stores binary files like images in S3.
# Fill in your Amazon S3 credentials below. # Fill in your Amazon S3 credentials below.

View file

@ -9,8 +9,12 @@ SandboxedModule = require('sandboxed-module')
describe "FileController", -> describe "FileController", ->
beforeEach -> beforeEach ->
@FsWrapper =
sendStreamToS3: 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,8 +31,6 @@ describe "FileController", ->
"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"

View file

@ -1,4 +1,3 @@
assert = require("chai").assert assert = require("chai").assert
sinon = require('sinon') sinon = require('sinon')
chai = require('chai') chai = require('chai')
@ -11,10 +10,16 @@ describe "FileHandler", ->
beforeEach -> beforeEach ->
@settings = @settings =
filestreamWrapper:"test"
s3: s3:
buckets: buckets:
user_files:"user_files" user_files:"user_files"
@FsWrapper =
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 =
@ -26,8 +31,6 @@ 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":@FsWrapper "./fsWrapper":@FsWrapper

View file

@ -0,0 +1,96 @@
logger = require("logger-sharelatex")
assert = require("chai").assert
sinon = require('sinon')
chai = require('chai')
should = chai.should()
expect = chai.expect
modulePath = "../../../app/js/fsWrapper.js"
SandboxedModule = require('sandboxed-module')
describe "fsWrapperTests", ->
beforeEach ->
@s3Wrapper =
getFileStream: sinon.stub()
checkIfFileExists: sinon.stub()
deleteFile: sinon.stub()
deleteDirectory: sinon.stub()
sendStreamToS3: sinon.stub()
insertFile: sinon.stub()
describe "test s3 mixin", ->
beforeEach ->
@settings =
filestoreBackend: "s3"
@requires =
"./s3Wrapper": @s3Wrapper
"settings-sharelatex": @settings
"logger-sharelatex":
log:->
err:->
@fsWrapper = SandboxedModule.require modulePath, requires: @requires
it "should load getFileStream", (done) ->
@fsWrapper.should.respondTo("getFileStream")
@fsWrapper.getFileStream()
@s3Wrapper.getFileStream.calledOnce.should.equal true
done()
it "should load checkIfFileExists", (done) ->
@fsWrapper.checkIfFileExists()
@s3Wrapper.checkIfFileExists.calledOnce.should.equal true
done()
it "should load deleteFile", (done) ->
@fsWrapper.deleteFile()
@s3Wrapper.deleteFile.calledOnce.should.equal true
done()
it "should load deleteDirectory", (done) ->
@fsWrapper.deleteDirectory()
@s3Wrapper.deleteDirectory.calledOnce.should.equal true
done()
it "should load sendStreamToS3", (done) ->
@fsWrapper.sendStreamToS3()
@s3Wrapper.sendStreamToS3.calledOnce.should.equal true
done()
it "should load insertFile", (done) ->
@fsWrapper.insertFile()
@s3Wrapper.insertFile.calledOnce.should.equal true
done()
describe "test invalid mixins", ->
it "should not load a null wrapper", (done) ->
@settings =
@requires =
"./s3Wrapper": @s3Wrapper
"settings-sharelatex": @settings
"logger-sharelatex":
log:->
err:->
try
@fsWrapper=SandboxedModule.require modulePath, requires: @requires
catch error
assert.equal("Unknown filestore backend: null",error.message)
done()
it "should not load an invalid wrapper", (done) ->
@settings =
filestoreBackend:"magic"
@requires =
"./s3Wrapper": @s3Wrapper
"settings-sharelatex": @settings
"logger-sharelatex":
log:->
err:->
try
@fsWrapper=SandboxedModule.require modulePath, requires: @requires
catch error
assert.equal("Unknown filestore backend: magic",error.message)
done()