S3PersistorTests.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. const sinon = require('sinon')
  2. const chai = require('chai')
  3. const { expect } = chai
  4. const SandboxedModule = require('sandboxed-module')
  5. const Errors = require('../../src/Errors')
  6. const { EventEmitter } = require('events')
  7. const MODULE_PATH = '../../src/S3Persistor.js'
  8. describe('S3PersistorTests', function () {
  9. const defaultS3Key = 'frog'
  10. const defaultS3Secret = 'prince'
  11. const defaultS3Credentials = {
  12. credentials: {
  13. accessKeyId: defaultS3Key,
  14. secretAccessKey: defaultS3Secret,
  15. },
  16. }
  17. const filename = '/wombat/potato.tex'
  18. const bucket = 'womBucket'
  19. const key = 'monKey'
  20. const destKey = 'donKey'
  21. const objectSize = 5555
  22. const genericError = new Error('guru meditation error')
  23. const files = [
  24. { Key: 'llama', Size: 11 },
  25. { Key: 'hippo', Size: 22 },
  26. ]
  27. const filesSize = 33
  28. const md5 = 'ffffffff00000000ffffffff00000000'
  29. const redirectUrl = 'https://wombat.potato/giraffe'
  30. let Logger,
  31. Transform,
  32. PassThrough,
  33. S3,
  34. Fs,
  35. ReadStream,
  36. Stream,
  37. StreamPromises,
  38. S3GetObjectRequest,
  39. S3Persistor,
  40. S3Client,
  41. S3NotFoundError,
  42. S3AccessDeniedError,
  43. FileNotFoundError,
  44. EmptyPromise,
  45. settings,
  46. Hash,
  47. crypto
  48. beforeEach(function () {
  49. settings = {
  50. secret: defaultS3Secret,
  51. key: defaultS3Key,
  52. partSize: 100 * 1024 * 1024,
  53. }
  54. Transform = class {
  55. once() {}
  56. }
  57. PassThrough = class {}
  58. Stream = {
  59. Transform,
  60. PassThrough,
  61. pipeline: sinon.stub().yields(),
  62. }
  63. StreamPromises = {
  64. pipeline: sinon.stub().resolves(),
  65. }
  66. EmptyPromise = {
  67. promise: sinon.stub().resolves(),
  68. }
  69. ReadStream = new EventEmitter()
  70. class FakeS3GetObjectRequest extends EventEmitter {
  71. constructor() {
  72. super()
  73. this.statusCode = 200
  74. this.err = null
  75. this.aborted = false
  76. }
  77. abort() {
  78. this.aborted = true
  79. }
  80. createReadStream() {
  81. setTimeout(() => {
  82. if (this.err) return ReadStream.emit('error', this.err)
  83. this.emit('httpHeaders', this.statusCode)
  84. })
  85. return ReadStream
  86. }
  87. }
  88. S3GetObjectRequest = new FakeS3GetObjectRequest()
  89. FileNotFoundError = new Error('File not found')
  90. FileNotFoundError.code = 'ENOENT'
  91. Fs = {
  92. createReadStream: sinon.stub().returns(ReadStream),
  93. }
  94. S3NotFoundError = new Error('not found')
  95. S3NotFoundError.code = 'NoSuchKey'
  96. S3AccessDeniedError = new Error('access denied')
  97. S3AccessDeniedError.code = 'AccessDenied'
  98. S3Client = {
  99. getObject: sinon.stub().returns(S3GetObjectRequest),
  100. headObject: sinon.stub().returns({
  101. promise: sinon.stub().resolves({
  102. ContentLength: objectSize,
  103. ETag: md5,
  104. }),
  105. }),
  106. listObjectsV2: sinon.stub().returns({
  107. promise: sinon.stub().resolves({
  108. Contents: files,
  109. }),
  110. }),
  111. upload: sinon
  112. .stub()
  113. .returns({ promise: sinon.stub().resolves({ ETag: `"${md5}"` }) }),
  114. copyObject: sinon.stub().returns(EmptyPromise),
  115. deleteObject: sinon.stub().returns(EmptyPromise),
  116. deleteObjects: sinon.stub().returns(EmptyPromise),
  117. getSignedUrlPromise: sinon.stub().resolves(redirectUrl),
  118. }
  119. S3 = sinon.stub().returns(S3Client)
  120. Hash = {
  121. end: sinon.stub(),
  122. read: sinon.stub().returns(md5),
  123. setEncoding: sinon.stub(),
  124. }
  125. crypto = {
  126. createHash: sinon.stub().returns(Hash),
  127. }
  128. Logger = {
  129. warn: sinon.stub(),
  130. }
  131. S3Persistor = new (SandboxedModule.require(MODULE_PATH, {
  132. requires: {
  133. 'aws-sdk/clients/s3': S3,
  134. '@overleaf/logger': Logger,
  135. './Errors': Errors,
  136. fs: Fs,
  137. stream: Stream,
  138. 'stream/promises': StreamPromises,
  139. crypto,
  140. },
  141. globals: { console, Buffer },
  142. }))(settings)
  143. })
  144. describe('getObjectStream', function () {
  145. describe('when called with valid parameters', function () {
  146. let stream
  147. beforeEach(async function () {
  148. stream = await S3Persistor.getObjectStream(bucket, key)
  149. })
  150. it('returns a PassThrough stream', function () {
  151. expect(stream).to.be.instanceOf(PassThrough)
  152. })
  153. it('sets the AWS client up with credentials from settings', function () {
  154. expect(S3).to.have.been.calledWith(defaultS3Credentials)
  155. })
  156. it('fetches the right key from the right bucket', function () {
  157. expect(S3Client.getObject).to.have.been.calledWith({
  158. Bucket: bucket,
  159. Key: key,
  160. })
  161. })
  162. it('pipes the stream through the meter', async function () {
  163. expect(Stream.pipeline).to.have.been.calledWith(
  164. ReadStream,
  165. sinon.match.instanceOf(Transform),
  166. sinon.match.instanceOf(PassThrough)
  167. )
  168. })
  169. it('does not abort the request', function () {
  170. expect(S3GetObjectRequest.aborted).to.equal(false)
  171. })
  172. })
  173. describe('when called with a byte range', function () {
  174. let stream
  175. beforeEach(async function () {
  176. stream = await S3Persistor.getObjectStream(bucket, key, {
  177. start: 5,
  178. end: 10,
  179. })
  180. })
  181. it('returns a PassThrough stream', function () {
  182. expect(stream).to.be.instanceOf(Stream.PassThrough)
  183. })
  184. it('passes the byte range on to S3', function () {
  185. expect(S3Client.getObject).to.have.been.calledWith({
  186. Bucket: bucket,
  187. Key: key,
  188. Range: 'bytes=5-10',
  189. })
  190. })
  191. })
  192. describe('when streaming fails', function () {
  193. let stream
  194. beforeEach(async function () {
  195. Stream.pipeline.yields(new Error())
  196. stream = await S3Persistor.getObjectStream(bucket, key)
  197. })
  198. it('returns a PassThrough stream', function () {
  199. expect(stream).to.be.instanceOf(Stream.PassThrough)
  200. })
  201. it('aborts the request', function () {
  202. expect(S3GetObjectRequest.aborted).to.equal(true)
  203. })
  204. })
  205. describe('when there are alternative credentials', function () {
  206. let stream
  207. const alternativeSecret = 'giraffe'
  208. const alternativeKey = 'hippo'
  209. const alternativeS3Credentials = {
  210. credentials: {
  211. accessKeyId: alternativeKey,
  212. secretAccessKey: alternativeSecret,
  213. },
  214. }
  215. beforeEach(async function () {
  216. settings.bucketCreds = {}
  217. settings.bucketCreds[bucket] = {
  218. auth_key: alternativeKey,
  219. auth_secret: alternativeSecret,
  220. }
  221. stream = await S3Persistor.getObjectStream(bucket, key)
  222. })
  223. it('returns a PassThrough stream', function () {
  224. expect(stream).to.be.instanceOf(Stream.PassThrough)
  225. })
  226. it('sets the AWS client up with the alternative credentials', function () {
  227. expect(S3).to.have.been.calledWith(alternativeS3Credentials)
  228. })
  229. it('fetches the right key from the right bucket', function () {
  230. expect(S3Client.getObject).to.have.been.calledWith({
  231. Bucket: bucket,
  232. Key: key,
  233. })
  234. })
  235. it('uses the default credentials for an unknown bucket', async function () {
  236. stream = await S3Persistor.getObjectStream('anotherBucket', key)
  237. expect(S3).to.have.been.calledTwice
  238. expect(S3.firstCall).to.have.been.calledWith(alternativeS3Credentials)
  239. expect(S3.secondCall).to.have.been.calledWith(defaultS3Credentials)
  240. })
  241. it('throws an error if there are no credentials for the bucket', async function () {
  242. delete settings.key
  243. delete settings.secret
  244. await expect(
  245. S3Persistor.getObjectStream('anotherBucket', key)
  246. ).to.eventually.be.rejected.and.be.an.instanceOf(Errors.SettingsError)
  247. })
  248. })
  249. describe('when given S3 options', function () {
  250. const httpOptions = { timeout: 2000 }
  251. const maxRetries = 2
  252. beforeEach(async function () {
  253. settings.httpOptions = httpOptions
  254. settings.maxRetries = maxRetries
  255. await S3Persistor.getObjectStream(bucket, key)
  256. })
  257. it('configures the S3 client appropriately', function () {
  258. expect(S3).to.have.been.calledWithMatch({ httpOptions, maxRetries })
  259. })
  260. })
  261. describe("when the file doesn't exist", function () {
  262. let error, stream
  263. beforeEach(async function () {
  264. S3GetObjectRequest.statusCode = 404
  265. try {
  266. stream = await S3Persistor.getObjectStream(bucket, key)
  267. } catch (err) {
  268. error = err
  269. }
  270. })
  271. it('does not return a stream', function () {
  272. expect(stream).not.to.exist
  273. })
  274. it('throws a NotFoundError', function () {
  275. expect(error).to.be.an.instanceOf(Errors.NotFoundError)
  276. })
  277. it('wraps the error', function () {
  278. expect(error.cause).to.exist
  279. })
  280. it('stores the bucket and key in the error', function () {
  281. expect(error.info).to.include({ bucketName: bucket, key })
  282. })
  283. })
  284. describe('when access to the file is denied', function () {
  285. let error, stream
  286. beforeEach(async function () {
  287. S3GetObjectRequest.statusCode = 403
  288. try {
  289. stream = await S3Persistor.getObjectStream(bucket, key)
  290. } catch (err) {
  291. error = err
  292. }
  293. })
  294. it('does not return a stream', function () {
  295. expect(stream).not.to.exist
  296. })
  297. it('throws a NotFoundError', function () {
  298. expect(error).to.be.an.instanceOf(Errors.NotFoundError)
  299. })
  300. it('wraps the error', function () {
  301. expect(error.cause).to.exist
  302. })
  303. it('stores the bucket and key in the error', function () {
  304. expect(error.info).to.include({ bucketName: bucket, key })
  305. })
  306. })
  307. describe('when S3 encounters an unknown error', function () {
  308. let error, stream
  309. beforeEach(async function () {
  310. S3GetObjectRequest.err = genericError
  311. try {
  312. stream = await S3Persistor.getObjectStream(bucket, key)
  313. } catch (err) {
  314. error = err
  315. }
  316. })
  317. it('does not return a stream', function () {
  318. expect(stream).not.to.exist
  319. })
  320. it('throws a ReadError', function () {
  321. expect(error).to.be.an.instanceOf(Errors.ReadError)
  322. })
  323. it('wraps the error', function () {
  324. expect(error.cause).to.exist
  325. })
  326. it('stores the bucket and key in the error', function () {
  327. expect(error.info).to.include({ bucketName: bucket, key })
  328. })
  329. })
  330. })
  331. describe('getRedirectUrl', function () {
  332. let signedUrl
  333. beforeEach(async function () {
  334. signedUrl = await S3Persistor.getRedirectUrl(bucket, key)
  335. })
  336. it('should request a signed URL', function () {
  337. expect(S3Client.getSignedUrlPromise).to.have.been.called
  338. })
  339. it('should return the url', function () {
  340. expect(signedUrl).to.equal(redirectUrl)
  341. })
  342. })
  343. describe('getObjectSize', function () {
  344. describe('when called with valid parameters', function () {
  345. let size
  346. beforeEach(async function () {
  347. size = await S3Persistor.getObjectSize(bucket, key)
  348. })
  349. it('should return the object size', function () {
  350. expect(size).to.equal(objectSize)
  351. })
  352. it('should pass the bucket and key to S3', function () {
  353. expect(S3Client.headObject).to.have.been.calledWith({
  354. Bucket: bucket,
  355. Key: key,
  356. })
  357. })
  358. })
  359. describe('when the object is not found', function () {
  360. let error
  361. beforeEach(async function () {
  362. S3Client.headObject = sinon.stub().returns({
  363. promise: sinon.stub().rejects(S3NotFoundError),
  364. })
  365. try {
  366. await S3Persistor.getObjectSize(bucket, key)
  367. } catch (err) {
  368. error = err
  369. }
  370. })
  371. it('should return a NotFoundError', function () {
  372. expect(error).to.be.an.instanceOf(Errors.NotFoundError)
  373. })
  374. it('should wrap the error', function () {
  375. expect(error.cause).to.equal(S3NotFoundError)
  376. })
  377. })
  378. describe('when S3 returns an error', function () {
  379. let error
  380. beforeEach(async function () {
  381. S3Client.headObject = sinon.stub().returns({
  382. promise: sinon.stub().rejects(genericError),
  383. })
  384. try {
  385. await S3Persistor.getObjectSize(bucket, key)
  386. } catch (err) {
  387. error = err
  388. }
  389. })
  390. it('should return a ReadError', function () {
  391. expect(error).to.be.an.instanceOf(Errors.ReadError)
  392. })
  393. it('should wrap the error', function () {
  394. expect(error.cause).to.equal(genericError)
  395. })
  396. })
  397. })
  398. describe('sendStream', function () {
  399. describe('with valid parameters', function () {
  400. beforeEach(async function () {
  401. return S3Persistor.sendStream(bucket, key, ReadStream)
  402. })
  403. it('should upload the stream', function () {
  404. expect(S3Client.upload).to.have.been.calledWith({
  405. Bucket: bucket,
  406. Key: key,
  407. Body: sinon.match.instanceOf(Stream.Transform),
  408. })
  409. })
  410. it('should upload files in a single part', function () {
  411. expect(S3Client.upload).to.have.been.calledWith(sinon.match.any, {
  412. partSize: 100 * 1024 * 1024,
  413. })
  414. })
  415. it('should meter the stream', function () {
  416. expect(Stream.pipeline).to.have.been.calledWith(
  417. ReadStream,
  418. sinon.match.instanceOf(Stream.Transform)
  419. )
  420. })
  421. })
  422. describe('when a hash is supplied', function () {
  423. beforeEach(async function () {
  424. return S3Persistor.sendStream(bucket, key, ReadStream, {
  425. sourceMd5: 'aaaaaaaabbbbbbbbaaaaaaaabbbbbbbb',
  426. })
  427. })
  428. it('sends the hash in base64', function () {
  429. expect(S3Client.upload).to.have.been.calledWith({
  430. Bucket: bucket,
  431. Key: key,
  432. Body: sinon.match.instanceOf(Transform),
  433. ContentMD5: 'qqqqqru7u7uqqqqqu7u7uw==',
  434. })
  435. })
  436. })
  437. describe('when metadata is supplied', function () {
  438. const contentType = 'text/csv'
  439. const contentEncoding = 'gzip'
  440. beforeEach(async function () {
  441. return S3Persistor.sendStream(bucket, key, ReadStream, {
  442. contentType,
  443. contentEncoding,
  444. })
  445. })
  446. it('sends the metadata to S3', function () {
  447. expect(S3Client.upload).to.have.been.calledWith({
  448. Bucket: bucket,
  449. Key: key,
  450. Body: sinon.match.instanceOf(Transform),
  451. ContentType: contentType,
  452. ContentEncoding: contentEncoding,
  453. })
  454. })
  455. })
  456. describe('when the upload fails', function () {
  457. let error
  458. beforeEach(async function () {
  459. S3Client.upload = sinon.stub().returns({
  460. promise: sinon.stub().rejects(genericError),
  461. })
  462. try {
  463. await S3Persistor.sendStream(bucket, key, ReadStream)
  464. } catch (err) {
  465. error = err
  466. }
  467. })
  468. it('throws a WriteError', function () {
  469. expect(error).to.be.an.instanceOf(Errors.WriteError)
  470. })
  471. })
  472. })
  473. describe('sendFile', function () {
  474. describe('with valid parameters', function () {
  475. beforeEach(async function () {
  476. return S3Persistor.sendFile(bucket, key, filename)
  477. })
  478. it('should create a read stream for the file', function () {
  479. expect(Fs.createReadStream).to.have.been.calledWith(filename)
  480. })
  481. it('should upload the stream', function () {
  482. expect(S3Client.upload).to.have.been.calledWith({
  483. Bucket: bucket,
  484. Key: key,
  485. Body: sinon.match.instanceOf(Transform),
  486. })
  487. })
  488. })
  489. })
  490. describe('getObjectMd5Hash', function () {
  491. describe('when the etag is a valid md5 hash', function () {
  492. let hash
  493. beforeEach(async function () {
  494. hash = await S3Persistor.getObjectMd5Hash(bucket, key)
  495. })
  496. it('should return the object hash', function () {
  497. expect(hash).to.equal(md5)
  498. })
  499. it('should get the hash from the object metadata', function () {
  500. expect(S3Client.headObject).to.have.been.calledWith({
  501. Bucket: bucket,
  502. Key: key,
  503. })
  504. })
  505. it('should not download the object', function () {
  506. expect(S3Client.getObject).not.to.have.been.called
  507. })
  508. })
  509. describe("when the etag isn't a valid md5 hash", function () {
  510. let hash
  511. beforeEach(async function () {
  512. S3Client.headObject = sinon.stub().returns({
  513. promise: sinon.stub().resolves({
  514. ETag: 'somethingthatisntanmd5',
  515. Bucket: bucket,
  516. Key: key,
  517. }),
  518. })
  519. hash = await S3Persistor.getObjectMd5Hash(bucket, key)
  520. })
  521. it('should re-fetch the file to verify it', function () {
  522. expect(S3Client.getObject).to.have.been.calledWith({
  523. Bucket: bucket,
  524. Key: key,
  525. })
  526. })
  527. it('should calculate the md5 hash from the file', function () {
  528. expect(Hash.read).to.have.been.called
  529. })
  530. it('should return the md5 hash', function () {
  531. expect(hash).to.equal(md5)
  532. })
  533. })
  534. })
  535. describe('copyObject', function () {
  536. describe('with valid parameters', function () {
  537. beforeEach(async function () {
  538. return S3Persistor.copyObject(bucket, key, destKey)
  539. })
  540. it('should copy the object', function () {
  541. expect(S3Client.copyObject).to.have.been.calledWith({
  542. Bucket: bucket,
  543. Key: destKey,
  544. CopySource: `${bucket}/${key}`,
  545. })
  546. })
  547. })
  548. describe('when the file does not exist', function () {
  549. let error
  550. beforeEach(async function () {
  551. S3Client.copyObject = sinon.stub().returns({
  552. promise: sinon.stub().rejects(S3NotFoundError),
  553. })
  554. try {
  555. await S3Persistor.copyObject(bucket, key, destKey)
  556. } catch (err) {
  557. error = err
  558. }
  559. })
  560. it('should throw a NotFoundError', function () {
  561. expect(error).to.be.an.instanceOf(Errors.NotFoundError)
  562. })
  563. })
  564. })
  565. describe('deleteObject', function () {
  566. describe('with valid parameters', function () {
  567. beforeEach(async function () {
  568. return S3Persistor.deleteObject(bucket, key)
  569. })
  570. it('should delete the object', function () {
  571. expect(S3Client.deleteObject).to.have.been.calledWith({
  572. Bucket: bucket,
  573. Key: key,
  574. })
  575. })
  576. })
  577. })
  578. describe('deleteDirectory', function () {
  579. describe('with valid parameters', function () {
  580. beforeEach(async function () {
  581. return S3Persistor.deleteDirectory(bucket, key)
  582. })
  583. it('should list the objects in the directory', function () {
  584. expect(S3Client.listObjectsV2).to.have.been.calledWith({
  585. Bucket: bucket,
  586. Prefix: key,
  587. })
  588. })
  589. it('should delete the objects using their keys', function () {
  590. expect(S3Client.deleteObjects).to.have.been.calledWith({
  591. Bucket: bucket,
  592. Delete: {
  593. Objects: [{ Key: 'llama' }, { Key: 'hippo' }],
  594. Quiet: true,
  595. },
  596. })
  597. })
  598. })
  599. describe('when there are no files', function () {
  600. beforeEach(async function () {
  601. S3Client.listObjectsV2 = sinon
  602. .stub()
  603. .returns({ promise: sinon.stub().resolves({ Contents: [] }) })
  604. return S3Persistor.deleteDirectory(bucket, key)
  605. })
  606. it('should list the objects in the directory', function () {
  607. expect(S3Client.listObjectsV2).to.have.been.calledWith({
  608. Bucket: bucket,
  609. Prefix: key,
  610. })
  611. })
  612. it('should not try to delete any objects', function () {
  613. expect(S3Client.deleteObjects).not.to.have.been.called
  614. })
  615. })
  616. describe('when there are more files available', function () {
  617. const continuationToken = 'wombat'
  618. beforeEach(async function () {
  619. S3Client.listObjectsV2.onCall(0).returns({
  620. promise: sinon.stub().resolves({
  621. Contents: files,
  622. IsTruncated: true,
  623. NextContinuationToken: continuationToken,
  624. }),
  625. })
  626. return S3Persistor.deleteDirectory(bucket, key)
  627. })
  628. it('should list the objects a second time, with a continuation token', function () {
  629. expect(S3Client.listObjectsV2).to.be.calledTwice
  630. expect(S3Client.listObjectsV2).to.be.calledWith({
  631. Bucket: bucket,
  632. Prefix: key,
  633. })
  634. expect(S3Client.listObjectsV2).to.be.calledWith({
  635. Bucket: bucket,
  636. Prefix: key,
  637. ContinuationToken: continuationToken,
  638. })
  639. })
  640. it('should delete both sets of files', function () {
  641. expect(S3Client.deleteObjects).to.have.been.calledTwice
  642. })
  643. })
  644. describe('when there is an error listing the objects', function () {
  645. let error
  646. beforeEach(async function () {
  647. S3Client.listObjectsV2 = sinon
  648. .stub()
  649. .returns({ promise: sinon.stub().rejects(genericError) })
  650. try {
  651. await S3Persistor.deleteDirectory(bucket, key)
  652. } catch (err) {
  653. error = err
  654. }
  655. })
  656. it('should generate a ReadError', function () {
  657. expect(error).to.be.an.instanceOf(Errors.ReadError)
  658. })
  659. it('should wrap the error', function () {
  660. expect(error.cause).to.equal(genericError)
  661. })
  662. it('should not try to delete any objects', function () {
  663. expect(S3Client.deleteObjects).not.to.have.been.called
  664. })
  665. })
  666. describe('when there is an error deleting the objects', function () {
  667. let error
  668. beforeEach(async function () {
  669. S3Client.deleteObjects = sinon
  670. .stub()
  671. .returns({ promise: sinon.stub().rejects(genericError) })
  672. try {
  673. await S3Persistor.deleteDirectory(bucket, key)
  674. } catch (err) {
  675. error = err
  676. }
  677. })
  678. it('should generate a WriteError', function () {
  679. expect(error).to.be.an.instanceOf(Errors.WriteError)
  680. })
  681. it('should wrap the error', function () {
  682. expect(error.cause).to.equal(genericError)
  683. })
  684. })
  685. })
  686. describe('directorySize', function () {
  687. describe('with valid parameters', function () {
  688. let size
  689. beforeEach(async function () {
  690. size = await S3Persistor.directorySize(bucket, key)
  691. })
  692. it('should list the objects in the directory', function () {
  693. expect(S3Client.listObjectsV2).to.have.been.calledWith({
  694. Bucket: bucket,
  695. Prefix: key,
  696. })
  697. })
  698. it('should return the directory size', function () {
  699. expect(size).to.equal(filesSize)
  700. })
  701. })
  702. describe('when there are no files', function () {
  703. let size
  704. beforeEach(async function () {
  705. S3Client.listObjectsV2 = sinon
  706. .stub()
  707. .returns({ promise: sinon.stub().resolves({ Contents: [] }) })
  708. size = await S3Persistor.directorySize(bucket, key)
  709. })
  710. it('should list the objects in the directory', function () {
  711. expect(S3Client.listObjectsV2).to.have.been.calledWith({
  712. Bucket: bucket,
  713. Prefix: key,
  714. })
  715. })
  716. it('should return zero', function () {
  717. expect(size).to.equal(0)
  718. })
  719. })
  720. describe('when there are more files available', function () {
  721. const continuationToken = 'wombat'
  722. let size
  723. beforeEach(async function () {
  724. S3Client.listObjectsV2.onCall(0).returns({
  725. promise: sinon.stub().resolves({
  726. Contents: files,
  727. IsTruncated: true,
  728. NextContinuationToken: continuationToken,
  729. }),
  730. })
  731. size = await S3Persistor.directorySize(bucket, key)
  732. })
  733. it('should list the objects a second time, with a continuation token', function () {
  734. expect(S3Client.listObjectsV2).to.be.calledTwice
  735. expect(S3Client.listObjectsV2).to.be.calledWith({
  736. Bucket: bucket,
  737. Prefix: key,
  738. })
  739. expect(S3Client.listObjectsV2).to.be.calledWith({
  740. Bucket: bucket,
  741. Prefix: key,
  742. ContinuationToken: continuationToken,
  743. })
  744. })
  745. it('should return the size of both sets of files', function () {
  746. expect(size).to.equal(filesSize * 2)
  747. })
  748. })
  749. describe('when there is an error listing the objects', function () {
  750. let error
  751. beforeEach(async function () {
  752. S3Client.listObjectsV2 = sinon
  753. .stub()
  754. .returns({ promise: sinon.stub().rejects(genericError) })
  755. try {
  756. await S3Persistor.directorySize(bucket, key)
  757. } catch (err) {
  758. error = err
  759. }
  760. })
  761. it('should generate a ReadError', function () {
  762. expect(error).to.be.an.instanceOf(Errors.ReadError)
  763. })
  764. it('should wrap the error', function () {
  765. expect(error.cause).to.equal(genericError)
  766. })
  767. })
  768. })
  769. describe('checkIfObjectExists', function () {
  770. describe('when the file exists', function () {
  771. let exists
  772. beforeEach(async function () {
  773. exists = await S3Persistor.checkIfObjectExists(bucket, key)
  774. })
  775. it('should get the object header', function () {
  776. expect(S3Client.headObject).to.have.been.calledWith({
  777. Bucket: bucket,
  778. Key: key,
  779. })
  780. })
  781. it('should return that the file exists', function () {
  782. expect(exists).to.equal(true)
  783. })
  784. })
  785. describe('when the file does not exist', function () {
  786. let exists
  787. beforeEach(async function () {
  788. S3Client.headObject = sinon
  789. .stub()
  790. .returns({ promise: sinon.stub().rejects(S3NotFoundError) })
  791. exists = await S3Persistor.checkIfObjectExists(bucket, key)
  792. })
  793. it('should get the object header', function () {
  794. expect(S3Client.headObject).to.have.been.calledWith({
  795. Bucket: bucket,
  796. Key: key,
  797. })
  798. })
  799. it('should return that the file does not exist', function () {
  800. expect(exists).to.equal(false)
  801. })
  802. })
  803. describe('when there is an error', function () {
  804. let error
  805. beforeEach(async function () {
  806. S3Client.headObject = sinon
  807. .stub()
  808. .returns({ promise: sinon.stub().rejects(genericError) })
  809. try {
  810. await S3Persistor.checkIfObjectExists(bucket, key)
  811. } catch (err) {
  812. error = err
  813. }
  814. })
  815. it('should generate a ReadError', function () {
  816. expect(error).to.be.an.instanceOf(Errors.ReadError)
  817. })
  818. it('should wrap the upstream ReadError', function () {
  819. expect(error.cause).to.be.an.instanceOf(Errors.ReadError)
  820. })
  821. it('should eventually wrap the error', function () {
  822. expect(error.cause.cause).to.equal(genericError)
  823. })
  824. })
  825. })
  826. })