From 91e7e6a2631b30b7a5859c7b1385002b665e3204 Mon Sep 17 00:00:00 2001 From: KhaledNjim <160496984+KhaledNjim@users.noreply.github.com> Date: Wed, 20 Nov 2024 08:25:23 +0100 Subject: [PATCH] Tw 2046 group name validation (#2119) --- assets/l10n/intl_en.arb | 3 +- lib/di/global/get_it_initializer.dart | 5 + .../validator/verify_name_view_state.dart | 8 ++ .../exception/verify_name_exception.dart | 21 ++++ .../extensions/list_validator_extension.dart | 20 ++++ .../validator_failure_extension.dart | 15 +++ .../composite_name_validator.dart | 20 ++++ .../verification/empty_name_validator.dart | 20 ++++ .../name_with_space_only_validator.dart | 22 ++++ .../model/verification/new_name_request.dart | 10 ++ lib/domain/model/verification/validator.dart | 7 ++ .../usecase/verify_name_interactor.dart | 21 ++++ lib/pages/chat_details/chat_details_edit.dart | 26 +++- .../chat_details/chat_details_edit_view.dart | 113 ++++++++++-------- lib/pages/new_group/new_group_chat_info.dart | 19 +++ .../new_group/new_group_chat_info_view.dart | 53 +++++--- .../new_group/new_group_info_controller.dart | 3 +- .../composite_name_validator_test.dart | 73 +++++++++++ .../usecase/verify_name_interactor_test.dart | 72 +++++++++++ 19 files changed, 460 insertions(+), 71 deletions(-) create mode 100644 lib/domain/app_state/validator/verify_name_view_state.dart create mode 100644 lib/domain/exception/verify_name_exception.dart create mode 100644 lib/domain/model/extensions/list_validator_extension.dart create mode 100644 lib/domain/model/extensions/validator_failure_extension.dart create mode 100644 lib/domain/model/verification/composite_name_validator.dart create mode 100644 lib/domain/model/verification/empty_name_validator.dart create mode 100644 lib/domain/model/verification/name_with_space_only_validator.dart create mode 100644 lib/domain/model/verification/new_name_request.dart create mode 100644 lib/domain/model/verification/validator.dart create mode 100644 lib/domain/usecase/verify_name_interactor.dart create mode 100644 test/domain/model/verification/composite_name_validator_test.dart create mode 100644 test/domain/usecase/verify_name_interactor_test.dart diff --git a/assets/l10n/intl_en.arb b/assets/l10n/intl_en.arb index 95a1a6071..73e5f7c4e 100644 --- a/assets/l10n/intl_en.arb +++ b/assets/l10n/intl_en.arb @@ -3098,5 +3098,6 @@ "logoutDialogWarning": "You will lose access to encrypted messages. We recommend that you enable chat backups before logging out", "copyNumber": "Copy number", "callViaCarrier": "Call via Carrier", - "scanQrCodeToJoin": "Installation of the mobile application will allow you to contact people from your phone's address book, your chats will be synchronised between devices" + "scanQrCodeToJoin": "Installation of the mobile application will allow you to contact people from your phone's address book, your chats will be synchronised between devices", + "thisFieldCannotBeBlank": "This field cannot be blank" } diff --git a/lib/di/global/get_it_initializer.dart b/lib/di/global/get_it_initializer.dart index 54bd0d199..763604cd6 100644 --- a/lib/di/global/get_it_initializer.dart +++ b/lib/di/global/get_it_initializer.dart @@ -79,6 +79,7 @@ import 'package:fluffychat/domain/usecase/search/search_recent_chat_interactor.d import 'package:fluffychat/domain/usecase/search/server_search_interactor.dart'; import 'package:fluffychat/domain/usecase/settings/save_language_interactor.dart'; import 'package:fluffychat/domain/usecase/settings/update_profile_interactor.dart'; +import 'package:fluffychat/domain/usecase/verify_name_interactor.dart'; import 'package:fluffychat/event/twake_event_dispatcher.dart'; import 'package:fluffychat/pages/chat/chat_pinned_events/pinned_events_controller.dart'; import 'package:fluffychat/utils/famedlysdk_store.dart'; @@ -344,6 +345,10 @@ class GetItInitializer { getIt.registerSingleton( DownloadMediaFileInteractor(), ); + + getIt.registerFactory( + () => VerifyNameInteractor(), + ); } void _bindingControllers() { diff --git a/lib/domain/app_state/validator/verify_name_view_state.dart b/lib/domain/app_state/validator/verify_name_view_state.dart new file mode 100644 index 000000000..f84a21c27 --- /dev/null +++ b/lib/domain/app_state/validator/verify_name_view_state.dart @@ -0,0 +1,8 @@ +import 'package:fluffychat/presentation/state/failure.dart'; +import 'package:fluffychat/presentation/state/success.dart'; + +class VerifyNameSuccessViewState extends UIState {} + +class VerifyNameFailure extends FeatureFailure { + const VerifyNameFailure(dynamic exception) : super(exception: exception); +} diff --git a/lib/domain/exception/verify_name_exception.dart b/lib/domain/exception/verify_name_exception.dart new file mode 100644 index 000000000..0b7c037b8 --- /dev/null +++ b/lib/domain/exception/verify_name_exception.dart @@ -0,0 +1,21 @@ +import 'package:equatable/equatable.dart'; + +abstract class VerifyNameException extends Equatable implements Exception { + static const nameWithOnlySpace = 'The name cannot contain only spaces'; + static const emptyName = 'The name cannot be empty'; + + final String? message; + const VerifyNameException(this.message); + + @override + List get props => [message]; +} + +class NameWithSpaceOnlyException extends VerifyNameException { + const NameWithSpaceOnlyException() + : super(VerifyNameException.nameWithOnlySpace); +} + +class EmptyNameException extends VerifyNameException { + const EmptyNameException() : super(VerifyNameException.emptyName); +} diff --git a/lib/domain/model/extensions/list_validator_extension.dart b/lib/domain/model/extensions/list_validator_extension.dart new file mode 100644 index 000000000..8589aea1f --- /dev/null +++ b/lib/domain/model/extensions/list_validator_extension.dart @@ -0,0 +1,20 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; +import 'package:fluffychat/domain/model/verification/new_name_request.dart'; +import 'package:fluffychat/domain/model/verification/validator.dart'; + +extension ListValidatorExtension on List { + Either getValidatorNameViewState( + NewNameRequest newNameRequest, + ) { + for (final validator in this) { + final either = validator.validate(newNameRequest); + if (either.isLeft()) { + return either; + } + } + return Right(VerifyNameSuccessViewState()); + } +} diff --git a/lib/domain/model/extensions/validator_failure_extension.dart b/lib/domain/model/extensions/validator_failure_extension.dart new file mode 100644 index 000000000..e68a97b76 --- /dev/null +++ b/lib/domain/model/extensions/validator_failure_extension.dart @@ -0,0 +1,15 @@ +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; +import 'package:fluffychat/domain/exception/verify_name_exception.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/l10n.dart'; + +extension ValidatorFailureExtension on VerifyNameFailure { + String getMessage(BuildContext context) { + if (exception is NameWithSpaceOnlyException || + exception is EmptyNameException) { + return L10n.of(context)!.thisFieldCannotBeBlank; + } else { + return ''; + } + } +} diff --git a/lib/domain/model/verification/composite_name_validator.dart b/lib/domain/model/verification/composite_name_validator.dart new file mode 100644 index 000000000..f192a4bfd --- /dev/null +++ b/lib/domain/model/verification/composite_name_validator.dart @@ -0,0 +1,20 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; +import 'package:fluffychat/domain/model/verification/new_name_request.dart'; +import 'package:fluffychat/domain/model/verification/validator.dart'; +import 'package:fluffychat/domain/model/extensions/list_validator_extension.dart'; + +class CompositeNameValidator extends Validator { + final List _listValidator; + + CompositeNameValidator(this._listValidator); + + @override + Either validate(NewNameRequest value) { + return _listValidator.isNotEmpty + ? _listValidator.getValidatorNameViewState(value) + : Right(VerifyNameSuccessViewState()); + } +} diff --git a/lib/domain/model/verification/empty_name_validator.dart b/lib/domain/model/verification/empty_name_validator.dart new file mode 100644 index 000000000..5cfbcd596 --- /dev/null +++ b/lib/domain/model/verification/empty_name_validator.dart @@ -0,0 +1,20 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; +import 'package:fluffychat/domain/exception/verify_name_exception.dart'; +import 'package:fluffychat/domain/model/verification/new_name_request.dart'; +import 'package:fluffychat/domain/model/verification/validator.dart'; + +class EmptyNameValidator extends Validator { + @override + Either validate(NewNameRequest value) { + if (value.value == null || value.value!.isEmpty) { + return const Left( + VerifyNameFailure(EmptyNameException()), + ); + } else { + return Right(VerifyNameSuccessViewState()); + } + } +} diff --git a/lib/domain/model/verification/name_with_space_only_validator.dart b/lib/domain/model/verification/name_with_space_only_validator.dart new file mode 100644 index 000000000..35bb41aa0 --- /dev/null +++ b/lib/domain/model/verification/name_with_space_only_validator.dart @@ -0,0 +1,22 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; +import 'package:fluffychat/domain/exception/verify_name_exception.dart'; +import 'package:fluffychat/domain/model/verification/new_name_request.dart'; +import 'package:fluffychat/domain/model/verification/validator.dart'; + +class NameWithSpaceOnlyValidator extends Validator { + @override + Either validate(NewNameRequest value) { + if (value.value != null && + value.value!.isNotEmpty && + value.value!.trim().isEmpty) { + return const Left( + VerifyNameFailure(NameWithSpaceOnlyException()), + ); + } else { + return Right(VerifyNameSuccessViewState()); + } + } +} diff --git a/lib/domain/model/verification/new_name_request.dart b/lib/domain/model/verification/new_name_request.dart new file mode 100644 index 000000000..51af99fef --- /dev/null +++ b/lib/domain/model/verification/new_name_request.dart @@ -0,0 +1,10 @@ +import 'package:equatable/equatable.dart'; + +class NewNameRequest with EquatableMixin { + final String? value; + + NewNameRequest(this.value); + + @override + List get props => [value]; +} diff --git a/lib/domain/model/verification/validator.dart b/lib/domain/model/verification/validator.dart new file mode 100644 index 000000000..266a63eab --- /dev/null +++ b/lib/domain/model/verification/validator.dart @@ -0,0 +1,7 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; + +abstract class Validator { + Either validate(T value); +} diff --git a/lib/domain/usecase/verify_name_interactor.dart b/lib/domain/usecase/verify_name_interactor.dart new file mode 100644 index 000000000..308ef83c7 --- /dev/null +++ b/lib/domain/usecase/verify_name_interactor.dart @@ -0,0 +1,21 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; +import 'package:fluffychat/domain/model/verification/composite_name_validator.dart'; +import 'package:fluffychat/domain/model/verification/new_name_request.dart'; +import 'package:fluffychat/domain/model/verification/validator.dart'; + +class VerifyNameInteractor { + Either execute( + String? newName, + List listValidator, + ) { + try { + return CompositeNameValidator(listValidator) + .validate(NewNameRequest(newName)); + } catch (exception) { + return Left(VerifyNameFailure(exception)); + } + } +} diff --git a/lib/pages/chat_details/chat_details_edit.dart b/lib/pages/chat_details/chat_details_edit.dart index 22319549d..05047421b 100644 --- a/lib/pages/chat_details/chat_details_edit.dart +++ b/lib/pages/chat_details/chat_details_edit.dart @@ -5,9 +5,14 @@ import 'package:fluffychat/di/global/get_it_initializer.dart'; import 'package:fluffychat/domain/app_state/room/update_group_chat_failure.dart'; import 'package:fluffychat/domain/app_state/room/update_group_chat_success.dart'; import 'package:fluffychat/domain/app_state/room/upload_content_state.dart'; +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; +import 'package:fluffychat/domain/model/extensions/validator_failure_extension.dart'; +import 'package:fluffychat/domain/model/verification/empty_name_validator.dart'; +import 'package:fluffychat/domain/model/verification/name_with_space_only_validator.dart'; import 'package:fluffychat/domain/usecase/room/update_group_chat_interactor.dart'; import 'package:fluffychat/domain/usecase/room/upload_content_for_web_interactor.dart'; import 'package:fluffychat/domain/usecase/room/upload_content_interactor.dart'; +import 'package:fluffychat/domain/usecase/verify_name_interactor.dart'; import 'package:fluffychat/pages/chat_details/chat_details_edit_context_menu_actions.dart'; import 'package:fluffychat/pages/chat_details/chat_details_edit_view.dart'; import 'package:fluffychat/pages/chat_details/chat_details_edit_view_style.dart'; @@ -54,6 +59,7 @@ class ChatDetailsEditController extends State final uploadContentInteractor = getIt.get(); final uploadContentWebInteractor = getIt.get(); + final verifyNameInteractor = getIt.get(); Room? room; @@ -73,6 +79,7 @@ class ChatDetailsEditController extends State final MenuController menuController = MenuController(); final isEditedGroupInfoNotifier = ValueNotifier(false); + final isValidGroupNameNotifier = ValueNotifier(false); Client get client => Matrix.of(context).client; @@ -443,6 +450,22 @@ class ChatDetailsEditController extends State avatarAssetEntity = null; } + String? getErrorMessage(String content) { + if (content == room?.name) { + return null; + } + final result = verifyNameInteractor.execute( + content, + [EmptyNameValidator(), NameWithSpaceOnlyValidator()], + ); + + return result.fold( + (failure) => + failure is VerifyNameFailure ? failure.getMessage(context) : null, + (success) => null, + ); + } + @override void dispose() { _clearImageInMemory(); @@ -463,11 +486,12 @@ class ChatDetailsEditController extends State if (_isEditAvatar) { return; } - isEditedGroupInfoNotifier.value = groupNameTextEditingController.text != room?.name; groupNameEmptyNotifier.value = groupNameTextEditingController.text.isEmpty; + isValidGroupNameNotifier.value = + getErrorMessage(groupNameTextEditingController.text) == null; }); } diff --git a/lib/pages/chat_details/chat_details_edit_view.dart b/lib/pages/chat_details/chat_details_edit_view.dart index 1b5b04a27..63fe346df 100644 --- a/lib/pages/chat_details/chat_details_edit_view.dart +++ b/lib/pages/chat_details/chat_details_edit_view.dart @@ -67,23 +67,28 @@ class ChatDetailsEditView extends StatelessWidget { ), const Spacer(), ValueListenableBuilder( - valueListenable: controller.isEditedGroupInfoNotifier, - builder: (context, value, child) { - if (!value) { - return const SizedBox.shrink(); - } - return child!; + valueListenable: controller.isValidGroupNameNotifier, + builder: (context, isValid, child) { + return ValueListenableBuilder( + valueListenable: controller.isEditedGroupInfoNotifier, + builder: (context, value, child) { + if (!value || !isValid) { + return const SizedBox.shrink(); + } + return child!; + }, + child: Padding( + padding: ChatDetailEditViewStyle.doneIconPadding, + child: IconButton( + highlightColor: Colors.transparent, + hoverColor: Colors.transparent, + splashColor: Colors.transparent, + onPressed: () => controller.handleSaveAction(context), + icon: const Icon(Icons.done), + ), + ), + ); }, - child: Padding( - padding: ChatDetailEditViewStyle.doneIconPadding, - child: IconButton( - highlightColor: Colors.transparent, - hoverColor: Colors.transparent, - splashColor: Colors.transparent, - onPressed: () => controller.handleSaveAction(context), - icon: const Icon(Icons.done), - ), - ), ), ], ), @@ -323,40 +328,48 @@ class _GroupNameField extends StatelessWidget { Widget build(BuildContext context) { return Padding( padding: ChatDetailEditViewStyle.textFieldPadding, - child: TextField( - style: ChatDetailEditViewStyle.textFieldStyle(context), - controller: controller.groupNameTextEditingController, - contextMenuBuilder: mobileTwakeContextMenuBuilder, - focusNode: controller.groupNameFocusNode, - decoration: InputDecoration( - border: OutlineInputBorder( - borderSide: BorderSide(color: Theme.of(context).colorScheme.shadow), - ), - labelText: L10n.of(context)!.widgetName, - labelStyle: ChatDetailEditViewStyle.textFieldLabelStyle(context), - hintText: L10n.of(context)!.enterGroupName, - hintStyle: ChatDetailEditViewStyle.textFieldHintStyle(context), - contentPadding: ChatDetailEditViewStyle.contentPadding, - suffixIcon: ValueListenableBuilder( - valueListenable: controller.groupNameEmptyNotifier, - builder: (context, isGroupNameEmpty, child) { - if (isGroupNameEmpty) { - return child!; - } - - return IconButton( - onPressed: () => - controller.groupNameTextEditingController.clear(), - icon: Icon( - Icons.cancel_outlined, - size: ChatDetailEditViewStyle.clearIconSize, - color: Theme.of(context).colorScheme.onSurfaceVariant, - ), - ); - }, - child: const SizedBox.shrink(), - ), - ), + child: ValueListenableBuilder( + valueListenable: controller.isValidGroupNameNotifier, + builder: (context, value, _) { + return TextField( + style: ChatDetailEditViewStyle.textFieldStyle(context), + controller: controller.groupNameTextEditingController, + contextMenuBuilder: mobileTwakeContextMenuBuilder, + focusNode: controller.groupNameFocusNode, + decoration: InputDecoration( + border: OutlineInputBorder( + borderSide: + BorderSide(color: Theme.of(context).colorScheme.shadow), + ), + labelText: L10n.of(context)!.widgetName, + labelStyle: ChatDetailEditViewStyle.textFieldLabelStyle(context), + hintText: L10n.of(context)!.enterGroupName, + hintStyle: ChatDetailEditViewStyle.textFieldHintStyle(context), + contentPadding: ChatDetailEditViewStyle.contentPadding, + errorText: controller.getErrorMessage( + controller.groupNameTextEditingController.text, + ), + suffixIcon: ValueListenableBuilder( + valueListenable: controller.groupNameEmptyNotifier, + builder: (context, isGroupNameEmpty, child) { + if (isGroupNameEmpty) { + return child!; + } + return IconButton( + onPressed: () => + controller.groupNameTextEditingController.clear(), + icon: Icon( + Icons.cancel_outlined, + size: ChatDetailEditViewStyle.clearIconSize, + color: Theme.of(context).colorScheme.onSurfaceVariant, + ), + ); + }, + child: const SizedBox.shrink(), + ), + ), + ); + }, ), ); } diff --git a/lib/pages/new_group/new_group_chat_info.dart b/lib/pages/new_group/new_group_chat_info.dart index 714fcb587..ab5a8449e 100644 --- a/lib/pages/new_group/new_group_chat_info.dart +++ b/lib/pages/new_group/new_group_chat_info.dart @@ -5,6 +5,10 @@ import 'package:fluffychat/app_state/failure.dart'; import 'package:fluffychat/app_state/success.dart'; import 'package:fluffychat/domain/app_state/room/create_new_group_chat_state.dart'; import 'package:fluffychat/domain/app_state/room/upload_content_state.dart'; +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; +import 'package:fluffychat/domain/model/extensions/validator_failure_extension.dart'; +import 'package:fluffychat/domain/model/verification/name_with_space_only_validator.dart'; +import 'package:fluffychat/domain/usecase/verify_name_interactor.dart'; import 'package:fluffychat/pages/new_group/new_group_chat_info_view.dart'; import 'package:fluffychat/pages/new_group/new_group_info_controller.dart'; import 'package:fluffychat/presentation/mixins/common_media_picker_mixin.dart'; @@ -55,6 +59,7 @@ class NewGroupChatInfoController extends State final groupNameTextEditingController = TextEditingController(); final avatarAssetEntityNotifier = ValueNotifier(null); final avatarFilePickerNotifier = ValueNotifier(null); + VerifyNameInteractor verifyNameInteractor = getIt.get(); final groupNameFocusNode = FocusNode(); StreamSubscription? createNewGroupChatInteractorStreamSubscription; @@ -311,6 +316,20 @@ class NewGroupChatInfoController extends State ); } + String? getErrorMessage(String content) { + return verifyNameInteractor + .execute(content, [NameWithSpaceOnlyValidator()]).fold( + (failure) { + if (failure is VerifyNameFailure) { + return failure.getMessage(context); + } else { + return null; + } + }, + (success) => null, + ); + } + @override void initState() { listenGroupNameChanged(); diff --git a/lib/pages/new_group/new_group_chat_info_view.dart b/lib/pages/new_group/new_group_chat_info_view.dart index 0965f6243..b32dbdb95 100644 --- a/lib/pages/new_group/new_group_chat_info_view.dart +++ b/lib/pages/new_group/new_group_chat_info_view.dart @@ -201,26 +201,43 @@ class NewGroupChatInfoView extends StatelessWidget { child: ValueListenableBuilder( valueListenable: newGroupInfoController.createRoomStateNotifier, builder: (context, value, child) { - return TextField( - controller: newGroupInfoController.groupNameTextEditingController, - focusNode: newGroupInfoController.groupNameFocusNode, - enabled: !newGroupInfoController.isCreatingRoom, - decoration: InputDecoration( - border: OutlineInputBorder( - borderSide: - BorderSide(color: Theme.of(context).colorScheme.shadow), - ), - labelText: L10n.of(context)!.widgetName, - labelStyle: Theme.of(context).textTheme.bodySmall?.copyWith( - color: Theme.of(context).colorScheme.onSurface, + return ValueListenableBuilder( + valueListenable: + newGroupInfoController.groupNameTextEditingController, + builder: (context, value, _) { + return TextField( + controller: + newGroupInfoController.groupNameTextEditingController, + focusNode: newGroupInfoController.groupNameFocusNode, + enabled: !newGroupInfoController.isCreatingRoom, + decoration: InputDecoration( + errorBorder: OutlineInputBorder( + borderSide: BorderSide( + color: LinagoraSysColors.material().error, + ), ), - hintText: L10n.of(context)!.enterGroupName, - hintStyle: Theme.of(context).textTheme.bodyLarge?.copyWith( - color: LinagoraRefColors.material().neutral[60], + border: OutlineInputBorder( + borderSide: + BorderSide(color: Theme.of(context).colorScheme.shadow), ), - contentPadding: NewGroupChatInfoStyle.contentPadding, - ), - contextMenuBuilder: mobileTwakeContextMenuBuilder, + errorText: newGroupInfoController.getErrorMessage( + newGroupInfoController.groupNameTextEditingController.text, + ), + errorStyle: + TextStyle(color: LinagoraSysColors.material().error), + labelText: L10n.of(context)!.widgetName, + labelStyle: Theme.of(context).textTheme.bodySmall?.copyWith( + color: Theme.of(context).colorScheme.onSurface, + ), + hintText: L10n.of(context)!.enterGroupName, + hintStyle: Theme.of(context).textTheme.bodyLarge?.copyWith( + color: LinagoraRefColors.material().neutral[60], + ), + contentPadding: NewGroupChatInfoStyle.contentPadding, + ), + contextMenuBuilder: mobileTwakeContextMenuBuilder, + ); + }, ); }, ), diff --git a/lib/pages/new_group/new_group_info_controller.dart b/lib/pages/new_group/new_group_info_controller.dart index d340be6bd..8464d2ba1 100644 --- a/lib/pages/new_group/new_group_info_controller.dart +++ b/lib/pages/new_group/new_group_info_controller.dart @@ -10,7 +10,8 @@ extension NewGroupInfoControllerExtension on NewGroupChatInfoController { groupNameTextEditingController.addListener(() { groupName = groupNameTextEditingController.text; haveGroupNameNotifier.value = - groupNameTextEditingController.text.isNotEmpty; + groupNameTextEditingController.text.isNotEmpty && + getErrorMessage(groupNameTextEditingController.text) == null; }); } diff --git a/test/domain/model/verification/composite_name_validator_test.dart b/test/domain/model/verification/composite_name_validator_test.dart new file mode 100644 index 000000000..30ad3caa5 --- /dev/null +++ b/test/domain/model/verification/composite_name_validator_test.dart @@ -0,0 +1,73 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/domain/model/verification/composite_name_validator.dart'; +import 'package:fluffychat/domain/model/verification/validator.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/annotations.dart'; +import 'package:mockito/mockito.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; +import 'package:fluffychat/domain/model/verification/new_name_request.dart'; +import 'composite_name_validator_test.mocks.dart'; + +@GenerateMocks([Validator]) +void main() { + group('CompositeNameValidator', () { + late CompositeNameValidator compositeValidator; + late MockValidator mockValidator1; + late MockValidator mockValidator2; + setUp(() { + compositeValidator = CompositeNameValidator([]); + mockValidator1 = MockValidator(); + mockValidator2 = MockValidator(); + }); + test('returns VerifyNameViewState when list of validators is empty', () { + final newNameRequest = NewNameRequest('ValidName'); + + final result = compositeValidator.validate(newNameRequest); + + expect(result, isA()); + expect(result, Right(VerifyNameSuccessViewState())); + }); + + test( + 'applies multiple validators and returns failure if any validator fails', + () { + final newNameRequest = NewNameRequest('InvalidName'); + + when(mockValidator1.validate(newNameRequest)) + .thenReturn(Right(VerifyNameSuccessViewState())); + when(mockValidator2.validate(newNameRequest)) + .thenReturn(const Left(VerifyNameFailure("Invalid name"))); + + final compositeValidator = + CompositeNameValidator([mockValidator1, mockValidator2]); + + final result = compositeValidator.validate(newNameRequest); + + expect(result, isA()); + verify(mockValidator1.validate(newNameRequest)).called(1); + verify(mockValidator2.validate(newNameRequest)).called(1); + }); + + test('returns success if all validators pass', () { + final mockValidator1 = MockValidator(); + final mockValidator2 = MockValidator(); + final newNameRequest = NewNameRequest('ValidName'); + + when(mockValidator1.validate(newNameRequest)) + .thenReturn(Right(VerifyNameSuccessViewState())); + when(mockValidator2.validate(newNameRequest)) + .thenReturn(Right(VerifyNameSuccessViewState())); + + final compositeValidator = + CompositeNameValidator([mockValidator1, mockValidator2]); + + final result = compositeValidator.validate(newNameRequest); + + expect(result, isA()); + verify(mockValidator1.validate(newNameRequest)).called(1); + verify(mockValidator2.validate(newNameRequest)).called(1); + }); + }); +} diff --git a/test/domain/usecase/verify_name_interactor_test.dart b/test/domain/usecase/verify_name_interactor_test.dart new file mode 100644 index 000000000..04f0272f1 --- /dev/null +++ b/test/domain/usecase/verify_name_interactor_test.dart @@ -0,0 +1,72 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/domain/usecase/verify_name_interactor.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/mockito.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; + +import '../model/verification/composite_name_validator_test.mocks.dart'; + +void main() { + group('VerifyNameInteractor', () { + late MockValidator mockValidator1; + late MockValidator mockValidator2; + late VerifyNameInteractor interactor; + setUp(() { + mockValidator1 = MockValidator(); + mockValidator2 = MockValidator(); + interactor = VerifyNameInteractor(); + }); + test('returns VerifyNameViewState success when all validators pass', () { + const newName = 'ValidName'; + + when(mockValidator1.validate(any)) + .thenReturn(Right(VerifyNameSuccessViewState())); + when(mockValidator2.validate(any)) + .thenReturn(Right(VerifyNameSuccessViewState())); + + final result = + interactor.execute(newName, [mockValidator1, mockValidator2]); + + expect(result, isA()); + expect(result, Right(VerifyNameSuccessViewState())); + verify(mockValidator1.validate(any)).called(1); + verify(mockValidator2.validate(any)).called(1); + }); + + test('returns failure when one of the validators fails', () { + final mockValidator1 = MockValidator(); + final mockValidator2 = MockValidator(); + final interactor = VerifyNameInteractor(); + const newName = 'InvalidName'; + + when(mockValidator1.validate(any)) + .thenReturn(Right(VerifyNameSuccessViewState())); + when(mockValidator2.validate(any)) + .thenReturn(const Left(VerifyNameFailure('Name is not valid'))); + + final result = + interactor.execute(newName, [mockValidator1, mockValidator2]); + + expect(result, isA()); + verify(mockValidator1.validate(any)).called(1); + verify(mockValidator2.validate(any)).called(1); + }); + + test('returns VerifyNameFailure when an exception occurs', () { + final mockValidator = MockValidator(); + final interactor = VerifyNameInteractor(); + const newName = 'ValidName'; + + // Mock a validator to throw an exception + when(mockValidator.validate(any)) + .thenThrow(Exception('Validation failed')); + + final result = interactor.execute(newName, [mockValidator]); + + expect(result, isA()); + expect((result as Left).value, isA()); + }); + }); +}