FileHandlerTests.coffee 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. it "should tell the filestore manager to delete the file", (done)->
  67. @handler.deleteFile @bucket, @key, =>
  68. @PersistorManager.deleteFile.calledWith(@bucket, @key).should.equal true
  69. done()
  70. it "should tell the filestore manager to delete the cached foler", (done)->
  71. @handler.deleteFile @bucket, @key, =>
  72. @PersistorManager.deleteFile.calledWith(@bucket, @stubbedConvetedKey).should.equal true
  73. done()
  74. describe "getFile", ->
  75. beforeEach ->
  76. @handler._getStandardFile = sinon.stub().callsArgWith(3)
  77. @handler._getConvertedFile = sinon.stub().callsArgWith(3)
  78. it "should call _getStandardFile if no format or style are defined", (done)->
  79. @handler.getFile @bucket, @key, null, =>
  80. @handler._getStandardFile.called.should.equal true
  81. @handler._getConvertedFile.called.should.equal false
  82. done()
  83. it "should call _getConvertedFile if a format is defined", (done)->
  84. @handler.getFile @bucket, @key, format:"png", =>
  85. @handler._getStandardFile.called.should.equal false
  86. @handler._getConvertedFile.called.should.equal true
  87. done()
  88. describe "_getStandardFile", ->
  89. beforeEach ->
  90. @fileStream = {on:->}
  91. @PersistorManager.getFileStream.callsArgWith(2, "err", @fileStream)
  92. it "should get the stream", (done)->
  93. @handler.getFile @bucket, @key, null, =>
  94. @PersistorManager.getFileStream.calledWith(@bucket, @key).should.equal true
  95. done()
  96. it "should return the stream and error", (done)->
  97. @handler.getFile @bucket, @key, null, (err, stream)=>
  98. err.should.equal "err"
  99. stream.should.equal @fileStream
  100. done()
  101. describe "_getConvertedFile", ->
  102. it "should getFileStream if it does exists", (done)->
  103. @PersistorManager.checkIfFileExists.callsArgWith(2, null, true)
  104. @PersistorManager.getFileStream.callsArgWith(2)
  105. @handler._getConvertedFile @bucket, @key, {}, =>
  106. @PersistorManager.getFileStream.calledWith(@bucket).should.equal true
  107. done()
  108. it "should call _getConvertedFileAndCache if it does exists", (done)->
  109. @PersistorManager.checkIfFileExists.callsArgWith(2, null, false)
  110. @handler._getConvertedFileAndCache = sinon.stub().callsArgWith(4)
  111. @handler._getConvertedFile @bucket, @key, {}, =>
  112. @handler._getConvertedFileAndCache.calledWith(@bucket, @key).should.equal true
  113. done()
  114. describe "_getConvertedFileAndCache", ->
  115. it "should _convertFile ", (done)->
  116. @stubbedStream = {"something":"here"}
  117. @PersistorManager.sendFile = sinon.stub().callsArgWith(3)
  118. @PersistorManager.getFileStream = sinon.stub().callsArgWith(2, null, @stubbedStream)
  119. @convetedKey = @key+"converted"
  120. @handler._convertFile = sinon.stub().callsArgWith(3, null, @stubbedPath)
  121. @ImageOptimiser.compressPng = sinon.stub().callsArgWith(1)
  122. @handler._getConvertedFileAndCache @bucket, @key, @convetedKey, {}, (err, fsStream)=>
  123. @handler._convertFile.called.should.equal true
  124. @PersistorManager.sendFile.calledWith(@bucket, @convetedKey, @stubbedPath).should.equal true
  125. @PersistorManager.getFileStream.calledWith(@bucket, @convetedKey).should.equal true
  126. @ImageOptimiser.compressPng.calledWith(@stubbedPath).should.equal true
  127. fsStream.should.equal @stubbedStream
  128. done()
  129. describe "_convertFile", ->
  130. beforeEach ->
  131. @FileConverter.convert.callsArgWith(2, null, @formattedStubbedPath)
  132. @FileConverter.thumbnail.callsArgWith(1, null, @formattedStubbedPath)
  133. @FileConverter.preview.callsArgWith(1, null, @formattedStubbedPath)
  134. @handler._writeS3FileToDisk = sinon.stub().callsArgWith(2, null, @stubbedPath)
  135. @LocalFileWriter.deleteFile.callsArgWith(1)
  136. it "should call thumbnail on the writer path if style was thumbnail was specified", (done)->
  137. @handler._convertFile @bucket, @key, style:"thumbnail", (err, path)=>
  138. path.should.equal @formattedStubbedPath
  139. @FileConverter.thumbnail.calledWith(@stubbedPath).should.equal true
  140. @LocalFileWriter.deleteFile.calledWith(@stubbedPath).should.equal true
  141. done()
  142. it "should call preview on the writer path if style was preview was specified", (done)->
  143. @handler._convertFile @bucket, @key, style:"preview", (err, path)=>
  144. path.should.equal @formattedStubbedPath
  145. @FileConverter.preview.calledWith(@stubbedPath).should.equal true
  146. @LocalFileWriter.deleteFile.calledWith(@stubbedPath).should.equal true
  147. done()
  148. it "should call convert on the writer path if a format was specified", (done)->
  149. @handler._convertFile @bucket, @key, format:@format, (err, path)=>
  150. path.should.equal @formattedStubbedPath
  151. @FileConverter.convert.calledWith(@stubbedPath, @format).should.equal true
  152. @LocalFileWriter.deleteFile.calledWith(@stubbedPath).should.equal true
  153. done()