FileHandlerTests.coffee 6.7 KB

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