S3PersistorTests.js 26 KB

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