S3PersistorTests.js 27 KB

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