Skip to content

Commit 7b97855

Browse files
ashwinkumar6Ashwin Kumar
andauthored
test(core): catch type issues when configuring from external json (#14101)
* test(core): catch type issues when configuring from external json * update amplify_outputs to reflect user groups in gen2 format * chore: code cleanup --------- Co-authored-by: Ashwin Kumar <ashwsrir@amazon.com>
1 parent 25dbd61 commit 7b97855

File tree

4 files changed

+221
-1
lines changed

4 files changed

+221
-1
lines changed

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ module.exports = {
1515
noImplicitAny: false,
1616
types: ['jest', 'jsdom'],
1717
},
18+
diagnostics: {
19+
warnOnly: false,
20+
},
1821
},
1922
],
2023
},

packages/core/__tests__/singleton/Singleton.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { AuthClass as Auth } from '../../src/singleton/Auth';
66
import { decodeJWT } from '../../src/singleton/Auth/utils';
77
import { CredentialsAndIdentityId } from '../../src/singleton/Auth/types';
88
import { ResourcesConfig, fetchAuthSession } from '../../src';
9+
import { AmplifyOutputs } from '../../src/libraryUtils';
10+
11+
import mockAmplifyOutputs from './utils/amplify_outputs.json';
912

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

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

170+
it(`should successfully configure from a 'amplify_outputs.json' file`, () => {
171+
type DeepRequired<T> = T extends object
172+
? { [K in keyof T]-?: DeepRequired<NonNullable<T[K]>> }
173+
: T;
174+
175+
const recreateObjectWithDeepRequired = <T>(obj: T): DeepRequired<T> =>
176+
obj as DeepRequired<T>;
177+
178+
// Ensures that all optional properties in mockAmplifyOutputs are defined
179+
const _amplifyOutputs: DeepRequired<AmplifyOutputs> =
180+
recreateObjectWithDeepRequired(mockAmplifyOutputs);
181+
182+
// Configures Amplify with mockAmplifyOutputs, relying on its inferred type
183+
Amplify.configure(mockAmplifyOutputs);
184+
});
185+
167186
it('dispatches hub event with parsed ResourceConfig from the legacy config', () => {
168187
Amplify.configure(mockLegacyConfig);
169188

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
{
2+
"version": "1",
3+
"auth": {
4+
"aws_region": "us-west-2",
5+
"user_pool_id": "mock-cup-id",
6+
"user_pool_client_id": "mock-cup-client-id",
7+
"identity_pool_id": "mock-idp-id",
8+
"authentication_flow_type": "CUSTOM_AUTH",
9+
"oauth": {
10+
"identity_providers": [
11+
"FACEBOOK",
12+
"SIGN_IN_WITH_APPLE",
13+
"GOOGLE",
14+
"Auth0"
15+
],
16+
"domain": "mock-oauth-domain",
17+
"scopes": ["phone"],
18+
"redirect_sign_in_uri": ["mock-sign-in-uri"],
19+
"redirect_sign_out_uri": ["mock-sign-out-uri"],
20+
"response_type": "token"
21+
},
22+
"standard_required_attributes": ["address", "locale", "email"],
23+
"username_attributes": ["phone_number", "email"],
24+
"user_verification_types": ["email"],
25+
"unauthenticated_identities_enabled": true,
26+
"mfa_configuration": "OPTIONAL",
27+
"mfa_methods": ["TOTP", "SMS"],
28+
"password_policy": {
29+
"require_lowercase": true,
30+
"require_numbers": true,
31+
"require_uppercase": true,
32+
"require_symbols": true,
33+
"min_length": 6
34+
},
35+
"groups": [
36+
{ "ADMIN": { "precedence": 0 } },
37+
{ "USER": { "precedence": 1 } },
38+
{ "TEST": { "precedence": 2 } }
39+
]
40+
},
41+
"data": {
42+
"aws_region": "us-west-2",
43+
"url": "mock-data-url",
44+
"api_key": "mock-data-api-key",
45+
"default_authorization_type": "API_KEY",
46+
"authorization_types": [],
47+
"conflict_resolution_mode": "AUTOMERGE",
48+
"model_introspection": {
49+
"version": 1,
50+
"models": {
51+
"Todo": {
52+
"name": "Todo",
53+
"fields": {
54+
"id": {
55+
"name": "id",
56+
"isArray": false,
57+
"type": "ID",
58+
"isRequired": true,
59+
"attributes": []
60+
},
61+
"content": {
62+
"name": "content",
63+
"isArray": false,
64+
"type": "String",
65+
"isRequired": false,
66+
"attributes": []
67+
},
68+
"createdAt": {
69+
"name": "createdAt",
70+
"isArray": false,
71+
"type": "AWSDateTime",
72+
"isRequired": false,
73+
"attributes": [],
74+
"isReadOnly": true
75+
},
76+
"updatedAt": {
77+
"name": "updatedAt",
78+
"isArray": false,
79+
"type": "AWSDateTime",
80+
"isRequired": false,
81+
"attributes": [],
82+
"isReadOnly": true
83+
}
84+
},
85+
"syncable": true,
86+
"pluralName": "Todos",
87+
"attributes": [
88+
{
89+
"type": "model",
90+
"properties": {}
91+
},
92+
{
93+
"type": "auth",
94+
"properties": {
95+
"rules": [
96+
{
97+
"provider": "userPools",
98+
"ownerField": "owner",
99+
"allow": "owner",
100+
"identityClaim": "cognito:username",
101+
"operations": ["create", "update", "delete", "read"]
102+
},
103+
{
104+
"allow": "private",
105+
"provider": "iam",
106+
"operations": ["create", "update", "delete", "read"]
107+
}
108+
]
109+
}
110+
}
111+
],
112+
"primaryKeyInfo": {
113+
"isCustomPrimaryKey": false,
114+
"primaryKeyFieldName": "id",
115+
"sortKeyFieldNames": []
116+
}
117+
}
118+
},
119+
"enums": {},
120+
"nonModels": {}
121+
}
122+
},
123+
"geo": {
124+
"aws_region": "us-west-2",
125+
"search_indices": {
126+
"items": ["mock-geo-search-item", "mock-geo-search-item-alt"],
127+
"default": "mock-geo-search-item"
128+
},
129+
"geofence_collections": {
130+
"items": ["mock-geo-fence-item", "mock-geo-fence-item-alt"],
131+
"default": "mock-geo-fence-item"
132+
},
133+
134+
"maps": {
135+
"items": {
136+
"map51addb38-dev": {
137+
"style": "VectorEsriStreets"
138+
},
139+
"map30grxcw31-prod": {
140+
"style": "VectorEsriStreets"
141+
}
142+
},
143+
"default": "map51addb38-dev"
144+
}
145+
},
146+
"custom": {
147+
"custom-prop": -51806024,
148+
"custom-prop-alt": 87599986,
149+
"events": {
150+
"url": "mock-events-url",
151+
"aws_region": "us-west-2",
152+
"default_authorization_type": "API_KEY",
153+
"api_key": "mock-events-api-key"
154+
}
155+
},
156+
"notifications": {
157+
"aws_region": "us-west-2",
158+
"amazon_pinpoint_app_id": "mock-pinpoint-app-id",
159+
"channels": ["IN_APP_MESSAGING", "APNS"]
160+
},
161+
"analytics": {
162+
"amazon_pinpoint": {
163+
"app_id": "mock-pinpoint-app-id",
164+
"aws_region": "us-west-2"
165+
}
166+
},
167+
"storage": {
168+
"aws_region": "us-west-2",
169+
"bucket_name": "mock-bucket-1",
170+
"buckets": [
171+
{
172+
"name": "mock-bucket-1",
173+
"bucket_name": "bucket1",
174+
"aws_region": "us-west-2",
175+
"paths": {
176+
"sub/*": {
177+
"authenticated": ["get", "list"]
178+
},
179+
"public/*": {
180+
"guest": ["get", "list", "write", "delete"],
181+
"authenticated": ["write", "get", "list", "delete"]
182+
}
183+
}
184+
},
185+
{
186+
"name": "mock-bucket-2",
187+
"bucket_name": "bucket2",
188+
"aws_region": "us-west-2",
189+
"paths": {
190+
"public/*": {
191+
"guest": ["get", "list", "write", "delete"],
192+
"authenticated": ["write", "get", "list", "delete"]
193+
}
194+
}
195+
}
196+
]
197+
}
198+
}

packages/core/src/singleton/AmplifyOutputs/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type UserGroupName = string;
1717
type UserGroupPrecedence = Record<string, number>;
1818
export interface AmplifyOutputsAuthProperties {
1919
aws_region: string;
20-
authentication_flow_type?: 'USER_SRP_AUTH' | 'CUSTOM_AUTH';
20+
authentication_flow_type?: string;
2121
user_pool_id: string;
2222
user_pool_client_id: string;
2323
identity_pool_id?: string;

0 commit comments

Comments
 (0)