FileHandlerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const { assert } = require('chai')
  14. const sinon = require('sinon')
  15. const chai = require('chai')
  16. const should = chai.should()
  17. const { expect } = chai
  18. const modulePath = '../../../app/js/FileHandler.js'
  19. const SandboxedModule = require('sandboxed-module')
  20. describe('FileHandler', function() {
  21. beforeEach(function() {
  22. this.settings = {
  23. s3: {
  24. buckets: {
  25. user_files: 'user_files'
  26. }
  27. }
  28. }
  29. this.PersistorManager = {
  30. getFileStream: sinon.stub(),
  31. checkIfFileExists: sinon.stub(),
  32. deleteFile: sinon.stub(),
  33. deleteDirectory: sinon.stub(),
  34. sendStream: sinon.stub(),
  35. insertFile: sinon.stub(),
  36. directorySize: sinon.stub()
  37. }
  38. this.LocalFileWriter = {
  39. writeStream: sinon.stub(),
  40. getStream: sinon.stub(),
  41. deleteFile: sinon.stub()
  42. }
  43. this.FileConverter = {
  44. convert: sinon.stub(),
  45. thumbnail: sinon.stub(),
  46. preview: sinon.stub()
  47. }
  48. this.keyBuilder = {
  49. addCachingToKey: sinon.stub(),
  50. getConvertedFolderKey: sinon.stub()
  51. }
  52. this.ImageOptimiser = { compressPng: sinon.stub() }
  53. this.handler = SandboxedModule.require(modulePath, {
  54. requires: {
  55. 'settings-sharelatex': this.settings,
  56. './PersistorManager': this.PersistorManager,
  57. './LocalFileWriter': this.LocalFileWriter,
  58. './FileConverter': this.FileConverter,
  59. './KeyBuilder': this.keyBuilder,
  60. './ImageOptimiser': this.ImageOptimiser,
  61. 'logger-sharelatex': {
  62. log() {},
  63. err() {}
  64. }
  65. }
  66. })
  67. this.bucket = 'my_bucket'
  68. this.key = 'key/here'
  69. this.stubbedPath = '/var/somewhere/path'
  70. this.format = 'png'
  71. return (this.formattedStubbedPath = `${this.stubbedPath}.${this.format}`)
  72. })
  73. describe('insertFile', function() {
  74. beforeEach(function() {
  75. this.stream = {}
  76. this.PersistorManager.deleteDirectory.callsArgWith(2)
  77. return this.PersistorManager.sendStream.callsArgWith(3)
  78. })
  79. it('should send file to the filestore', function(done) {
  80. return this.handler.insertFile(this.bucket, this.key, this.stream, () => {
  81. this.PersistorManager.sendStream
  82. .calledWith(this.bucket, this.key, this.stream)
  83. .should.equal(true)
  84. return done()
  85. })
  86. })
  87. return it('should delete the convetedKey folder', function(done) {
  88. this.keyBuilder.getConvertedFolderKey.returns(this.stubbedConvetedKey)
  89. return this.handler.insertFile(this.bucket, this.key, this.stream, () => {
  90. this.PersistorManager.deleteDirectory
  91. .calledWith(this.bucket, this.stubbedConvetedKey)
  92. .should.equal(true)
  93. return done()
  94. })
  95. })
  96. })
  97. describe('deleteFile', function() {
  98. beforeEach(function() {
  99. this.keyBuilder.getConvertedFolderKey.returns(this.stubbedConvetedKey)
  100. this.PersistorManager.deleteFile.callsArgWith(2)
  101. return this.PersistorManager.deleteDirectory.callsArgWith(2)
  102. })
  103. it('should tell the filestore manager to delete the file', function(done) {
  104. return this.handler.deleteFile(this.bucket, this.key, () => {
  105. this.PersistorManager.deleteFile
  106. .calledWith(this.bucket, this.key)
  107. .should.equal(true)
  108. return done()
  109. })
  110. })
  111. return it('should tell the filestore manager to delete the cached foler', function(done) {
  112. return this.handler.deleteFile(this.bucket, this.key, () => {
  113. this.PersistorManager.deleteDirectory
  114. .calledWith(this.bucket, this.stubbedConvetedKey)
  115. .should.equal(true)
  116. return done()
  117. })
  118. })
  119. })
  120. describe('getFile', function() {
  121. beforeEach(function() {
  122. this.handler._getStandardFile = sinon.stub().callsArgWith(3)
  123. return (this.handler._getConvertedFile = sinon.stub().callsArgWith(3))
  124. })
  125. it('should call _getStandardFile if no format or style are defined', function(done) {
  126. return this.handler.getFile(this.bucket, this.key, null, () => {
  127. this.handler._getStandardFile.called.should.equal(true)
  128. this.handler._getConvertedFile.called.should.equal(false)
  129. return done()
  130. })
  131. })
  132. it('should pass options to _getStandardFile', function(done) {
  133. const options = { start: 0, end: 8 }
  134. return this.handler.getFile(this.bucket, this.key, options, () => {
  135. expect(this.handler._getStandardFile.lastCall.args[2].start).to.equal(0)
  136. expect(this.handler._getStandardFile.lastCall.args[2].end).to.equal(8)
  137. return done()
  138. })
  139. })
  140. return it('should call _getConvertedFile if a format is defined', function(done) {
  141. return this.handler.getFile(
  142. this.bucket,
  143. this.key,
  144. { format: 'png' },
  145. () => {
  146. this.handler._getStandardFile.called.should.equal(false)
  147. this.handler._getConvertedFile.called.should.equal(true)
  148. return done()
  149. }
  150. )
  151. })
  152. })
  153. describe('_getStandardFile', function() {
  154. beforeEach(function() {
  155. this.fileStream = { on() {} }
  156. return this.PersistorManager.getFileStream.callsArgWith(
  157. 3,
  158. 'err',
  159. this.fileStream
  160. )
  161. })
  162. it('should get the stream', function(done) {
  163. return this.handler.getFile(this.bucket, this.key, null, () => {
  164. this.PersistorManager.getFileStream
  165. .calledWith(this.bucket, this.key)
  166. .should.equal(true)
  167. return done()
  168. })
  169. })
  170. it('should return the stream and error', function(done) {
  171. return this.handler.getFile(
  172. this.bucket,
  173. this.key,
  174. null,
  175. (err, stream) => {
  176. err.should.equal('err')
  177. stream.should.equal(this.fileStream)
  178. return done()
  179. }
  180. )
  181. })
  182. return it('should pass options to PersistorManager', function(done) {
  183. return this.handler.getFile(
  184. this.bucket,
  185. this.key,
  186. { start: 0, end: 8 },
  187. () => {
  188. expect(
  189. this.PersistorManager.getFileStream.lastCall.args[2].start
  190. ).to.equal(0)
  191. expect(
  192. this.PersistorManager.getFileStream.lastCall.args[2].end
  193. ).to.equal(8)
  194. return done()
  195. }
  196. )
  197. })
  198. })
  199. describe('_getConvertedFile', function() {
  200. it('should getFileStream if it does exists', function(done) {
  201. this.PersistorManager.checkIfFileExists.callsArgWith(2, null, true)
  202. this.PersistorManager.getFileStream.callsArgWith(3)
  203. return this.handler._getConvertedFile(this.bucket, this.key, {}, () => {
  204. this.PersistorManager.getFileStream
  205. .calledWith(this.bucket)
  206. .should.equal(true)
  207. return done()
  208. })
  209. })
  210. return it('should call _getConvertedFileAndCache if it does exists', function(done) {
  211. this.PersistorManager.checkIfFileExists.callsArgWith(2, null, false)
  212. this.handler._getConvertedFileAndCache = sinon.stub().callsArgWith(4)
  213. return this.handler._getConvertedFile(this.bucket, this.key, {}, () => {
  214. this.handler._getConvertedFileAndCache
  215. .calledWith(this.bucket, this.key)
  216. .should.equal(true)
  217. return done()
  218. })
  219. })
  220. })
  221. describe('_getConvertedFileAndCache', () =>
  222. it('should _convertFile ', function(done) {
  223. this.stubbedStream = { something: 'here' }
  224. this.localStream = {
  225. on() {}
  226. }
  227. this.PersistorManager.sendFile = sinon.stub().callsArgWith(3)
  228. this.LocalFileWriter.getStream = sinon
  229. .stub()
  230. .callsArgWith(1, null, this.localStream)
  231. this.convetedKey = this.key + 'converted'
  232. this.handler._convertFile = sinon
  233. .stub()
  234. .callsArgWith(3, null, this.stubbedPath)
  235. this.ImageOptimiser.compressPng = sinon.stub().callsArgWith(1)
  236. return this.handler._getConvertedFileAndCache(
  237. this.bucket,
  238. this.key,
  239. this.convetedKey,
  240. {},
  241. (err, fsStream) => {
  242. this.handler._convertFile.called.should.equal(true)
  243. this.PersistorManager.sendFile
  244. .calledWith(this.bucket, this.convetedKey, this.stubbedPath)
  245. .should.equal(true)
  246. this.ImageOptimiser.compressPng
  247. .calledWith(this.stubbedPath)
  248. .should.equal(true)
  249. this.LocalFileWriter.getStream
  250. .calledWith(this.stubbedPath)
  251. .should.equal(true)
  252. fsStream.should.equal(this.localStream)
  253. return done()
  254. }
  255. )
  256. }))
  257. describe('_convertFile', function() {
  258. beforeEach(function() {
  259. this.FileConverter.convert.callsArgWith(
  260. 2,
  261. null,
  262. this.formattedStubbedPath
  263. )
  264. this.FileConverter.thumbnail.callsArgWith(
  265. 1,
  266. null,
  267. this.formattedStubbedPath
  268. )
  269. this.FileConverter.preview.callsArgWith(
  270. 1,
  271. null,
  272. this.formattedStubbedPath
  273. )
  274. this.handler._writeS3FileToDisk = sinon
  275. .stub()
  276. .callsArgWith(3, null, this.stubbedPath)
  277. return this.LocalFileWriter.deleteFile.callsArgWith(1)
  278. })
  279. it('should call thumbnail on the writer path if style was thumbnail was specified', function(done) {
  280. return this.handler._convertFile(
  281. this.bucket,
  282. this.key,
  283. { style: 'thumbnail' },
  284. (err, path) => {
  285. path.should.equal(this.formattedStubbedPath)
  286. this.FileConverter.thumbnail
  287. .calledWith(this.stubbedPath)
  288. .should.equal(true)
  289. this.LocalFileWriter.deleteFile
  290. .calledWith(this.stubbedPath)
  291. .should.equal(true)
  292. return done()
  293. }
  294. )
  295. })
  296. it('should call preview on the writer path if style was preview was specified', function(done) {
  297. return this.handler._convertFile(
  298. this.bucket,
  299. this.key,
  300. { style: 'preview' },
  301. (err, path) => {
  302. path.should.equal(this.formattedStubbedPath)
  303. this.FileConverter.preview
  304. .calledWith(this.stubbedPath)
  305. .should.equal(true)
  306. this.LocalFileWriter.deleteFile
  307. .calledWith(this.stubbedPath)
  308. .should.equal(true)
  309. return done()
  310. }
  311. )
  312. })
  313. return it('should call convert on the writer path if a format was specified', function(done) {
  314. return this.handler._convertFile(
  315. this.bucket,
  316. this.key,
  317. { format: this.format },
  318. (err, path) => {
  319. path.should.equal(this.formattedStubbedPath)
  320. this.FileConverter.convert
  321. .calledWith(this.stubbedPath, this.format)
  322. .should.equal(true)
  323. this.LocalFileWriter.deleteFile
  324. .calledWith(this.stubbedPath)
  325. .should.equal(true)
  326. return done()
  327. }
  328. )
  329. })
  330. })
  331. return describe('getDirectorySize', function() {
  332. beforeEach(function() {
  333. return this.PersistorManager.directorySize.callsArgWith(2)
  334. })
  335. return it('should call the filestore manager to get directory size', function(done) {
  336. return this.handler.getDirectorySize(this.bucket, this.key, () => {
  337. this.PersistorManager.directorySize
  338. .calledWith(this.bucket, this.key)
  339. .should.equal(true)
  340. return done()
  341. })
  342. })
  343. })
  344. })