FileHandlerTests.coffee 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. assert = require("chai").assert
  2. sinon = require('sinon')
  3. chai = require('chai')
  4. should = chai.should()
  5. expect = chai.expect
  6. modulePath = "../../../app/js/FileHandler.js"
  7. SandboxedModule = require('sandboxed-module')
  8. describe "FileHandler", ->
  9. beforeEach ->
  10. @settings =
  11. s3:
  12. buckets:
  13. user_files:"user_files"
  14. @s3Wrapper =
  15. getFileStream: sinon.stub()
  16. checkIfFileExists: sinon.stub()
  17. deleteFile: sinon.stub()
  18. deleteDirectory: sinon.stub()
  19. sendStreamToS3: sinon.stub()
  20. insertFile: sinon.stub()
  21. @LocalFileWriter =
  22. writeStream: sinon.stub()
  23. @FileConverter =
  24. convert: sinon.stub()
  25. thumbnail: sinon.stub()
  26. preview: sinon.stub()
  27. @keyBuilder =
  28. addCachingToKey: sinon.stub()
  29. getConvertedFolderKey: sinon.stub()
  30. @ImageOptimiser =
  31. compressPng: sinon.stub()
  32. @handler = SandboxedModule.require modulePath, requires:
  33. "settings-sharelatex": @settings
  34. "./s3Wrapper":@s3Wrapper
  35. "./LocalFileWriter":@LocalFileWriter
  36. "./FileConverter":@FileConverter
  37. "./KeyBuilder": @keyBuilder
  38. "./ImageOptimiser":@ImageOptimiser
  39. "logger-sharelatex":
  40. log:->
  41. err:->
  42. @bucket = "my_bucket"
  43. @key = "key/here"
  44. @stubbedPath = "/var/somewhere/path"
  45. @format = "png"
  46. @formattedStubbedPath = "#{@stubbedPath}.#{@format}"
  47. describe "insertFile", ->
  48. beforeEach ->
  49. @stream = {}
  50. @s3Wrapper.deleteDirectory.callsArgWith(2)
  51. @s3Wrapper.sendStreamToS3.callsArgWith(3)
  52. it "should send file to s3", (done)->
  53. @handler.insertFile @bucket, @key, @stream, =>
  54. @s3Wrapper.sendStreamToS3.calledWith(@bucket, @key, @stream).should.equal true
  55. done()
  56. it "should delete the convetedKey folder", (done)->
  57. @keyBuilder.getConvertedFolderKey.returns(@stubbedConvetedKey)
  58. @handler.insertFile @bucket, @key, @stream, =>
  59. @s3Wrapper.deleteDirectory.calledWith(@bucket, @stubbedConvetedKey).should.equal true
  60. done()
  61. describe "deleteFile", ->
  62. beforeEach ->
  63. @keyBuilder.getConvertedFolderKey.returns(@stubbedConvetedKey)
  64. @s3Wrapper.deleteFile.callsArgWith(2)
  65. it "should tell the s3 wrapper to delete the file", (done)->
  66. @handler.deleteFile @bucket, @key, =>
  67. @s3Wrapper.deleteFile.calledWith(@bucket, @key).should.equal true
  68. done()
  69. it "should tell the s3 wrapper to delete the cached foler", (done)->
  70. @handler.deleteFile @bucket, @key, =>
  71. @s3Wrapper.deleteFile.calledWith(@bucket, @stubbedConvetedKey).should.equal true
  72. done()
  73. describe "getFile", ->
  74. beforeEach ->
  75. @handler._getStandardFile = sinon.stub().callsArgWith(3)
  76. @handler._getConvertedFile = sinon.stub().callsArgWith(3)
  77. it "should call _getStandardFile if no format or style are defined", (done)->
  78. @handler.getFile @bucket, @key, null, =>
  79. @handler._getStandardFile.called.should.equal true
  80. @handler._getConvertedFile.called.should.equal false
  81. done()
  82. it "should call _getConvertedFile if a format is defined", (done)->
  83. @handler.getFile @bucket, @key, format:"png", =>
  84. @handler._getStandardFile.called.should.equal false
  85. @handler._getConvertedFile.called.should.equal true
  86. done()
  87. describe "_getStandardFile", ->
  88. beforeEach ->
  89. @fileStream = {on:->}
  90. @s3Wrapper.getFileStream.callsArgWith(2, "err", @fileStream)
  91. it "should get the stream from s3 ", (done)->
  92. @handler.getFile @bucket, @key, null, =>
  93. @s3Wrapper.getFileStream.calledWith(@bucket, @key).should.equal true
  94. done()
  95. it "should return the stream and error", (done)->
  96. @handler.getFile @bucket, @key, null, (err, stream)=>
  97. err.should.equal "err"
  98. stream.should.equal @fileStream
  99. done()
  100. describe "_getConvertedFile", ->
  101. it "should getFileStream if it does exists", (done)->
  102. @s3Wrapper.checkIfFileExists.callsArgWith(2, null, true)
  103. @s3Wrapper.getFileStream.callsArgWith(2)
  104. @handler._getConvertedFile @bucket, @key, {}, =>
  105. @s3Wrapper.getFileStream.calledWith(@bucket).should.equal true
  106. done()
  107. it "should call _getConvertedFileAndCache if it does exists", (done)->
  108. @s3Wrapper.checkIfFileExists.callsArgWith(2, null, false)
  109. @handler._getConvertedFileAndCache = sinon.stub().callsArgWith(4)
  110. @handler._getConvertedFile @bucket, @key, {}, =>
  111. @handler._getConvertedFileAndCache.calledWith(@bucket, @key).should.equal true
  112. done()
  113. describe "_getConvertedFileAndCache", ->
  114. it "should _convertFile ", (done)->
  115. @s3Wrapper.sendFileToS3 = sinon.stub().callsArgWith(3)
  116. @s3Wrapper.getFileStream = sinon.stub().callsArgWith(2)
  117. @convetedKey = @key+"converted"
  118. @handler._convertFile = sinon.stub().callsArgWith(3, null, @stubbedPath)
  119. @ImageOptimiser.compressPng = sinon.stub().callsArgWith(1)
  120. @handler._getConvertedFileAndCache @bucket, @key, @convetedKey, {}, =>
  121. @handler._convertFile.called.should.equal true
  122. @s3Wrapper.sendFileToS3.calledWith(@bucket, @convetedKey, @stubbedPath).should.equal true
  123. @s3Wrapper.getFileStream.calledWith(@bucket, @convetedKey).should.equal true
  124. @ImageOptimiser.compressPng.calledWith(@stubbedPath).should.equal true
  125. done()
  126. describe "_convertFile", ->
  127. beforeEach ->
  128. @FileConverter.convert.callsArgWith(2, null, @formattedStubbedPath)
  129. @FileConverter.thumbnail.callsArgWith(1, null, @formattedStubbedPath)
  130. @FileConverter.preview.callsArgWith(1, null, @formattedStubbedPath)
  131. @handler._writeS3FileToDisk = sinon.stub().callsArgWith(2, null, @stubbedPath)
  132. it "should call thumbnail on the writer path if style was thumbnail was specified", (done)->
  133. @handler._convertFile @bucket, @key, style:"thumbnail", (err, path)=>
  134. path.should.equal @formattedStubbedPath
  135. @FileConverter.thumbnail.calledWith(@stubbedPath).should.equal true
  136. done()
  137. it "should call preview on the writer path if style was preview was specified", (done)->
  138. @handler._convertFile @bucket, @key, style:"preview", (err, path)=>
  139. path.should.equal @formattedStubbedPath
  140. @FileConverter.preview.calledWith(@stubbedPath).should.equal true
  141. done()
  142. it "should call convert on the writer path if a format was specified", (done)->
  143. @handler._convertFile @bucket, @key, format:@format, (err, path)=>
  144. path.should.equal @formattedStubbedPath
  145. @FileConverter.convert.calledWith(@stubbedPath, @format).should.equal true
  146. done()