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

(chore): Added early error message for configuration error #14222

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,27 @@ describe('Analytics KinesisFirehose Provider Util: resolveConfig', () => {
};

const getConfigSpy = jest.spyOn(Amplify, 'getConfig');
const assertConfiguredSpy = jest.spyOn(Amplify, 'assertConfigured');

beforeEach(() => {
getConfigSpy.mockReset();
assertConfiguredSpy.mockReset();
});

it('throws if Amplify is not configured', () => {
assertConfiguredSpy.mockImplementation(() => {
throw new Error(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

expect(resolveConfig).toThrow(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

it('returns required config', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
getConfigSpy.mockReturnValue({
Analytics: { KinesisFirehose: providedConfig },
});
Expand All @@ -30,6 +45,7 @@ describe('Analytics KinesisFirehose Provider Util: resolveConfig', () => {
});

it('use default config for optional fields', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
const requiredFields = {
region: 'us-east-1',
bufferSize: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,27 @@ describe('Analytics Kinesis Provider Util: resolveConfig', () => {
};

const getConfigSpy = jest.spyOn(Amplify, 'getConfig');
const assertConfiguredSpy = jest.spyOn(Amplify, 'assertConfigured');

beforeEach(() => {
getConfigSpy.mockReset();
assertConfiguredSpy.mockReset();
});

it('throws if Amplify is not configured', () => {
assertConfiguredSpy.mockImplementation(() => {
throw new Error(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

expect(resolveConfig).toThrow(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

it('returns required config', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
getConfigSpy.mockReturnValue({
Analytics: { Kinesis: kinesisConfig },
});
Expand All @@ -30,6 +45,7 @@ describe('Analytics Kinesis Provider Util: resolveConfig', () => {
});

it('use default config for optional fields', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
const requiredFields = {
region: 'us-east-1',
bufferSize: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,27 @@ describe('Analytics Personalize Provider Util: resolveConfig', () => {
};

const getConfigSpy = jest.spyOn(Amplify, 'getConfig');
const assertConfiguredSpy = jest.spyOn(Amplify, 'assertConfigured');

beforeEach(() => {
getConfigSpy.mockReset();
assertConfiguredSpy.mockReset();
});

it('throws if Amplify is not configured', () => {
assertConfiguredSpy.mockImplementation(() => {
throw new Error(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

expect(resolveConfig).toThrow(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

it('returns required config', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
getConfigSpy.mockReturnValue({
Analytics: { Personalize: providedConfig },
});
Expand All @@ -35,6 +50,7 @@ describe('Analytics Personalize Provider Util: resolveConfig', () => {
});

it('use default config for optional fields', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
const requiredFields = {
region: 'us-east-1',
trackingId: 'trackingId1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,27 @@ describe('Analytics Pinpoint Provider Util: resolveConfig', () => {
};
// create spies
const getConfigSpy = jest.spyOn(Amplify, 'getConfig');
const assertConfiguredSpy = jest.spyOn(Amplify, 'assertConfigured');

beforeEach(() => {
getConfigSpy.mockReset();
assertConfiguredSpy.mockReset();
});

it('throws if Amplify is not configured', () => {
assertConfiguredSpy.mockImplementation(() => {
throw new Error(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

expect(resolveConfig).toThrow(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

it('returns required config', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
getConfigSpy.mockReturnValue({
Analytics: { Pinpoint: pinpointConfig },
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { DEFAULT_KINESIS_FIREHOSE_CONFIG } from './constants';

export const resolveConfig = () => {
Amplify.assertConfigured();
const config = Amplify.getConfig().Analytics?.KinesisFirehose;
const {
region,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { DEFAULT_KINESIS_CONFIG } from './constants';

export const resolveConfig = () => {
Amplify.assertConfigured();
const config = Amplify.getConfig().Analytics?.Kinesis;
const {
region,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from './constants';

export const resolveConfig = () => {
Amplify.assertConfigured();
const config = Amplify.getConfig().Analytics?.Personalize;
const {
region,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
* @internal
*/
export const resolveConfig = () => {
Amplify.assertConfigured();
const { appId, region, bufferSize, flushSize, flushInterval, resendLimit } =
Amplify.getConfig().Analytics?.Pinpoint ?? {};
assertValidationError(!!appId, AnalyticsValidationErrorCode.NoAppId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ const mockAuthenticatedHandler = authenticatedHandler as jest.Mock;
const mockUnauthenticatedHandler = unauthenticatedHandler as jest.Mock;
const mockParseJsonError = parseJsonError as jest.Mock;
const mockFetchAuthSession = jest.fn();
const mockAssertConfigured = jest.fn();
const mockAmplifyInstance = {
Auth: {
fetchAuthSession: mockFetchAuthSession,
},
isConfigured: true,
assertConfigured: mockAssertConfigured,
} as any as AmplifyClassV6;

const successResponse = {
Expand All @@ -52,6 +55,7 @@ describe('internal post', () => {
mockFetchAuthSession.mockResolvedValue({ credentials });
mockAuthenticatedHandler.mockResolvedValue(successResponse);
mockUnauthenticatedHandler.mockResolvedValue(successResponse);
mockAssertConfigured.mockReturnValue(true);
});

it('should call authenticatedHandler with specified region from signingServiceInfo', async () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/api-rest/__tests__/apis/common/publicApis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ const mockConfig = {
const mockParseJsonError = parseJsonError as jest.Mock;
const mockRestHeaders = jest.fn();
const mockGetConfig = jest.fn();
const mockAssertConfigured = jest.fn();
const mockAmplifyInstance = {
Auth: {
fetchAuthSession: mockFetchAuthSession,
},
getConfig: mockGetConfig,
assertConfigured: mockAssertConfigured,
libraryOptions: {
API: {
REST: {
Expand Down Expand Up @@ -86,6 +88,7 @@ describe('public APIs', () => {
mockAuthenticatedHandler.mockResolvedValue(mockSuccessResponse);
mockUnauthenticatedHandler.mockResolvedValue(mockSuccessResponse);
mockGetConfig.mockReturnValue(mockConfig);
mockAssertConfigured.mockReturnValue(true);
});
const APIs = [
{ name: 'get', fn: get, method: 'GET' },
Expand Down
2 changes: 2 additions & 0 deletions packages/api-rest/src/apis/common/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const transferHandler = async (
) => boolean,
signingServiceInfo?: SigningServiceInfo,
): Promise<RestApiResponse> => {
amplify.assertConfigured();
const { url, method, headers, body, withCredentials, abortSignal } = options;
const resolvedBody = body
? body instanceof FormData
Expand Down Expand Up @@ -101,6 +102,7 @@ export const transferHandler = async (
const resolveCredentials = async (
amplify: AmplifyClassV6,
): Promise<AWSCredentials | null> => {
amplify.assertConfigured();
try {
const { credentials } = await amplify.Auth.fetchAuthSession();
if (credentials) {
Expand Down
1 change: 1 addition & 0 deletions packages/api-rest/src/apis/common/internalPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* @param postInput.abortController The abort controller used to cancel the POST request
* @returns a {@link RestApiResponse}
*
* @throws an {@link AmplifyError} with `Network Error` as the `message` when the external resource is unreachable due to one

Check warning on line 50 in packages/api-rest/src/apis/common/internalPost.ts

View workflow job for this annotation

GitHub Actions / unit-tests / Unit Test - @aws-amplify/api-rest

The type 'AmplifyError' is undefined
* of the following reasons:
* 1. no network connection
* 2. CORS error
Expand All @@ -57,6 +57,7 @@
amplify: AmplifyClassV6,
{ url, options, abortController }: InternalPostInput,
): Promise<RestApiResponse> => {
amplify.assertConfigured();
const controller = abortController ?? new AbortController();
const responsePromise = createCancellableOperation(async () => {
const response = transferHandler(
Expand Down
1 change: 1 addition & 0 deletions packages/api-rest/src/apis/common/publicApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const publicHandler = (
method: string,
) =>
createCancellableOperation(async abortSignal => {
amplify.assertConfigured();
const { apiName, options: apiOptions = {}, path: apiPath } = options;
const url = resolveApiUrl(
amplify,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { authAPITestParams } from './testUtils/authApiTestParams';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('../../../src/client/utils/store');
jest.mock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jest.mock('../../../src/providers/cognito/factories');

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock(
'../../../src/foundation/factories/serviceClients/cognitoIdentityProvider',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand All @@ -34,6 +34,7 @@ describe('fetchUserAttributes', () => {
const mockCreateCognitoUserPoolEndpointResolver = jest.mocked(
createCognitoUserPoolEndpointResolver,
);
const mockAssertConfigured = Amplify.assertConfigured as jest.Mock;

beforeAll(() => {
setUpGetConfig(Amplify);
Expand All @@ -43,6 +44,7 @@ describe('fetchUserAttributes', () => {
});

beforeEach(() => {
mockAssertConfigured.mockReturnValue(undefined);
mockGetUser.mockResolvedValue({
UserAttributes: [
{ Name: 'email', Value: 'XXXXXXXXXXXXX' },
Expand All @@ -60,6 +62,7 @@ describe('fetchUserAttributes', () => {
mockGetUser.mockReset();
mockFetchAuthSession.mockClear();
mockCreateGetUserClient.mockClear();
mockAssertConfigured.mockReset();
});

it('should return the current user attributes into a map format', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand Down
Loading
Loading