Просмотр исходного кода

move test wrapper into fsWrapper where appropriate

Oliver Matthews 12 лет назад
Родитель
Сommit
c43533b5cb

+ 16 - 6
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)

+ 11 - 0
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()

+ 10 - 13
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()

+ 21 - 25
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()