FileHandlerTests.js 11 KB

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