From 44ebed2ee7937e7669e217d461344bf9ba633f40 Mon Sep 17 00:00:00 2001 From: Allan Zheng Date: Thu, 24 Oct 2024 10:23:01 -0700 Subject: [PATCH] test(storage): add server api unit test --- .../__tests__/providers/s3/apis/copy.test.ts | 3 - .../providers/s3/apis/server/copy.test.ts | 54 +++++++++++++ .../s3/apis/server/getProperties.test.ts | 61 +++++++++++++++ .../providers/s3/apis/server/getUrl.test.ts | 59 ++++++++++++++ .../providers/s3/apis/server/list.test.ts | 77 +++++++++++++++++++ .../providers/s3/apis/server/remove.test.ts | 48 ++++++++++++ 6 files changed, 299 insertions(+), 3 deletions(-) create mode 100644 packages/storage/__tests__/providers/s3/apis/server/copy.test.ts create mode 100644 packages/storage/__tests__/providers/s3/apis/server/getProperties.test.ts create mode 100644 packages/storage/__tests__/providers/s3/apis/server/getUrl.test.ts create mode 100644 packages/storage/__tests__/providers/s3/apis/server/list.test.ts create mode 100644 packages/storage/__tests__/providers/s3/apis/server/remove.test.ts diff --git a/packages/storage/__tests__/providers/s3/apis/copy.test.ts b/packages/storage/__tests__/providers/s3/apis/copy.test.ts index e8e0d3b5d6b..606786ebfc2 100644 --- a/packages/storage/__tests__/providers/s3/apis/copy.test.ts +++ b/packages/storage/__tests__/providers/s3/apis/copy.test.ts @@ -22,12 +22,9 @@ describe('client-side copy', () => { const input: CopyInput = { source: { key: 'source-key', - bucket: { bucketName: 'source-bucket', region: 'source-region' }, - expectedBucketOwner: '123', }, destination: { key: 'destination-key', - expectedBucketOwner: '123', }, }; expect(copy(input)).toEqual(mockInternalResult); diff --git a/packages/storage/__tests__/providers/s3/apis/server/copy.test.ts b/packages/storage/__tests__/providers/s3/apis/server/copy.test.ts new file mode 100644 index 00000000000..06ce54b5b6b --- /dev/null +++ b/packages/storage/__tests__/providers/s3/apis/server/copy.test.ts @@ -0,0 +1,54 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { getAmplifyServerContext } from '@aws-amplify/core/internals/adapter-core'; + +import { CopyInput, CopyWithPathInput } from '../../../../../src'; +import { copy } from '../../../../../src/providers/s3/apis/server'; +import { copy as internalCopyImpl } from '../../../../../src/providers/s3/apis/internal/copy'; + +jest.mock('../../../../../src/providers/s3/apis/internal/copy'); +jest.mock('@aws-amplify/core/internals/adapter-core'); + +const mockInternalCopyImpl = jest.mocked(internalCopyImpl); +const mockGetAmplifyServerContext = jest.mocked(getAmplifyServerContext); +const mockInternalResult = 'RESULT' as any; +const mockAmplifyClass = 'AMPLIFY_CLASS' as any; +const mockAmplifyContextSpec = { + token: { value: Symbol('123') }, +}; + +describe('server-side copy', () => { + beforeEach(() => { + mockGetAmplifyServerContext.mockReturnValue({ + amplify: mockAmplifyClass, + }); + mockInternalCopyImpl.mockReturnValue(mockInternalResult); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should pass through input with key and output to internal implementation', async () => { + const input: CopyInput = { + source: { + key: 'source-key', + }, + destination: { + key: 'destination-key', + }, + }; + expect(copy(mockAmplifyContextSpec, input)).toEqual(mockInternalResult); + expect(mockInternalCopyImpl).toBeCalledWith(mockAmplifyClass, input); + }); + + it('should pass through input with path and output to internal implementation', async () => { + const input: CopyWithPathInput = { + source: { path: 'abc' }, + destination: { path: 'abc' }, + }; + expect(copy(mockAmplifyContextSpec, input)).toEqual(mockInternalResult); + expect(mockInternalCopyImpl).toBeCalledWith(mockAmplifyClass, input); + }); +}); diff --git a/packages/storage/__tests__/providers/s3/apis/server/getProperties.test.ts b/packages/storage/__tests__/providers/s3/apis/server/getProperties.test.ts new file mode 100644 index 00000000000..9afd1403d55 --- /dev/null +++ b/packages/storage/__tests__/providers/s3/apis/server/getProperties.test.ts @@ -0,0 +1,61 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { getAmplifyServerContext } from '@aws-amplify/core/internals/adapter-core'; + +import { + GetPropertiesInput, + GetPropertiesWithPathInput, +} from '../../../../../src'; +import { getProperties } from '../../../../../src/providers/s3/apis/server'; +import { getProperties as internalGetPropertiesImpl } from '../../../../../src/providers/s3/apis/internal/getProperties'; + +jest.mock('../../../../../src/providers/s3/apis/internal/getProperties'); +jest.mock('@aws-amplify/core/internals/adapter-core'); + +const mockInternalGetPropertiesImpl = jest.mocked(internalGetPropertiesImpl); +const mockGetAmplifyServerContext = jest.mocked(getAmplifyServerContext); +const mockInternalResult = 'RESULT' as any; +const mockAmplifyClass = 'AMPLIFY_CLASS' as any; +const mockAmplifyContextSpec = { + token: { value: Symbol('123') }, +}; + +describe('server-side getProperties', () => { + beforeEach(() => { + mockGetAmplifyServerContext.mockReturnValue({ + amplify: mockAmplifyClass, + }); + mockInternalGetPropertiesImpl.mockReturnValue(mockInternalResult); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should pass through input with key and output to internal implementation', async () => { + const input: GetPropertiesInput = { + key: 'source-key', + }; + expect(getProperties(mockAmplifyContextSpec, input)).toEqual( + mockInternalResult, + ); + expect(mockInternalGetPropertiesImpl).toBeCalledWith( + mockAmplifyClass, + input, + ); + }); + + it('should pass through input with path and output to internal implementation', async () => { + const input: GetPropertiesWithPathInput = { + path: 'abc', + }; + expect(getProperties(mockAmplifyContextSpec, input)).toEqual( + mockInternalResult, + ); + expect(mockInternalGetPropertiesImpl).toBeCalledWith( + mockAmplifyClass, + input, + ); + }); +}); diff --git a/packages/storage/__tests__/providers/s3/apis/server/getUrl.test.ts b/packages/storage/__tests__/providers/s3/apis/server/getUrl.test.ts new file mode 100644 index 00000000000..3dfac7a58dc --- /dev/null +++ b/packages/storage/__tests__/providers/s3/apis/server/getUrl.test.ts @@ -0,0 +1,59 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { getAmplifyServerContext } from '@aws-amplify/core/internals/adapter-core'; + +import { GetUrlInput, GetUrlWithPathInput } from '../../../../../src'; +import { getUrl } from '../../../../../src/providers/s3/apis/server'; +import { getUrl as internalGetUrlImpl } from '../../../../../src/providers/s3/apis/internal/getUrl'; + +jest.mock('../../../../../src/providers/s3/apis/internal/getUrl'); +jest.mock('@aws-amplify/core/internals/adapter-core'); + +const mockInternalGetUrlImpl = jest.mocked(internalGetUrlImpl); +const mockGetAmplifyServerContext = jest.mocked(getAmplifyServerContext); +const mockInternalResult = 'RESULT' as any; +const mockAmplifyClass = 'AMPLIFY_CLASS' as any; + +describe('server-side getUrl', () => { + beforeEach(() => { + mockGetAmplifyServerContext.mockReturnValue({ + amplify: mockAmplifyClass, + }); + mockInternalGetUrlImpl.mockReturnValue(mockInternalResult); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should pass through input with key and output to internal implementation', async () => { + const input: GetUrlInput = { + key: 'source-key', + }; + expect( + getUrl( + { + token: { value: Symbol('123') }, + }, + input, + ), + ).toEqual(mockInternalResult); + expect(mockInternalGetUrlImpl).toBeCalledWith(mockAmplifyClass, input); + }); + + it('should pass through input with path and output to internal implementation', async () => { + const input: GetUrlWithPathInput = { + path: 'abc', + }; + expect( + getUrl( + { + token: { value: Symbol('123') }, + }, + input, + ), + ).toEqual(mockInternalResult); + expect(mockInternalGetUrlImpl).toBeCalledWith(mockAmplifyClass, input); + }); +}); diff --git a/packages/storage/__tests__/providers/s3/apis/server/list.test.ts b/packages/storage/__tests__/providers/s3/apis/server/list.test.ts new file mode 100644 index 00000000000..febd469afa3 --- /dev/null +++ b/packages/storage/__tests__/providers/s3/apis/server/list.test.ts @@ -0,0 +1,77 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { getAmplifyServerContext } from '@aws-amplify/core/internals/adapter-core'; + +import { + ListAllInput, + ListAllWithPathInput, + ListPaginateInput, + ListPaginateWithPathInput, +} from '../../../../../src'; +import { list } from '../../../../../src/providers/s3/apis/server'; +import { list as internalListImpl } from '../../../../../src/providers/s3/apis/internal/list'; + +jest.mock('../../../../../src/providers/s3/apis/internal/list'); +jest.mock('@aws-amplify/core/internals/adapter-core'); + +const mockInternalListImpl = jest.mocked(internalListImpl); +const mockGetAmplifyServerContext = jest.mocked(getAmplifyServerContext); +const mockInternalResult = 'RESULT' as any; +const mockAmplifyClass = 'AMPLIFY_CLASS' as any; +const mockAmplifyContextSpec = { + token: { value: Symbol('123') }, +}; + +describe('server-side list', () => { + beforeEach(() => { + mockGetAmplifyServerContext.mockReturnValue({ + amplify: mockAmplifyClass, + }); + mockInternalListImpl.mockReturnValue(mockInternalResult); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should pass through list all input with key and output to internal implementation', async () => { + const input: ListAllInput = { + prefix: 'source-key', + }; + expect(list(mockAmplifyContextSpec, input)).toEqual(mockInternalResult); + expect(mockInternalListImpl).toBeCalledWith(mockAmplifyClass, input); + }); + + it('should pass through list paginate input with key and output to internal implementation', async () => { + const input: ListPaginateInput = { + prefix: 'source-key', + options: { + nextToken: '123', + pageSize: 10, + }, + }; + expect(list(mockAmplifyContextSpec, input)).toEqual(mockInternalResult); + expect(mockInternalListImpl).toBeCalledWith(mockAmplifyClass, input); + }); + + it('should pass through list all input with path and output to internal implementation', async () => { + const input: ListAllWithPathInput = { + path: 'abc', + }; + expect(list(mockAmplifyContextSpec, input)).toEqual(mockInternalResult); + expect(mockInternalListImpl).toBeCalledWith(mockAmplifyClass, input); + }); + + it('should pass through list paginate input with path and output to internal implementation', async () => { + const input: ListPaginateWithPathInput = { + path: 'abc', + options: { + nextToken: '123', + pageSize: 10, + }, + }; + expect(list(mockAmplifyContextSpec, input)).toEqual(mockInternalResult); + expect(mockInternalListImpl).toBeCalledWith(mockAmplifyClass, input); + }); +}); diff --git a/packages/storage/__tests__/providers/s3/apis/server/remove.test.ts b/packages/storage/__tests__/providers/s3/apis/server/remove.test.ts new file mode 100644 index 00000000000..861c3ce0d24 --- /dev/null +++ b/packages/storage/__tests__/providers/s3/apis/server/remove.test.ts @@ -0,0 +1,48 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { getAmplifyServerContext } from '@aws-amplify/core/internals/adapter-core'; + +import { RemoveInput, RemoveWithPathInput } from '../../../../../src'; +import { remove } from '../../../../../src/providers/s3/apis/server'; +import { remove as internalRemoveImpl } from '../../../../../src/providers/s3/apis/internal/remove'; + +jest.mock('../../../../../src/providers/s3/apis/internal/remove'); +jest.mock('@aws-amplify/core/internals/adapter-core'); + +const mockInternalRemoveImpl = jest.mocked(internalRemoveImpl); +const mockGetAmplifyServerContext = jest.mocked(getAmplifyServerContext); +const mockInternalResult = 'RESULT' as any; +const mockAmplifyClass = 'AMPLIFY_CLASS' as any; +const mockAmplifyContextSpec = { + token: { value: Symbol('123') }, +}; + +describe('server-side remove', () => { + beforeEach(() => { + mockGetAmplifyServerContext.mockReturnValue({ + amplify: mockAmplifyClass, + }); + mockInternalRemoveImpl.mockReturnValue(mockInternalResult); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should pass through input with key and output to internal implementation', async () => { + const input: RemoveInput = { + key: 'source-key', + }; + expect(remove(mockAmplifyContextSpec, input)).toEqual(mockInternalResult); + expect(mockInternalRemoveImpl).toBeCalledWith(mockAmplifyClass, input); + }); + + it('should pass through input with path and output to internal implementation', async () => { + const input: RemoveWithPathInput = { + path: 'abc', + }; + expect(remove(mockAmplifyContextSpec, input)).toEqual(mockInternalResult); + expect(mockInternalRemoveImpl).toBeCalledWith(mockAmplifyClass, input); + }); +});