S3PersistorManagerTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const { assert } = require('chai')
  15. const sinon = require('sinon')
  16. const chai = require('chai')
  17. const should = chai.should()
  18. const { expect } = chai
  19. const modulePath = '../../../app/js/S3PersistorManager.js'
  20. const SandboxedModule = require('sandboxed-module')
  21. describe('S3PersistorManagerTests', function() {
  22. beforeEach(function() {
  23. this.settings = {
  24. filestore: {
  25. backend: 's3',
  26. s3: {
  27. secret: 'secret',
  28. key: 'this_key'
  29. },
  30. stores: {
  31. user_files: 'sl_user_files'
  32. }
  33. }
  34. }
  35. this.knoxClient = {
  36. putFile: sinon.stub(),
  37. copyFile: sinon.stub(),
  38. list: sinon.stub(),
  39. deleteMultiple: sinon.stub(),
  40. get: sinon.stub()
  41. }
  42. this.knox = { createClient: sinon.stub().returns(this.knoxClient) }
  43. this.s3EventHandlers = {}
  44. this.s3Request = {
  45. on: sinon.stub().callsFake((event, callback) => {
  46. return (this.s3EventHandlers[event] = callback)
  47. }),
  48. send: sinon.stub()
  49. }
  50. this.s3Response = {
  51. httpResponse: {
  52. createUnbufferedStream: sinon.stub()
  53. }
  54. }
  55. this.s3Client = {
  56. copyObject: sinon.stub(),
  57. headObject: sinon.stub(),
  58. getObject: sinon.stub().returns(this.s3Request)
  59. }
  60. this.awsS3 = sinon.stub().returns(this.s3Client)
  61. this.LocalFileWriter = {
  62. writeStream: sinon.stub(),
  63. deleteFile: sinon.stub()
  64. }
  65. this.request = sinon.stub()
  66. this.requires = {
  67. knox: this.knox,
  68. 'aws-sdk/clients/s3': this.awsS3,
  69. 'settings-sharelatex': this.settings,
  70. './LocalFileWriter': this.LocalFileWriter,
  71. 'logger-sharelatex': {
  72. log() {},
  73. err() {}
  74. },
  75. request: this.request,
  76. './Errors': (this.Errors = { NotFoundError: sinon.stub() })
  77. }
  78. this.key = 'my/key'
  79. this.bucketName = 'my-bucket'
  80. this.error = 'my errror'
  81. return (this.S3PersistorManager = SandboxedModule.require(modulePath, {
  82. requires: this.requires
  83. }))
  84. })
  85. describe('getFileStream', function() {
  86. describe('success', function() {
  87. beforeEach(function() {
  88. this.expectedStream = { expectedStream: true }
  89. this.expectedStream.on = sinon.stub()
  90. this.s3Request.send.callsFake(() => {
  91. return this.s3EventHandlers.httpHeaders(
  92. 200,
  93. {},
  94. this.s3Response,
  95. 'OK'
  96. )
  97. })
  98. return this.s3Response.httpResponse.createUnbufferedStream.returns(
  99. this.expectedStream
  100. )
  101. })
  102. it('returns a stream', function(done) {
  103. return this.S3PersistorManager.getFileStream(
  104. this.bucketName,
  105. this.key,
  106. {},
  107. (err, stream) => {
  108. if (err != null) {
  109. return done(err)
  110. }
  111. expect(stream).to.equal(this.expectedStream)
  112. return done()
  113. }
  114. )
  115. })
  116. it('sets the AWS client up with credentials from settings', function(done) {
  117. return this.S3PersistorManager.getFileStream(
  118. this.bucketName,
  119. this.key,
  120. {},
  121. (err, stream) => {
  122. if (err != null) {
  123. return done(err)
  124. }
  125. expect(this.awsS3.lastCall.args).to.deep.equal([
  126. {
  127. credentials: {
  128. accessKeyId: this.settings.filestore.s3.key,
  129. secretAccessKey: this.settings.filestore.s3.secret
  130. }
  131. }
  132. ])
  133. return done()
  134. }
  135. )
  136. })
  137. it('fetches the right key from the right bucket', function(done) {
  138. return this.S3PersistorManager.getFileStream(
  139. this.bucketName,
  140. this.key,
  141. {},
  142. (err, stream) => {
  143. if (err != null) {
  144. return done(err)
  145. }
  146. expect(this.s3Client.getObject.lastCall.args).to.deep.equal([
  147. {
  148. Bucket: this.bucketName,
  149. Key: this.key
  150. }
  151. ])
  152. return done()
  153. }
  154. )
  155. })
  156. it('accepts alternative credentials', function(done) {
  157. const accessKeyId = 'that_key'
  158. const secret = 'that_secret'
  159. const opts = {
  160. credentials: {
  161. auth_key: accessKeyId,
  162. auth_secret: secret
  163. }
  164. }
  165. return this.S3PersistorManager.getFileStream(
  166. this.bucketName,
  167. this.key,
  168. opts,
  169. (err, stream) => {
  170. if (err != null) {
  171. return done(err)
  172. }
  173. expect(this.awsS3.lastCall.args).to.deep.equal([
  174. {
  175. credentials: {
  176. accessKeyId,
  177. secretAccessKey: secret
  178. }
  179. }
  180. ])
  181. expect(stream).to.equal(this.expectedStream)
  182. return done()
  183. }
  184. )
  185. })
  186. return it('accepts byte range', function(done) {
  187. const start = 0
  188. const end = 8
  189. const opts = { start, end }
  190. return this.S3PersistorManager.getFileStream(
  191. this.bucketName,
  192. this.key,
  193. opts,
  194. (err, stream) => {
  195. if (err != null) {
  196. return done(err)
  197. }
  198. expect(this.s3Client.getObject.lastCall.args).to.deep.equal([
  199. {
  200. Bucket: this.bucketName,
  201. Key: this.key,
  202. Range: `bytes=${start}-${end}`
  203. }
  204. ])
  205. expect(stream).to.equal(this.expectedStream)
  206. return done()
  207. }
  208. )
  209. })
  210. })
  211. return describe('errors', function() {
  212. describe("when the file doesn't exist", function() {
  213. beforeEach(function() {
  214. return this.s3Request.send.callsFake(() => {
  215. return this.s3EventHandlers.httpHeaders(
  216. 404,
  217. {},
  218. this.s3Response,
  219. 'Not found'
  220. )
  221. })
  222. })
  223. return it('returns a NotFoundError that indicates the bucket and key', function(done) {
  224. return this.S3PersistorManager.getFileStream(
  225. this.bucketName,
  226. this.key,
  227. {},
  228. (err, stream) => {
  229. expect(err).to.be.instanceof(this.Errors.NotFoundError)
  230. const errMsg = this.Errors.NotFoundError.lastCall.args[0]
  231. expect(errMsg).to.match(new RegExp(`.*${this.bucketName}.*`))
  232. expect(errMsg).to.match(new RegExp(`.*${this.key}.*`))
  233. return done()
  234. }
  235. )
  236. })
  237. })
  238. describe('when S3 encounters an unkown error', function() {
  239. beforeEach(function() {
  240. return this.s3Request.send.callsFake(() => {
  241. return this.s3EventHandlers.httpHeaders(
  242. 500,
  243. {},
  244. this.s3Response,
  245. 'Internal server error'
  246. )
  247. })
  248. })
  249. return it('returns an error', function(done) {
  250. return this.S3PersistorManager.getFileStream(
  251. this.bucketName,
  252. this.key,
  253. {},
  254. (err, stream) => {
  255. expect(err).to.be.instanceof(Error)
  256. return done()
  257. }
  258. )
  259. })
  260. })
  261. return describe('when the S3 request errors out before receiving HTTP headers', function() {
  262. beforeEach(function() {
  263. return this.s3Request.send.callsFake(() => {
  264. return this.s3EventHandlers.error(new Error('connection failed'))
  265. })
  266. })
  267. return it('returns an error', function(done) {
  268. return this.S3PersistorManager.getFileStream(
  269. this.bucketName,
  270. this.key,
  271. {},
  272. (err, stream) => {
  273. expect(err).to.be.instanceof(Error)
  274. return done()
  275. }
  276. )
  277. })
  278. })
  279. })
  280. })
  281. describe('getFileSize', function() {
  282. it('should obtain the file size from S3', function(done) {
  283. const expectedFileSize = 123
  284. this.s3Client.headObject.yields(
  285. new Error('s3Client.headObject got unexpected arguments')
  286. )
  287. this.s3Client.headObject
  288. .withArgs({
  289. Bucket: this.bucketName,
  290. Key: this.key
  291. })
  292. .yields(null, { ContentLength: expectedFileSize })
  293. return this.S3PersistorManager.getFileSize(
  294. this.bucketName,
  295. this.key,
  296. (err, fileSize) => {
  297. if (err != null) {
  298. return done(err)
  299. }
  300. expect(fileSize).to.equal(expectedFileSize)
  301. return done()
  302. }
  303. )
  304. })
  305. ;[403, 404].forEach(statusCode =>
  306. it(`should throw NotFoundError when S3 responds with ${statusCode}`, function(done) {
  307. const error = new Error()
  308. error.statusCode = statusCode
  309. this.s3Client.headObject.yields(error)
  310. return this.S3PersistorManager.getFileSize(
  311. this.bucketName,
  312. this.key,
  313. (err, fileSize) => {
  314. expect(err).to.be.an.instanceof(this.Errors.NotFoundError)
  315. return done()
  316. }
  317. )
  318. })
  319. )
  320. return it('should rethrow any other error', function(done) {
  321. const error = new Error()
  322. this.s3Client.headObject.yields(error)
  323. this.s3Client.headObject.yields(error)
  324. return this.S3PersistorManager.getFileSize(
  325. this.bucketName,
  326. this.key,
  327. (err, fileSize) => {
  328. expect(err).to.equal(error)
  329. return done()
  330. }
  331. )
  332. })
  333. })
  334. describe('sendFile', function() {
  335. beforeEach(function() {
  336. return this.knoxClient.putFile.returns({ on() {} })
  337. })
  338. it('should put file with knox', function(done) {
  339. this.LocalFileWriter.deleteFile.callsArgWith(1)
  340. this.knoxClient.putFile.callsArgWith(2, this.error)
  341. return this.S3PersistorManager.sendFile(
  342. this.bucketName,
  343. this.key,
  344. this.fsPath,
  345. err => {
  346. this.knoxClient.putFile
  347. .calledWith(this.fsPath, this.key)
  348. .should.equal(true)
  349. err.should.equal(this.error)
  350. return done()
  351. }
  352. )
  353. })
  354. return it('should delete the file and pass the error with it', function(done) {
  355. this.LocalFileWriter.deleteFile.callsArgWith(1)
  356. this.knoxClient.putFile.callsArgWith(2, this.error)
  357. return this.S3PersistorManager.sendFile(
  358. this.bucketName,
  359. this.key,
  360. this.fsPath,
  361. err => {
  362. this.knoxClient.putFile
  363. .calledWith(this.fsPath, this.key)
  364. .should.equal(true)
  365. err.should.equal(this.error)
  366. return done()
  367. }
  368. )
  369. })
  370. })
  371. describe('sendStream', function() {
  372. beforeEach(function() {
  373. this.fsPath = 'to/some/where'
  374. this.origin = { on() {} }
  375. return (this.S3PersistorManager.sendFile = sinon.stub().callsArgWith(3))
  376. })
  377. it('should send stream to LocalFileWriter', function(done) {
  378. this.LocalFileWriter.deleteFile.callsArgWith(1)
  379. this.LocalFileWriter.writeStream.callsArgWith(2, null, this.fsPath)
  380. return this.S3PersistorManager.sendStream(
  381. this.bucketName,
  382. this.key,
  383. this.origin,
  384. () => {
  385. this.LocalFileWriter.writeStream
  386. .calledWith(this.origin)
  387. .should.equal(true)
  388. return done()
  389. }
  390. )
  391. })
  392. it('should return the error from LocalFileWriter', function(done) {
  393. this.LocalFileWriter.deleteFile.callsArgWith(1)
  394. this.LocalFileWriter.writeStream.callsArgWith(2, this.error)
  395. return this.S3PersistorManager.sendStream(
  396. this.bucketName,
  397. this.key,
  398. this.origin,
  399. err => {
  400. err.should.equal(this.error)
  401. return done()
  402. }
  403. )
  404. })
  405. return it('should send the file to the filestore', function(done) {
  406. this.LocalFileWriter.deleteFile.callsArgWith(1)
  407. this.LocalFileWriter.writeStream.callsArgWith(2)
  408. return this.S3PersistorManager.sendStream(
  409. this.bucketName,
  410. this.key,
  411. this.origin,
  412. err => {
  413. this.S3PersistorManager.sendFile.called.should.equal(true)
  414. return done()
  415. }
  416. )
  417. })
  418. })
  419. describe('copyFile', function() {
  420. beforeEach(function() {
  421. this.sourceKey = 'my/key'
  422. return (this.destKey = 'my/dest/key')
  423. })
  424. it('should use AWS SDK to copy file', function(done) {
  425. this.s3Client.copyObject.callsArgWith(1, this.error)
  426. return this.S3PersistorManager.copyFile(
  427. this.bucketName,
  428. this.sourceKey,
  429. this.destKey,
  430. err => {
  431. err.should.equal(this.error)
  432. this.s3Client.copyObject
  433. .calledWith({
  434. Bucket: this.bucketName,
  435. Key: this.destKey,
  436. CopySource: this.bucketName + '/' + this.key
  437. })
  438. .should.equal(true)
  439. return done()
  440. }
  441. )
  442. })
  443. return it('should return a NotFoundError object if the original file does not exist', function(done) {
  444. const NoSuchKeyError = { code: 'NoSuchKey' }
  445. this.s3Client.copyObject.callsArgWith(1, NoSuchKeyError)
  446. return this.S3PersistorManager.copyFile(
  447. this.bucketName,
  448. this.sourceKey,
  449. this.destKey,
  450. err => {
  451. expect(err instanceof this.Errors.NotFoundError).to.equal(true)
  452. return done()
  453. }
  454. )
  455. })
  456. })
  457. describe('deleteDirectory', () =>
  458. it('should list the contents passing them onto multi delete', function(done) {
  459. const data = { Contents: [{ Key: '1234' }, { Key: '456' }] }
  460. this.knoxClient.list.callsArgWith(1, null, data)
  461. this.knoxClient.deleteMultiple.callsArgWith(1)
  462. return this.S3PersistorManager.deleteDirectory(
  463. this.bucketName,
  464. this.key,
  465. err => {
  466. this.knoxClient.deleteMultiple
  467. .calledWith(['1234', '456'])
  468. .should.equal(true)
  469. return done()
  470. }
  471. )
  472. }))
  473. describe('deleteFile', function() {
  474. it('should use correct options', function(done) {
  475. this.request.callsArgWith(1)
  476. return this.S3PersistorManager.deleteFile(
  477. this.bucketName,
  478. this.key,
  479. err => {
  480. const opts = this.request.args[0][0]
  481. assert.deepEqual(opts.aws, {
  482. key: this.settings.filestore.s3.key,
  483. secret: this.settings.filestore.s3.secret,
  484. bucket: this.bucketName
  485. })
  486. opts.method.should.equal('delete')
  487. opts.timeout.should.equal(30 * 1000)
  488. opts.uri.should.equal(
  489. `https://${this.bucketName}.s3.amazonaws.com/${this.key}`
  490. )
  491. return done()
  492. }
  493. )
  494. })
  495. return it('should return the error', function(done) {
  496. this.request.callsArgWith(1, this.error)
  497. return this.S3PersistorManager.deleteFile(
  498. this.bucketName,
  499. this.key,
  500. err => {
  501. err.should.equal(this.error)
  502. return done()
  503. }
  504. )
  505. })
  506. })
  507. describe('checkIfFileExists', function() {
  508. it('should use correct options', function(done) {
  509. this.request.callsArgWith(1, null, { statusCode: 200 })
  510. return this.S3PersistorManager.checkIfFileExists(
  511. this.bucketName,
  512. this.key,
  513. err => {
  514. const opts = this.request.args[0][0]
  515. assert.deepEqual(opts.aws, {
  516. key: this.settings.filestore.s3.key,
  517. secret: this.settings.filestore.s3.secret,
  518. bucket: this.bucketName
  519. })
  520. opts.method.should.equal('head')
  521. opts.timeout.should.equal(30 * 1000)
  522. opts.uri.should.equal(
  523. `https://${this.bucketName}.s3.amazonaws.com/${this.key}`
  524. )
  525. return done()
  526. }
  527. )
  528. })
  529. it('should return true for a 200', function(done) {
  530. this.request.callsArgWith(1, null, { statusCode: 200 })
  531. return this.S3PersistorManager.checkIfFileExists(
  532. this.bucketName,
  533. this.key,
  534. (err, exists) => {
  535. exists.should.equal(true)
  536. return done()
  537. }
  538. )
  539. })
  540. it('should return false for a non 200', function(done) {
  541. this.request.callsArgWith(1, null, { statusCode: 404 })
  542. return this.S3PersistorManager.checkIfFileExists(
  543. this.bucketName,
  544. this.key,
  545. (err, exists) => {
  546. exists.should.equal(false)
  547. return done()
  548. }
  549. )
  550. })
  551. return it('should return the error', function(done) {
  552. this.request.callsArgWith(1, this.error, {})
  553. return this.S3PersistorManager.checkIfFileExists(
  554. this.bucketName,
  555. this.key,
  556. err => {
  557. err.should.equal(this.error)
  558. return done()
  559. }
  560. )
  561. })
  562. })
  563. return describe('directorySize', () =>
  564. it('should sum directory files size', function(done) {
  565. const data = { Contents: [{ Size: 1024 }, { Size: 2048 }] }
  566. this.knoxClient.list.callsArgWith(1, null, data)
  567. return this.S3PersistorManager.directorySize(
  568. this.bucketName,
  569. this.key,
  570. (err, totalSize) => {
  571. totalSize.should.equal(3072)
  572. return done()
  573. }
  574. )
  575. }))
  576. })