| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070 |
- const sinon = require('sinon')
- const chai = require('chai')
- const { expect } = chai
- const SandboxedModule = require('sandboxed-module')
- const Errors = require('../../src/Errors')
- const { EventEmitter } = require('node:events')
- const { Readable } = require('node:stream')
- const mockS3 = require('./S3ClientMock')
- const MODULE_PATH = '../../src/S3Persistor.js'
- describe('S3PersistorTests', function () {
- const defaultS3Key = 'frog'
- const defaultS3Secret = 'prince'
- const defaultS3Credentials = {
- credentials: {
- accessKeyId: defaultS3Key,
- secretAccessKey: defaultS3Secret,
- },
- region: 'us-east-1',
- }
- const filename = '/wombat/potato.tex'
- const bucket = 'womBucket'
- const key = 'monKey'
- const destKey = 'donKey'
- const objectSize = 5555
- const genericError = new Error('guru meditation error')
- const files = [
- { Key: 'llama', Size: 11 },
- { Key: 'hippo', Size: 22 },
- ]
- const filesSize = 33
- const md5 = 'ffffffff00000000ffffffff00000000'
- const redirectUrl = 'https://wombat.potato/giraffe'
- let Logger,
- Transform,
- PassThrough,
- S3,
- awsRequestPresigner,
- nodeHttpHandler,
- awsLibStorage,
- awsLibStorageUpload,
- abortSignal,
- Fs,
- ReadStream,
- Stream,
- StreamPromises,
- S3Persistor,
- S3NotFoundError,
- S3AccessDeniedError,
- FileNotFoundError,
- settings,
- Hash,
- crypto
- beforeEach(function () {
- settings = {
- secret: defaultS3Secret,
- key: defaultS3Key,
- partSize: 100 * 1024 * 1024,
- }
- Transform = class {
- once() {}
- }
- PassThrough = class {}
- Stream = {
- Transform,
- PassThrough,
- pipeline: sinon.stub().yields(),
- }
- StreamPromises = {
- pipeline: sinon.stub().resolves(),
- }
- ReadStream = new EventEmitter()
- FileNotFoundError = new Error('File not found')
- FileNotFoundError.code = 'ENOENT'
- Fs = {
- createReadStream: sinon.stub().returns(ReadStream),
- }
- S3NotFoundError = new Error('not found')
- S3NotFoundError.name = 'NoSuchKey'
- S3AccessDeniedError = new Error('access denied')
- S3AccessDeniedError.code = 'AccessDenied'
- S3 = mockS3()
- awsLibStorageUpload = sinon.stub().returns({
- done: sinon.stub().resolves(),
- })
- awsLibStorage = {
- Upload: awsLibStorageUpload,
- }
- awsRequestPresigner = {
- getSignedUrl: sinon.stub().resolves(redirectUrl),
- }
- nodeHttpHandler = {
- NodeHttpHandler: sinon.stub(),
- }
- Hash = {
- end: sinon.stub(),
- read: sinon.stub().returns(md5),
- setEncoding: sinon.stub(),
- }
- crypto = {
- createHash: sinon.stub().returns(Hash),
- }
- Logger = {
- warn: sinon.stub(),
- }
- abortSignal = sinon.stub()
- const AbortCtrl = sinon.stub().returns({
- signal: {},
- abort: abortSignal,
- })
- S3Persistor = new (SandboxedModule.require(MODULE_PATH, {
- requires: {
- '@aws-sdk/client-s3': S3,
- '@aws-sdk/lib-storage': awsLibStorage,
- '@aws-sdk/s3-request-presigner': awsRequestPresigner,
- '@overleaf/logger': Logger,
- '@aws-sdk/node-http-handler': nodeHttpHandler,
- './Errors': Errors,
- fs: Fs,
- stream: Stream,
- 'stream/promises': StreamPromises,
- crypto,
- },
- globals: { console, Buffer, AbortController: AbortCtrl },
- }).S3Persistor)(settings)
- })
- describe('getObjectStream', function () {
- describe('when called with valid parameters', function () {
- let stream
- beforeEach(async function () {
- S3.mockSend(S3.GetObjectCommand, {
- Body: Readable.from('content'),
- ContentEncoding: 'gzip',
- })
- stream = await S3Persistor.getObjectStream(bucket, key)
- })
- it('returns a PassThrough stream', function () {
- expect(stream).to.be.instanceOf(PassThrough)
- })
- it('fetches the right key from the right bucket', function () {
- S3.assertSendCalledWith(S3.GetObjectCommand, {
- Bucket: bucket,
- Key: key,
- })
- })
- it('pipes the stream through the meter', async function () {
- expect(Stream.pipeline).to.have.been.calledWith(
- sinon.match.instanceOf(Readable),
- sinon.match.instanceOf(Transform),
- sinon.match.instanceOf(PassThrough)
- )
- })
- it('does not abort the request', function () {
- expect(abortSignal).not.to.have.been.called
- })
- })
- describe('when called with a byte range', function () {
- let stream
- beforeEach(async function () {
- S3.mockSend(S3.GetObjectCommand, {
- Body: Readable.from('this is a longer content'),
- ContentEncoding: 'gzip',
- })
- stream = await S3Persistor.getObjectStream(bucket, key, {
- start: 5,
- end: 10,
- })
- })
- it('returns a PassThrough stream', function () {
- expect(stream).to.be.instanceOf(Stream.PassThrough)
- })
- it('passes the byte range on to S3', function () {
- S3.assertSendCalledWith(S3.GetObjectCommand, {
- Bucket: bucket,
- Key: key,
- Range: 'bytes=5-10',
- })
- })
- })
- describe('when streaming fails', function () {
- let stream
- beforeEach(async function () {
- S3.mockSend(S3.GetObjectCommand, {
- Body: Readable.from('content'),
- ContentEncoding: 'gzip',
- })
- Stream.pipeline.yields(new Error())
- stream = await S3Persistor.getObjectStream(bucket, key)
- })
- it('returns a PassThrough stream', function () {
- expect(stream).to.be.instanceOf(Stream.PassThrough)
- })
- it('aborts the request', function () {
- expect(abortSignal).to.have.been.calledOnce
- })
- })
- describe('when there are alternative credentials', function () {
- let stream
- const alternativeSecret = 'giraffe'
- const alternativeKey = 'hippo'
- const alternativeS3Credentials = {
- credentials: {
- accessKeyId: alternativeKey,
- secretAccessKey: alternativeSecret,
- },
- region: 'us-east-1',
- }
- beforeEach(async function () {
- S3.mockSend(S3.GetObjectCommand, {
- Body: Readable.from('content'),
- ContentEncoding: 'gzip',
- })
- settings.bucketCreds = {}
- settings.bucketCreds[bucket] = {
- auth_key: alternativeKey,
- auth_secret: alternativeSecret,
- }
- stream = await S3Persistor.getObjectStream(bucket, key)
- })
- it('returns a PassThrough stream', function () {
- expect(stream).to.be.instanceOf(Stream.PassThrough)
- })
- it('sets the AWS client up with the alternative credentials', function () {
- expect(S3.S3Client).to.have.been.calledWith(alternativeS3Credentials)
- })
- it('fetches the right key from the right bucket', function () {
- S3.assertSendCalledWith(S3.GetObjectCommand, {
- Bucket: bucket,
- Key: key,
- })
- })
- it('uses the default credentials for an unknown bucket', async function () {
- stream = await S3Persistor.getObjectStream('anotherBucket', key)
- expect(S3.S3Client).to.have.been.calledTwice
- expect(S3.S3Client.firstCall).to.have.been.calledWith(
- alternativeS3Credentials
- )
- expect(S3.S3Client.secondCall).to.have.been.calledWith(
- defaultS3Credentials
- )
- })
- })
- describe('without hard-coded credentials', function () {
- it('uses the default provider chain', async function () {
- delete settings.key
- delete settings.secret
- S3.mockSend(S3.GetObjectCommand, {
- Body: Readable.from('content'),
- ContentEncoding: 'gzip',
- })
- await S3Persistor.getObjectStream(bucket, key)
- expect(S3.S3Client).to.have.been.calledOnce
- expect(S3.S3Client.args[0].credentials).to.not.exist
- })
- })
- describe('when given S3 options', function () {
- const httpOptions = { connectionTimeout: 2000 }
- const maxRetries = 2
- beforeEach(async function () {
- settings.httpOptions = httpOptions
- settings.maxRetries = maxRetries
- S3.mockSend(S3.GetObjectCommand, {
- Body: Readable.from('content'),
- ContentEncoding: 'gzip',
- })
- await S3Persistor.getObjectStream(bucket, key)
- })
- it('configures the options and the requestHandler with NodeHttpHandler', function () {
- expect(S3.S3Client).to.have.been.calledWithMatch({
- requestHandler: sinon.match.any,
- maxAttempts: maxRetries + 1,
- })
- expect(nodeHttpHandler.NodeHttpHandler).to.have.been.calledWithMatch(
- httpOptions
- )
- })
- })
- describe("when the file doesn't exist", function () {
- let error, stream
- beforeEach(async function () {
- S3.mockSend(S3.GetObjectCommand, S3NotFoundError, { rejects: true })
- try {
- stream = await S3Persistor.getObjectStream(bucket, key)
- } catch (err) {
- error = err
- }
- })
- it('does not return a stream', function () {
- expect(stream).not.to.exist
- })
- it('throws a NotFoundError', function () {
- expect(error).to.be.an.instanceOf(Errors.NotFoundError)
- })
- it('wraps the error', function () {
- expect(error.cause).to.exist
- })
- it('stores the bucket and key in the error', function () {
- expect(error.info).to.include({ bucketName: bucket, key })
- })
- })
- describe("when the file doesn't exist -- SSEC", function () {
- let error, stream
- beforeEach(async function () {
- S3.mockSend(S3.GetObjectCommand, S3NotFoundError, { rejects: true })
- try {
- stream = await S3Persistor.getObjectStream(bucket, key)
- } catch (err) {
- error = err
- }
- })
- it('does not return a stream', function () {
- expect(stream).not.to.exist
- })
- it('throws a NotFoundError', function () {
- expect(error).to.be.an.instanceOf(Errors.NotFoundError)
- })
- it('wraps the error', function () {
- expect(error.cause).to.exist
- })
- it('stores the bucket and key in the error', function () {
- expect(error.info).to.include({ bucketName: bucket, key })
- })
- })
- describe('when access to the file is denied', function () {
- let error, stream
- beforeEach(async function () {
- S3.mockSend(S3.GetObjectCommand, S3AccessDeniedError, { rejects: true })
- try {
- stream = await S3Persistor.getObjectStream(bucket, key)
- } catch (err) {
- error = err
- }
- })
- it('does not return a stream', function () {
- expect(stream).not.to.exist
- })
- it('throws a NotFoundError', function () {
- expect(error).to.be.an.instanceOf(Errors.NotFoundError)
- })
- it('wraps the error', function () {
- expect(error.cause).to.equal(S3AccessDeniedError)
- })
- it('stores the bucket and key in the error', function () {
- expect(error.info).to.include({ bucketName: bucket, key })
- })
- })
- describe('when S3 encounters an unknown error', function () {
- let error, stream
- beforeEach(async function () {
- S3.mockSend(S3.GetObjectCommand, genericError, { rejects: true })
- try {
- stream = await S3Persistor.getObjectStream(bucket, key)
- } catch (err) {
- error = err
- }
- })
- it('does not return a stream', function () {
- expect(stream).not.to.exist
- })
- it('throws a ReadError', function () {
- expect(error).to.be.an.instanceOf(Errors.ReadError)
- })
- it('wraps the error', function () {
- expect(error.cause).to.exist
- })
- it('stores the bucket and key in the error', function () {
- expect(error.info).to.include({ bucketName: bucket, key })
- })
- })
- })
- describe('getRedirectUrl', function () {
- let signedUrl
- beforeEach(async function () {
- signedUrl = await S3Persistor.getRedirectUrl(bucket, key)
- })
- it('should request a signed URL', function () {
- expect(awsRequestPresigner.getSignedUrl).to.have.been.called
- })
- it('should return the url', function () {
- expect(signedUrl).to.equal(redirectUrl)
- })
- })
- describe('getObjectSize', function () {
- describe('when called with valid parameters', function () {
- let size
- beforeEach(async function () {
- S3.mockSend(S3.HeadObjectCommand, { ContentLength: objectSize })
- size = await S3Persistor.getObjectSize(bucket, key)
- })
- it('should return the object size', function () {
- expect(size).to.equal(objectSize)
- })
- it('should pass the bucket and key to S3', function () {
- S3.assertSendCalledWith(S3.HeadObjectCommand, {
- Bucket: bucket,
- Key: key,
- })
- })
- })
- describe('when the object is not found', function () {
- let error
- beforeEach(async function () {
- S3.mockSend(S3.HeadObjectCommand, S3NotFoundError, { rejects: true })
- try {
- await S3Persistor.getObjectSize(bucket, key)
- } catch (err) {
- error = err
- }
- })
- it('should return a NotFoundError', function () {
- expect(error).to.be.an.instanceOf(Errors.NotFoundError)
- })
- it('should wrap the error', function () {
- expect(error.cause).to.equal(S3NotFoundError)
- })
- })
- describe('when S3 returns an error', function () {
- let error
- beforeEach(async function () {
- S3.mockSend(S3.HeadObjectCommand, genericError, { rejects: true })
- try {
- await S3Persistor.getObjectSize(bucket, key)
- } catch (err) {
- error = err
- }
- })
- it('should return a ReadError', function () {
- expect(error).to.be.an.instanceOf(Errors.ReadError)
- })
- it('should wrap the error', function () {
- expect(error.cause).to.equal(genericError)
- })
- })
- })
- describe('sendStream', function () {
- describe('with valid parameters', function () {
- beforeEach(async function () {
- await S3Persistor.sendStream(bucket, key, ReadStream)
- })
- it('should upload the stream in a single part', function () {
- expect(awsLibStorageUpload).to.have.been.calledWith({
- client: S3.s3ClientStub,
- params: {
- Bucket: bucket,
- Key: key,
- Body: sinon.match.instanceOf(Stream.Transform),
- },
- partSize: 100 * 1024 * 1024,
- })
- })
- it('should meter the stream', function () {
- expect(Stream.pipeline).to.have.been.calledWith(
- ReadStream,
- sinon.match.instanceOf(Stream.Transform)
- )
- })
- })
- describe('when a hash is supplied', function () {
- beforeEach(async function () {
- await S3Persistor.sendStream(bucket, key, ReadStream, {
- sourceMd5: 'aaaaaaaabbbbbbbbaaaaaaaabbbbbbbb',
- })
- })
- it('sends the hash in base64', function () {
- expect(awsLibStorageUpload).to.have.been.calledWith({
- client: S3.s3ClientStub,
- params: {
- Bucket: bucket,
- Key: key,
- Body: sinon.match.instanceOf(Transform),
- ContentMD5: 'qqqqqru7u7uqqqqqu7u7uw==',
- },
- partSize: 100 * 1024 * 1024,
- })
- })
- })
- describe('when metadata is supplied', function () {
- const contentType = 'text/csv'
- const contentEncoding = 'gzip'
- beforeEach(async function () {
- await S3Persistor.sendStream(bucket, key, ReadStream, {
- contentType,
- contentEncoding,
- })
- })
- it('sends the metadata to S3', function () {
- expect(awsLibStorageUpload).to.have.been.calledWith({
- client: S3.s3ClientStub,
- params: {
- Bucket: bucket,
- Key: key,
- Body: sinon.match.instanceOf(Transform),
- ContentType: contentType,
- ContentEncoding: contentEncoding,
- },
- partSize: 100 * 1024 * 1024,
- })
- })
- })
- describe('when multipart upload is disabled', function () {
- const contentType = 'text/csv'
- const contentEncoding = 'gzip'
- beforeEach(async function () {
- S3.mockSend(S3.PutObjectCommand, {})
- settings.disableMultiPartUpload = true
- await S3Persistor.sendStream(bucket, key, ReadStream, {
- contentType,
- contentEncoding,
- })
- })
- it('configures the options to not to retry requests', function () {
- expect(S3.S3Client).to.have.been.calledWithMatch({
- maxAttempts: 1,
- })
- })
- })
- describe('when the upload fails', function () {
- let error
- beforeEach(async function () {
- awsLibStorageUpload.rejects(genericError)
- try {
- await S3Persistor.sendStream(bucket, key, ReadStream)
- } catch (err) {
- error = err
- }
- })
- it('throws a WriteError', function () {
- expect(error).to.be.an.instanceOf(Errors.WriteError)
- })
- })
- })
- describe('sendFile', function () {
- describe('with valid parameters', function () {
- beforeEach(async function () {
- S3.s3ClientStub.send.resolves()
- await S3Persistor.sendFile(bucket, key, filename)
- })
- it('should create a read stream for the file', function () {
- expect(Fs.createReadStream).to.have.been.calledWith(filename)
- })
- it('should upload the stream', function () {
- expect(awsLibStorageUpload).to.have.been.calledWith({
- client: S3.s3ClientStub,
- params: {
- Bucket: bucket,
- Key: key,
- Body: sinon.match.instanceOf(Transform),
- },
- partSize: settings.partSize,
- })
- })
- })
- })
- describe('getObjectMd5Hash', function () {
- describe('when the etag is a valid md5 hash', function () {
- let hash
- beforeEach(async function () {
- S3.mockSend(S3.HeadObjectCommand, {
- ContentLength: objectSize,
- ETag: md5,
- })
- hash = await S3Persistor.getObjectMd5Hash(bucket, key)
- })
- it('should return the object hash', function () {
- expect(hash).to.equal(md5)
- })
- it('should get the hash from the object metadata', function () {
- S3.assertSendCalledWith(S3.HeadObjectCommand, {
- Bucket: bucket,
- Key: key,
- })
- })
- it('should not download the object', function () {
- S3.assertSendNotCalledWith(S3.GetObjectCommand)
- })
- })
- describe("when the etag isn't a valid md5 hash", function () {
- let hash
- beforeEach(async function () {
- S3.mockSend(S3.GetObjectCommand, {
- ContentLength: objectSize,
- ETag: md5,
- })
- S3.mockSend(S3.HeadObjectCommand, {
- ETag: 'somethingthatisntanmd5',
- Bucket: bucket,
- Key: key,
- })
- hash = await S3Persistor.getObjectMd5Hash(bucket, key)
- })
- it('should re-fetch the file to verify it', function () {
- S3.assertSendCalledWith(S3.GetObjectCommand, {
- Bucket: bucket,
- Key: key,
- })
- })
- it('should calculate the md5 hash from the file', function () {
- expect(Hash.read).to.have.been.called
- })
- it('should return the md5 hash', function () {
- expect(hash).to.equal(md5)
- })
- })
- })
- describe('copyObject', function () {
- describe('with valid parameters', function () {
- beforeEach(async function () {
- S3.mockSend(S3.CopyObjectCommand)
- await S3Persistor.copyObject(bucket, key, destKey)
- })
- it('should copy the object', function () {
- S3.assertSendCalledWith(S3.CopyObjectCommand, {
- Bucket: bucket,
- Key: destKey,
- CopySource: `/${bucket}/${key}`,
- })
- })
- })
- describe('when the file does not exist', function () {
- let error
- beforeEach(async function () {
- S3.mockSend(S3.CopyObjectCommand, S3NotFoundError, { rejects: true })
- try {
- await S3Persistor.copyObject(bucket, key, destKey)
- } catch (err) {
- error = err
- }
- })
- it('should throw a NotFoundError', function () {
- expect(error).to.be.an.instanceOf(Errors.NotFoundError)
- })
- })
- })
- describe('deleteObject', function () {
- describe('with valid parameters', function () {
- beforeEach(async function () {
- S3.mockSend(S3.DeleteObjectCommand)
- await S3Persistor.deleteObject(bucket, key)
- })
- it('should delete the object', function () {
- S3.assertSendCalledWith(S3.DeleteObjectCommand, {
- Bucket: bucket,
- Key: key,
- })
- })
- })
- })
- describe('deleteDirectory', function () {
- describe('with valid parameters', function () {
- beforeEach(async function () {
- S3.mockSend(S3.ListObjectsV2Command, { Contents: files })
- await S3Persistor.deleteDirectory(bucket, key)
- })
- it('should list the objects in the directory', function () {
- S3.assertSendCalledWith(S3.ListObjectsV2Command, {
- Bucket: bucket,
- Prefix: key,
- })
- })
- it('should delete the objects using their keys', function () {
- S3.s3ClientStub.send.withArgs(new S3.DeleteObjectsCommand()).resolves()
- S3.assertSendCalledWith(
- S3.DeleteObjectsCommand,
- {
- Bucket: bucket,
- Delete: {
- Objects: [{ Key: 'llama' }, { Key: 'hippo' }],
- Quiet: true,
- },
- },
- 1
- )
- })
- })
- describe('when there are no files', function () {
- beforeEach(async function () {
- S3.mockSend(S3.ListObjectsV2Command, { Contents: [] })
- await S3Persistor.deleteDirectory(bucket, key)
- })
- it('should list the objects in the directory', function () {
- S3.assertSendCalledWith(S3.ListObjectsV2Command, {
- Bucket: bucket,
- Prefix: key,
- })
- })
- it('should not try to delete any objects', function () {
- S3.assertSendNotCalledWith(S3.DeleteObjectsCommand)
- })
- })
- describe('when there are more files available', function () {
- const continuationToken = 'wombat'
- beforeEach(async function () {
- S3.mockSend(
- S3.ListObjectsV2Command,
- {
- Contents: files,
- IsTruncated: true,
- NextContinuationToken: continuationToken,
- },
- {
- nextResponses: [{ Contents: [{ Key: 'last-file', Size: 33 }] }],
- }
- )
- S3.mockSend(S3.DeleteObjectsCommand)
- return S3Persistor.deleteDirectory(bucket, key)
- })
- it('should list the objects a second time, with a continuation token', function () {
- S3.assertSendCallCount(S3.ListObjectsV2Command, 2)
- expect(S3.s3ClientStub.send.firstCall.args[0].payload).to.deep.equal({
- Bucket: bucket,
- Prefix: key,
- })
- expect(S3.s3ClientStub.send.thirdCall.args[0].payload).to.deep.equal({
- Bucket: bucket,
- Prefix: key,
- ContinuationToken: continuationToken,
- })
- })
- it('should delete both sets of files', function () {
- S3.assertSendCallCount(S3.DeleteObjectsCommand, 2)
- })
- })
- describe('when there is an error listing the objects', function () {
- let error
- beforeEach(async function () {
- S3.mockSend(S3.ListObjectsV2Command, genericError, { rejects: true })
- try {
- await S3Persistor.deleteDirectory(bucket, key)
- } catch (err) {
- error = err
- }
- })
- it('should generate a ReadError', function () {
- expect(error).to.be.an.instanceOf(Errors.ReadError)
- })
- it('should wrap the error', function () {
- expect(error.cause).to.equal(genericError)
- })
- it('should not try to delete any objects', function () {
- // call count should be 1, only the ListObjectsV2Command tested above
- expect(S3.s3ClientStub.send.callCount).to.equal(1)
- })
- })
- describe('when there is an error deleting the objects', function () {
- let error
- beforeEach(async function () {
- S3.mockSend(S3.ListObjectsV2Command, { Contents: files })
- S3.mockSend(S3.DeleteObjectsCommand, genericError, { rejects: true })
- try {
- await S3Persistor.deleteDirectory(bucket, key)
- } catch (err) {
- error = err
- }
- })
- it('should generate a WriteError', function () {
- expect(error).to.be.an.instanceOf(Errors.WriteError)
- })
- it('should wrap the error', function () {
- expect(error.cause).to.equal(genericError)
- })
- })
- })
- describe('directorySize', function () {
- describe('with valid parameters', function () {
- let size
- beforeEach(async function () {
- S3.mockSend(S3.ListObjectsV2Command, { Contents: files })
- size = await S3Persistor.directorySize(bucket, key)
- })
- it('should list the objects in the directory', function () {
- S3.assertSendCalledWith(S3.ListObjectsV2Command, {
- Bucket: bucket,
- Prefix: key,
- })
- })
- it('should return the directory size', function () {
- expect(size).to.equal(filesSize)
- })
- })
- describe('when there are no files', function () {
- let size
- beforeEach(async function () {
- S3.mockSend(S3.ListObjectsV2Command, { Contents: [] })
- size = await S3Persistor.directorySize(bucket, key)
- })
- it('should list the objects in the directory', function () {
- S3.assertSendCalledWith(S3.ListObjectsV2Command, {
- Bucket: bucket,
- Prefix: key,
- })
- })
- it('should return zero', function () {
- expect(size).to.equal(0)
- })
- })
- describe('when there are more files available', function () {
- const continuationToken = 'wombat'
- let size
- beforeEach(async function () {
- S3.mockSend(
- S3.ListObjectsV2Command,
- {
- Contents: files,
- IsTruncated: true,
- NextContinuationToken: continuationToken,
- },
- {
- nextResponses: [{ Contents: [{ Key: 'last-file', Size: 33 }] }],
- }
- )
- size = await S3Persistor.directorySize(bucket, key)
- })
- it('should list the objects a second time, with a continuation token', function () {
- S3.assertSendCallCount(S3.ListObjectsV2Command, 2)
- expect(S3.s3ClientStub.send.firstCall.args[0].payload).to.deep.equal({
- Bucket: bucket,
- Prefix: key,
- })
- expect(S3.s3ClientStub.send.secondCall.args[0].payload).to.deep.equal({
- Bucket: bucket,
- Prefix: key,
- ContinuationToken: continuationToken,
- })
- })
- it('should return the size of both sets of files', function () {
- expect(size).to.equal(filesSize * 2)
- })
- })
- describe('when there is an error listing the objects', function () {
- let error
- beforeEach(async function () {
- S3.mockSend(S3.ListObjectsV2Command, genericError, { rejects: true })
- try {
- await S3Persistor.directorySize(bucket, key)
- } catch (err) {
- error = err
- }
- })
- it('should generate a ReadError', function () {
- expect(error).to.be.an.instanceOf(Errors.ReadError)
- })
- it('should wrap the error', function () {
- expect(error.cause).to.equal(genericError)
- })
- })
- })
- describe('checkIfObjectExists', function () {
- describe('when the file exists', function () {
- let exists
- beforeEach(async function () {
- S3.mockSend(S3.HeadObjectCommand, { ContentLength: objectSize })
- exists = await S3Persistor.checkIfObjectExists(bucket, key)
- })
- it('should get the object header', function () {
- S3.assertSendCalledWith(S3.HeadObjectCommand, {
- Bucket: bucket,
- Key: key,
- })
- })
- it('should return that the file exists', function () {
- expect(exists).to.equal(true)
- })
- })
- describe('when the file does not exist', function () {
- let exists
- beforeEach(async function () {
- S3.mockSend(S3.HeadObjectCommand, S3NotFoundError, { rejects: true })
- S3.s3ClientStub.send.rejects(S3NotFoundError)
- exists = await S3Persistor.checkIfObjectExists(bucket, key)
- })
- it('should get the object header', function () {
- S3.assertSendCalledWith(S3.HeadObjectCommand, {
- Bucket: bucket,
- Key: key,
- })
- })
- it('should return that the file does not exist', function () {
- expect(exists).to.equal(false)
- })
- })
- describe('when there is an error', function () {
- let error
- beforeEach(async function () {
- S3.mockSend(S3.HeadObjectCommand, genericError, { rejects: true })
- try {
- await S3Persistor.checkIfObjectExists(bucket, key)
- } catch (err) {
- error = err
- }
- })
- it('should generate a ReadError', function () {
- expect(error).to.be.an.instanceOf(Errors.ReadError)
- })
- it('should wrap the upstream ReadError', function () {
- expect(error.cause).to.be.an.instanceOf(Errors.ReadError)
- })
- it('should eventually wrap the error', function () {
- expect(error.cause.cause).to.equal(genericError)
- })
- })
- })
- })
|