Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Amplify.configure dispatches Hub event with unparsed config object #12930

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 82 additions & 53 deletions packages/core/__tests__/singleton/Singleton.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { Amplify } from '../../src/singleton';
import { Hub, AMPLIFY_SYMBOL } from '../../src/Hub';
import { AuthClass as Auth } from '../../src/singleton/Auth';
import { decodeJWT } from '../../src/singleton/Auth/utils';
import { CredentialsAndIdentityId } from '../../src/singleton/Auth/types';
import { TextEncoder, TextDecoder } from 'util';
import { fetchAuthSession, ResourcesConfig } from '../../src';
Object.assign(global, { TextDecoder, TextEncoder });

jest.mock('../../src/Hub', () => ({
...jest.requireActual('../../src/Hub'),
Hub: {
dispatch: jest.fn(),
},
}));

const mockHubDispatch = Hub.dispatch as jest.Mock;

type ArgumentTypes<F extends Function> = F extends (...args: infer A) => any
? A
: never;
Expand Down Expand Up @@ -85,64 +96,82 @@ const modelIntrospection: ModelIntrospection = {
};

describe('Amplify.configure() and Amplify.getConfig()', () => {
it('should take the legacy CLI shaped config object for configuring and return it from getConfig()', () => {
const mockLegacyConfig = {
aws_project_region: 'us-west-2',
aws_cognito_identity_pool_id: 'aws_cognito_identity_pool_id',
aws_cognito_region: 'aws_cognito_region',
aws_user_pools_id: 'aws_user_pools_id',
aws_user_pools_web_client_id: 'aws_user_pools_web_client_id',
oauth: {},
aws_cognito_username_attributes: [],
aws_cognito_social_providers: [],
aws_cognito_signup_attributes: [],
aws_cognito_mfa_configuration: 'OFF',
aws_cognito_mfa_types: ['SMS'],
aws_cognito_password_protection_settings: {
passwordPolicyMinLength: 8,
passwordPolicyCharacters: [],
const mockLegacyConfig = {
aws_project_region: 'us-west-2',
aws_cognito_identity_pool_id: 'aws_cognito_identity_pool_id',
aws_cognito_region: 'aws_cognito_region',
aws_user_pools_id: 'aws_user_pools_id',
aws_user_pools_web_client_id: 'aws_user_pools_web_client_id',
oauth: {},
aws_cognito_username_attributes: [],
aws_cognito_social_providers: [],
aws_cognito_signup_attributes: [],
aws_cognito_mfa_configuration: 'OFF',
aws_cognito_mfa_types: ['SMS'],
aws_cognito_password_protection_settings: {
passwordPolicyMinLength: 8,
passwordPolicyCharacters: [],
},
aws_cognito_verification_mechanisms: ['PHONE_NUMBER'],
aws_appsync_graphqlEndpoint: 'https://some.domain.com/graphql',
aws_appsync_region: 'us-west-1',
aws_appsync_authenticationType: 'AMAZON_COGNITO_USER_POOLS',
aws_appsync_apiKey: 'some-key',
modelIntrospection,
};
const expectedResourceConfig: ResourcesConfig = {
Auth: {
Cognito: {
allowGuestAccess: true,
identityPoolId: 'aws_cognito_identity_pool_id',
userPoolClientId: 'aws_user_pools_web_client_id',
userPoolId: 'aws_user_pools_id',
loginWith: { email: false, phone: false, username: true },
mfa: { smsEnabled: true, status: 'off', totpEnabled: false },
passwordFormat: {
minLength: 8,
requireLowercase: false,
requireNumbers: false,
requireSpecialCharacters: false,
requireUppercase: false,
},
userAttributes: { phone_number: { required: true } },
},
aws_cognito_verification_mechanisms: ['PHONE_NUMBER'],
aws_appsync_graphqlEndpoint: 'https://some.domain.com/graphql',
aws_appsync_region: 'us-west-1',
aws_appsync_authenticationType: 'AMAZON_COGNITO_USER_POOLS',
aws_appsync_apiKey: 'some-key',
modelIntrospection,
};
},
API: {
GraphQL: {
apiKey: 'some-key',
defaultAuthMode: 'userPool',
endpoint: 'https://some.domain.com/graphql',
region: 'us-west-1',
modelIntrospection,
},
},
};

afterEach(() => {
mockHubDispatch.mockClear();
});

it('should take the legacy CLI shaped config object for configuring and return it from getConfig()', () => {
Amplify.configure(mockLegacyConfig);
const result = Amplify.getConfig();
const expected: ResourcesConfig = {
Auth: {
Cognito: {
allowGuestAccess: true,
identityPoolId: 'aws_cognito_identity_pool_id',
userPoolClientId: 'aws_user_pools_web_client_id',
userPoolId: 'aws_user_pools_id',
loginWith: { email: false, phone: false, username: true },
mfa: { smsEnabled: true, status: 'off', totpEnabled: false },
passwordFormat: {
minLength: 8,
requireLowercase: false,
requireNumbers: false,
requireSpecialCharacters: false,
requireUppercase: false,
},
userAttributes: { phone_number: { required: true } },
},
},
API: {
GraphQL: {
apiKey: 'some-key',
defaultAuthMode: 'userPool',
endpoint: 'https://some.domain.com/graphql',
region: 'us-west-1',
modelIntrospection,
},
},
};

expect(result).toEqual(expected);
expect(result).toEqual(expectedResourceConfig);
});

it('dispatches hub event with parsed ResourceConfig from the legacy config', () => {
Amplify.configure(mockLegacyConfig);

expect(mockHubDispatch).toHaveBeenCalledWith(
'core',
{
event: 'configure',
data: expectedResourceConfig,
},
'Configure',
AMPLIFY_SYMBOL
);
});

it('should take the v6 shaped config object for configuring and return it from getConfig()', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/singleton/Amplify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class AmplifyClass {
'core',
{
event: 'configure',
data: resourcesConfig,
data: this.resourcesConfig,
jimblanc marked this conversation as resolved.
Show resolved Hide resolved
},
'Configure',
AMPLIFY_SYMBOL
Expand Down
Loading