S3PersistorTests.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. }).S3Persistor)(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. })
  242. describe('without hard-coded credentials', function () {
  243. it('uses the default provider chain', async function () {
  244. delete settings.key
  245. delete settings.secret
  246. await S3Persistor.getObjectStream(bucket, key)
  247. expect(S3).to.have.been.calledOnce
  248. expect(S3.args[0].credentials).to.not.exist
  249. })
  250. })
  251. describe('when given S3 options', function () {
  252. const httpOptions = { timeout: 2000 }
  253. const maxRetries = 2
  254. beforeEach(async function () {
  255. settings.httpOptions = httpOptions
  256. settings.maxRetries = maxRetries
  257. await S3Persistor.getObjectStream(bucket, key)
  258. })
  259. it('configures the S3 client appropriately', function () {
  260. expect(S3).to.have.been.calledWithMatch({ httpOptions, maxRetries })
  261. })
  262. })
  263. describe("when the file doesn't exist", function () {
  264. let error, stream
  265. beforeEach(async function () {
  266. S3GetObjectRequest.statusCode = 404
  267. try {
  268. stream = await S3Persistor.getObjectStream(bucket, key)
  269. } catch (err) {
  270. error = err
  271. }
  272. })
  273. it('does not return a stream', function () {
  274. expect(stream).not.to.exist
  275. })
  276. it('throws a NotFoundError', function () {
  277. expect(error).to.be.an.instanceOf(Errors.NotFoundError)
  278. })
  279. it('wraps the error', function () {
  280. expect(error.cause).to.exist
  281. })
  282. it('stores the bucket and key in the error', function () {
  283. expect(error.info).to.include({ bucketName: bucket, key })
  284. })
  285. })
  286. describe('when access to the file is denied', function () {
  287. let error, stream
  288. beforeEach(async function () {
  289. S3GetObjectRequest.statusCode = 403
  290. try {
  291. stream = await S3Persistor.getObjectStream(bucket, key)
  292. } catch (err) {
  293. error = err
  294. }
  295. })
  296. it('does not return a stream', function () {
  297. expect(stream).not.to.exist
  298. })
  299. it('throws a NotFoundError', function () {
  300. expect(error).to.be.an.instanceOf(Errors.NotFoundError)
  301. })
  302. it('wraps the error', function () {
  303. expect(error.cause).to.exist
  304. })
  305. it('stores the bucket and key in the error', function () {
  306. expect(error.info).to.include({ bucketName: bucket, key })
  307. })
  308. })
  309. describe('when S3 encounters an unknown error', function () {
  310. let error, stream
  311. beforeEach(async function () {
  312. S3GetObjectRequest.err = genericError
  313. try {
  314. stream = await S3Persistor.getObjectStream(bucket, key)
  315. } catch (err) {
  316. error = err
  317. }
  318. })
  319. it('does not return a stream', function () {
  320. expect(stream).not.to.exist
  321. })
  322. it('throws a ReadError', function () {
  323. expect(error).to.be.an.instanceOf(Errors.ReadError)
  324. })
  325. it('wraps the error', function () {
  326. expect(error.cause).to.exist
  327. })
  328. it('stores the bucket and key in the error', function () {
  329. expect(error.info).to.include({ bucketName: bucket, key })
  330. })
  331. })
  332. })
  333. describe('getRedirectUrl', function () {
  334. let signedUrl
  335. beforeEach(async function () {
  336. signedUrl = await S3Persistor.getRedirectUrl(bucket, key)
  337. })
  338. it('should request a signed URL', function () {
  339. expect(S3Client.getSignedUrlPromise).to.have.been.called
  340. })
  341. it('should return the url', function () {
  342. expect(signedUrl).to.equal(redirectUrl)
  343. })
  344. })
  345. describe('getObjectSize', function () {
  346. describe('when called with valid parameters', function () {
  347. let size
  348. beforeEach(async function () {
  349. size = await S3Persistor.getObjectSize(bucket, key)
  350. })
  351. it('should return the object size', function () {
  352. expect(size).to.equal(objectSize)
  353. })
  354. it('should pass the bucket and key to S3', function () {
  355. expect(S3Client.headObject).to.have.been.calledWith({
  356. Bucket: bucket,
  357. Key: key,
  358. })
  359. })
  360. })
  361. describe('when the object is not found', function () {
  362. let error
  363. beforeEach(async function () {
  364. S3Client.headObject = sinon.stub().returns({
  365. promise: sinon.stub().rejects(S3NotFoundError),
  366. })
  367. try {
  368. await S3Persistor.getObjectSize(bucket, key)
  369. } catch (err) {
  370. error = err
  371. }
  372. })
  373. it('should return a NotFoundError', function () {
  374. expect(error).to.be.an.instanceOf(Errors.NotFoundError)
  375. })
  376. it('should wrap the error', function () {
  377. expect(error.cause).to.equal(S3NotFoundError)
  378. })
  379. })
  380. describe('when S3 returns an error', function () {
  381. let error
  382. beforeEach(async function () {
  383. S3Client.headObject = sinon.stub().returns({
  384. promise: sinon.stub().rejects(genericError),
  385. })
  386. try {
  387. await S3Persistor.getObjectSize(bucket, key)
  388. } catch (err) {
  389. error = err
  390. }
  391. })
  392. it('should return a ReadError', function () {
  393. expect(error).to.be.an.instanceOf(Errors.ReadError)
  394. })
  395. it('should wrap the error', function () {
  396. expect(error.cause).to.equal(genericError)
  397. })
  398. })
  399. })
  400. describe('sendStream', function () {
  401. describe('with valid parameters', function () {
  402. beforeEach(async function () {
  403. return S3Persistor.sendStream(bucket, key, ReadStream)
  404. })
  405. it('should upload the stream', function () {
  406. expect(S3Client.upload).to.have.been.calledWith({
  407. Bucket: bucket,
  408. Key: key,
  409. Body: sinon.match.instanceOf(Stream.Transform),
  410. })
  411. })
  412. it('should upload files in a single part', function () {
  413. expect(S3Client.upload).to.have.been.calledWith(sinon.match.any, {
  414. partSize: 100 * 1024 * 1024,
  415. })
  416. })
  417. it('should meter the stream', function () {
  418. expect(Stream.pipeline).to.have.been.calledWith(
  419. ReadStream,
  420. sinon.match.instanceOf(Stream.Transform)
  421. )
  422. })
  423. })
  424. describe('when a hash is supplied', function () {
  425. beforeEach(async function () {
  426. return S3Persistor.sendStream(bucket, key, ReadStream, {
  427. sourceMd5: 'aaaaaaaabbbbbbbbaaaaaaaabbbbbbbb',
  428. })
  429. })
  430. it('sends the hash in base64', function () {
  431. expect(S3Client.upload).to.have.been.calledWith({
  432. Bucket: bucket,
  433. Key: key,
  434. Body: sinon.match.instanceOf(Transform),
  435. ContentMD5: 'qqqqqru7u7uqqqqqu7u7uw==',
  436. })
  437. })
  438. })
  439. describe('when metadata is supplied', function () {
  440. const contentType = 'text/csv'
  441. const contentEncoding = 'gzip'
  442. beforeEach(async function () {
  443. return S3Persistor.sendStream(bucket, key, ReadStream, {
  444. contentType,
  445. contentEncoding,
  446. })
  447. })
  448. it('sends the metadata to S3', function () {
  449. expect(S3Client.upload).to.have.been.calledWith({
  450. Bucket: bucket,
  451. Key: key,
  452. Body: sinon.match.instanceOf(Transform),
  453. ContentType: contentType,
  454. ContentEncoding: contentEncoding,
  455. })
  456. })
  457. })
  458. describe('when the upload fails', function () {
  459. let error
  460. beforeEach(async function () {
  461. S3Client.upload = sinon.stub().returns({
  462. promise: sinon.stub().rejects(genericError),
  463. })
  464. try {
  465. await S3Persistor.sendStream(bucket, key, ReadStream)
  466. } catch (err) {
  467. error = err
  468. }
  469. })
  470. it('throws a WriteError', function () {
  471. expect(error).to.be.an.instanceOf(Errors.WriteError)
  472. })
  473. })
  474. })
  475. describe('sendFile', function () {
  476. describe('with valid parameters', function () {
  477. beforeEach(async function () {
  478. return S3Persistor.sendFile(bucket, key, filename)
  479. })
  480. it('should create a read stream for the file', function () {
  481. expect(Fs.createReadStream).to.have.been.calledWith(filename)
  482. })
  483. it('should upload the stream', function () {
  484. expect(S3Client.upload).to.have.been.calledWith({
  485. Bucket: bucket,
  486. Key: key,
  487. Body: sinon.match.instanceOf(Transform),
  488. })
  489. })
  490. })
  491. })
  492. describe('getObjectMd5Hash', function () {
  493. describe('when the etag is a valid md5 hash', function () {
  494. let hash
  495. beforeEach(async function () {
  496. hash = await S3Persistor.getObjectMd5Hash(bucket, key)
  497. })
  498. it('should return the object hash', function () {
  499. expect(hash).to.equal(md5)
  500. })
  501. it('should get the hash from the object metadata', function () {
  502. expect(S3Client.headObject).to.have.been.calledWith({
  503. Bucket: bucket,
  504. Key: key,
  505. })
  506. })
  507. it('should not download the object', function () {
  508. expect(S3Client.getObject).not.to.have.been.called
  509. })
  510. })
  511. describe("when the etag isn't a valid md5 hash", function () {
  512. let hash
  513. beforeEach(async function () {
  514. S3Client.headObject = sinon.stub().returns({
  515. promise: sinon.stub().resolves({
  516. ETag: 'somethingthatisntanmd5',
  517. Bucket: bucket,
  518. Key: key,
  519. }),
  520. })
  521. hash = await S3Persistor.getObjectMd5Hash(bucket, key)
  522. })
  523. it('should re-fetch the file to verify it', function () {
  524. expect(S3Client.getObject).to.have.been.calledWith({
  525. Bucket: bucket,
  526. Key: key,
  527. })
  528. })
  529. it('should calculate the md5 hash from the file', function () {
  530. expect(Hash.read).to.have.been.called
  531. })
  532. it('should return the md5 hash', function () {
  533. expect(hash).to.equal(md5)
  534. })
  535. })
  536. })
  537. describe('copyObject', function () {
  538. describe('with valid parameters', function () {
  539. beforeEach(async function () {
  540. return S3Persistor.copyObject(bucket, key, destKey)
  541. })
  542. it('should copy the object', function () {
  543. expect(S3Client.copyObject).to.have.been.calledWith({
  544. Bucket: bucket,
  545. Key: destKey,
  546. CopySource: `${bucket}/${key}`,
  547. })
  548. })
  549. })
  550. describe('when the file does not exist', function () {
  551. let error
  552. beforeEach(async function () {
  553. S3Client.copyObject = sinon.stub().returns({
  554. promise: sinon.stub().rejects(S3NotFoundError),
  555. })
  556. try {
  557. await S3Persistor.copyObject(bucket, key, destKey)
  558. } catch (err) {
  559. error = err
  560. }
  561. })
  562. it('should throw a NotFoundError', function () {
  563. expect(error).to.be.an.instanceOf(Errors.NotFoundError)
  564. })
  565. })
  566. })
  567. describe('deleteObject', function () {
  568. describe('with valid parameters', function () {
  569. beforeEach(async function () {
  570. return S3Persistor.deleteObject(bucket, key)
  571. })
  572. it('should delete the object', function () {
  573. expect(S3Client.deleteObject).to.have.been.calledWith({
  574. Bucket: bucket,
  575. Key: key,
  576. })
  577. })
  578. })
  579. })
  580. describe('deleteDirectory', function () {
  581. describe('with valid parameters', function () {
  582. beforeEach(async function () {
  583. return S3Persistor.deleteDirectory(bucket, key)
  584. })
  585. it('should list the objects in the directory', function () {
  586. expect(S3Client.listObjectsV2).to.have.been.calledWith({
  587. Bucket: bucket,
  588. Prefix: key,
  589. })
  590. })
  591. it('should delete the objects using their keys', function () {
  592. expect(S3Client.deleteObjects).to.have.been.calledWith({
  593. Bucket: bucket,
  594. Delete: {
  595. Objects: [{ Key: 'llama' }, { Key: 'hippo' }],
  596. Quiet: true,
  597. },
  598. })
  599. })
  600. })
  601. describe('when there are no files', function () {
  602. beforeEach(async function () {
  603. S3Client.listObjectsV2 = sinon
  604. .stub()
  605. .returns({ promise: sinon.stub().resolves({ Contents: [] }) })
  606. return S3Persistor.deleteDirectory(bucket, key)
  607. })
  608. it('should list the objects in the directory', function () {
  609. expect(S3Client.listObjectsV2).to.have.been.calledWith({
  610. Bucket: bucket,
  611. Prefix: key,
  612. })
  613. })
  614. it('should not try to delete any objects', function () {
  615. expect(S3Client.deleteObjects).not.to.have.been.called
  616. })
  617. })
  618. describe('when there are more files available', function () {
  619. const continuationToken = 'wombat'
  620. beforeEach(async function () {
  621. S3Client.listObjectsV2.onCall(0).returns({
  622. promise: sinon.stub().resolves({
  623. Contents: files,
  624. IsTruncated: true,
  625. NextContinuationToken: continuationToken,
  626. }),
  627. })
  628. return S3Persistor.deleteDirectory(bucket, key)
  629. })
  630. it('should list the objects a second time, with a continuation token', function () {
  631. expect(S3Client.listObjectsV2).to.be.calledTwice
  632. expect(S3Client.listObjectsV2).to.be.calledWith({
  633. Bucket: bucket,
  634. Prefix: key,
  635. })
  636. expect(S3Client.listObjectsV2).to.be.calledWith({
  637. Bucket: bucket,
  638. Prefix: key,
  639. ContinuationToken: continuationToken,
  640. })
  641. })
  642. it('should delete both sets of files', function () {
  643. expect(S3Client.deleteObjects).to.have.been.calledTwice
  644. })
  645. })
  646. describe('when there is an error listing the objects', function () {
  647. let error
  648. beforeEach(async function () {
  649. S3Client.listObjectsV2 = sinon
  650. .stub()
  651. .returns({ promise: sinon.stub().rejects(genericError) })
  652. try {
  653. await S3Persistor.deleteDirectory(bucket, key)
  654. } catch (err) {
  655. error = err
  656. }
  657. })
  658. it('should generate a ReadError', function () {
  659. expect(error).to.be.an.instanceOf(Errors.ReadError)
  660. })
  661. it('should wrap the error', function () {
  662. expect(error.cause).to.equal(genericError)
  663. })
  664. it('should not try to delete any objects', function () {
  665. expect(S3Client.deleteObjects).not.to.have.been.called
  666. })
  667. })
  668. describe('when there is an error deleting the objects', function () {
  669. let error
  670. beforeEach(async function () {
  671. S3Client.deleteObjects = sinon
  672. .stub()
  673. .returns({ promise: sinon.stub().rejects(genericError) })
  674. try {
  675. await S3Persistor.deleteDirectory(bucket, key)
  676. } catch (err) {
  677. error = err
  678. }
  679. })
  680. it('should generate a WriteError', function () {
  681. expect(error).to.be.an.instanceOf(Errors.WriteError)
  682. })
  683. it('should wrap the error', function () {
  684. expect(error.cause).to.equal(genericError)
  685. })
  686. })
  687. })
  688. describe('directorySize', function () {
  689. describe('with valid parameters', function () {
  690. let size
  691. beforeEach(async function () {
  692. size = await S3Persistor.directorySize(bucket, key)
  693. })
  694. it('should list the objects in the directory', function () {
  695. expect(S3Client.listObjectsV2).to.have.been.calledWith({
  696. Bucket: bucket,
  697. Prefix: key,
  698. })
  699. })
  700. it('should return the directory size', function () {
  701. expect(size).to.equal(filesSize)
  702. })
  703. })
  704. describe('when there are no files', function () {
  705. let size
  706. beforeEach(async function () {
  707. S3Client.listObjectsV2 = sinon
  708. .stub()
  709. .returns({ promise: sinon.stub().resolves({ Contents: [] }) })
  710. size = await S3Persistor.directorySize(bucket, key)
  711. })
  712. it('should list the objects in the directory', function () {
  713. expect(S3Client.listObjectsV2).to.have.been.calledWith({
  714. Bucket: bucket,
  715. Prefix: key,
  716. })
  717. })
  718. it('should return zero', function () {
  719. expect(size).to.equal(0)
  720. })
  721. })
  722. describe('when there are more files available', function () {
  723. const continuationToken = 'wombat'
  724. let size
  725. beforeEach(async function () {
  726. S3Client.listObjectsV2.onCall(0).returns({
  727. promise: sinon.stub().resolves({
  728. Contents: files,
  729. IsTruncated: true,
  730. NextContinuationToken: continuationToken,
  731. }),
  732. })
  733. size = await S3Persistor.directorySize(bucket, key)
  734. })
  735. it('should list the objects a second time, with a continuation token', function () {
  736. expect(S3Client.listObjectsV2).to.be.calledTwice
  737. expect(S3Client.listObjectsV2).to.be.calledWith({
  738. Bucket: bucket,
  739. Prefix: key,
  740. })
  741. expect(S3Client.listObjectsV2).to.be.calledWith({
  742. Bucket: bucket,
  743. Prefix: key,
  744. ContinuationToken: continuationToken,
  745. })
  746. })
  747. it('should return the size of both sets of files', function () {
  748. expect(size).to.equal(filesSize * 2)
  749. })
  750. })
  751. describe('when there is an error listing the objects', function () {
  752. let error
  753. beforeEach(async function () {
  754. S3Client.listObjectsV2 = sinon
  755. .stub()
  756. .returns({ promise: sinon.stub().rejects(genericError) })
  757. try {
  758. await S3Persistor.directorySize(bucket, key)
  759. } catch (err) {
  760. error = err
  761. }
  762. })
  763. it('should generate a ReadError', function () {
  764. expect(error).to.be.an.instanceOf(Errors.ReadError)
  765. })
  766. it('should wrap the error', function () {
  767. expect(error.cause).to.equal(genericError)
  768. })
  769. })
  770. })
  771. describe('checkIfObjectExists', function () {
  772. describe('when the file exists', function () {
  773. let exists
  774. beforeEach(async function () {
  775. exists = await S3Persistor.checkIfObjectExists(bucket, key)
  776. })
  777. it('should get the object header', function () {
  778. expect(S3Client.headObject).to.have.been.calledWith({
  779. Bucket: bucket,
  780. Key: key,
  781. })
  782. })
  783. it('should return that the file exists', function () {
  784. expect(exists).to.equal(true)
  785. })
  786. })
  787. describe('when the file does not exist', function () {
  788. let exists
  789. beforeEach(async function () {
  790. S3Client.headObject = sinon
  791. .stub()
  792. .returns({ promise: sinon.stub().rejects(S3NotFoundError) })
  793. exists = await S3Persistor.checkIfObjectExists(bucket, key)
  794. })
  795. it('should get the object header', function () {
  796. expect(S3Client.headObject).to.have.been.calledWith({
  797. Bucket: bucket,
  798. Key: key,
  799. })
  800. })
  801. it('should return that the file does not exist', function () {
  802. expect(exists).to.equal(false)
  803. })
  804. })
  805. describe('when there is an error', function () {
  806. let error
  807. beforeEach(async function () {
  808. S3Client.headObject = sinon
  809. .stub()
  810. .returns({ promise: sinon.stub().rejects(genericError) })
  811. try {
  812. await S3Persistor.checkIfObjectExists(bucket, key)
  813. } catch (err) {
  814. error = err
  815. }
  816. })
  817. it('should generate a ReadError', function () {
  818. expect(error).to.be.an.instanceOf(Errors.ReadError)
  819. })
  820. it('should wrap the upstream ReadError', function () {
  821. expect(error.cause).to.be.an.instanceOf(Errors.ReadError)
  822. })
  823. it('should eventually wrap the error', function () {
  824. expect(error.cause.cause).to.equal(genericError)
  825. })
  826. })
  827. })
  828. })