FileHandlerTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. const { Errors } = require('@overleaf/object-persistor')
  8. chai.use(require('sinon-chai'))
  9. chai.use(require('chai-as-promised'))
  10. describe('FileHandler', function () {
  11. let PersistorManager,
  12. LocalFileWriter,
  13. FileConverter,
  14. KeyBuilder,
  15. ImageOptimiser,
  16. FileHandler,
  17. Settings,
  18. fs
  19. const bucket = 'my_bucket'
  20. const key = `${new ObjectId()}/${new ObjectId()}`
  21. const convertedFolderKey = `${new ObjectId()}/${new ObjectId()}`
  22. const projectKey = `${new ObjectId()}/`
  23. const sourceStream = 'sourceStream'
  24. const convertedKey = 'convertedKey'
  25. const redirectUrl = 'https://wombat.potato/giraffe'
  26. const readStream = {
  27. stream: 'readStream',
  28. on: sinon.stub(),
  29. }
  30. beforeEach(function () {
  31. PersistorManager = {
  32. getObjectStream: sinon.stub().resolves(sourceStream),
  33. getRedirectUrl: sinon.stub().resolves(redirectUrl),
  34. checkIfObjectExists: sinon.stub().resolves(),
  35. deleteObject: sinon.stub().resolves(),
  36. deleteDirectory: sinon.stub().resolves(),
  37. sendStream: sinon.stub().resolves(),
  38. insertFile: sinon.stub().resolves(),
  39. sendFile: sinon.stub().resolves(),
  40. directorySize: sinon.stub().resolves(),
  41. }
  42. LocalFileWriter = {
  43. // the callback style is used for detached cleanup calls
  44. deleteFile: sinon.stub().yields(),
  45. promises: {
  46. writeStream: sinon.stub().resolves(),
  47. deleteFile: sinon.stub().resolves(),
  48. },
  49. }
  50. FileConverter = {
  51. promises: {
  52. convert: sinon.stub().resolves(),
  53. thumbnail: sinon.stub().resolves(),
  54. preview: sinon.stub().resolves(),
  55. },
  56. }
  57. KeyBuilder = {
  58. addCachingToKey: sinon.stub().returns(convertedKey),
  59. getConvertedFolderKey: sinon.stub().returns(convertedFolderKey),
  60. }
  61. ImageOptimiser = {
  62. promises: {
  63. compressPng: sinon.stub().resolves(),
  64. },
  65. }
  66. Settings = {
  67. filestore: {
  68. stores: { template_files: 'template_files', user_files: 'user_files' },
  69. },
  70. }
  71. fs = {
  72. createReadStream: sinon.stub().returns(readStream),
  73. }
  74. const ObjectPersistor = { Errors }
  75. FileHandler = SandboxedModule.require(modulePath, {
  76. requires: {
  77. './PersistorManager': PersistorManager,
  78. './LocalFileWriter': LocalFileWriter,
  79. './FileConverter': FileConverter,
  80. './KeyBuilder': KeyBuilder,
  81. './ImageOptimiser': ImageOptimiser,
  82. '@overleaf/settings': Settings,
  83. '@overleaf/object-persistor': ObjectPersistor,
  84. '@overleaf/metrics': {
  85. gauge: sinon.stub(),
  86. Timer: sinon.stub().returns({ done: sinon.stub() }),
  87. },
  88. fs,
  89. },
  90. globals: { console, process },
  91. })
  92. })
  93. describe('insertFile', function () {
  94. const stream = 'stream'
  95. it('should send file to the filestore', function (done) {
  96. FileHandler.insertFile(bucket, key, stream, err => {
  97. expect(err).not.to.exist
  98. expect(PersistorManager.sendStream).to.have.been.calledWith(
  99. bucket,
  100. key,
  101. stream
  102. )
  103. done()
  104. })
  105. })
  106. it('should not make a delete request for the convertedKey folder', function (done) {
  107. FileHandler.insertFile(bucket, key, stream, err => {
  108. expect(err).not.to.exist
  109. expect(PersistorManager.deleteDirectory).not.to.have.been.called
  110. done()
  111. })
  112. })
  113. it('should accept templates-api key format', function (done) {
  114. KeyBuilder.getConvertedFolderKey.returns(
  115. '5ecba29f1a294e007d0bccb4/v/0/pdf'
  116. )
  117. FileHandler.insertFile(bucket, key, stream, err => {
  118. expect(err).not.to.exist
  119. done()
  120. })
  121. })
  122. it('should throw an error when the key is in the wrong format', function (done) {
  123. KeyBuilder.getConvertedFolderKey.returns('wombat')
  124. FileHandler.insertFile(bucket, key, stream, err => {
  125. expect(err).to.exist
  126. done()
  127. })
  128. })
  129. })
  130. describe('deleteFile', function () {
  131. it('should tell the filestore manager to delete the file', function (done) {
  132. FileHandler.deleteFile(bucket, key, err => {
  133. expect(err).not.to.exist
  134. expect(PersistorManager.deleteObject).to.have.been.calledWith(
  135. bucket,
  136. key
  137. )
  138. done()
  139. })
  140. })
  141. it('should not tell the filestore manager to delete the cached folder', function (done) {
  142. FileHandler.deleteFile(bucket, key, err => {
  143. expect(err).not.to.exist
  144. expect(PersistorManager.deleteDirectory).not.to.have.been.called
  145. done()
  146. })
  147. })
  148. it('should accept templates-api key format', function (done) {
  149. KeyBuilder.getConvertedFolderKey.returns(
  150. '5ecba29f1a294e007d0bccb4/v/0/pdf'
  151. )
  152. FileHandler.deleteFile(bucket, key, err => {
  153. expect(err).not.to.exist
  154. done()
  155. })
  156. })
  157. it('should throw an error when the key is in the wrong format', function (done) {
  158. KeyBuilder.getConvertedFolderKey.returns('wombat')
  159. FileHandler.deleteFile(bucket, key, err => {
  160. expect(err).to.exist
  161. done()
  162. })
  163. })
  164. describe('when conversions are enabled', function () {
  165. beforeEach(function () {
  166. Settings.enableConversions = true
  167. })
  168. it('should delete the convertedKey folder for template files', function (done) {
  169. FileHandler.deleteFile(
  170. Settings.filestore.stores.template_files,
  171. key,
  172. err => {
  173. expect(err).not.to.exist
  174. expect(PersistorManager.deleteDirectory).to.have.been.calledWith(
  175. Settings.filestore.stores.template_files,
  176. convertedFolderKey
  177. )
  178. done()
  179. }
  180. )
  181. })
  182. it('should not delete the convertedKey folder for user files', function (done) {
  183. FileHandler.deleteFile(
  184. Settings.filestore.stores.user_files,
  185. key,
  186. err => {
  187. expect(err).not.to.exist
  188. expect(PersistorManager.deleteDirectory).to.not.have.been.called
  189. done()
  190. }
  191. )
  192. })
  193. })
  194. })
  195. describe('deleteProject', function () {
  196. it('should tell the filestore manager to delete the folder', function (done) {
  197. FileHandler.deleteProject(bucket, projectKey, err => {
  198. expect(err).not.to.exist
  199. expect(PersistorManager.deleteDirectory).to.have.been.calledWith(
  200. bucket,
  201. projectKey
  202. )
  203. done()
  204. })
  205. })
  206. it('should throw an error when the key is in the wrong format', function (done) {
  207. FileHandler.deleteProject(bucket, 'wombat', err => {
  208. expect(err).to.exist
  209. done()
  210. })
  211. })
  212. })
  213. describe('getFile', function () {
  214. it('should return the source stream no format or style are defined', function (done) {
  215. FileHandler.getFile(bucket, key, null, (err, stream) => {
  216. expect(err).not.to.exist
  217. expect(stream).to.equal(sourceStream)
  218. done()
  219. })
  220. })
  221. it('should pass options through to PersistorManager', function (done) {
  222. const options = { start: 0, end: 8 }
  223. FileHandler.getFile(bucket, key, options, err => {
  224. expect(err).not.to.exist
  225. expect(PersistorManager.getObjectStream).to.have.been.calledWith(
  226. bucket,
  227. key,
  228. options
  229. )
  230. done()
  231. })
  232. })
  233. describe('when a format is defined', function () {
  234. let result
  235. describe('when the file is not cached', function () {
  236. beforeEach(function (done) {
  237. FileHandler.getFile(bucket, key, { format: 'png' }, (err, stream) => {
  238. result = { err, stream }
  239. done()
  240. })
  241. })
  242. it('should convert the file', function () {
  243. expect(FileConverter.promises.convert).to.have.been.called
  244. })
  245. it('should compress the converted file', function () {
  246. expect(ImageOptimiser.promises.compressPng).to.have.been.called
  247. })
  248. it('should return the the converted stream', function () {
  249. expect(result.err).not.to.exist
  250. expect(result.stream).to.equal(readStream)
  251. expect(PersistorManager.getObjectStream).to.have.been.calledWith(
  252. bucket,
  253. key
  254. )
  255. })
  256. })
  257. describe('when the file is cached', function () {
  258. beforeEach(function (done) {
  259. PersistorManager.checkIfObjectExists = sinon.stub().resolves(true)
  260. FileHandler.getFile(bucket, key, { format: 'png' }, (err, stream) => {
  261. result = { err, stream }
  262. done()
  263. })
  264. })
  265. it('should not convert the file', function () {
  266. expect(FileConverter.promises.convert).not.to.have.been.called
  267. })
  268. it('should not compress the converted file again', function () {
  269. expect(ImageOptimiser.promises.compressPng).not.to.have.been.called
  270. })
  271. it('should return the cached stream', function () {
  272. expect(result.err).not.to.exist
  273. expect(result.stream).to.equal(sourceStream)
  274. expect(PersistorManager.getObjectStream).to.have.been.calledWith(
  275. bucket,
  276. convertedKey
  277. )
  278. })
  279. })
  280. })
  281. describe('when a style is defined', function () {
  282. it('generates a thumbnail when requested', function (done) {
  283. FileHandler.getFile(bucket, key, { style: 'thumbnail' }, err => {
  284. expect(err).not.to.exist
  285. expect(FileConverter.promises.thumbnail).to.have.been.called
  286. expect(FileConverter.promises.preview).not.to.have.been.called
  287. done()
  288. })
  289. })
  290. it('generates a preview when requested', function (done) {
  291. FileHandler.getFile(bucket, key, { style: 'preview' }, err => {
  292. expect(err).not.to.exist
  293. expect(FileConverter.promises.thumbnail).not.to.have.been.called
  294. expect(FileConverter.promises.preview).to.have.been.called
  295. done()
  296. })
  297. })
  298. })
  299. })
  300. describe('getRedirectUrl', function () {
  301. beforeEach(function () {
  302. Settings.filestore = {
  303. allowRedirects: true,
  304. stores: {
  305. userFiles: bucket,
  306. },
  307. }
  308. })
  309. it('should return a redirect url', function (done) {
  310. FileHandler.getRedirectUrl(bucket, key, (err, url) => {
  311. expect(err).not.to.exist
  312. expect(url).to.equal(redirectUrl)
  313. done()
  314. })
  315. })
  316. it('should call the persistor to get a redirect url', function (done) {
  317. FileHandler.getRedirectUrl(bucket, key, () => {
  318. expect(PersistorManager.getRedirectUrl).to.have.been.calledWith(
  319. bucket,
  320. key
  321. )
  322. done()
  323. })
  324. })
  325. it('should return null if options are supplied', function (done) {
  326. FileHandler.getRedirectUrl(
  327. bucket,
  328. key,
  329. { start: 100, end: 200 },
  330. (err, url) => {
  331. expect(err).not.to.exist
  332. expect(url).to.be.null
  333. done()
  334. }
  335. )
  336. })
  337. it('should return null if the bucket is not one of the defined ones', function (done) {
  338. FileHandler.getRedirectUrl('a_different_bucket', key, (err, url) => {
  339. expect(err).not.to.exist
  340. expect(url).to.be.null
  341. done()
  342. })
  343. })
  344. it('should return null if redirects are not enabled', function (done) {
  345. Settings.filestore.allowRedirects = false
  346. FileHandler.getRedirectUrl(bucket, key, (err, url) => {
  347. expect(err).not.to.exist
  348. expect(url).to.be.null
  349. done()
  350. })
  351. })
  352. })
  353. describe('getDirectorySize', function () {
  354. it('should call the filestore manager to get directory size', function (done) {
  355. FileHandler.getDirectorySize(bucket, key, err => {
  356. expect(err).not.to.exist
  357. expect(PersistorManager.directorySize).to.have.been.calledWith(
  358. bucket,
  359. key
  360. )
  361. done()
  362. })
  363. })
  364. })
  365. })