FileHandlerTests.js 8.5 KB

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