From f68080d56c5b5a9afefdc40ebd78d7ce741de956 Mon Sep 17 00:00:00 2001 From: Oliver Matthews Date: Tue, 25 Feb 2014 09:10:22 +0000 Subject: [PATCH 1/3] create mixin to allow switching of backend --- .../app/coffee/FileController.coffee | 8 ++--- .../filestore/app/coffee/FileHandler.coffee | 29 +++++++++---------- .../filestore/app/coffee/fsWrapper.coffee | 9 ++++++ .../config/settings.development.coffee | 5 ++++ .../unit/coffee/FileControllerTests.coffee | 2 +- .../test/unit/coffee/FileHandlerTests.coffee | 4 +-- 6 files changed, 34 insertions(+), 23 deletions(-) create mode 100644 services/filestore/app/coffee/fsWrapper.coffee diff --git a/services/filestore/app/coffee/FileController.coffee b/services/filestore/app/coffee/FileController.coffee index 83c02334e3..85046b222a 100644 --- a/services/filestore/app/coffee/FileController.coffee +++ b/services/filestore/app/coffee/FileController.coffee @@ -1,4 +1,4 @@ -s3Wrapper = require("./s3Wrapper") +fsWrapper = require("./fsWrapper") settings = require("settings-sharelatex") logger = require("logger-sharelatex") FileHandler = require("./FileHandler") @@ -37,9 +37,9 @@ module.exports = oldProject_id = req.body.source.project_id oldFile_id = req.body.source.file_id logger.log key:key, bucket:bucket, oldProject_id:oldProject_id, oldFile_id:oldFile_id, "reciving request to copy file" - s3Wrapper.copyFile bucket, "#{oldProject_id}/#{oldFile_id}", key, (err)-> + fsWrapper.copyFile bucket, "#{oldProject_id}/#{oldFile_id}", key, (err)-> if err? - logger.log err:err, oldProject_id:oldProject_id, oldFile_id:oldFile_id, "something went wrong copying file in s3Wrapper" + logger.log err:err, oldProject_id:oldProject_id, oldFile_id:oldFile_id, "something went wrong copying file" res.send 500 else res.send 200 @@ -50,7 +50,7 @@ module.exports = logger.log key:key, bucket:bucket, "reciving request to delete file" FileHandler.deleteFile bucket, key, (err)-> if err? - logger.log err:err, key:key, bucket:bucket, "something went wrong deleting file in s3Wrapper" + logger.log err:err, key:key, bucket:bucket, "something went wrong deleting file" res.send 500 else res.send 204 diff --git a/services/filestore/app/coffee/FileHandler.coffee b/services/filestore/app/coffee/FileHandler.coffee index 6d647fdac5..eea1469845 100644 --- a/services/filestore/app/coffee/FileHandler.coffee +++ b/services/filestore/app/coffee/FileHandler.coffee @@ -1,5 +1,5 @@ settings = require("settings-sharelatex") -s3Wrapper = require("./s3Wrapper") +fsWrapper = require("./fsWrapper") LocalFileWriter = require("./LocalFileWriter") logger = require("logger-sharelatex") FileConverter = require("./FileConverter") @@ -12,15 +12,15 @@ module.exports = insertFile: (bucket, key, stream, callback)-> convetedKey = KeyBuilder.getConvertedFolderKey(key) - s3Wrapper.deleteDirectory bucket, convetedKey, -> - s3Wrapper.sendStreamToS3 bucket, key, stream, -> + fsWrapper.deleteDirectory bucket, convetedKey, -> + fsWrapper.sendStreamToS3 bucket, key, stream, -> callback() deleteFile: (bucket, key, callback)-> convetedKey = KeyBuilder.getConvertedFolderKey(bucket, key) async.parallel [ - (done)-> s3Wrapper.deleteFile bucket, key, done - (done)-> s3Wrapper.deleteFile bucket, convetedKey, done + (done)-> fsWrapper.deleteFile bucket, key, done + (done)-> fsWrapper.deleteFile bucket, convetedKey, done ], callback getFile: (bucket, key, opts = {}, callback)-> @@ -31,16 +31,16 @@ module.exports = @_getConvertedFile bucket, key, opts, callback _getStandardFile: (bucket, key, opts, callback)-> - s3Wrapper.getFileStream bucket, key, (err, fileStream)-> + fsWrapper.getFileStream bucket, key, (err, fileStream)-> if err? logger.err bucket:bucket, key:key, opts:opts, "error getting fileStream" callback err, fileStream _getConvertedFile: (bucket, key, opts, callback)-> convetedKey = KeyBuilder.addCachingToKey(key, opts) - s3Wrapper.checkIfFileExists bucket, convetedKey, (err, exists)=> + fsWrapper.checkIfFileExists bucket, convetedKey, (err, exists)=> if exists - s3Wrapper.getFileStream bucket, convetedKey, callback + fsWrapper.getFileStream bucket, convetedKey, callback else @_getConvertedFileAndCache bucket, key, convetedKey, opts, callback @@ -53,13 +53,13 @@ module.exports = if err? logger.err err:err, fsPath:fsPath, bucket:bucket, key:key, opts:opts, "something went wrong optimising png file" return callback(err) - s3Wrapper.sendFileToS3 bucket, convetedKey, fsPath, (err)-> + fsWrapper.sendFileToS3 bucket, convetedKey, fsPath, (err)-> if err? - logger.err err:err, bucket:bucket, key:key, convetedKey:convetedKey, opts:opts, "something went wrong seing file to s3" + logger.err err:err, bucket:bucket, key:key, convetedKey:convetedKey, opts:opts, "something went wrong sending the file" return callback(err) - s3Wrapper.getFileStream bucket, convetedKey, callback + fsWrapper.getFileStream bucket, convetedKey, callback - _convertFile: (bucket, origonalKey, opts, callback)-> + _convertFile: (bucket, origonalKey, opts, callback)-> @_writeS3FileToDisk bucket, origonalKey, (err, origonalFsPath)-> if opts.format? FileConverter.convert origonalFsPath, opts.format, callback @@ -72,9 +72,6 @@ module.exports = _writeS3FileToDisk: (bucket, key, callback)-> - s3Wrapper.getFileStream bucket, key, (err, fileStream)-> + fsWrapper.getFileStream bucket, key, (err, fileStream)-> LocalFileWriter.writeStream fileStream, key, callback - - - diff --git a/services/filestore/app/coffee/fsWrapper.coffee b/services/filestore/app/coffee/fsWrapper.coffee new file mode 100644 index 0000000000..3815fcae12 --- /dev/null +++ b/services/filestore/app/coffee/fsWrapper.coffee @@ -0,0 +1,9 @@ +settings = require("settings-sharelatex") +wrappedFs = switch settings.filestoreWrapper + when "s3" then require("./s3Wrapper") + else null + +if !wrappedFs + throw new Error( "Unknown filestore wrapper #{settings.filestoreWrapper}" ) + +module.exports[name] = method for name,method of wrappedFs diff --git a/services/filestore/config/settings.development.coffee b/services/filestore/config/settings.development.coffee index 3f390837d8..f4f0c482e1 100644 --- a/services/filestore/config/settings.development.coffee +++ b/services/filestore/config/settings.development.coffee @@ -4,6 +4,11 @@ module.exports = port: 3009 host: "localhost" + # which backend to use + # current options are: + # "s3" - Amazon S3 + filestoreWrapper: "s3" + # ShareLaTeX stores binary files like images in S3. # Fill in your Amazon S3 credentials below. s3: diff --git a/services/filestore/test/unit/coffee/FileControllerTests.coffee b/services/filestore/test/unit/coffee/FileControllerTests.coffee index 29bc9d8205..fa13fe91b4 100644 --- a/services/filestore/test/unit/coffee/FileControllerTests.coffee +++ b/services/filestore/test/unit/coffee/FileControllerTests.coffee @@ -27,7 +27,7 @@ describe "FileController", -> @controller = SandboxedModule.require modulePath, requires: "./LocalFileWriter":@LocalFileWriter "./FileHandler": @FileHandler - "./s3Wrapper":@s3Wrapper + "./fsWrapper":@s3Wrapper "settings-sharelatex": @settings "logger-sharelatex": log:-> diff --git a/services/filestore/test/unit/coffee/FileHandlerTests.coffee b/services/filestore/test/unit/coffee/FileHandlerTests.coffee index 0b2ea034fe..f5c8184f78 100644 --- a/services/filestore/test/unit/coffee/FileHandlerTests.coffee +++ b/services/filestore/test/unit/coffee/FileHandlerTests.coffee @@ -34,7 +34,7 @@ describe "FileHandler", -> compressPng: sinon.stub() @handler = SandboxedModule.require modulePath, requires: "settings-sharelatex": @settings - "./s3Wrapper":@s3Wrapper + "./fsWrapper":@s3Wrapper "./LocalFileWriter":@LocalFileWriter "./FileConverter":@FileConverter "./KeyBuilder": @keyBuilder @@ -174,4 +174,4 @@ describe "FileHandler", -> - \ No newline at end of file + From c43533b5cbb37c94cd7d1046a59384886a131fad Mon Sep 17 00:00:00 2001 From: Oliver Matthews Date: Tue, 25 Feb 2014 10:54:51 +0000 Subject: [PATCH 2/3] move test wrapper into fsWrapper where appropriate --- .../filestore/app/coffee/fsWrapper.coffee | 22 ++++++--- .../filestore/app/coffee/testWrapper.coffee | 11 +++++ .../unit/coffee/FileControllerTests.coffee | 23 ++++------ .../test/unit/coffee/FileHandlerTests.coffee | 46 +++++++++---------- 4 files changed, 58 insertions(+), 44 deletions(-) create mode 100644 services/filestore/app/coffee/testWrapper.coffee diff --git a/services/filestore/app/coffee/fsWrapper.coffee b/services/filestore/app/coffee/fsWrapper.coffee index 3815fcae12..aff75d79f8 100644 --- a/services/filestore/app/coffee/fsWrapper.coffee +++ b/services/filestore/app/coffee/fsWrapper.coffee @@ -1,9 +1,19 @@ settings = require("settings-sharelatex") -wrappedFs = switch settings.filestoreWrapper - when "s3" then require("./s3Wrapper") - else null +logger = require("logger-sharelatex") +s3Wrapper = require("./s3Wrapper") +testWrapper = require("./testWrapper") -if !wrappedFs - throw new Error( "Unknown filestore wrapper #{settings.filestoreWrapper}" ) +module.exports = + selectBackend: (backend) -> + wrappedFs = switch backend + when "s3" then s3Wrapper + when "test" then testWrapper + else null -module.exports[name] = method for name,method of wrappedFs + 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) diff --git a/services/filestore/app/coffee/testWrapper.coffee b/services/filestore/app/coffee/testWrapper.coffee new file mode 100644 index 0000000000..966d4869b0 --- /dev/null +++ b/services/filestore/app/coffee/testWrapper.coffee @@ -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() diff --git a/services/filestore/test/unit/coffee/FileControllerTests.coffee b/services/filestore/test/unit/coffee/FileControllerTests.coffee index fa13fe91b4..7ff2c5f14c 100644 --- a/services/filestore/test/unit/coffee/FileControllerTests.coffee +++ b/services/filestore/test/unit/coffee/FileControllerTests.coffee @@ -9,13 +9,8 @@ SandboxedModule = require('sandboxed-module') describe "FileController", -> beforeEach -> - @s3Wrapper = - sendStreamToS3: sinon.stub() - getAndPipe: sinon.stub() - copyFile: sinon.stub() - deleteFile:sinon.stub() - @settings = + filestreamWrapper:"test" s3: buckets: user_files:"user_files" @@ -27,11 +22,13 @@ describe "FileController", -> @controller = SandboxedModule.require modulePath, requires: "./LocalFileWriter":@LocalFileWriter "./FileHandler": @FileHandler - "./fsWrapper":@s3Wrapper + "./fsWrapper":@FsWrapper "settings-sharelatex": @settings "logger-sharelatex": log:-> err:-> + @FsWrapper = require("../../../app/js/fsWrapper.js") + @FsWrapper.selectBackend("test") @project_id = "project_id" @file_id = "file_id" @bucket = "user_files" @@ -43,7 +40,7 @@ describe "FileController", -> params: project_id:@project_id file_id:@file_id - @res = + @res = setHeader: -> @fileStream = {} @@ -74,7 +71,7 @@ describe "FileController", -> 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) @res.send = => @FileHandler.insertFile.calledWith(@bucket, @key, @req).should.equal true @@ -91,17 +88,17 @@ describe "FileController", -> project_id: @oldProject_id file_id: @oldFile_id - it "should send bucket name and both keys to s3Wrapper", (done)-> - @s3Wrapper.copyFile.callsArgWith(3) + it "should send bucket name and both keys to FsWrapper", (done)-> + @FsWrapper.copyFile.callsArgWith(3) @res.send = (code)=> 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() @controller.copyFile @req, @res it "should send a 500 if there was an error", (done)-> - @s3Wrapper.copyFile.callsArgWith(3, "error") + @FsWrapper.copyFile.callsArgWith(3, "error") @res.send = (code)=> code.should.equal 500 done() diff --git a/services/filestore/test/unit/coffee/FileHandlerTests.coffee b/services/filestore/test/unit/coffee/FileHandlerTests.coffee index f5c8184f78..d642031263 100644 --- a/services/filestore/test/unit/coffee/FileHandlerTests.coffee +++ b/services/filestore/test/unit/coffee/FileHandlerTests.coffee @@ -11,16 +11,10 @@ describe "FileHandler", -> beforeEach -> @settings = + filestreamWrapper:"test" s3: buckets: user_files:"user_files" - @s3Wrapper = - getFileStream: sinon.stub() - checkIfFileExists: sinon.stub() - deleteFile: sinon.stub() - deleteDirectory: sinon.stub() - sendStreamToS3: sinon.stub() - insertFile: sinon.stub() @LocalFileWriter = writeStream: sinon.stub() @FileConverter = @@ -32,9 +26,11 @@ describe "FileHandler", -> getConvertedFolderKey: sinon.stub() @ImageOptimiser = compressPng: sinon.stub() + @FsWrapper = require("../../../app/js/fsWrapper.js") + @FsWrapper.selectBackend("test") @handler = SandboxedModule.require modulePath, requires: "settings-sharelatex": @settings - "./fsWrapper":@s3Wrapper + "./fsWrapper":@FsWrapper "./LocalFileWriter":@LocalFileWriter "./FileConverter":@FileConverter "./KeyBuilder": @keyBuilder @@ -51,33 +47,33 @@ describe "FileHandler", -> describe "insertFile", -> beforeEach -> @stream = {} - @s3Wrapper.deleteDirectory.callsArgWith(2) - @s3Wrapper.sendStreamToS3.callsArgWith(3) + @FsWrapper.deleteDirectory.callsArgWith(2) + @FsWrapper.sendStreamToS3.callsArgWith(3) it "should send file to s3", (done)-> @handler.insertFile @bucket, @key, @stream, => - @s3Wrapper.sendStreamToS3.calledWith(@bucket, @key, @stream).should.equal true + @FsWrapper.sendStreamToS3.calledWith(@bucket, @key, @stream).should.equal true done() it "should delete the convetedKey folder", (done)-> @keyBuilder.getConvertedFolderKey.returns(@stubbedConvetedKey) @handler.insertFile @bucket, @key, @stream, => - @s3Wrapper.deleteDirectory.calledWith(@bucket, @stubbedConvetedKey).should.equal true + @FsWrapper.deleteDirectory.calledWith(@bucket, @stubbedConvetedKey).should.equal true done() describe "deleteFile", -> beforeEach -> @keyBuilder.getConvertedFolderKey.returns(@stubbedConvetedKey) - @s3Wrapper.deleteFile.callsArgWith(2) + @FsWrapper.deleteFile.callsArgWith(2) it "should tell the s3 wrapper to delete the file", (done)-> @handler.deleteFile @bucket, @key, => - @s3Wrapper.deleteFile.calledWith(@bucket, @key).should.equal true + @FsWrapper.deleteFile.calledWith(@bucket, @key).should.equal true done() it "should tell the s3 wrapper to delete the cached foler", (done)-> @handler.deleteFile @bucket, @key, => - @s3Wrapper.deleteFile.calledWith(@bucket, @stubbedConvetedKey).should.equal true + @FsWrapper.deleteFile.calledWith(@bucket, @stubbedConvetedKey).should.equal true done() describe "getFile", -> @@ -103,11 +99,11 @@ describe "FileHandler", -> beforeEach -> @fileStream = {on:->} - @s3Wrapper.getFileStream.callsArgWith(2, "err", @fileStream) + @FsWrapper.getFileStream.callsArgWith(2, "err", @fileStream) it "should get the stream from s3 ", (done)-> @handler.getFile @bucket, @key, null, => - @s3Wrapper.getFileStream.calledWith(@bucket, @key).should.equal true + @FsWrapper.getFileStream.calledWith(@bucket, @key).should.equal true done() it "should return the stream and error", (done)-> @@ -119,14 +115,14 @@ describe "FileHandler", -> describe "_getConvertedFile", -> it "should getFileStream if it does exists", (done)-> - @s3Wrapper.checkIfFileExists.callsArgWith(2, null, true) - @s3Wrapper.getFileStream.callsArgWith(2) + @FsWrapper.checkIfFileExists.callsArgWith(2, null, true) + @FsWrapper.getFileStream.callsArgWith(2) @handler._getConvertedFile @bucket, @key, {}, => - @s3Wrapper.getFileStream.calledWith(@bucket).should.equal true + @FsWrapper.getFileStream.calledWith(@bucket).should.equal true 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._getConvertedFile @bucket, @key, {}, => @handler._getConvertedFileAndCache.calledWith(@bucket, @key).should.equal true @@ -135,15 +131,15 @@ describe "FileHandler", -> describe "_getConvertedFileAndCache", -> it "should _convertFile ", (done)-> - @s3Wrapper.sendFileToS3 = sinon.stub().callsArgWith(3) - @s3Wrapper.getFileStream = sinon.stub().callsArgWith(2) + @FsWrapper.sendFileToS3 = sinon.stub().callsArgWith(3) + @FsWrapper.getFileStream = sinon.stub().callsArgWith(2) @convetedKey = @key+"converted" @handler._convertFile = sinon.stub().callsArgWith(3, null, @stubbedPath) @ImageOptimiser.compressPng = sinon.stub().callsArgWith(1) @handler._getConvertedFileAndCache @bucket, @key, @convetedKey, {}, => @handler._convertFile.called.should.equal true - @s3Wrapper.sendFileToS3.calledWith(@bucket, @convetedKey, @stubbedPath).should.equal true - @s3Wrapper.getFileStream.calledWith(@bucket, @convetedKey).should.equal true + @FsWrapper.sendFileToS3.calledWith(@bucket, @convetedKey, @stubbedPath).should.equal true + @FsWrapper.getFileStream.calledWith(@bucket, @convetedKey).should.equal true @ImageOptimiser.compressPng.calledWith(@stubbedPath).should.equal true done() From 5f87dfb3bf5b36bee17119f39c0b5d2799d54da4 Mon Sep 17 00:00:00 2001 From: Oliver Matthews Date: Tue, 25 Feb 2014 15:52:30 +0000 Subject: [PATCH 3/3] add unit tests for fsWrapper make other unit tests no longer depend on fsWrapper --- .../filestore/app/coffee/fsWrapper.coffee | 21 ++-- .../filestore/app/coffee/testWrapper.coffee | 11 --- .../config/settings.development.coffee | 2 +- .../unit/coffee/FileControllerTests.coffee | 12 ++- .../test/unit/coffee/FileHandlerTests.coffee | 13 ++- .../test/unit/coffee/fsWrapperTests.coffee | 96 +++++++++++++++++++ 6 files changed, 118 insertions(+), 37 deletions(-) delete mode 100644 services/filestore/app/coffee/testWrapper.coffee create mode 100644 services/filestore/test/unit/coffee/fsWrapperTests.coffee diff --git a/services/filestore/app/coffee/fsWrapper.coffee b/services/filestore/app/coffee/fsWrapper.coffee index aff75d79f8..be761cd07e 100644 --- a/services/filestore/app/coffee/fsWrapper.coffee +++ b/services/filestore/app/coffee/fsWrapper.coffee @@ -1,19 +1,10 @@ settings = require("settings-sharelatex") logger = require("logger-sharelatex") s3Wrapper = require("./s3Wrapper") -testWrapper = require("./testWrapper") -module.exports = - selectBackend: (backend) -> - wrappedFs = switch backend - when "s3" then s3Wrapper - when "test" then testWrapper - else null - - 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) +logger.log backend:settings.filestoreBackend,"Loading backend" +module.exports = switch settings.filestoreBackend + when "s3" + s3Wrapper + else + throw new Error( "Unknown filestore backend: #{settings.filestoreBackend}" ) diff --git a/services/filestore/app/coffee/testWrapper.coffee b/services/filestore/app/coffee/testWrapper.coffee deleted file mode 100644 index 966d4869b0..0000000000 --- a/services/filestore/app/coffee/testWrapper.coffee +++ /dev/null @@ -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() diff --git a/services/filestore/config/settings.development.coffee b/services/filestore/config/settings.development.coffee index f4f0c482e1..7bd2ea59a1 100644 --- a/services/filestore/config/settings.development.coffee +++ b/services/filestore/config/settings.development.coffee @@ -7,7 +7,7 @@ module.exports = # which backend to use # current options are: # "s3" - Amazon S3 - filestoreWrapper: "s3" + filestoreBackend: "s3" # ShareLaTeX stores binary files like images in S3. # Fill in your Amazon S3 credentials below. diff --git a/services/filestore/test/unit/coffee/FileControllerTests.coffee b/services/filestore/test/unit/coffee/FileControllerTests.coffee index 7ff2c5f14c..8a6a671dab 100644 --- a/services/filestore/test/unit/coffee/FileControllerTests.coffee +++ b/services/filestore/test/unit/coffee/FileControllerTests.coffee @@ -9,8 +9,12 @@ SandboxedModule = require('sandboxed-module') describe "FileController", -> beforeEach -> + @FsWrapper = + sendStreamToS3: sinon.stub() + copyFile: sinon.stub() + deleteFile:sinon.stub() + @settings = - filestreamWrapper:"test" s3: buckets: user_files:"user_files" @@ -27,17 +31,15 @@ describe "FileController", -> "logger-sharelatex": log:-> err:-> - @FsWrapper = require("../../../app/js/fsWrapper.js") - @FsWrapper.selectBackend("test") @project_id = "project_id" @file_id = "file_id" @bucket = "user_files" @key = "#{@project_id}/#{@file_id}" - @req = + @req = key:@key bucket:@bucket query:{} - params: + params: project_id:@project_id file_id:@file_id @res = diff --git a/services/filestore/test/unit/coffee/FileHandlerTests.coffee b/services/filestore/test/unit/coffee/FileHandlerTests.coffee index d642031263..2c23c2ec17 100644 --- a/services/filestore/test/unit/coffee/FileHandlerTests.coffee +++ b/services/filestore/test/unit/coffee/FileHandlerTests.coffee @@ -1,4 +1,3 @@ - assert = require("chai").assert sinon = require('sinon') chai = require('chai') @@ -11,23 +10,27 @@ describe "FileHandler", -> beforeEach -> @settings = - filestreamWrapper:"test" s3: buckets: user_files:"user_files" + @FsWrapper = + getFileStream: sinon.stub() + checkIfFileExists: sinon.stub() + deleteFile: sinon.stub() + deleteDirectory: sinon.stub() + sendStreamToS3: sinon.stub() + insertFile: sinon.stub() @LocalFileWriter = writeStream: sinon.stub() @FileConverter = convert: sinon.stub() thumbnail: sinon.stub() preview: sinon.stub() - @keyBuilder = + @keyBuilder = addCachingToKey: sinon.stub() getConvertedFolderKey: sinon.stub() @ImageOptimiser = compressPng: sinon.stub() - @FsWrapper = require("../../../app/js/fsWrapper.js") - @FsWrapper.selectBackend("test") @handler = SandboxedModule.require modulePath, requires: "settings-sharelatex": @settings "./fsWrapper":@FsWrapper diff --git a/services/filestore/test/unit/coffee/fsWrapperTests.coffee b/services/filestore/test/unit/coffee/fsWrapperTests.coffee new file mode 100644 index 0000000000..d8543766b2 --- /dev/null +++ b/services/filestore/test/unit/coffee/fsWrapperTests.coffee @@ -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() + +