FileHandlerTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 redirectUrl = 'https://wombat.potato/giraffe'
  25. const readStream = {
  26. stream: 'readStream',
  27. on: sinon.stub()
  28. }
  29. beforeEach(function() {
  30. PersistorManager = {
  31. promises: {
  32. getFileStream: sinon.stub().resolves(sourceStream),
  33. getRedirectUrl: sinon.stub().resolves(redirectUrl),
  34. checkIfFileExists: sinon.stub().resolves(),
  35. deleteFile: 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. }
  43. LocalFileWriter = {
  44. // the callback style is used for detached cleanup calls
  45. deleteFile: sinon.stub().yields(),
  46. promises: {
  47. writeStream: sinon.stub().resolves(),
  48. deleteFile: sinon.stub().resolves()
  49. }
  50. }
  51. FileConverter = {
  52. promises: {
  53. convert: sinon.stub().resolves(),
  54. thumbnail: sinon.stub().resolves(),
  55. preview: sinon.stub().resolves()
  56. }
  57. }
  58. KeyBuilder = {
  59. addCachingToKey: sinon.stub().returns(convertedKey),
  60. getConvertedFolderKey: sinon.stub().returns(convertedFolderKey)
  61. }
  62. ImageOptimiser = {
  63. promises: {
  64. compressPng: sinon.stub().resolves()
  65. }
  66. }
  67. Settings = {}
  68. fs = {
  69. createReadStream: sinon.stub().returns(readStream)
  70. }
  71. FileHandler = SandboxedModule.require(modulePath, {
  72. requires: {
  73. './PersistorManager': PersistorManager,
  74. './LocalFileWriter': LocalFileWriter,
  75. './FileConverter': FileConverter,
  76. './KeyBuilder': KeyBuilder,
  77. './ImageOptimiser': ImageOptimiser,
  78. 'settings-sharelatex': Settings,
  79. fs: fs
  80. },
  81. globals: { console }
  82. })
  83. })
  84. describe('insertFile', function() {
  85. const stream = 'stream'
  86. it('should send file to the filestore', function(done) {
  87. FileHandler.insertFile(bucket, key, stream, err => {
  88. expect(err).not.to.exist
  89. expect(PersistorManager.promises.sendStream).to.have.been.calledWith(
  90. bucket,
  91. key,
  92. stream
  93. )
  94. done()
  95. })
  96. })
  97. it('should not make a delete request for the convertedKey folder', function(done) {
  98. FileHandler.insertFile(bucket, key, stream, err => {
  99. expect(err).not.to.exist
  100. expect(PersistorManager.promises.deleteDirectory).not.to.have.been
  101. .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(
  129. PersistorManager.promises.deleteDirectory
  130. ).to.have.been.calledWith(bucket, convertedFolderKey)
  131. done()
  132. })
  133. })
  134. })
  135. })
  136. describe('deleteFile', function() {
  137. it('should tell the filestore manager to delete the file', function(done) {
  138. FileHandler.deleteFile(bucket, key, err => {
  139. expect(err).not.to.exist
  140. expect(PersistorManager.promises.deleteFile).to.have.been.calledWith(
  141. bucket,
  142. key
  143. )
  144. done()
  145. })
  146. })
  147. it('should not tell the filestore manager to delete the cached folder', function(done) {
  148. FileHandler.deleteFile(bucket, key, err => {
  149. expect(err).not.to.exist
  150. expect(PersistorManager.promises.deleteDirectory).not.to.have.been
  151. .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(
  179. PersistorManager.promises.deleteDirectory
  180. ).to.have.been.calledWith(bucket, convertedFolderKey)
  181. done()
  182. })
  183. })
  184. })
  185. })
  186. describe('deleteProject', function() {
  187. it('should tell the filestore manager to delete the folder', function(done) {
  188. FileHandler.deleteProject(bucket, projectKey, err => {
  189. expect(err).not.to.exist
  190. expect(
  191. PersistorManager.promises.deleteDirectory
  192. ).to.have.been.calledWith(bucket, projectKey)
  193. done()
  194. })
  195. })
  196. it('should throw an error when the key is in the wrong format', function(done) {
  197. FileHandler.deleteProject(bucket, 'wombat', err => {
  198. expect(err).to.exist
  199. done()
  200. })
  201. })
  202. })
  203. describe('getFile', function() {
  204. it('should return the source stream no format or style are defined', function(done) {
  205. FileHandler.getFile(bucket, key, null, (err, stream) => {
  206. expect(err).not.to.exist
  207. expect(stream).to.equal(sourceStream)
  208. done()
  209. })
  210. })
  211. it('should pass options through to PersistorManager', function(done) {
  212. const options = { start: 0, end: 8 }
  213. FileHandler.getFile(bucket, key, options, err => {
  214. expect(err).not.to.exist
  215. expect(PersistorManager.promises.getFileStream).to.have.been.calledWith(
  216. bucket,
  217. key,
  218. options
  219. )
  220. done()
  221. })
  222. })
  223. describe('when a format is defined', function() {
  224. let result
  225. describe('when the file is not cached', function() {
  226. beforeEach(function(done) {
  227. FileHandler.getFile(bucket, key, { format: 'png' }, (err, stream) => {
  228. result = { err, stream }
  229. done()
  230. })
  231. })
  232. it('should convert the file', function() {
  233. expect(FileConverter.promises.convert).to.have.been.called
  234. })
  235. it('should compress the converted file', function() {
  236. expect(ImageOptimiser.promises.compressPng).to.have.been.called
  237. })
  238. it('should return the the converted stream', function() {
  239. expect(result.err).not.to.exist
  240. expect(result.stream).to.equal(readStream)
  241. expect(
  242. PersistorManager.promises.getFileStream
  243. ).to.have.been.calledWith(bucket, key)
  244. })
  245. })
  246. describe('when the file is cached', function() {
  247. beforeEach(function(done) {
  248. PersistorManager.promises.checkIfFileExists = sinon
  249. .stub()
  250. .resolves(true)
  251. FileHandler.getFile(bucket, key, { format: 'png' }, (err, stream) => {
  252. result = { err, stream }
  253. done()
  254. })
  255. })
  256. it('should not convert the file', function() {
  257. expect(FileConverter.promises.convert).not.to.have.been.called
  258. })
  259. it('should not compress the converted file again', function() {
  260. expect(ImageOptimiser.promises.compressPng).not.to.have.been.called
  261. })
  262. it('should return the cached stream', function() {
  263. expect(result.err).not.to.exist
  264. expect(result.stream).to.equal(sourceStream)
  265. expect(
  266. PersistorManager.promises.getFileStream
  267. ).to.have.been.calledWith(bucket, convertedKey)
  268. })
  269. })
  270. })
  271. describe('when a style is defined', function() {
  272. it('generates a thumbnail when requested', function(done) {
  273. FileHandler.getFile(bucket, key, { style: 'thumbnail' }, err => {
  274. expect(err).not.to.exist
  275. expect(FileConverter.promises.thumbnail).to.have.been.called
  276. expect(FileConverter.promises.preview).not.to.have.been.called
  277. done()
  278. })
  279. })
  280. it('generates a preview when requested', function(done) {
  281. FileHandler.getFile(bucket, key, { style: 'preview' }, err => {
  282. expect(err).not.to.exist
  283. expect(FileConverter.promises.thumbnail).not.to.have.been.called
  284. expect(FileConverter.promises.preview).to.have.been.called
  285. done()
  286. })
  287. })
  288. })
  289. })
  290. describe('getRedirectUrl', function() {
  291. beforeEach(function() {
  292. Settings.filestore = {
  293. allowRedirects: true,
  294. stores: {
  295. userFiles: bucket
  296. }
  297. }
  298. })
  299. it('should return a redirect url', function(done) {
  300. FileHandler.getRedirectUrl(bucket, key, (err, url) => {
  301. expect(err).not.to.exist
  302. expect(url).to.equal(redirectUrl)
  303. done()
  304. })
  305. })
  306. it('should call the persistor to get a redirect url', function(done) {
  307. FileHandler.getRedirectUrl(bucket, key, () => {
  308. expect(
  309. PersistorManager.promises.getRedirectUrl
  310. ).to.have.been.calledWith(bucket, key)
  311. done()
  312. })
  313. })
  314. it('should return null if options are supplied', function(done) {
  315. FileHandler.getRedirectUrl(
  316. bucket,
  317. key,
  318. { start: 100, end: 200 },
  319. (err, url) => {
  320. expect(err).not.to.exist
  321. expect(url).to.be.null
  322. done()
  323. }
  324. )
  325. })
  326. it('should return null if the bucket is not one of the defined ones', function(done) {
  327. FileHandler.getRedirectUrl('a_different_bucket', key, (err, url) => {
  328. expect(err).not.to.exist
  329. expect(url).to.be.null
  330. done()
  331. })
  332. })
  333. it('should return null if redirects are not enabled', function(done) {
  334. Settings.filestore.allowRedirects = false
  335. FileHandler.getRedirectUrl(bucket, key, (err, url) => {
  336. expect(err).not.to.exist
  337. expect(url).to.be.null
  338. done()
  339. })
  340. })
  341. })
  342. describe('getDirectorySize', function() {
  343. it('should call the filestore manager to get directory size', function(done) {
  344. FileHandler.getDirectorySize(bucket, key, err => {
  345. expect(err).not.to.exist
  346. expect(PersistorManager.promises.directorySize).to.have.been.calledWith(
  347. bucket,
  348. key
  349. )
  350. done()
  351. })
  352. })
  353. })
  354. })