FileHandlerTests.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. const sinon = require('sinon')
  2. const chai = require('chai')
  3. const { expect } = chai
  4. const modulePath = '../../../app/js/FileHandler.js'
  5. const SandboxedModule = require('sandboxed-module')
  6. chai.use(require('sinon-chai'))
  7. chai.use(require('chai-as-promised'))
  8. describe('FileHandler', function() {
  9. let PersistorManager,
  10. LocalFileWriter,
  11. FileConverter,
  12. KeyBuilder,
  13. ImageOptimiser,
  14. FileHandler,
  15. fs
  16. const settings = {
  17. s3: {
  18. buckets: {
  19. user_files: 'user_files'
  20. }
  21. }
  22. }
  23. const bucket = 'my_bucket'
  24. const key = 'key/here'
  25. const convertedFolderKey = 'convertedFolder'
  26. const sourceStream = 'sourceStream'
  27. const convertedKey = 'convertedKey'
  28. const readStream = {
  29. stream: 'readStream',
  30. on: sinon.stub()
  31. }
  32. beforeEach(function() {
  33. PersistorManager = {
  34. promises: {
  35. getFileStream: sinon.stub().resolves(sourceStream),
  36. checkIfFileExists: sinon.stub().resolves(),
  37. deleteFile: sinon.stub().resolves(),
  38. deleteDirectory: sinon.stub().resolves(),
  39. sendStream: sinon.stub().resolves(),
  40. insertFile: sinon.stub().resolves(),
  41. sendFile: sinon.stub().resolves(),
  42. directorySize: sinon.stub().resolves()
  43. }
  44. }
  45. LocalFileWriter = {
  46. // the callback style is used for detached cleanup calls
  47. deleteFile: sinon.stub().yields(),
  48. promises: {
  49. writeStream: sinon.stub().resolves(),
  50. deleteFile: sinon.stub().resolves()
  51. }
  52. }
  53. FileConverter = {
  54. promises: {
  55. convert: sinon.stub().resolves(),
  56. thumbnail: sinon.stub().resolves(),
  57. preview: sinon.stub().resolves()
  58. }
  59. }
  60. KeyBuilder = {
  61. addCachingToKey: sinon.stub().returns(convertedKey),
  62. getConvertedFolderKey: sinon.stub().returns(convertedFolderKey)
  63. }
  64. ImageOptimiser = {
  65. promises: {
  66. compressPng: sinon.stub().resolves()
  67. }
  68. }
  69. fs = {
  70. createReadStream: sinon.stub().returns(readStream)
  71. }
  72. FileHandler = SandboxedModule.require(modulePath, {
  73. requires: {
  74. 'settings-sharelatex': settings,
  75. './PersistorManager': PersistorManager,
  76. './LocalFileWriter': LocalFileWriter,
  77. './FileConverter': FileConverter,
  78. './KeyBuilder': KeyBuilder,
  79. './ImageOptimiser': ImageOptimiser,
  80. fs: fs
  81. },
  82. globals: { console }
  83. })
  84. })
  85. describe('insertFile', function() {
  86. const stream = 'stream'
  87. it('should send file to the filestore', function(done) {
  88. FileHandler.insertFile(bucket, key, stream, err => {
  89. expect(err).not.to.exist
  90. expect(PersistorManager.promises.sendStream).to.have.been.calledWith(
  91. bucket,
  92. key,
  93. stream
  94. )
  95. done()
  96. })
  97. })
  98. it('should delete the convertedKey folder', function(done) {
  99. FileHandler.insertFile(bucket, key, stream, err => {
  100. expect(err).not.to.exist
  101. expect(
  102. PersistorManager.promises.deleteDirectory
  103. ).to.have.been.calledWith(bucket, convertedFolderKey)
  104. done()
  105. })
  106. })
  107. })
  108. describe('deleteFile', function() {
  109. it('should tell the filestore manager to delete the file', function(done) {
  110. FileHandler.deleteFile(bucket, key, err => {
  111. expect(err).not.to.exist
  112. expect(PersistorManager.promises.deleteFile).to.have.been.calledWith(
  113. bucket,
  114. key
  115. )
  116. done()
  117. })
  118. })
  119. it('should tell the filestore manager to delete the cached folder', function(done) {
  120. FileHandler.deleteFile(bucket, key, err => {
  121. expect(err).not.to.exist
  122. expect(
  123. PersistorManager.promises.deleteDirectory
  124. ).to.have.been.calledWith(bucket, convertedFolderKey)
  125. done()
  126. })
  127. })
  128. })
  129. describe('getFile', function() {
  130. it('should return the source stream no format or style are defined', function(done) {
  131. FileHandler.getFile(bucket, key, null, (err, stream) => {
  132. expect(err).not.to.exist
  133. expect(stream).to.equal(sourceStream)
  134. done()
  135. })
  136. })
  137. it('should pass options through to PersistorManager', function(done) {
  138. const options = { start: 0, end: 8 }
  139. FileHandler.getFile(bucket, key, options, err => {
  140. expect(err).not.to.exist
  141. expect(PersistorManager.promises.getFileStream).to.have.been.calledWith(
  142. bucket,
  143. key,
  144. options
  145. )
  146. done()
  147. })
  148. })
  149. describe('when a format is defined', function() {
  150. let result
  151. describe('when the file is not cached', function() {
  152. beforeEach(function(done) {
  153. FileHandler.getFile(bucket, key, { format: 'png' }, (err, stream) => {
  154. result = { err, stream }
  155. done()
  156. })
  157. })
  158. it('should convert the file', function() {
  159. expect(FileConverter.promises.convert).to.have.been.called
  160. })
  161. it('should compress the converted file', function() {
  162. expect(ImageOptimiser.promises.compressPng).to.have.been.called
  163. })
  164. it('should return the the converted stream', function() {
  165. expect(result.err).not.to.exist
  166. expect(result.stream).to.equal(readStream)
  167. expect(
  168. PersistorManager.promises.getFileStream
  169. ).to.have.been.calledWith(bucket, key)
  170. })
  171. })
  172. describe('when the file is cached', function() {
  173. beforeEach(function(done) {
  174. PersistorManager.promises.checkIfFileExists = sinon
  175. .stub()
  176. .resolves(true)
  177. FileHandler.getFile(bucket, key, { format: 'png' }, (err, stream) => {
  178. result = { err, stream }
  179. done()
  180. })
  181. })
  182. it('should not convert the file', function() {
  183. expect(FileConverter.promises.convert).not.to.have.been.called
  184. })
  185. it('should not compress the converted file again', function() {
  186. expect(ImageOptimiser.promises.compressPng).not.to.have.been.called
  187. })
  188. it('should return the cached stream', function() {
  189. expect(result.err).not.to.exist
  190. expect(result.stream).to.equal(sourceStream)
  191. expect(
  192. PersistorManager.promises.getFileStream
  193. ).to.have.been.calledWith(bucket, convertedKey)
  194. })
  195. })
  196. })
  197. describe('when a style is defined', function() {
  198. it('generates a thumbnail when requested', function(done) {
  199. FileHandler.getFile(bucket, key, { style: 'thumbnail' }, err => {
  200. expect(err).not.to.exist
  201. expect(FileConverter.promises.thumbnail).to.have.been.called
  202. expect(FileConverter.promises.preview).not.to.have.been.called
  203. done()
  204. })
  205. })
  206. it('generates a preview when requested', function(done) {
  207. FileHandler.getFile(bucket, key, { style: 'preview' }, err => {
  208. expect(err).not.to.exist
  209. expect(FileConverter.promises.thumbnail).not.to.have.been.called
  210. expect(FileConverter.promises.preview).to.have.been.called
  211. done()
  212. })
  213. })
  214. })
  215. })
  216. describe('getDirectorySize', function() {
  217. it('should call the filestore manager to get directory size', function(done) {
  218. FileHandler.getDirectorySize(bucket, key, err => {
  219. expect(err).not.to.exist
  220. expect(PersistorManager.promises.directorySize).to.have.been.calledWith(
  221. bucket,
  222. key
  223. )
  224. done()
  225. })
  226. })
  227. })
  228. })