diff --git a/packages/storage/__tests__/providers/s3/apis/uploadData/index.test.ts b/packages/storage/__tests__/providers/s3/apis/uploadData/index.test.ts index 938ca8863ee..2b740c37e53 100644 --- a/packages/storage/__tests__/providers/s3/apis/uploadData/index.test.ts +++ b/packages/storage/__tests__/providers/s3/apis/uploadData/index.test.ts @@ -1,6 +1,8 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 +import { AmplifyClassV6 } from '@aws-amplify/core'; + import { uploadData } from '../../../../../src/providers/s3/apis'; import { MAX_OBJECT_SIZE } from '../../../../../src/providers/s3/utils/constants'; import { createUploadTask } from '../../../../../src/providers/s3/utils'; @@ -8,13 +10,15 @@ import { StorageValidationErrorCode, validationErrorMap, } from '../../../../../src/errors/types/validation'; -import { putObjectJob } from '../../../../../src/providers/s3/apis/uploadData/putObjectJob'; -import { getMultipartUploadHandlers } from '../../../../../src/providers/s3/apis/uploadData/multipart'; +import { putObjectJob } from '../../../../../src/providers/s3/apis/internal/uploadData/putObjectJob'; +import { getMultipartUploadHandlers } from '../../../../../src/providers/s3/apis/internal/uploadData/multipart'; import { UploadDataInput, UploadDataWithPathInput } from '../../../../../src'; jest.mock('../../../../../src/providers/s3/utils/'); -jest.mock('../../../../../src/providers/s3/apis/uploadData/putObjectJob'); -jest.mock('../../../../../src/providers/s3/apis/uploadData/multipart'); +jest.mock( + '../../../../../src/providers/s3/apis/internal/uploadData/putObjectJob', +); +jest.mock('../../../../../src/providers/s3/apis/internal/uploadData/multipart'); const testPath = 'testPath/object'; const mockCreateUploadTask = createUploadTask as jest.Mock; @@ -172,6 +176,7 @@ describe('uploadData with path', () => { uploadData(testInput); expect(mockPutObjectJob).toHaveBeenCalledWith( + expect.any(AmplifyClassV6), testInput, expect.any(AbortSignal), expect.any(Number), @@ -212,6 +217,7 @@ describe('uploadData with path', () => { expect(mockPutObjectJob).not.toHaveBeenCalled(); expect(mockGetMultipartUploadHandlers).toHaveBeenCalledWith( + expect.any(AmplifyClassV6), testInput, expect.any(Number), ); diff --git a/packages/storage/__tests__/providers/s3/apis/uploadData/multipartHandlers.test.ts b/packages/storage/__tests__/providers/s3/apis/uploadData/multipartHandlers.test.ts index 8957c9ef764..b0f0b248730 100644 --- a/packages/storage/__tests__/providers/s3/apis/uploadData/multipartHandlers.test.ts +++ b/packages/storage/__tests__/providers/s3/apis/uploadData/multipartHandlers.test.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { AWSCredentials } from '@aws-amplify/core/internals/utils'; -import { Amplify, defaultStorage } from '@aws-amplify/core'; +import { AmplifyClassV6, defaultStorage } from '@aws-amplify/core'; import { abortMultipartUpload, @@ -12,18 +12,33 @@ import { listParts, uploadPart, } from '../../../../../src/providers/s3/utils/client'; -import { getMultipartUploadHandlers } from '../../../../../src/providers/s3/apis/uploadData/multipart'; +import { getMultipartUploadHandlers } from '../../../../../src/providers/s3/apis/internal/uploadData/multipart'; import { StorageValidationErrorCode, validationErrorMap, } from '../../../../../src/errors/types/validation'; import { UPLOADS_STORAGE_KEY } from '../../../../../src/providers/s3/utils/constants'; -import { byteLength } from '../../../../../src/providers/s3/apis/uploadData/byteLength'; +import { byteLength } from '../../../../../src/providers/s3/apis/internal/uploadData/byteLength'; import { CanceledError } from '../../../../../src/errors/CanceledError'; import { StorageOptions } from '../../../../../src/types'; import '../testUtils'; -jest.mock('@aws-amplify/core'); +jest.mock('@aws-amplify/core', () => ({ + ConsoleLogger: jest.fn(() => ({ + debug: jest.fn(), + })), + defaultStorage: { + getItem: jest.fn(), + setItem: jest.fn(), + }, + AmplifyClassV6: jest.fn(() => ({ + libraryOptions: {}, + getConfig: jest.fn(), + Auth: { + fetchAuthSession: jest.fn(), + }, + })), +})); jest.mock('../../../../../src/providers/s3/utils/client'); const credentials: AWSCredentials = { @@ -32,7 +47,6 @@ const credentials: AWSCredentials = { secretAccessKey: 'secretAccessKey', }; const defaultIdentityId = 'defaultIdentityId'; -const mockFetchAuthSession = Amplify.Auth.fetchAuthSession as jest.Mock; const bucket = 'bucket'; const region = 'region'; const defaultKey = 'key'; @@ -133,12 +147,16 @@ const resetS3Mocks = () => { /* TODO Remove suite when `key` parameter is removed */ describe('getMultipartUploadHandlers with key', () => { + let amplify: AmplifyClassV6; + let mockFetchAuthSession: jest.Mock; beforeAll(() => { + amplify = new AmplifyClassV6(); + mockFetchAuthSession = amplify.Auth.fetchAuthSession as jest.Mock; mockFetchAuthSession.mockResolvedValue({ credentials, identityId: defaultIdentityId, }); - (Amplify.getConfig as jest.Mock).mockReturnValue({ + (amplify.getConfig as jest.Mock).mockReturnValue({ Storage: { S3: { bucket, @@ -156,6 +174,7 @@ describe('getMultipartUploadHandlers with key', () => { it('should return multipart upload handlers', async () => { const multipartUploadHandlers = getMultipartUploadHandlers( + amplify, { key: defaultKey, data: { size: 5 * 1024 * 1024 } as any, @@ -200,7 +219,7 @@ describe('getMultipartUploadHandlers with key', () => { `should upload a %s type body that splits in 2 parts using ${accessLevelMsg} accessLevel`, async (_, twoPartsPayload) => { mockMultipartUploadSuccess(); - const { multipartUploadJob } = getMultipartUploadHandlers({ + const { multipartUploadJob } = getMultipartUploadHandlers(amplify, { key: defaultKey, data: twoPartsPayload, options: options as StorageOptions, @@ -232,7 +251,7 @@ describe('getMultipartUploadHandlers with key', () => { it('should throw if unsupported payload type is provided', async () => { mockMultipartUploadSuccess(); - const { multipartUploadJob } = getMultipartUploadHandlers({ + const { multipartUploadJob } = getMultipartUploadHandlers(amplify, { key: defaultKey, data: 1 as any, }); @@ -261,6 +280,7 @@ describe('getMultipartUploadHandlers with key', () => { } as any as File; mockMultipartUploadSuccess(); const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { key: defaultKey, data: file, @@ -287,6 +307,7 @@ describe('getMultipartUploadHandlers with key', () => { }); const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { key: defaultKey, data: new ArrayBuffer(8 * MB), @@ -309,7 +330,7 @@ describe('getMultipartUploadHandlers with key', () => { mockCreateMultipartUpload.mockReset(); mockCreateMultipartUpload.mockRejectedValueOnce(new Error('error')); - const { multipartUploadJob } = getMultipartUploadHandlers({ + const { multipartUploadJob } = getMultipartUploadHandlers(amplify, { key: defaultKey, data: new ArrayBuffer(8 * MB), }); @@ -322,7 +343,7 @@ describe('getMultipartUploadHandlers with key', () => { mockCompleteMultipartUpload.mockReset(); mockCompleteMultipartUpload.mockRejectedValueOnce(new Error('error')); - const { multipartUploadJob } = getMultipartUploadHandlers({ + const { multipartUploadJob } = getMultipartUploadHandlers(amplify, { key: defaultKey, data: new ArrayBuffer(8 * MB), }); @@ -340,7 +361,7 @@ describe('getMultipartUploadHandlers with key', () => { }); mockUploadPart.mockRejectedValueOnce(new Error('error')); - const { multipartUploadJob } = getMultipartUploadHandlers({ + const { multipartUploadJob } = getMultipartUploadHandlers(amplify, { key: defaultKey, data: new ArrayBuffer(8 * MB), }); @@ -355,7 +376,7 @@ describe('getMultipartUploadHandlers with key', () => { const mockBucket = 'bucket-1'; const mockRegion = 'region-1'; mockMultipartUploadSuccess(); - const { multipartUploadJob } = getMultipartUploadHandlers({ + const { multipartUploadJob } = getMultipartUploadHandlers(amplify, { key: 'key', data: mockData, options: { @@ -381,7 +402,7 @@ describe('getMultipartUploadHandlers with key', () => { it('should override bucket in putObject call when bucket as string', async () => { mockMultipartUploadSuccess(); - const { multipartUploadJob } = getMultipartUploadHandlers({ + const { multipartUploadJob } = getMultipartUploadHandlers(amplify, { key: 'key', data: mockData, options: { @@ -420,6 +441,7 @@ describe('getMultipartUploadHandlers with key', () => { mockMultipartUploadSuccess(); const size = 8 * MB; const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { key: defaultKey, data: new ArrayBuffer(size), @@ -448,6 +470,7 @@ describe('getMultipartUploadHandlers with key', () => { mockListParts.mockResolvedValueOnce({ Parts: [], $metadata: {} }); const size = 8 * MB; const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { key: defaultKey, data: new ArrayBuffer(size), @@ -466,6 +489,7 @@ describe('getMultipartUploadHandlers with key', () => { mockListParts.mockResolvedValueOnce({ Parts: [], $metadata: {} }); const size = 8 * MB; const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { key: defaultKey, data: new File([new ArrayBuffer(size)], 'someName'), @@ -501,6 +525,7 @@ describe('getMultipartUploadHandlers with key', () => { mockListParts.mockResolvedValueOnce({ Parts: [], $metadata: {} }); const size = 8 * MB; const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { key: defaultKey, data: new ArrayBuffer(size), @@ -519,6 +544,7 @@ describe('getMultipartUploadHandlers with key', () => { mockListParts.mockResolvedValueOnce({ Parts: [], $metadata: {} }); const size = 8 * MB; const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { key: defaultKey, data: new ArrayBuffer(size), @@ -546,6 +572,7 @@ describe('getMultipartUploadHandlers with key', () => { mockListParts.mockResolvedValueOnce({ Parts: [], $metadata: {} }); const size = 8 * MB; const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { key: defaultKey, data: new ArrayBuffer(size), @@ -568,6 +595,7 @@ describe('getMultipartUploadHandlers with key', () => { mockListParts.mockResolvedValueOnce({ Parts: [], $metadata: {} }); const size = 8 * MB; const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { key: defaultKey, data: new ArrayBuffer(size), @@ -588,10 +616,13 @@ describe('getMultipartUploadHandlers with key', () => { describe('cancel()', () => { it('should abort in-flight uploadPart requests and throw if upload is canceled', async () => { - const { multipartUploadJob, onCancel } = getMultipartUploadHandlers({ - key: defaultKey, - data: new ArrayBuffer(8 * MB), - }); + const { multipartUploadJob, onCancel } = getMultipartUploadHandlers( + amplify, + { + key: defaultKey, + data: new ArrayBuffer(8 * MB), + }, + ); let partCount = 0; mockMultipartUploadCancellation(() => { partCount++; @@ -616,7 +647,7 @@ describe('getMultipartUploadHandlers with key', () => { describe('pause() & resume()', () => { it('should abort in-flight uploadPart requests if upload is paused', async () => { const { multipartUploadJob, onPause, onResume } = - getMultipartUploadHandlers({ + getMultipartUploadHandlers(amplify, { key: defaultKey, data: new ArrayBuffer(8 * MB), }); @@ -641,6 +672,7 @@ describe('getMultipartUploadHandlers with key', () => { const onProgress = jest.fn(); mockMultipartUploadSuccess(); const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { key: defaultKey, data: new ArrayBuffer(8 * MB), @@ -692,6 +724,7 @@ describe('getMultipartUploadHandlers with key', () => { const onProgress = jest.fn(); const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { key: defaultKey, data: new ArrayBuffer(8 * MB), @@ -713,12 +746,16 @@ describe('getMultipartUploadHandlers with key', () => { }); describe('getMultipartUploadHandlers with path', () => { + let mockFetchAuthSession: jest.Mock; + let amplify: AmplifyClassV6; beforeAll(() => { + amplify = new AmplifyClassV6(); + mockFetchAuthSession = amplify.Auth.fetchAuthSession as jest.Mock; mockFetchAuthSession.mockResolvedValue({ credentials, identityId: defaultIdentityId, }); - (Amplify.getConfig as jest.Mock).mockReturnValue({ + (amplify.getConfig as jest.Mock).mockReturnValue({ Storage: { S3: { bucket, @@ -736,6 +773,7 @@ describe('getMultipartUploadHandlers with path', () => { it('should return multipart upload handlers', async () => { const multipartUploadHandlers = getMultipartUploadHandlers( + amplify, { path: testPath, data: { size: 5 * 1024 * 1024 } as any, @@ -773,7 +811,7 @@ describe('getMultipartUploadHandlers with path', () => { `should upload a %s type body that splits into 2 parts to path ${expectedKey}`, async (_, twoPartsPayload) => { mockMultipartUploadSuccess(); - const { multipartUploadJob } = getMultipartUploadHandlers({ + const { multipartUploadJob } = getMultipartUploadHandlers(amplify, { path: inputPath, data: twoPartsPayload, }); @@ -804,7 +842,7 @@ describe('getMultipartUploadHandlers with path', () => { it('should throw if unsupported payload type is provided', async () => { mockMultipartUploadSuccess(); - const { multipartUploadJob } = getMultipartUploadHandlers({ + const { multipartUploadJob } = getMultipartUploadHandlers(amplify, { path: testPath, data: 1 as any, }); @@ -833,6 +871,7 @@ describe('getMultipartUploadHandlers with path', () => { } as any as File; mockMultipartUploadSuccess(); const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { path: testPath, data: file, @@ -859,6 +898,7 @@ describe('getMultipartUploadHandlers with path', () => { }); const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { path: testPath, data: new ArrayBuffer(8 * MB), @@ -881,7 +921,7 @@ describe('getMultipartUploadHandlers with path', () => { mockCreateMultipartUpload.mockReset(); mockCreateMultipartUpload.mockRejectedValueOnce(new Error('error')); - const { multipartUploadJob } = getMultipartUploadHandlers({ + const { multipartUploadJob } = getMultipartUploadHandlers(amplify, { path: testPath, data: new ArrayBuffer(8 * MB), }); @@ -894,7 +934,7 @@ describe('getMultipartUploadHandlers with path', () => { mockCompleteMultipartUpload.mockReset(); mockCompleteMultipartUpload.mockRejectedValueOnce(new Error('error')); - const { multipartUploadJob } = getMultipartUploadHandlers({ + const { multipartUploadJob } = getMultipartUploadHandlers(amplify, { path: testPath, data: new ArrayBuffer(8 * MB), }); @@ -912,7 +952,7 @@ describe('getMultipartUploadHandlers with path', () => { }); mockUploadPart.mockRejectedValueOnce(new Error('error')); - const { multipartUploadJob } = getMultipartUploadHandlers({ + const { multipartUploadJob } = getMultipartUploadHandlers(amplify, { path: testPath, data: new ArrayBuffer(8 * MB), }); @@ -927,7 +967,7 @@ describe('getMultipartUploadHandlers with path', () => { const mockBucket = 'bucket-1'; const mockRegion = 'region-1'; mockMultipartUploadSuccess(); - const { multipartUploadJob } = getMultipartUploadHandlers({ + const { multipartUploadJob } = getMultipartUploadHandlers(amplify, { path: 'path/', data: mockData, options: { @@ -955,7 +995,7 @@ describe('getMultipartUploadHandlers with path', () => { }); it('should override bucket in putObject call when bucket as string', async () => { mockMultipartUploadSuccess(); - const { multipartUploadJob } = getMultipartUploadHandlers({ + const { multipartUploadJob } = getMultipartUploadHandlers(amplify, { path: 'path/', data: mockData, options: { @@ -997,6 +1037,7 @@ describe('getMultipartUploadHandlers with path', () => { mockMultipartUploadSuccess(); const size = 8 * MB; const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { path: testPath, data: new ArrayBuffer(size), @@ -1025,6 +1066,7 @@ describe('getMultipartUploadHandlers with path', () => { mockListParts.mockResolvedValueOnce({ Parts: [], $metadata: {} }); const size = 8 * MB; const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { path: testPath, data: new ArrayBuffer(size), @@ -1043,6 +1085,7 @@ describe('getMultipartUploadHandlers with path', () => { mockListParts.mockResolvedValueOnce({ Parts: [], $metadata: {} }); const size = 8 * MB; const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { path: testPath, data: new File([new ArrayBuffer(size)], 'someName'), @@ -1081,6 +1124,7 @@ describe('getMultipartUploadHandlers with path', () => { mockListParts.mockResolvedValueOnce({ Parts: [], $metadata: {} }); const size = 8 * MB; const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { path: testPath, data: new ArrayBuffer(size), @@ -1099,6 +1143,7 @@ describe('getMultipartUploadHandlers with path', () => { mockListParts.mockResolvedValueOnce({ Parts: [], $metadata: {} }); const size = 8 * MB; const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { path: testPath, data: new ArrayBuffer(size), @@ -1124,6 +1169,7 @@ describe('getMultipartUploadHandlers with path', () => { mockListParts.mockResolvedValueOnce({ Parts: [], $metadata: {} }); const size = 8 * MB; const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { path: testPath, data: new ArrayBuffer(size), @@ -1146,6 +1192,7 @@ describe('getMultipartUploadHandlers with path', () => { mockListParts.mockResolvedValueOnce({ Parts: [], $metadata: {} }); const size = 8 * MB; const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { path: testPath, data: new ArrayBuffer(size), @@ -1166,10 +1213,13 @@ describe('getMultipartUploadHandlers with path', () => { describe('cancel()', () => { it('should abort in-flight uploadPart requests and throw if upload is canceled', async () => { - const { multipartUploadJob, onCancel } = getMultipartUploadHandlers({ - path: testPath, - data: new ArrayBuffer(8 * MB), - }); + const { multipartUploadJob, onCancel } = getMultipartUploadHandlers( + amplify, + { + path: testPath, + data: new ArrayBuffer(8 * MB), + }, + ); let partCount = 0; mockMultipartUploadCancellation(() => { partCount++; @@ -1194,7 +1244,7 @@ describe('getMultipartUploadHandlers with path', () => { describe('pause() & resume()', () => { it('should abort in-flight uploadPart requests if upload is paused', async () => { const { multipartUploadJob, onPause, onResume } = - getMultipartUploadHandlers({ + getMultipartUploadHandlers(amplify, { path: testPath, data: new ArrayBuffer(8 * MB), }); @@ -1219,6 +1269,7 @@ describe('getMultipartUploadHandlers with path', () => { const onProgress = jest.fn(); mockMultipartUploadSuccess(); const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { path: testPath, data: new ArrayBuffer(8 * MB), @@ -1270,6 +1321,7 @@ describe('getMultipartUploadHandlers with path', () => { const onProgress = jest.fn(); const { multipartUploadJob } = getMultipartUploadHandlers( + amplify, { path: testPath, data: new ArrayBuffer(8 * MB), diff --git a/packages/storage/__tests__/providers/s3/apis/uploadData/putObjectJob.test.ts b/packages/storage/__tests__/providers/s3/apis/uploadData/putObjectJob.test.ts index aa9cf2ff8cd..fa5d534961c 100644 --- a/packages/storage/__tests__/providers/s3/apis/uploadData/putObjectJob.test.ts +++ b/packages/storage/__tests__/providers/s3/apis/uploadData/putObjectJob.test.ts @@ -2,11 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 import { AWSCredentials } from '@aws-amplify/core/internals/utils'; -import { Amplify } from '@aws-amplify/core'; +import { AmplifyClassV6 } from '@aws-amplify/core'; import { putObject } from '../../../../../src/providers/s3/utils/client'; import { calculateContentMd5 } from '../../../../../src/providers/s3/utils'; -import { putObjectJob } from '../../../../../src/providers/s3/apis/uploadData/putObjectJob'; +import { putObjectJob } from '../../../../../src/providers/s3/apis/internal/uploadData/putObjectJob'; import '../testUtils'; jest.mock('../../../../../src/providers/s3/utils/client'); @@ -19,14 +19,20 @@ jest.mock('../../../../../src/providers/s3/utils', () => { }; }); jest.mock('@aws-amplify/core', () => ({ - ConsoleLogger: jest.fn(), - fetchAuthSession: jest.fn(), - Amplify: { + ConsoleLogger: jest.fn(() => ({ + debug: jest.fn(), + })), + defaultStorage: { + getItem: jest.fn(), + setItem: jest.fn(), + }, + AmplifyClassV6: jest.fn(() => ({ + libraryOptions: {}, getConfig: jest.fn(), Auth: { fetchAuthSession: jest.fn(), }, - }, + })), })); const testPath = 'testPath/object'; @@ -36,24 +42,10 @@ const credentials: AWSCredentials = { secretAccessKey: 'secretAccessKey', }; const identityId = 'identityId'; -const mockFetchAuthSession = jest.mocked(Amplify.Auth.fetchAuthSession); const mockPutObject = jest.mocked(putObject); const bucket = 'bucket'; const region = 'region'; -mockFetchAuthSession.mockResolvedValue({ - credentials, - identityId, -}); -jest.mocked(Amplify.getConfig).mockReturnValue({ - Storage: { - S3: { - bucket, - region, - buckets: { 'default-bucket': { bucketName: bucket, region } }, - }, - }, -}); mockPutObject.mockResolvedValue({ ETag: 'eTag', VersionId: 'versionId', @@ -62,8 +54,26 @@ mockPutObject.mockResolvedValue({ /* TODO Remove suite when `key` parameter is removed */ describe('putObjectJob with key', () => { + let amplify: AmplifyClassV6; + let mockFetchAuthSession: jest.Mock; + beforeEach(() => { mockPutObject.mockClear(); + amplify = new AmplifyClassV6(); + mockFetchAuthSession = amplify.Auth.fetchAuthSession as jest.Mock; + mockFetchAuthSession.mockResolvedValue({ + credentials, + identityId, + }); + (amplify.getConfig as jest.Mock).mockReturnValue({ + Storage: { + S3: { + bucket, + region, + buckets: { 'default-bucket': { bucketName: bucket, region } }, + }, + }, + }); }); it('should supply the correct parameters to putObject API handler', async () => { @@ -78,6 +88,7 @@ describe('putObjectJob with key', () => { const useAccelerateEndpoint = true; const job = putObjectJob( + amplify, { key: inputKey, data, @@ -125,7 +136,7 @@ describe('putObjectJob with key', () => { }); it('should set ContentMD5 if object lock is enabled', async () => { - Amplify.libraryOptions = { + amplify.libraryOptions = { Storage: { S3: { isObjectLockEnabled: true, @@ -133,6 +144,7 @@ describe('putObjectJob with key', () => { }, }; const job = putObjectJob( + amplify, { key: 'key', data: 'data', @@ -151,6 +163,7 @@ describe('putObjectJob with key', () => { const mockRegion = 'region-1'; const job = putObjectJob( + amplify, { key: 'key', data, @@ -185,6 +198,7 @@ describe('putObjectJob with key', () => { const abortController = new AbortController(); const data = 'data'; const job = putObjectJob( + amplify, { key: 'key', data, @@ -215,8 +229,26 @@ describe('putObjectJob with key', () => { }); describe('putObjectJob with path', () => { + let amplify: AmplifyClassV6; + let mockFetchAuthSession: jest.Mock; + beforeEach(() => { mockPutObject.mockClear(); + amplify = new AmplifyClassV6(); + mockFetchAuthSession = amplify.Auth.fetchAuthSession as jest.Mock; + mockFetchAuthSession.mockResolvedValue({ + credentials, + identityId, + }); + (amplify.getConfig as jest.Mock).mockReturnValue({ + Storage: { + S3: { + bucket, + region, + buckets: { 'default-bucket': { bucketName: bucket, region } }, + }, + }, + }); }); test.each([ @@ -241,6 +273,7 @@ describe('putObjectJob with path', () => { const useAccelerateEndpoint = true; const job = putObjectJob( + amplify, { path: inputPath, data, @@ -289,7 +322,7 @@ describe('putObjectJob with path', () => { ); it('should set ContentMD5 if object lock is enabled', async () => { - Amplify.libraryOptions = { + amplify.libraryOptions = { Storage: { S3: { isObjectLockEnabled: true, @@ -297,6 +330,7 @@ describe('putObjectJob with path', () => { }, }; const job = putObjectJob( + amplify, { path: testPath, data: 'data', @@ -315,6 +349,7 @@ describe('putObjectJob with path', () => { const mockRegion = 'region-1'; const job = putObjectJob( + amplify, { path: 'path/', data, @@ -349,6 +384,7 @@ describe('putObjectJob with path', () => { const abortController = new AbortController(); const data = 'data'; const job = putObjectJob( + amplify, { path: 'path/', data, diff --git a/packages/storage/src/providers/s3/utils/resolveS3ConfigAndInput.ts b/packages/storage/src/providers/s3/utils/resolveS3ConfigAndInput.ts index 1e731ec2a12..0a07a2ea49b 100644 --- a/packages/storage/src/providers/s3/utils/resolveS3ConfigAndInput.ts +++ b/packages/storage/src/providers/s3/utils/resolveS3ConfigAndInput.ts @@ -88,7 +88,7 @@ export const resolveS3ConfigAndInput = async ( // use conditional assign to make tsc happy because StorageOptions is a union type that may not have targetIdentityId targetIdentityId: apiOptions?.accessLevel === 'protected' - ? (apiOptions?.targetIdentityId ?? identityId) + ? apiOptions?.targetIdentityId ?? identityId : identityId, });