From 53101a6ec52dd288969c454fcf9aaf57ddb953c7 Mon Sep 17 00:00:00 2001 From: Jordan Nelson Date: Tue, 19 Sep 2023 11:27:22 -0400 Subject: [PATCH 1/3] fix: authenticator date validator --- .../form_fields/sign_up_form_field.dart | 3 ++ .../test/sign_up_form_test.dart | 46 +++++++++++++++++++ .../lib/src/mock_authenticator_app.dart | 6 +++ .../lib/src/pages/sign_up_page.dart | 19 ++++++++ 4 files changed, 74 insertions(+) diff --git a/packages/authenticator/amplify_authenticator/lib/src/widgets/form_fields/sign_up_form_field.dart b/packages/authenticator/amplify_authenticator/lib/src/widgets/form_fields/sign_up_form_field.dart index d010547155..8c9d00679c 100644 --- a/packages/authenticator/amplify_authenticator/lib/src/widgets/form_fields/sign_up_form_field.dart +++ b/packages/authenticator/amplify_authenticator/lib/src/widgets/form_fields/sign_up_form_field.dart @@ -780,6 +780,9 @@ class _SignUpDateFieldState extends _SignUpFormFieldState @override FormFieldValidator get validator { + if (widget.validatorOverride != null) { + return widget.validatorOverride!; + } return simpleValidator( stringResolver.inputs.resolve( context, diff --git a/packages/authenticator/amplify_authenticator/test/sign_up_form_test.dart b/packages/authenticator/amplify_authenticator/test/sign_up_form_test.dart index 72ee1b142e..578162db80 100644 --- a/packages/authenticator/amplify_authenticator/test/sign_up_form_test.dart +++ b/packages/authenticator/amplify_authenticator/test/sign_up_form_test.dart @@ -73,6 +73,52 @@ void main() { }, ); + testWidgets( + 'displays message when submitted with invalid birth date', + (tester) async { + await tester.pumpWidget( + MockAuthenticatorApp( + initialStep: AuthenticatorStep.signUp, + signUpForm: SignUpForm.custom( + fields: [ + SignUpFormField.username(), + SignUpFormField.birthdate( + validator: (value) { + if (value == null || value.isEmpty) return null; + final age = + DateTime.now().difference(DateTime.parse(value)); + if (age < const Duration(days: 365 * 18)) { + return 'You must be 18 years or older.'; + } + return null; + }, + ), + SignUpFormField.password(), + SignUpFormField.passwordConfirmation(), + ], + ), + ), + ); + await tester.pumpAndSettle(); + + final signUpPage = SignUpPage(tester: tester); + + await signUpPage.enterUsername('user123'); + await signUpPage.enterBirthDate('01/01/2020'); + await signUpPage.enterPassword('Password123'); + await signUpPage.enterPasswordConfirmation('Password123'); + + await signUpPage.submitSignUp(); + + final usernameFieldError = find.descendant( + of: signUpPage.birthdateField, + matching: find.text('You must be 18 years or older.'), + ); + + expect(usernameFieldError, findsOneWidget); + }, + ); + testWidgets( 'displays message when password does not meet requirements', (tester) async { diff --git a/packages/authenticator/amplify_authenticator_test/lib/src/mock_authenticator_app.dart b/packages/authenticator/amplify_authenticator_test/lib/src/mock_authenticator_app.dart index 2d69b60a10..651d3452e7 100644 --- a/packages/authenticator/amplify_authenticator_test/lib/src/mock_authenticator_app.dart +++ b/packages/authenticator/amplify_authenticator_test/lib/src/mock_authenticator_app.dart @@ -22,6 +22,8 @@ class MockAuthenticatorApp extends StatefulWidget { this.darkTheme, this.initialStep = AuthenticatorStep.signIn, this.authPlugin, + this.signInForm, + this.signUpForm, this.child, }); @@ -30,6 +32,8 @@ class MockAuthenticatorApp extends StatefulWidget { final ThemeData? darkTheme; final AuthenticatorStep initialStep; final AuthPluginInterface? authPlugin; + final SignInForm? signInForm; + final SignUpForm? signUpForm; final Widget? child; @override @@ -102,6 +106,8 @@ class _MockAuthenticatorAppState extends State { return Authenticator( key: authenticatorKey, authBlocOverride: _authBloc, + signInForm: widget.signInForm, + signUpForm: widget.signUpForm, child: widget.child ?? MaterialApp( debugShowCheckedModeBanner: false, diff --git a/packages/authenticator/amplify_authenticator_test/lib/src/pages/sign_up_page.dart b/packages/authenticator/amplify_authenticator_test/lib/src/pages/sign_up_page.dart index a8ca710810..60259d4dbb 100644 --- a/packages/authenticator/amplify_authenticator_test/lib/src/pages/sign_up_page.dart +++ b/packages/authenticator/amplify_authenticator_test/lib/src/pages/sign_up_page.dart @@ -21,6 +21,15 @@ class SignUpPage extends AuthenticatorPage { Finder get phoneField => find.byKey(keyPhoneNumberSignUpFormField); Finder get preferredUsernameField => find.byKey(keyPreferredUsernameSignUpFormField); + Finder get birthdateField => find.byKey(keyBirthdateSignUpFormField); + Finder get datePickerTextField => find.descendant( + of: find.byType(InputDatePickerFormField), + matching: find.byType(TextFormField), + ); + Finder get datePickerOkayButton => find.descendant( + of: find.byType(DatePickerDialog), + matching: find.byType(TextButton).at(1), + ); Finder get signUpButton => find.byKey(keySignUpButton); Finder get selectEmailButton => find.byKey(keyEmailUsernameToggleButton); @@ -67,6 +76,16 @@ class SignUpPage extends AuthenticatorPage { await tester.enterText(preferredUsernameField, username); } + /// When I type a new "preferred username" + Future enterBirthDate(String value) async { + await tester.ensureVisible(birthdateField); + await tester.tap(birthdateField); + await tester.pumpAndSettle(); + await tester.enterText(datePickerTextField, value); + await tester.tap(datePickerOkayButton); + await tester.pumpAndSettle(); + } + /// When I click the "Create Account" button Future submitSignUp() async { await tester.ensureVisible(signUpButton); From 6c3ff65cbd86103b58e2a83427ae1a09b12ab5c0 Mon Sep 17 00:00:00 2001 From: Jordan Nelson Date: Tue, 19 Sep 2023 11:29:13 -0400 Subject: [PATCH 2/3] chore: formatting --- .../amplify_authenticator/test/sign_up_form_test.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/authenticator/amplify_authenticator/test/sign_up_form_test.dart b/packages/authenticator/amplify_authenticator/test/sign_up_form_test.dart index 578162db80..8a83833686 100644 --- a/packages/authenticator/amplify_authenticator/test/sign_up_form_test.dart +++ b/packages/authenticator/amplify_authenticator/test/sign_up_form_test.dart @@ -85,8 +85,9 @@ void main() { SignUpFormField.birthdate( validator: (value) { if (value == null || value.isEmpty) return null; - final age = - DateTime.now().difference(DateTime.parse(value)); + final age = DateTime.now().difference( + DateTime.parse(value), + ); if (age < const Duration(days: 365 * 18)) { return 'You must be 18 years or older.'; } From 92306a978cc3dad3fea6955cb650316a39f4bc84 Mon Sep 17 00:00:00 2001 From: Jordan Nelson Date: Tue, 19 Sep 2023 11:29:52 -0400 Subject: [PATCH 3/3] chore: fix comment --- .../amplify_authenticator_test/lib/src/pages/sign_up_page.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/authenticator/amplify_authenticator_test/lib/src/pages/sign_up_page.dart b/packages/authenticator/amplify_authenticator_test/lib/src/pages/sign_up_page.dart index 60259d4dbb..83ccdd3121 100644 --- a/packages/authenticator/amplify_authenticator_test/lib/src/pages/sign_up_page.dart +++ b/packages/authenticator/amplify_authenticator_test/lib/src/pages/sign_up_page.dart @@ -76,7 +76,7 @@ class SignUpPage extends AuthenticatorPage { await tester.enterText(preferredUsernameField, username); } - /// When I type a new "preferred username" + /// When I type a new "birth date" Future enterBirthDate(String value) async { await tester.ensureVisible(birthdateField); await tester.tap(birthdateField);