Skip to content

Commit c4bfbba

Browse files
committed
chore: trim down on overlapping tests and moved getOtpCode earlier in files
1 parent 0672db3 commit c4bfbba

File tree

5 files changed

+62
-143
lines changed

5 files changed

+62
-143
lines changed

packages/auth/amplify_auth_cognito/example/integration_test/mfa_email_totp_optional_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ void main() {
337337

338338
await signOutUser(assertComplete: true);
339339

340+
final otpResult = await getOtpCode(UserAttribute.email(username));
341+
340342
final resignInRes = await Amplify.Auth.signIn(
341343
username: username,
342344
password: password,
@@ -347,7 +349,6 @@ void main() {
347349
.isNotNull()
348350
.deepEquals({MfaType.email, MfaType.totp});
349351

350-
final otpResult = await getOtpCode(UserAttribute.email(username));
351352
final selectRes = await Amplify.Auth.confirmSignIn(
352353
confirmationValue: 'EMAIL',
353354
);

packages/auth/amplify_auth_cognito/example/integration_test/mfa_email_totp_required_test.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ void main() {
1818
final username = env.generateUsername();
1919
final password = generatePassword();
2020

21+
final otpResult = await getOtpCode(UserAttribute.email(username));
22+
2123
// Create a user with no phone number.
2224
await adminCreateUser(
2325
username,
@@ -37,7 +39,6 @@ void main() {
3739
'When an email is registered and the userpool has email MFA enabled, Cognito will automatically enable email MFA as the preferred MFA method.',
3840
).equals(AuthSignInStep.confirmSignInWithEmailMfaCode);
3941

40-
final otpResult = await getOtpCode(UserAttribute.email(username));
4142
final setupRes = await Amplify.Auth.confirmSignIn(
4243
confirmationValue: await otpResult.code,
4344
);
@@ -52,6 +53,8 @@ void main() {
5253

5354
await signOutUser(assertComplete: true);
5455

56+
final otpResult2 = await getOtpCode(UserAttribute.email(username));
57+
5558
final resignInRes = await Amplify.Auth.signIn(
5659
username: username,
5760
password: password,
@@ -63,7 +66,6 @@ void main() {
6366
.has((d) => d.deliveryMedium, 'deliveryMedium')
6467
.equals(DeliveryMedium.email);
6568

66-
final otpResult2 = await getOtpCode(UserAttribute.email(username));
6769
final confirmRes = await Amplify.Auth.confirmSignIn(
6870
confirmationValue: await otpResult2.code,
6971
);
@@ -74,6 +76,8 @@ void main() {
7476
final username = env.generateUsername();
7577
final password = generatePassword();
7678

79+
final otpResult = await getOtpCode(UserAttribute.email(username));
80+
7781
// Create a user with an unverified phone number.
7882
await adminCreateUser(
7983
username,
@@ -96,7 +100,6 @@ void main() {
96100
'MFA is required so Cognito automatically enables EMAIL MFA',
97101
).equals(AuthSignInStep.confirmSignInWithEmailMfaCode);
98102

99-
final otpResult = await getOtpCode(UserAttribute.email(username));
100103
final confirmRes = await Amplify.Auth.confirmSignIn(
101104
confirmationValue: await otpResult.code,
102105
);
@@ -212,6 +215,8 @@ void main() {
212215
{
213216
await signOutUser(assertComplete: true);
214217

218+
final otpResult = await getOtpCode(UserAttribute.email(username));
219+
215220
final signInRes = await Amplify.Auth.signIn(
216221
username: username,
217222
password: password,
@@ -225,7 +230,6 @@ void main() {
225230
.has((d) => d.deliveryMedium, 'deliveryMedium')
226231
.equals(DeliveryMedium.email);
227232

228-
final otpResult = await getOtpCode(UserAttribute.email(username));
229233
final confirmRes = await Amplify.Auth.confirmSignIn(
230234
confirmationValue: await otpResult.code,
231235
);

packages/auth/amplify_auth_cognito/example/integration_test/mfa_sms_email_optional_test.dart

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -174,45 +174,6 @@ void main() {
174174
),
175175
);
176176

177-
// Verify we can set EMAIL as preferred and forego selection.
178-
179-
await cognitoPlugin.updateMfaPreference(
180-
email: MfaPreference.preferred,
181-
);
182-
check(
183-
await cognitoPlugin.fetchMfaPreference(),
184-
because: 'EMAIL should be marked preferred',
185-
).equals(
186-
const UserMfaPreference(
187-
enabled: {MfaType.sms, MfaType.email},
188-
preferred: MfaType.email,
189-
),
190-
);
191-
192-
{
193-
await signOutUser(assertComplete: true);
194-
195-
final signInRes = await Amplify.Auth.signIn(
196-
username: username,
197-
password: password,
198-
);
199-
check(signInRes.nextStep.signInStep)
200-
.equals(AuthSignInStep.confirmSignInWithEmailMfaCode);
201-
check(signInRes.nextStep.codeDeliveryDetails)
202-
.isNotNull()
203-
.has((d) => d.deliveryMedium, 'deliveryMedium')
204-
.equals(DeliveryMedium.email);
205-
206-
final otpResult2 = await getOtpCode(
207-
env.getLoginAttribute(username),
208-
);
209-
210-
final confirmRes = await Amplify.Auth.confirmSignIn(
211-
confirmationValue: await otpResult2.code,
212-
);
213-
check(confirmRes.nextStep.signInStep).equals(AuthSignInStep.done);
214-
}
215-
216177
// Verify we can switch to SMS as preferred.
217178

218179
await cognitoPlugin.updateMfaPreference(
@@ -334,6 +295,8 @@ void main() {
334295

335296
await signOutUser(assertComplete: true);
336297

298+
final mfaCode = await getOtpCode(UserAttribute.phone(phoneNumber));
299+
337300
final resignInRes = await Amplify.Auth.signIn(
338301
username: username,
339302
password: password,
@@ -347,7 +310,6 @@ void main() {
347310
.isNotNull()
348311
.startsWith('+');
349312

350-
final mfaCode = await getOtpCode(UserAttribute.phone(phoneNumber));
351313
final confirmRes = await Amplify.Auth.confirmSignIn(
352314
confirmationValue: await mfaCode.code,
353315
);
@@ -414,15 +376,15 @@ void main() {
414376
{
415377
await signOutUser(assertComplete: true);
416378

379+
final otpResult = await getOtpCode(
380+
env.getLoginAttribute(username),
381+
);
382+
417383
final signInRes = await Amplify.Auth.signIn(
418384
username: username,
419385
password: password,
420386
);
421387

422-
final otpResult = await getOtpCode(
423-
env.getLoginAttribute(username),
424-
);
425-
426388
check(signInRes.nextStep.signInStep)
427389
.equals(AuthSignInStep.confirmSignInWithEmailMfaCode);
428390
check(signInRes.nextStep.codeDeliveryDetails)

packages/auth/amplify_auth_cognito/example/integration_test/mfa_sms_email_required_test.dart

Lines changed: 42 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ void main() {
5757

5858
await signOutUser(assertComplete: true);
5959

60+
// Verify we can sign in with EMAIL MFA as the preferred method and forego selection.
61+
62+
final otpResult2 = await getOtpCode(
63+
env.getLoginAttribute(username),
64+
);
65+
6066
final resignInRes = await Amplify.Auth.signIn(
6167
username: username,
6268
password: password,
@@ -68,10 +74,6 @@ void main() {
6874
.has((d) => d.deliveryMedium, 'deliveryMedium')
6975
.equals(DeliveryMedium.email);
7076

71-
final otpResult2 = await getOtpCode(
72-
env.getLoginAttribute(username),
73-
);
74-
7577
final confirmRes = await Amplify.Auth.confirmSignIn(
7678
confirmationValue: await otpResult2.code,
7779
);
@@ -83,6 +85,8 @@ void main() {
8385
final password = generatePassword();
8486
final phoneNumber = generatePhoneNumber();
8587

88+
final mfaCode = await getOtpCode(UserAttribute.phone(phoneNumber));
89+
8690
// Verify we can set EMAIL as preferred and forego selection.
8791
await adminCreateUser(
8892
username,
@@ -95,22 +99,19 @@ void main() {
9599
},
96100
);
97101

98-
{
99-
final mfaCode = await getOtpCode(UserAttribute.phone(phoneNumber));
100-
final signInRes = await Amplify.Auth.signIn(
101-
username: username,
102-
password: password,
103-
);
104-
check(
105-
signInRes.nextStep.signInStep,
106-
because: 'MFA is required so Cognito automatically enables SMS MFA',
107-
).equals(AuthSignInStep.confirmSignInWithSmsMfaCode);
102+
final signInRes = await Amplify.Auth.signIn(
103+
username: username,
104+
password: password,
105+
);
106+
check(
107+
signInRes.nextStep.signInStep,
108+
because: 'MFA is required so Cognito automatically enables SMS MFA',
109+
).equals(AuthSignInStep.confirmSignInWithSmsMfaCode);
108110

109-
final confirmRes = await Amplify.Auth.confirmSignIn(
110-
confirmationValue: await mfaCode.code,
111-
);
112-
check(confirmRes.nextStep.signInStep).equals(AuthSignInStep.done);
113-
}
111+
final confirmRes = await Amplify.Auth.confirmSignIn(
112+
confirmationValue: await mfaCode.code,
113+
);
114+
check(confirmRes.nextStep.signInStep).equals(AuthSignInStep.done);
114115

115116
check(
116117
await cognitoPlugin.fetchMfaPreference(),
@@ -136,6 +137,9 @@ void main() {
136137
await signOutUser(assertComplete: true);
137138

138139
{
140+
final otpResult = await getOtpCode(
141+
env.getLoginAttribute(username),
142+
);
139143
final resignInRes = await Amplify.Auth.signIn(
140144
username: username,
141145
password: password,
@@ -149,10 +153,6 @@ void main() {
149153
.has((d) => d.deliveryMedium, 'deliveryMedium')
150154
.equals(DeliveryMedium.email);
151155

152-
final otpResult = await getOtpCode(
153-
env.getLoginAttribute(username),
154-
);
155-
156156
final confirmRes = await Amplify.Auth.confirmSignIn(
157157
confirmationValue: await otpResult.code,
158158
);
@@ -166,43 +166,6 @@ void main() {
166166
),
167167
);
168168

169-
// Verify we can switch to SMS as preferred.
170-
171-
await cognitoPlugin.updateMfaPreference(
172-
sms: MfaPreference.preferred,
173-
);
174-
check(await cognitoPlugin.fetchMfaPreference()).equals(
175-
const UserMfaPreference(
176-
enabled: {MfaType.sms, MfaType.email},
177-
preferred: MfaType.sms,
178-
),
179-
);
180-
181-
{
182-
await signOutUser(assertComplete: true);
183-
184-
final mfaCode = await getOtpCode(UserAttribute.phone(phoneNumber));
185-
final signInRes = await Amplify.Auth.signIn(
186-
username: username,
187-
password: password,
188-
);
189-
check(
190-
signInRes.nextStep.signInStep,
191-
because: 'Preference is SMS MFA now',
192-
).equals(AuthSignInStep.confirmSignInWithSmsMfaCode);
193-
check(signInRes.nextStep.codeDeliveryDetails).isNotNull()
194-
..has((d) => d.deliveryMedium, 'deliveryMedium')
195-
.equals(DeliveryMedium.sms)
196-
..has((d) => d.destination, 'destination')
197-
.isNotNull()
198-
.startsWith('+');
199-
200-
final confirmRes = await Amplify.Auth.confirmSignIn(
201-
confirmationValue: await mfaCode.code,
202-
);
203-
check(confirmRes.nextStep.signInStep).equals(AuthSignInStep.done);
204-
}
205-
206169
// Verify marking enabled does not change preference.
207170
await cognitoPlugin.updateMfaPreference(
208171
sms: MfaPreference.enabled,
@@ -214,7 +177,7 @@ void main() {
214177
).equals(
215178
const UserMfaPreference(
216179
enabled: {MfaType.sms, MfaType.email},
217-
preferred: MfaType.sms,
180+
preferred: MfaType.email,
218181
),
219182
);
220183

@@ -246,6 +209,8 @@ void main() {
246209
final password = generatePassword();
247210
final phoneNumber = generatePhoneNumber();
248211

212+
final mfaCode = await getOtpCode(UserAttribute.phone(phoneNumber));
213+
249214
// Create a user with an unverified phone number.
250215
await adminCreateUser(
251216
username,
@@ -258,22 +223,19 @@ void main() {
258223
},
259224
);
260225

261-
{
262-
final mfaCode = await getOtpCode(UserAttribute.phone(phoneNumber));
263-
final signInRes = await Amplify.Auth.signIn(
264-
username: username,
265-
password: password,
266-
);
267-
check(
268-
signInRes.nextStep.signInStep,
269-
because: 'MFA is required so Cognito automatically enables SMS MFA',
270-
).equals(AuthSignInStep.confirmSignInWithSmsMfaCode);
226+
final signInRes = await Amplify.Auth.signIn(
227+
username: username,
228+
password: password,
229+
);
230+
check(
231+
signInRes.nextStep.signInStep,
232+
because: 'MFA is required so Cognito automatically enables SMS MFA',
233+
).equals(AuthSignInStep.confirmSignInWithSmsMfaCode);
271234

272-
final confirmRes = await Amplify.Auth.confirmSignIn(
273-
confirmationValue: await mfaCode.code,
274-
);
275-
check(confirmRes.nextStep.signInStep).equals(AuthSignInStep.done);
276-
}
235+
final confirmRes = await Amplify.Auth.confirmSignIn(
236+
confirmationValue: await mfaCode.code,
237+
);
238+
check(confirmRes.nextStep.signInStep).equals(AuthSignInStep.done);
277239

278240
check(
279241
await cognitoPlugin.fetchMfaPreference(),
@@ -287,17 +249,6 @@ void main() {
287249

288250
// Verify we can set SMS as preferred and forego selection.
289251

290-
await cognitoPlugin.updateMfaPreference(
291-
sms: MfaPreference.preferred,
292-
email: MfaPreference.enabled,
293-
);
294-
check(await cognitoPlugin.fetchMfaPreference()).equals(
295-
const UserMfaPreference(
296-
enabled: {MfaType.sms, MfaType.email},
297-
preferred: MfaType.sms,
298-
),
299-
);
300-
301252
{
302253
await signOutUser(assertComplete: true);
303254

@@ -338,6 +289,10 @@ void main() {
338289
{
339290
await signOutUser(assertComplete: true);
340291

292+
final otpResult = await getOtpCode(
293+
env.getLoginAttribute(username),
294+
);
295+
341296
final signInRes = await Amplify.Auth.signIn(
342297
username: username,
343298
password: password,
@@ -351,10 +306,6 @@ void main() {
351306
.has((d) => d.deliveryMedium, 'deliveryMedium')
352307
.equals(DeliveryMedium.email);
353308

354-
final otpResult = await getOtpCode(
355-
env.getLoginAttribute(username),
356-
);
357-
358309
final confirmRes = await Amplify.Auth.confirmSignIn(
359310
confirmationValue: await otpResult.code,
360311
);

0 commit comments

Comments
 (0)