Skip to content

Commit

Permalink
fix(storage): credentials expires after 1 hour
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP committed May 3, 2024
1 parent c794c14 commit 0ba9cf0
Show file tree
Hide file tree
Showing 16 changed files with 295 additions and 118 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-unused-imports": "^3.0.0",
"expect": "^29.7.0",
"glob": "^10.3.10",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
Expand Down
7 changes: 4 additions & 3 deletions packages/storage/__tests__/providers/s3/apis/copy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
CopyOutput,
CopyWithPathOutput,
} from '../../../../src/providers/s3/types';
import './testUtils';

jest.mock('../../../../src/providers/s3/utils/client');
jest.mock('@aws-amplify/core', () => ({
Expand Down Expand Up @@ -185,7 +186,7 @@ describe('copy API', () => {
});
expect(key).toEqual(destinationKey);
expect(copyObject).toHaveBeenCalledTimes(1);
expect(copyObject).toHaveBeenCalledWith(copyObjectClientConfig, {
await expect(copyObject).toBeLastCalledWithConfigAndInput(copyObjectClientConfig, {
...copyObjectClientBaseParams,
CopySource: expectedSourceKey,
Key: expectedDestinationKey,
Expand Down Expand Up @@ -238,7 +239,7 @@ describe('copy API', () => {
});
expect(path).toEqual(expectedDestinationPath);
expect(copyObject).toHaveBeenCalledTimes(1);
expect(copyObject).toHaveBeenCalledWith(copyObjectClientConfig, {
await expect(copyObject).toBeLastCalledWithConfigAndInput(copyObjectClientConfig, {
...copyObjectClientBaseParams,
CopySource: `${bucket}/${expectedSourcePath}`,
Key: expectedDestinationPath,
Expand Down Expand Up @@ -269,7 +270,7 @@ describe('copy API', () => {
});
} catch (error: any) {
expect(copyObject).toHaveBeenCalledTimes(1);
expect(copyObject).toHaveBeenCalledWith(copyObjectClientConfig, {
await expect(copyObject).toBeLastCalledWithConfigAndInput(copyObjectClientConfig, {
...copyObjectClientBaseParams,
CopySource: `${bucket}/public/${sourceKey}`,
Key: `public/${destinationKey}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
ItemWithKey,
ItemWithPath,
} from '../../../../src/providers/s3/types/outputs';
import './testUtils';

jest.mock('../../../../src/providers/s3/utils/client');
jest.mock('../../../../src/providers/s3/utils');
Expand Down Expand Up @@ -141,7 +142,7 @@ describe('downloadData with key', () => {
body: 'body',
});
expect(getObject).toHaveBeenCalledTimes(1);
expect(getObject).toHaveBeenCalledWith(
await expect(getObject).toBeLastCalledWithConfigAndInput(
{
credentials,
region,
Expand Down Expand Up @@ -287,7 +288,7 @@ describe('downloadData with path', () => {
body: 'body',
});
expect(getObject).toHaveBeenCalledTimes(1);
expect(getObject).toHaveBeenCalledWith(
await expect(getObject).toBeLastCalledWithConfigAndInput(
{
credentials,
region,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
GetPropertiesOutput,
GetPropertiesWithPathOutput,
} from '../../../../src/providers/s3/types';
import './testUtils';

jest.mock('../../../../src/providers/s3/utils/client');
jest.mock('@aws-amplify/core', () => ({
Expand Down Expand Up @@ -144,7 +145,7 @@ describe('getProperties with key', () => {
...expectedResult,
});
expect(headObject).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledWith(config, headObjectOptions);
await expect(headObject).toBeLastCalledWithConfigAndInput(config, headObjectOptions);
},
);
});
Expand All @@ -165,7 +166,7 @@ describe('getProperties with key', () => {
await getPropertiesWrapper({ key: inputKey });
} catch (error: any) {
expect(headObject).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledWith(
await expect(headObject).toBeLastCalledWithConfigAndInput(
{
credentials,
region: 'region',
Expand Down Expand Up @@ -264,7 +265,7 @@ describe('Happy cases: With path', () => {
...expectedResult,
});
expect(headObject).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledWith(config, headObjectOptions);
await expect(headObject).toBeLastCalledWithConfigAndInput(config, headObjectOptions);
},
);
});
Expand All @@ -285,7 +286,7 @@ describe('Happy cases: With path', () => {
await getPropertiesWrapper({ path: inputPath });
} catch (error: any) {
expect(headObject).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledWith(
await expect(headObject).toBeLastCalledWithConfigAndInput(
{
credentials,
region: 'region',
Expand Down
25 changes: 9 additions & 16 deletions packages/storage/__tests__/providers/s3/apis/getUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
GetUrlOutput,
GetUrlWithPathOutput,
} from '../../../../src/providers/s3/types';
import './testUtils';

jest.mock('../../../../src/providers/s3/utils/client');
jest.mock('@aws-amplify/core', () => ({
Expand All @@ -30,8 +31,8 @@ jest.mock('@aws-amplify/core', () => ({

const bucket = 'bucket';
const region = 'region';
const mockFetchAuthSession = Amplify.Auth.fetchAuthSession as jest.Mock;
const mockGetConfig = Amplify.getConfig as jest.Mock;
const mockFetchAuthSession = jest.mocked(Amplify.Auth.fetchAuthSession);
const mockGetConfig = jest.mocked(Amplify.getConfig);
const credentials: AWSCredentials = {
accessKeyId: 'accessKeyId',
sessionToken: 'sessionToken',
Expand Down Expand Up @@ -67,19 +68,15 @@ describe('getUrl test with key', () => {
};
const key = 'key';
beforeEach(() => {
(headObject as jest.MockedFunction<typeof headObject>).mockResolvedValue({
jest.mocked(headObject).mockResolvedValue({
ContentLength: 100,
ContentType: 'text/plain',
ETag: 'etag',
LastModified: new Date('01-01-1980'),
Metadata: { meta: 'value' },
$metadata: {} as any,
});
(
getPresignedGetObjectUrl as jest.MockedFunction<
typeof getPresignedGetObjectUrl
>
).mockResolvedValue(mockURL);
jest.mocked(getPresignedGetObjectUrl).mockResolvedValue(mockURL);
});
afterEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -130,7 +127,7 @@ describe('getUrl test with key', () => {
};
expect(getPresignedGetObjectUrl).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledWith(config, headObjectOptions);
expect(headObject).toBeLastCalledWithConfigAndInput(config, headObjectOptions);
expect({ url, expiresAt }).toEqual(expectedResult);
},
);
Expand Down Expand Up @@ -186,19 +183,15 @@ describe('getUrl test with path', () => {
userAgentValue: expect.any(String),
};
beforeEach(() => {
(headObject as jest.MockedFunction<typeof headObject>).mockResolvedValue({
jest.mocked(headObject).mockResolvedValue({
ContentLength: 100,
ContentType: 'text/plain',
ETag: 'etag',
LastModified: new Date('01-01-1980'),
Metadata: { meta: 'value' },
$metadata: {} as any,
});
(
getPresignedGetObjectUrl as jest.MockedFunction<
typeof getPresignedGetObjectUrl
>
).mockResolvedValue(mockURL);
jest.mocked(getPresignedGetObjectUrl).mockResolvedValue(mockURL);
});
afterEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -228,7 +221,7 @@ describe('getUrl test with path', () => {
});
expect(getPresignedGetObjectUrl).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledWith(config, headObjectOptions);
expect(headObject).toBeLastCalledWithConfigAndInput(config, headObjectOptions);
expect({ url, expiresAt }).toEqual({
url: mockURL,
expiresAt: expect.any(Date),
Expand Down
75 changes: 47 additions & 28 deletions packages/storage/__tests__/providers/s3/apis/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ListPaginateOutput,
ListPaginateWithPathOutput,
} from '../../../../src/providers/s3/types';
import './testUtils';

jest.mock('../../../../src/providers/s3/utils/client');
jest.mock('@aws-amplify/core', () => ({
Expand Down Expand Up @@ -170,7 +171,9 @@ describe('list API', () => {
});
expect(response.nextToken).toEqual(nextToken);
expect(listObjectsV2).toHaveBeenCalledTimes(1);
expect(listObjectsV2).toHaveBeenCalledWith(listObjectClientConfig, {
await expect(
listObjectsV2,
).toBeLastCalledWithConfigAndInput(listObjectClientConfig, {
Bucket: bucket,
MaxKeys: 1000,
Prefix: expectedKey,
Expand Down Expand Up @@ -208,7 +211,9 @@ describe('list API', () => {
});
expect(response.nextToken).toEqual(nextToken);
expect(listObjectsV2).toHaveBeenCalledTimes(1);
expect(listObjectsV2).toHaveBeenCalledWith(listObjectClientConfig, {
await expect(
listObjectsV2,
).toBeLastCalledWithConfigAndInput(listObjectClientConfig, {
Bucket: bucket,
Prefix: expectedKey,
ContinuationToken: nextToken,
Expand All @@ -234,7 +239,9 @@ describe('list API', () => {
expect(response.items).toEqual([]);

expect(response.nextToken).toEqual(undefined);
expect(listObjectsV2).toHaveBeenCalledWith(listObjectClientConfig, {
await expect(
listObjectsV2,
).toBeLastCalledWithConfigAndInput(listObjectClientConfig, {
Bucket: bucket,
MaxKeys: 1000,
Prefix: expectedKey,
Expand Down Expand Up @@ -266,7 +273,7 @@ describe('list API', () => {
expect(listObjectsV2).toHaveBeenCalledTimes(3);

// first input recieves undefined as the Continuation Token
expect(listObjectsV2).toHaveBeenNthCalledWith(
await expect(listObjectsV2).toHaveBeenNthCalledWithConfigAndInput(
1,
listObjectClientConfig,
{
Expand All @@ -277,7 +284,7 @@ describe('list API', () => {
},
);
// last input recieves TEST_TOKEN as the Continuation Token
expect(listObjectsV2).toHaveBeenNthCalledWith(
await expect(listObjectsV2).toHaveBeenNthCalledWithConfigAndInput(
3,
listObjectClientConfig,
{
Expand Down Expand Up @@ -340,11 +347,14 @@ describe('list API', () => {
});
expect(response.nextToken).toEqual(nextToken);
expect(listObjectsV2).toHaveBeenCalledTimes(1);
expect(listObjectsV2).toHaveBeenCalledWith(listObjectClientConfig, {
Bucket: bucket,
MaxKeys: 1000,
Prefix: resolvePath(inputPath),
});
expect(listObjectsV2).toBeLastCalledWithConfigAndInput(
listObjectClientConfig,
{
Bucket: bucket,
MaxKeys: 1000,
Prefix: resolvePath(inputPath),
},
);
},
);

Expand Down Expand Up @@ -379,12 +389,15 @@ describe('list API', () => {
});
expect(response.nextToken).toEqual(nextToken);
expect(listObjectsV2).toHaveBeenCalledTimes(1);
expect(listObjectsV2).toHaveBeenCalledWith(listObjectClientConfig, {
Bucket: bucket,
Prefix: resolvePath(inputPath),
ContinuationToken: nextToken,
MaxKeys: customPageSize,
});
expect(listObjectsV2).toBeLastCalledWithConfigAndInput(
listObjectClientConfig,
{
Bucket: bucket,
Prefix: resolvePath(inputPath),
ContinuationToken: nextToken,
MaxKeys: customPageSize,
},
);
},
);

Expand All @@ -400,11 +413,14 @@ describe('list API', () => {
expect(response.items).toEqual([]);

expect(response.nextToken).toEqual(undefined);
expect(listObjectsV2).toHaveBeenCalledWith(listObjectClientConfig, {
Bucket: bucket,
MaxKeys: 1000,
Prefix: resolvePath(path),
});
expect(listObjectsV2).toBeLastCalledWithConfigAndInput(
listObjectClientConfig,
{
Bucket: bucket,
MaxKeys: 1000,
Prefix: resolvePath(path),
},
);
},
);

Expand Down Expand Up @@ -432,7 +448,7 @@ describe('list API', () => {
expect(listObjectsV2).toHaveBeenCalledTimes(3);

// first input recieves undefined as the Continuation Token
expect(listObjectsV2).toHaveBeenNthCalledWith(
await expect(listObjectsV2).toHaveBeenNthCalledWithConfigAndInput(
1,
listObjectClientConfig,
{
Expand All @@ -443,7 +459,7 @@ describe('list API', () => {
},
);
// last input recieves TEST_TOKEN as the Continuation Token
expect(listObjectsV2).toHaveBeenNthCalledWith(
await expect(listObjectsV2).toHaveBeenNthCalledWithConfigAndInput(
3,
listObjectClientConfig,
{
Expand Down Expand Up @@ -473,11 +489,14 @@ describe('list API', () => {
} catch (error: any) {
expect.assertions(3);
expect(listObjectsV2).toHaveBeenCalledTimes(1);
expect(listObjectsV2).toHaveBeenCalledWith(listObjectClientConfig, {
Bucket: bucket,
MaxKeys: 1000,
Prefix: 'public/',
});
expect(listObjectsV2).toBeLastCalledWithConfigAndInput(
listObjectClientConfig,
{
Bucket: bucket,
MaxKeys: 1000,
Prefix: 'public/',
},
);
expect(error.$metadata.httpStatusCode).toBe(404);
}
});
Expand Down
7 changes: 4 additions & 3 deletions packages/storage/__tests__/providers/s3/apis/remove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
RemoveOutput,
RemoveWithPathOutput,
} from '../../../../src/providers/s3/types';
import './testUtils';

jest.mock('../../../../src/providers/s3/utils/client');
jest.mock('@aws-amplify/core', () => ({
Expand Down Expand Up @@ -104,7 +105,7 @@ describe('remove API', () => {
});
expect(key).toEqual(inputKey);
expect(deleteObject).toHaveBeenCalledTimes(1);
expect(deleteObject).toHaveBeenCalledWith(deleteObjectClientConfig, {
expect(deleteObject).toBeLastCalledWithConfigAndInput(deleteObjectClientConfig, {
Bucket: bucket,
Key: expectedKey,
});
Expand Down Expand Up @@ -143,7 +144,7 @@ describe('remove API', () => {
const { path } = await removeWrapper({ path: inputPath });
expect(path).toEqual(resolvedPath);
expect(deleteObject).toHaveBeenCalledTimes(1);
expect(deleteObject).toHaveBeenCalledWith(deleteObjectClientConfig, {
await expect(deleteObject).toBeLastCalledWithConfigAndInput(deleteObjectClientConfig, {
Bucket: bucket,
Key: resolvedPath,
});
Expand All @@ -169,7 +170,7 @@ describe('remove API', () => {
await remove({ key });
} catch (error: any) {
expect(deleteObject).toHaveBeenCalledTimes(1);
expect(deleteObject).toHaveBeenCalledWith(deleteObjectClientConfig, {
await expect(deleteObject).toBeLastCalledWithConfigAndInput(deleteObjectClientConfig, {
Bucket: bucket,
Key: `public/${key}`,
});
Expand Down
Loading

0 comments on commit 0ba9cf0

Please sign in to comment.