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

test(core): catch type issues when configuring from external json #14101

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ module.exports = {
noImplicitAny: false,
types: ['jest', 'jsdom'],
},
diagnostics: {
warnOnly: false,
},
},
],
},
Expand Down
19 changes: 19 additions & 0 deletions packages/core/__tests__/singleton/Singleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { AuthClass as Auth } from '../../src/singleton/Auth';
import { decodeJWT } from '../../src/singleton/Auth/utils';
import { CredentialsAndIdentityId } from '../../src/singleton/Auth/types';
import { ResourcesConfig, fetchAuthSession } from '../../src';
import { AmplifyOutputs } from '../../src/libraryUtils';

import mockAmplifyOutputs from './utils/amplify_outputs.json';

Object.assign(global, { TextDecoder, TextEncoder });

Expand Down Expand Up @@ -164,6 +167,22 @@ describe('Amplify.configure() and Amplify.getConfig()', () => {
expect(result).toEqual(expectedResourceConfig);
});

it(`should successfully configure from a 'amplify_outputs.json' file`, () => {
type DeepRequired<T> = T extends object
? { [K in keyof T]-?: DeepRequired<NonNullable<T[K]>> }
: T;

const recreateObject = <T>(obj: T): DeepRequired<T> =>
obj as DeepRequired<T>;

// Ensures that all optional properties in mockAmplifyOutputs are defined
const _amplifyOutputs: DeepRequired<AmplifyOutputs> =
recreateObject(mockAmplifyOutputs);
Comment on lines +178 to +180
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't quite get this - are we validating all possible fields are contained in the amplify_outputs.json file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep exactly, when a new optional key is added in AmplifyOutputs we want to make sure the mockAmplifyOutputs used here is also updated to include this field. With this approach if amplify_outputs isn't updated this line would crib

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. amplify_outputs.json will be updated manually? Is there any checks too that the manual update conforms to CLI JSON schema?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 we need to figure out a way to make sure our outputs.json conforms to CLI schema.Might be out of scope for this PR.


// Configures Amplify with mockAmplifyOutputs, relying on its inferred type
Amplify.configure(mockAmplifyOutputs);
});

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

Expand Down
207 changes: 207 additions & 0 deletions packages/core/__tests__/singleton/utils/amplify_outputs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
{
"version": "1",
"auth": {
"aws_region": "us-west-2",
"user_pool_id": "mock-cup-id",
"user_pool_client_id": "mock-cup-client-id",
"identity_pool_id": "mock-idp-id",
"authentication_flow_type": "CUSTOM_AUTH",
"oauth": {
"identity_providers": [
"FACEBOOK",
"SIGN_IN_WITH_APPLE",
"GOOGLE",
"Auth0"
],
"domain": "mock-oauth-domain",
"scopes": ["phone"],
"redirect_sign_in_uri": ["mock-sign-in-uri"],
"redirect_sign_out_uri": ["mock-sign-out-uri"],
"response_type": "token"
},
"standard_required_attributes": ["address", "locale", "email"],
"username_attributes": ["phone_number", "email"],
"user_verification_types": ["email"],
"unauthenticated_identities_enabled": true,
"mfa_configuration": "OPTIONAL",
"mfa_methods": ["TOTP", "SMS"],
"password_policy": {
"require_lowercase": true,
"require_numbers": true,
"require_uppercase": true,
"require_symbols": true,
"min_length": 6
},
"groups": [
ashwinkumar6 marked this conversation as resolved.
Show resolved Hide resolved
{
"ADMIN": { "precedence": 0 },
"USER": { "precedence": 1 }
},
{
"USER": { "precedence": 0 }
},
{
"TEST": { "precedence": 2 },
"USER": { "precedence": 1 },
"ADMIN": { "precedence": 0 }
}
]
},
"data": {
"aws_region": "us-west-2",
"url": "mock-data-url",
"api_key": "mock-data-api-key",
"default_authorization_type": "API_KEY",
"authorization_types": [],
"conflict_resolution_mode": "AUTOMERGE",
"model_introspection": {
"version": 1,
"models": {
"Todo": {
"name": "Todo",
"fields": {
"id": {
"name": "id",
"isArray": false,
"type": "ID",
"isRequired": true,
"attributes": []
},
"content": {
"name": "content",
"isArray": false,
"type": "String",
"isRequired": false,
"attributes": []
},
"createdAt": {
"name": "createdAt",
"isArray": false,
"type": "AWSDateTime",
"isRequired": false,
"attributes": [],
"isReadOnly": true
},
"updatedAt": {
"name": "updatedAt",
"isArray": false,
"type": "AWSDateTime",
"isRequired": false,
"attributes": [],
"isReadOnly": true
}
},
"syncable": true,
"pluralName": "Todos",
"attributes": [
{
"type": "model",
"properties": {}
},
{
"type": "auth",
"properties": {
"rules": [
{
"provider": "userPools",
"ownerField": "owner",
"allow": "owner",
"identityClaim": "cognito:username",
"operations": ["create", "update", "delete", "read"]
},
{
"allow": "private",
"provider": "iam",
"operations": ["create", "update", "delete", "read"]
}
]
}
}
],
"primaryKeyInfo": {
"isCustomPrimaryKey": false,
"primaryKeyFieldName": "id",
"sortKeyFieldNames": []
}
}
},
"enums": {},
"nonModels": {}
}
},
"geo": {
"aws_region": "us-west-2",
"search_indices": {
"items": ["mock-geo-search-item", "mock-geo-search-item-alt"],
"default": "mock-geo-search-item"
},
"geofence_collections": {
"items": ["mock-geo-fence-item", "mock-geo-fence-item-alt"],
"default": "mock-geo-fence-item"
},

"maps": {
"items": {
"map51addb38-dev": {
"style": "VectorEsriStreets"
},
"map30grxcw31-prod": {
"style": "VectorEsriStreets"
}
},
"default": "map51addb38-dev"
}
},
"custom": {
"custom-prop": -51806024,
"custom-prop-alt": 87599986,
"events": {
"url": "mock-events-url",
"aws_region": "us-west-2",
"default_authorization_type": "API_KEY",
"api_key": "mock-events-api-key"
}
},
"notifications": {
"aws_region": "us-west-2",
"amazon_pinpoint_app_id": "mock-pinpoint-app-id",
"channels": ["IN_APP_MESSAGING", "APNS"]
},
"analytics": {
"amazon_pinpoint": {
"app_id": "mock-pinpoint-app-id",
"aws_region": "us-west-2"
}
},
"storage": {
"aws_region": "us-west-2",
"bucket_name": "mock-bucket-1",
"buckets": [
{
"name": "mock-bucket-1",
"bucket_name": "bucket1",
"aws_region": "us-west-2",
"paths": {
"sub/*": {
"authenticated": ["get", "list"]
},
"public/*": {
"guest": ["get", "list", "write", "delete"],
"authenticated": ["write", "get", "list", "delete"]
}
}
},
{
"name": "mock-bucket-2",
"bucket_name": "bucket2",
"aws_region": "us-west-2",
"paths": {
"public/*": {
"guest": ["get", "list", "write", "delete"],
"authenticated": ["write", "get", "list", "delete"]
}
}
}
]
}
}
2 changes: 1 addition & 1 deletion packages/core/src/singleton/AmplifyOutputs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type UserGroupName = string;
type UserGroupPrecedence = Record<string, number>;
export interface AmplifyOutputsAuthProperties {
aws_region: string;
authentication_flow_type?: 'USER_SRP_AUTH' | 'CUSTOM_AUTH';
authentication_flow_type?: string;
user_pool_id: string;
user_pool_client_id: string;
identity_pool_id?: string;
Expand Down
Loading