Skip to content

Commit 53cb005

Browse files
author
aws-amplify-bot
committed
fix: fixed some tests
1 parent 4b26596 commit 53cb005

4 files changed

Lines changed: 64 additions & 65 deletions

File tree

packages/amplify-provider-awscloudformation/src/__tests__/aws-utils/S3Service.test.ts

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,57 @@
11
import { $TSContext } from '@aws-amplify/amplify-cli-core';
22
import { createS3Service } from '../../aws-utils/S3Service';
3+
import { HeadBucketCommand } from '@aws-sdk/client-s3';
34

4-
jest.mock('aws-sdk', () => {
5-
const mockHeadBucketForUsEast1 = jest.fn().mockImplementation((params: { Bucket: string }) => {
6-
return {
7-
promise: jest.fn().mockImplementation(() => {
8-
if (params.Bucket.includes('us-east-1')) {
9-
return Promise.resolve({});
10-
}
11-
return Promise.reject({ code: 'BadRequest', region: 'eu-south-1' });
12-
}),
13-
};
5+
// Mock the AWS SDK v3 S3Client
6+
jest.mock('@aws-sdk/client-s3', () => {
7+
const mockSendForUsEast1 = jest.fn().mockImplementation(async (command) => {
8+
if (command instanceof HeadBucketCommand) {
9+
const params = command.input;
10+
if (params.Bucket!.includes('us-east-1')) {
11+
return {};
12+
}
13+
const error = new Error('BadRequest');
14+
error.name = 'BadRequest';
15+
throw error;
16+
}
17+
return {};
18+
});
19+
20+
const mockSendForEuSouth1 = jest.fn().mockImplementation(async (command) => {
21+
if (command instanceof HeadBucketCommand) {
22+
const params = command.input;
23+
if (params.Bucket!.includes('eu-south-1')) {
24+
return {};
25+
}
26+
const error = new Error('BadRequest');
27+
error.name = 'BadRequest';
28+
throw error;
29+
}
30+
return {};
1431
});
1532

16-
const mockHeadBucketForEuSouth1 = jest.fn().mockImplementation((params) => {
33+
// Mock S3Client constructor
34+
const mockS3Client = jest.fn().mockImplementation((options) => {
35+
const region = options.region || 'us-east-1';
36+
1737
return {
18-
promise: jest.fn().mockImplementation(() => {
19-
if (params.Bucket.includes('eu-south-1')) {
20-
return Promise.resolve({});
21-
}
22-
return Promise.reject({ code: 'BadRequest', region: 'us-east-1' });
23-
}),
38+
send: region === 'eu-south-1' ? mockSendForEuSouth1 : mockSendForUsEast1,
39+
config: {
40+
region,
41+
credentials: options.credentials,
42+
},
2443
};
2544
});
2645

2746
return {
28-
S3: jest.fn((options) => {
29-
const region = options.region || 'us-east-1';
30-
if (region === 'eu-south-1') {
31-
return {
32-
headBucket: mockHeadBucketForEuSouth1,
33-
};
34-
}
47+
S3Client: mockS3Client,
48+
HeadBucketCommand: jest.fn().mockImplementation((params) => {
3549
return {
36-
config: {
37-
region,
38-
},
39-
headBucket: mockHeadBucketForUsEast1,
50+
input: params,
4051
};
4152
}),
53+
ListBucketsCommand: jest.fn(),
54+
GetBucketLocationCommand: jest.fn(),
4255
};
4356
});
4457

packages/amplify-provider-awscloudformation/src/__tests__/system-config-manager.test.ts

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { $TSContext } from '@aws-amplify/amplify-cli-core';
22
import fs from 'fs-extra';
3-
import { CredentialProviderChain, ProcessCredentials } from 'aws-sdk';
3+
import { fromProcess } from '@aws-sdk/credential-providers';
44
import { getProfileCredentials, getProfiledAwsConfig } from '../system-config-manager';
55

66
jest.setTimeout(15000);
@@ -11,9 +11,8 @@ jest.mock('../utils/aws-logger', () => ({
1111
jest.mock('fs-extra');
1212
const fs_mock = fs as jest.Mocked<typeof fs>;
1313

14-
jest.mock('aws-sdk');
15-
const ProcessCredentialsMock = ProcessCredentials as jest.MockedClass<typeof ProcessCredentials>;
16-
const CredentialProviderChainMock = CredentialProviderChain as jest.MockedClass<typeof CredentialProviderChain>;
14+
jest.mock('@aws-sdk/credential-providers');
15+
const fromProcessMock = fromProcess as jest.MockedFunction<typeof fromProcess>;
1716

1817
const context_stub = {
1918
print: {
@@ -29,14 +28,14 @@ describe('profile tests', () => {
2928
fs_mock.existsSync.mockReturnValue(true);
3029

3130
describe('credential process loading', () => {
32-
const mockResolvePromise = jest.fn().mockReturnValue(
33-
Promise.resolve({
34-
accessKeyId: 'chainTestAccessKey',
35-
secretAccessKey: 'chainTestSecret',
36-
sessionToken: 'chainTestSessionToken',
37-
expireTime: new Date(1234),
38-
}),
39-
);
31+
const mockCredentials = {
32+
accessKeyId: 'chainTestAccessKey',
33+
secretAccessKey: 'chainTestSecret',
34+
sessionToken: 'chainTestSessionToken',
35+
expiration: new Date(1234),
36+
};
37+
38+
const mockCredentialProvider = jest.fn().mockResolvedValue(mockCredentials);
4039

4140
beforeEach(() => {
4241
// setup
@@ -47,24 +46,7 @@ describe('profile tests', () => {
4746

4847
fs_mock.readFileSync.mockReturnValue(awsConfigContent);
4948

50-
CredentialProviderChainMock.mockImplementation(() => ({
51-
providers: [],
52-
resolvePromise: mockResolvePromise,
53-
resolve: jest.fn(),
54-
}));
55-
56-
ProcessCredentialsMock.mockImplementation(() => ({
57-
accessKeyId: 'testAccessKey',
58-
secretAccessKey: 'testSecret',
59-
sessionToken: 'testSessionToken',
60-
expireTime: new Date(1234),
61-
expired: false,
62-
get: jest.fn(),
63-
getPromise: jest.fn(),
64-
needsRefresh: jest.fn(),
65-
refresh: jest.fn(),
66-
refreshPromise: jest.fn(),
67-
}));
49+
fromProcessMock.mockReturnValue(mockCredentialProvider);
6850
});
6951

7052
it('should use credential_process defined in config file', async () => {
@@ -73,24 +55,29 @@ describe('profile tests', () => {
7355

7456
// expect
7557
expect(profile_config).toBeDefined();
76-
expect(mockResolvePromise).toBeCalled();
58+
expect(fromProcessMock).toHaveBeenCalledWith({ profile: 'fake' });
59+
expect(mockCredentialProvider).toHaveBeenCalled();
7760
expect(profile_config.accessKeyId).toBe('chainTestAccessKey');
7861
expect(profile_config.secretAccessKey).toBe('chainTestSecret');
7962
expect(profile_config.sessionToken).toBe('chainTestSessionToken');
8063
expect(profile_config.expiration).toEqual(new Date(1234));
8164
});
8265

83-
it('sets AWS_SDK_LOAD_CONFIG while ProcessCredentials executes', async () => {
66+
it('sets AWS_SDK_LOAD_CONFIG while credential provider executes', async () => {
8467
const sdkLoadConfigOriginal = process.env.AWS_SDK_LOAD_CONFIG;
85-
mockResolvePromise.mockImplementationOnce(() => {
68+
mockCredentialProvider.mockImplementationOnce(() => {
8669
expect(process.env.AWS_SDK_LOAD_CONFIG).toBeTruthy();
87-
8870
return Promise.resolve({
8971
accessKeyId: 'chainTestAccessKey',
72+
secretAccessKey: 'chainTestSecret',
73+
sessionToken: 'chainTestSessionToken',
74+
expiration: new Date(1234),
9075
});
9176
});
77+
9278
await getProfiledAwsConfig(context_stub, 'fake');
93-
expect(mockResolvePromise).toBeCalled();
79+
expect(fromProcessMock).toHaveBeenCalledWith({ profile: 'fake' });
80+
expect(mockCredentialProvider).toHaveBeenCalled();
9481
expect(process.env.AWS_SDK_LOAD_CONFIG).toStrictEqual(sdkLoadConfigOriginal);
9582
});
9683
});

packages/amplify-provider-awscloudformation/src/aws-utils/DynamoDBService.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { $TSAny, $TSContext } from '@aws-amplify/amplify-cli-core';
2-
import { NodeHttpHandler } from '@smithy/node-http-handler';
32
import { IDynamoDBService } from '@aws-amplify/amplify-util-import';
43
import {
54
DynamoDBClient,

packages/amplify-provider-awscloudformation/src/system-config-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { $TSAny, $TSContext, AmplifyError, JSONUtilities, pathManager, SecretFileMode, spinner } from '@aws-amplify/amplify-cli-core';
22

3-
import { STSClient, AssumeRoleCommand, AssumeRoleCommandInput, Credentials as STSCredentials } from '@aws-sdk/client-sts';
3+
import { STSClient, AssumeRoleCommand, AssumeRoleCommandInput } from '@aws-sdk/client-sts';
44
import { fromProcess } from '@aws-sdk/credential-providers';
55
import { NodeHttpHandler } from '@smithy/node-http-handler';
66
import * as fs from 'fs-extra';

0 commit comments

Comments
 (0)