From 2780988cc6f8b08f1a1686400cae587c29a55842 Mon Sep 17 00:00:00 2001 From: Naif-Deriv Date: Thu, 20 Jun 2024 13:21:01 +0400 Subject: [PATCH] fix review comments. --- .../service/auth_tracking_mixin.dart | 5 ++-- .../features/auth/cubit/deriv_auth_cubit.dart | 5 +++- .../auth/services/base_auth_service.dart | 23 +----------------- .../auth/services/deriv_auth_service.dart | 24 +++++++++++++++---- .../layouts/deriv_login_layout.dart | 8 +++++-- packages/deriv_auth/pubspec.yaml | 6 +++++ .../auth/cubit/deriv_auth_cubit_test.dart | 19 +++++++++++++++ .../deriv_get_started_layout_test.dart | 23 ++++++++++++++++++ .../layouts/deriv_login_layout_test.dart | 18 ++++++++++++++ .../layouts/deriv_signup_layout_test.dart | 18 ++++++++++++++ 10 files changed, 118 insertions(+), 31 deletions(-) diff --git a/packages/deriv_auth/lib/core/analytics/service/auth_tracking_mixin.dart b/packages/deriv_auth/lib/core/analytics/service/auth_tracking_mixin.dart index 91da1abd4..5c5c931de 100644 --- a/packages/deriv_auth/lib/core/analytics/service/auth_tracking_mixin.dart +++ b/packages/deriv_auth/lib/core/analytics/service/auth_tracking_mixin.dart @@ -1,9 +1,10 @@ import 'package:deriv_auth/deriv_auth.dart'; import 'package:deriv_auth/core/analytics/data/auth_tracking_repository.dart'; -/// +/// Mixin for tracking user authentication actions. mixin AuthTrackingMixin { - final AuthTrackingRepository _repository = AuthTrackingRepository.instance; + late final AuthTrackingRepository _repository = + AuthTrackingRepository.instance; /// Track user opened login form. void trackUserOpenedLoginForm() => _repository.trackUserOpenedLoginForm(); diff --git a/packages/deriv_auth/lib/features/auth/cubit/deriv_auth_cubit.dart b/packages/deriv_auth/lib/features/auth/cubit/deriv_auth_cubit.dart index ba7b143b8..9fb83959a 100644 --- a/packages/deriv_auth/lib/features/auth/cubit/deriv_auth_cubit.dart +++ b/packages/deriv_auth/lib/features/auth/cubit/deriv_auth_cubit.dart @@ -12,6 +12,7 @@ import 'package:deriv_auth/core/services/token/models/enums.dart'; import 'package:deriv_auth/core/services/token/models/login_request.dart'; import 'package:deriv_auth/features/auth/deriv_auth_io.dart'; import 'package:deriv_auth/features/auth/services/base_auth_service.dart'; +import 'package:deriv_auth/features/auth/services/deriv_auth_service.dart'; import 'package:deriv_auth/features/social_auth/models/social_auth_dto.dart'; part 'deriv_auth_state.dart'; @@ -26,7 +27,9 @@ class DerivAuthCubit extends Cubit required this.authService, }) : super(DerivAuthLoadingState()) { AuthTrackingRepository.init( - authService.connectionInfo.appId, + authService is DerivAuthService + ? (authService as DerivAuthService).connectionInfo.appId + : (throw Exception('Connection Info is not provided.')), derivRudderstack: DerivRudderstack(), ); } diff --git a/packages/deriv_auth/lib/features/auth/services/base_auth_service.dart b/packages/deriv_auth/lib/features/auth/services/base_auth_service.dart index 480031ac7..184acc869 100644 --- a/packages/deriv_auth/lib/features/auth/services/base_auth_service.dart +++ b/packages/deriv_auth/lib/features/auth/services/base_auth_service.dart @@ -1,33 +1,12 @@ -import 'package:deriv_auth/core/connection_info.dart'; import 'package:deriv_auth/core/models/account_model.dart'; import 'package:deriv_auth/core/models/authorize_model.dart'; import 'package:deriv_auth/core/models/landig_comany_model.dart'; -import 'package:deriv_auth/core/services/jwt/services/base_jwt_service.dart'; import 'package:deriv_auth/core/services/token/models/login_request.dart'; -import 'package:deriv_auth/core/services/token/services/base_token_service.dart'; -import 'package:deriv_auth/features/auth/repository/base_auth_repository.dart'; /// Interface to define all authentication-related functionality. abstract class BaseAuthService { /// Constructor for [BaseAuthService]. - BaseAuthService({ - required this.authRepository, - required this.jwtService, - required this.connectionInfo, - required this.tokenService, - }); - - /// Client connection info. - final AuthConnectionInfo connectionInfo; - - /// Interface for all jwtRelated functions. - final BaseJwtService jwtService; - - /// Interface of all client related functions. - final BaseAuthRepository authRepository; - - /// Token service. - final BaseTokenService tokenService; + BaseAuthService(); /// Function before logging user in. Future onLoginRequest({ diff --git a/packages/deriv_auth/lib/features/auth/services/deriv_auth_service.dart b/packages/deriv_auth/lib/features/auth/services/deriv_auth_service.dart index 473e88b54..1a0d6c697 100644 --- a/packages/deriv_auth/lib/features/auth/services/deriv_auth_service.dart +++ b/packages/deriv_auth/lib/features/auth/services/deriv_auth_service.dart @@ -1,4 +1,5 @@ import 'package:collection/collection.dart'; +import 'package:deriv_auth/core/connection_info.dart'; import 'package:deriv_auth/core/constants/constants.dart'; import 'package:deriv_auth/core/exceptions/deriv_auth_exception.dart'; @@ -7,8 +8,11 @@ import 'package:deriv_auth/core/models/account_model.dart'; import 'package:deriv_auth/core/models/auth_error/auth_error.dart'; import 'package:deriv_auth/core/models/authorize_model.dart'; import 'package:deriv_auth/core/models/landig_comany_model.dart'; +import 'package:deriv_auth/core/services/jwt/services/base_jwt_service.dart'; import 'package:deriv_auth/core/services/token/models/login_request.dart'; import 'package:deriv_auth/core/services/token/models/login_response.dart'; +import 'package:deriv_auth/core/services/token/services/base_token_service.dart'; +import 'package:deriv_auth/features/auth/repository/base_auth_repository.dart'; import 'package:deriv_http_client/deriv_http_client.dart'; import 'base_auth_service.dart'; @@ -17,12 +21,24 @@ import 'base_auth_service.dart'; class DerivAuthService extends BaseAuthService { /// Initializes a [DerivAuthService] class. DerivAuthService({ - required super.authRepository, - required super.jwtService, - required super.connectionInfo, - required super.tokenService, + required this.authRepository, + required this.jwtService, + required this.connectionInfo, + required this.tokenService, }); + /// Client connection info. + final AuthConnectionInfo connectionInfo; + + /// Interface for all jwtRelated functions. + final BaseJwtService jwtService; + + /// Interface of all client related functions. + final BaseAuthRepository authRepository; + + /// Token service. + final BaseTokenService tokenService; + @override Future onLoginRequest({ required GetTokensRequestModel request, diff --git a/packages/deriv_auth/lib/features/login/presentation/layouts/deriv_login_layout.dart b/packages/deriv_auth/lib/features/login/presentation/layouts/deriv_login_layout.dart index 9bf31f9a6..5ec252b9c 100644 --- a/packages/deriv_auth/lib/features/login/presentation/layouts/deriv_login_layout.dart +++ b/packages/deriv_auth/lib/features/login/presentation/layouts/deriv_login_layout.dart @@ -1,5 +1,6 @@ import 'dart:async'; +import 'package:deriv_auth/core/analytics/service/auth_tracking_mixin.dart'; import 'package:deriv_auth/deriv_auth.dart'; import 'package:deriv_passkeys/deriv_passkeys.dart'; import 'package:deriv_theme/deriv_theme.dart'; @@ -100,7 +101,8 @@ class DerivLoginLayout extends StatefulWidget { State createState() => _DerivLoginLayoutState(); } -class _DerivLoginLayoutState extends State { +class _DerivLoginLayoutState extends State + with AuthTrackingMixin { final GlobalKey _formKey = GlobalKey(); final TextEditingController _emailController = TextEditingController(); @@ -162,7 +164,9 @@ class _DerivLoginLayoutState extends State { if (widget.isSocialAuthEnabled) const SizedBox(height: ThemeProvider.margin24), widget.isPasskeysEnabled - ? const ContinueWithPasskeyButton() + ? ContinueWithPasskeyButton( + onTap: trackLoginWithPasskey, + ) : const SizedBox.shrink(), DerivSocialAuthPanel( socialAuthStateHandler: widget.socialAuthStateHandler, diff --git a/packages/deriv_auth/pubspec.yaml b/packages/deriv_auth/pubspec.yaml index 1f953e4b9..7fc7e2fd3 100644 --- a/packages/deriv_auth/pubspec.yaml +++ b/packages/deriv_auth/pubspec.yaml @@ -12,6 +12,12 @@ dependencies: flutter: sdk: flutter + analytics: + git: + url: git@github.com:regentmarkets/flutter-deriv-packages.git + path: packages/analytics + ref: analytics-v2.0.0 + deriv_theme: git: url: git@github.com:regentmarkets/flutter-deriv-packages.git diff --git a/packages/deriv_auth/test/features/auth/cubit/deriv_auth_cubit_test.dart b/packages/deriv_auth/test/features/auth/cubit/deriv_auth_cubit_test.dart index 73829497f..cffb70907 100644 --- a/packages/deriv_auth/test/features/auth/cubit/deriv_auth_cubit_test.dart +++ b/packages/deriv_auth/test/features/auth/cubit/deriv_auth_cubit_test.dart @@ -1,4 +1,6 @@ +import 'package:analytics/sdk/rudderstack/sdk/deriv_rudderstack_sdk.dart'; import 'package:bloc_test/bloc_test.dart'; +import 'package:deriv_auth/core/analytics/data/auth_tracking_repository.dart'; import 'package:deriv_auth/core/exceptions/deriv_auth_exception.dart'; import 'package:deriv_auth/core/models/account_model.dart'; import 'package:deriv_auth/core/models/auth_error/auth_error.dart'; @@ -16,11 +18,28 @@ import '../mocked_data/mocked_auth_models.dart'; class MockAuthService extends Mock implements BaseAuthService {} +class MockDerivRudderStack extends Mock implements DerivRudderstack {} + void main() { + late final MockDerivRudderStack mockDerivRudderstack; + late final DerivAuthCubit authCubit; late final MockAuthService service; setUpAll(() async { + mockDerivRudderstack = MockDerivRudderStack(); + + AuthTrackingRepository.init( + 'test', + derivRudderstack: mockDerivRudderstack, + ); + + when(() => mockDerivRudderstack.track( + eventName: any(named: 'eventName'), + properties: any(named: 'properties'))).thenAnswer( + (_) => Future.value(true), + ); + service = MockAuthService(); authCubit = DerivAuthCubit(authService: service); }); diff --git a/packages/deriv_auth/test/features/get_started/presentation/layouts/deriv_get_started_layout_test.dart b/packages/deriv_auth/test/features/get_started/presentation/layouts/deriv_get_started_layout_test.dart index 23aa26b08..3e9271705 100644 --- a/packages/deriv_auth/test/features/get_started/presentation/layouts/deriv_get_started_layout_test.dart +++ b/packages/deriv_auth/test/features/get_started/presentation/layouts/deriv_get_started_layout_test.dart @@ -1,6 +1,8 @@ // ignore_for_file: always_specify_types +import 'package:analytics/sdk/rudderstack/sdk/deriv_rudderstack_sdk.dart'; import 'package:bloc_test/bloc_test.dart'; +import 'package:deriv_auth/core/analytics/data/auth_tracking_repository.dart'; import 'package:deriv_auth/features/get_started/models/deriv_get_started_slide_model.dart'; import 'package:deriv_auth/features/get_started/presentation/layouts/deriv_get_started_layout.dart'; import 'package:deriv_language_selector/deriv_language_selector.dart'; @@ -14,6 +16,8 @@ import 'package:patrol_finders/patrol_finders.dart'; import '../../../../pump_app.dart'; +class MockDerivRudderStack extends Mock implements DerivRudderstack {} + class MockDerivGetStartedSlideModel extends Mock implements DerivGetStartedSlideModel {} @@ -22,6 +26,8 @@ class MockLanguageCubit extends MockCubit void main() { group('DerivGetStartedLayout', () { + late final MockDerivRudderStack mockDerivRudderstack; + late MockDerivGetStartedSlideModel mockSlideModel; const String appLogoIconPath = 'assets/icons/ic_logo_extended.svg'; @@ -31,6 +37,23 @@ void main() { setUpAll(() { mockSlideModel = MockDerivGetStartedSlideModel(); + + mockDerivRudderstack = MockDerivRudderStack(); + + AuthTrackingRepository.init( + 'test', + derivRudderstack: mockDerivRudderstack, + ); + + when(() => mockDerivRudderstack.track( + eventName: any(named: 'eventName'), + properties: any(named: 'properties'))).thenAnswer( + (_) => Future.value(true), + ); + + when(() => mockSlideModel.imagePath) + .thenReturn('assets/images/charts.svg'); + when(() => mockSlideModel.supportingText).thenReturn('Supporting text'); when(() => mockSlideModel.imagePath) .thenReturn('assets/images/charts.svg'); when(() => mockSlideModel.supportingText).thenReturn('Supporting text'); diff --git a/packages/deriv_auth/test/features/login/presentation/layouts/deriv_login_layout_test.dart b/packages/deriv_auth/test/features/login/presentation/layouts/deriv_login_layout_test.dart index 9d8ddba69..d1bedacdb 100644 --- a/packages/deriv_auth/test/features/login/presentation/layouts/deriv_login_layout_test.dart +++ b/packages/deriv_auth/test/features/login/presentation/layouts/deriv_login_layout_test.dart @@ -1,4 +1,6 @@ +import 'package:analytics/sdk/rudderstack/sdk/deriv_rudderstack_sdk.dart'; import 'package:bloc_test/bloc_test.dart'; +import 'package:deriv_auth/core/analytics/data/auth_tracking_repository.dart'; import 'package:deriv_auth/core/models/landig_comany_model.dart'; import 'package:deriv_auth/deriv_auth.dart'; import 'package:deriv_passkeys/deriv_passkeys.dart'; @@ -18,8 +20,11 @@ class MockDerivPasskeysBloc extends MockBloc implements DerivPasskeysBloc {} +class MockDerivRudderStackService extends Mock implements DerivRudderstack {} + void main() { group('DerivLoginLayout', () { + late MockDerivRudderStackService mockDerivRudderstack; late MockAuthCubit authCubit; late MockSocialAuthCubit socialAuthCubit; late MockDerivPasskeysBloc derivPasskeysBloc; @@ -27,10 +32,23 @@ void main() { const String welcomeLabel = 'Welcome Back'; setUpAll(() { + mockDerivRudderstack = MockDerivRudderStackService(); authCubit = MockAuthCubit(); socialAuthCubit = MockSocialAuthCubit(); derivPasskeysBloc = MockDerivPasskeysBloc(); + AuthTrackingRepository.init( + 'test', + derivRudderstack: mockDerivRudderstack, + ); + + when(() => mockDerivRudderstack.track( + eventName: any(named: 'eventName'), + properties: any(named: 'properties'), + )).thenAnswer( + (_) => Future.value(true), + ); + when(() => derivPasskeysBloc.state).thenReturn( DerivPasskeysInitializedState(), ); diff --git a/packages/deriv_auth/test/features/signup/presentation/layouts/deriv_signup_layout_test.dart b/packages/deriv_auth/test/features/signup/presentation/layouts/deriv_signup_layout_test.dart index e48555b03..bcd5aec8a 100644 --- a/packages/deriv_auth/test/features/signup/presentation/layouts/deriv_signup_layout_test.dart +++ b/packages/deriv_auth/test/features/signup/presentation/layouts/deriv_signup_layout_test.dart @@ -1,3 +1,5 @@ +import 'package:analytics/sdk/rudderstack/sdk/deriv_rudderstack_sdk.dart'; +import 'package:deriv_auth/core/analytics/data/auth_tracking_repository.dart'; import 'package:deriv_auth/deriv_auth.dart'; import 'package:deriv_ui/deriv_ui.dart'; import 'package:flutter/material.dart'; @@ -11,8 +13,11 @@ import '../../../../mocks.dart'; import '../../../../pump_app.dart'; import '../../../social_auth/mocks/mock_social_provider_model.dart'; +class MockDerivRudderStack extends Mock implements DerivRudderstack {} + void main() { group('DerivSignupLayout', () { + late final MockDerivRudderStack mockDerivRudderstack; late MockSignupCubit signupCubit; late MockAuthCubit authCubit; late MockSocialAuthCubit socialAuthCubit; @@ -25,6 +30,19 @@ void main() { authCubit = MockAuthCubit(); socialAuthCubit = MockSocialAuthCubit(); + mockDerivRudderstack = MockDerivRudderStack(); + + AuthTrackingRepository.init( + 'test', + derivRudderstack: mockDerivRudderstack, + ); + + when(() => mockDerivRudderstack.track( + eventName: any(named: 'eventName'), + properties: any(named: 'properties'))).thenAnswer( + (_) => Future.value(true), + ); + registerFallbackValue(SocialAuthProvider.google); when(() => signupCubit.state)