FileHandlerTests.js 9.4 KB

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