From 420799b1f3ec6a891401ae1ed11030caa42a905a Mon Sep 17 00:00:00 2001 From: dab246 Date: Mon, 5 Aug 2024 17:09:05 +0700 Subject: [PATCH] TF-3023 Write widget test for SMimeSignatureStatus icon --- .../presentation_email_extension.dart | 5 +- .../utils/smime_signature_constant.dart | 5 + ...formation_sender_and_receiver_builder.dart | 3 +- ...nder_and_receiver_builder_widget_test.dart | 277 ++++++++++++++++++ 4 files changed, 287 insertions(+), 3 deletions(-) create mode 100644 lib/features/email/presentation/utils/smime_signature_constant.dart create mode 100644 test/features/email/presentation/information_sender_and_receiver_builder_widget_test.dart diff --git a/lib/features/email/presentation/extensions/presentation_email_extension.dart b/lib/features/email/presentation/extensions/presentation_email_extension.dart index 4dd642f5de..d1385cd9b8 100644 --- a/lib/features/email/presentation/extensions/presentation_email_extension.dart +++ b/lib/features/email/presentation/extensions/presentation_email_extension.dart @@ -2,14 +2,15 @@ import 'package:model/email/presentation_email.dart'; import 'package:model/extensions/list_email_header_extension.dart'; import 'package:tmail_ui_user/features/email/presentation/model/smime_signature_status.dart'; +import 'package:tmail_ui_user/features/email/presentation/utils/smime_signature_constant.dart'; extension PresentationEmailExtension on PresentationEmail { SMimeSignatureStatus get sMimeStatus { final status = emailHeader?.toSet().sMimeStatus; - if (status == 'Good signature') { + if (status == SMimeSignatureConstant.GOOD_SIGNATURE) { return SMimeSignatureStatus.goodSignature; - } else if (status == 'Bad signature') { + } else if (status == SMimeSignatureConstant.BAD_SIGNATURE) { return SMimeSignatureStatus.badSignature; } else { return SMimeSignatureStatus.notSigned; diff --git a/lib/features/email/presentation/utils/smime_signature_constant.dart b/lib/features/email/presentation/utils/smime_signature_constant.dart new file mode 100644 index 0000000000..167718e752 --- /dev/null +++ b/lib/features/email/presentation/utils/smime_signature_constant.dart @@ -0,0 +1,5 @@ + +class SMimeSignatureConstant { + static const String GOOD_SIGNATURE = 'Good signature'; + static const String BAD_SIGNATURE = 'Bad signature'; +} \ No newline at end of file diff --git a/lib/features/email/presentation/widgets/information_sender_and_receiver_builder.dart b/lib/features/email/presentation/widgets/information_sender_and_receiver_builder.dart index c9f164bfa8..592cd1bce8 100644 --- a/lib/features/email/presentation/widgets/information_sender_and_receiver_builder.dart +++ b/lib/features/email/presentation/widgets/information_sender_and_receiver_builder.dart @@ -32,7 +32,7 @@ class InformationSenderAndReceiverBuilder extends StatelessWidget { required this.emailSelected, required this.responsiveUtils, required this.imagePaths, - required this.emailUnsubscribe, + this.emailUnsubscribe, this.maxBodyHeight, this.openEmailAddressDetailAction, this.onEmailActionClick, @@ -66,6 +66,7 @@ class InformationSenderAndReceiverBuilder extends StatelessWidget { )), if (emailSelected.sMimeStatus != SMimeSignatureStatus.notSigned) Tooltip( + key: const Key('smime_signature_status_icon'), message: emailSelected.sMimeStatus.getTooltipMessage(context), child: MouseRegion( cursor: SystemMouseCursors.click, diff --git a/test/features/email/presentation/information_sender_and_receiver_builder_widget_test.dart b/test/features/email/presentation/information_sender_and_receiver_builder_widget_test.dart new file mode 100644 index 0000000000..60d358c669 --- /dev/null +++ b/test/features/email/presentation/information_sender_and_receiver_builder_widget_test.dart @@ -0,0 +1,277 @@ +import 'package:core/presentation/resources/image_paths.dart'; +import 'package:core/presentation/utils/responsive_utils.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_localizations/flutter_localizations.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:get/get.dart'; +import 'package:jmap_dart_client/jmap/core/id.dart'; +import 'package:jmap_dart_client/jmap/mail/email/email.dart'; +import 'package:jmap_dart_client/jmap/mail/email/email_address.dart'; +import 'package:jmap_dart_client/jmap/mail/email/email_header.dart'; +import 'package:model/email/email_property.dart'; +import 'package:model/email/presentation_email.dart'; +import 'package:tmail_ui_user/features/email/presentation/widgets/information_sender_and_receiver_builder.dart'; +import 'package:tmail_ui_user/main/localizations/app_localizations_delegate.dart'; +import 'package:tmail_ui_user/main/localizations/localization_service.dart'; + +void main() { + group('InformationSenderAndReceiverBuilder::widgetTest', () { + final responsiveUtils = ResponsiveUtils(); + final imagePaths = ImagePaths(); + + Widget makeTestableWidget({required Widget child}) { + return GetMaterialApp( + localizationsDelegates: const [ + AppLocalizationsDelegate(), + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + ], + supportedLocales: LocalizationService.supportedLocales, + locale: LocalizationService.defaultLocale, + home: Scaffold(body: child), + ); + } + + group('SMimeSignatureStatusIcon::test', () { + testWidgets('should be displayed when email header has X-SMIME-Status', (tester) async { + final presentationEmail = PresentationEmail( + id: EmailId(Id('a123')), + from: { + EmailAddress('example', 'example@linagora.com') + }, + emailHeader: [ + EmailHeader(EmailProperty.headerSMimeStatusKey, 'Good signature') + ] + ); + final widget = makeTestableWidget( + child: InformationSenderAndReceiverBuilder( + emailSelected: presentationEmail, + responsiveUtils: responsiveUtils, + imagePaths: imagePaths, + emailUnsubscribe: null, + ), + ); + + await tester.pumpWidget(widget); + + await tester.pumpAndSettle(); + + expect( + find.byKey(const Key('smime_signature_status_icon')), + findsOneWidget); + }); + + testWidgets( + 'should be displayed and have good message \n' + 'when email header has X-SMIME-Status = "Good signature"', + (tester) async { + final presentationEmail = PresentationEmail( + id: EmailId(Id('a123')), + from: { + EmailAddress('example', 'example@linagora.com') + }, + emailHeader: [ + EmailHeader(EmailProperty.headerSMimeStatusKey, 'Good signature') + ] + ); + final widget = makeTestableWidget( + child: InformationSenderAndReceiverBuilder( + emailSelected: presentationEmail, + responsiveUtils: responsiveUtils, + imagePaths: imagePaths, + emailUnsubscribe: null, + ), + ); + + await tester.pumpWidget(widget); + + await tester.pumpAndSettle(); + + expect( + find.byKey(const Key('smime_signature_status_icon')), + findsOneWidget); + + final sMimeSignatureStatusIconWidgetFinder = find.byKey(const Key('smime_signature_status_icon')); + final sMimeSignatureStatusIconWidget = tester.widget(sMimeSignatureStatusIconWidgetFinder); + expect( + sMimeSignatureStatusIconWidget.message, + 'The authenticity of this message had been verified with SMime signature.'); + }); + + testWidgets( + 'should be displayed and have bad message \n' + 'when email header has X-SMIME-Status = "Bad signature"', + (tester) async { + final presentationEmail = PresentationEmail( + id: EmailId(Id('a123')), + from: { + EmailAddress('example', 'example@linagora.com') + }, + emailHeader: [ + EmailHeader(EmailProperty.headerSMimeStatusKey, 'Bad signature') + ] + ); + final widget = makeTestableWidget( + child: InformationSenderAndReceiverBuilder( + emailSelected: presentationEmail, + responsiveUtils: responsiveUtils, + imagePaths: imagePaths, + emailUnsubscribe: null, + ), + ); + + await tester.pumpWidget(widget); + + await tester.pumpAndSettle(); + + expect( + find.byKey(const Key('smime_signature_status_icon')), + findsOneWidget); + + final sMimeSignatureStatusIconWidgetFinder = find.byKey(const Key('smime_signature_status_icon')); + final sMimeSignatureStatusIconWidget = tester.widget(sMimeSignatureStatusIconWidgetFinder); + expect( + sMimeSignatureStatusIconWidget.message, + 'This message failed SMime signature verification.'); + }); + + testWidgets('should not be displayed when email header do not have X-SMIME-Status', (tester) async { + final presentationEmail = PresentationEmail( + id: EmailId(Id('a123')), + from: { + EmailAddress('example', 'example@linagora.com') + } + ); + final widget = makeTestableWidget( + child: InformationSenderAndReceiverBuilder( + emailSelected: presentationEmail, + responsiveUtils: responsiveUtils, + imagePaths: imagePaths, + emailUnsubscribe: null, + ), + ); + + await tester.pumpWidget(widget); + + await tester.pumpAndSettle(); + + expect( + find.byKey(const Key('smime_signature_status_icon')), + findsNothing); + }); + + testWidgets('should not be displayed when email header have X-SMIME-Status = "Good Signatures"', (tester) async { + final presentationEmail = PresentationEmail( + id: EmailId(Id('a123')), + from: { + EmailAddress('example', 'example@linagora.com') + }, + emailHeader: [ + EmailHeader(EmailProperty.headerSMimeStatusKey, 'Good Signatures') + ] + ); + final widget = makeTestableWidget( + child: InformationSenderAndReceiverBuilder( + emailSelected: presentationEmail, + responsiveUtils: responsiveUtils, + imagePaths: imagePaths, + emailUnsubscribe: null, + ), + ); + + await tester.pumpWidget(widget); + + await tester.pumpAndSettle(); + + expect( + find.byKey(const Key('smime_signature_status_icon')), + findsNothing); + }); + + testWidgets('should not be displayed when email header have X-SMIME-Status = "Good signatures"', (tester) async { + final presentationEmail = PresentationEmail( + id: EmailId(Id('a123')), + from: { + EmailAddress('example', 'example@linagora.com') + }, + emailHeader: [ + EmailHeader(EmailProperty.headerSMimeStatusKey, 'Good signatures') + ] + ); + final widget = makeTestableWidget( + child: InformationSenderAndReceiverBuilder( + emailSelected: presentationEmail, + responsiveUtils: responsiveUtils, + imagePaths: imagePaths, + emailUnsubscribe: null, + ), + ); + + await tester.pumpWidget(widget); + + await tester.pumpAndSettle(); + + expect( + find.byKey(const Key('smime_signature_status_icon')), + findsNothing); + }); + + testWidgets('should not be displayed when email header have X-SMIME-Status = "Bad Signatures"', (tester) async { + final presentationEmail = PresentationEmail( + id: EmailId(Id('a123')), + from: { + EmailAddress('example', 'example@linagora.com') + }, + emailHeader: [ + EmailHeader(EmailProperty.headerSMimeStatusKey, 'Bad Signatures') + ] + ); + final widget = makeTestableWidget( + child: InformationSenderAndReceiverBuilder( + emailSelected: presentationEmail, + responsiveUtils: responsiveUtils, + imagePaths: imagePaths, + emailUnsubscribe: null, + ), + ); + + await tester.pumpWidget(widget); + + await tester.pumpAndSettle(); + + expect( + find.byKey(const Key('smime_signature_status_icon')), + findsNothing); + }); + + testWidgets('should not be displayed when email header have X-SMIME-Status = "Bad signatures"', (tester) async { + final presentationEmail = PresentationEmail( + id: EmailId(Id('a123')), + from: { + EmailAddress('example', 'example@linagora.com') + }, + emailHeader: [ + EmailHeader(EmailProperty.headerSMimeStatusKey, 'Bad signatures') + ] + ); + final widget = makeTestableWidget( + child: InformationSenderAndReceiverBuilder( + emailSelected: presentationEmail, + responsiveUtils: responsiveUtils, + imagePaths: imagePaths, + emailUnsubscribe: null, + ), + ); + + await tester.pumpWidget(widget); + + await tester.pumpAndSettle(); + + expect( + find.byKey(const Key('smime_signature_status_icon')), + findsNothing); + }); + }); + }); +} \ No newline at end of file