FileHandlerTests.js 12 KB

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