games_app/prediqo_mobile/test/totp_section_load_test.dart

240 lines
6.9 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:prediqo_mobile/core/config/app_config.dart';
import 'package:prediqo_mobile/core/di/injection.dart';
import 'package:prediqo_mobile/core/theme/app_theme.dart';
import 'package:prediqo_mobile/data/repositories/auth_repository_impl.dart';
import 'package:prediqo_mobile/domain/repositories/repositories.dart';
import 'package:prediqo_mobile/features/auth/bloc/auth_bloc.dart';
import 'package:prediqo_mobile/features/auth/presentation/totp_setup_screen.dart';
import 'package:prediqo_mobile/l10n/locale_controller.dart';
import 'package:prediqo_mobile/shared/widgets/pred_skeleton.dart';
import 'package:prediqo_mobile/shared/widgets/section_error.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'support/e2e_fixtures.dart';
class _ControllableAuth implements AuthRepository {
Completer<UserProfile>? hold;
UserProfile? cached;
var getProfileCalls = 0;
Object? failWith;
_ControllableAuth({this.cached});
@override
UserProfile? peekLastProfile() => cached;
@override
void invalidateProfile() => cached = null;
@override
Future<UserProfile> getProfile({bool force = false}) async {
getProfileCalls++;
if (failWith != null) throw failWith!;
if (hold != null) return hold!.future;
return cached ??
const UserProfile(
email: 'ada@prediqo.test',
countryOfResidence: 'FR',
gender: 'female',
totpEnabled: false,
);
}
@override
List<CountryOption>? peekLastCountries() => null;
@override
void invalidateCountries() {}
@override
Future<List<CountryOption>> getCountries({bool force = false}) async =>
const [CountryOption(code: 'FR', name: 'France')];
@override
Future<LoginResult> login(
String email,
String password, {
String? totpCode,
String? captchaToken,
}) async =>
throw UnimplementedError();
@override
Future<RegisterResult> register(RegisterRequest request) async =>
throw UnimplementedError();
@override
Future<({bool valid, String? reason})> validateReferralCode(String code) async =>
(valid: true, reason: null);
@override
Future<void> resendVerificationEmail(String email) async {}
@override
Future<void> logout() async {}
@override
Future<bool> isLoggedIn() async => true;
@override
Future<({String? userId, String? role})> getStoredUser() async =>
(userId: 'u1', role: 'user');
@override
Future<Map<String, dynamic>> setup2FA({String? password, String? totpCode}) async =>
{'qrCodeUrl': 'data:image/png;base64,', 'secret': 'TEST'};
@override
Future<List<String>> enable2FA(String code) async => const [];
@override
Future<void> disable2FA({required String password, required String code}) async {}
@override
Future<List<String>> regenerateRecoveryCodes(String totpCode) async => const [];
@override
Future<void> updateProfile({
String? firstName,
String? middleName,
String? lastName,
String? dateOfBirth,
required String email,
String? phone,
required String countryOfResidence,
String? nationality,
required String gender,
}) async {}
@override
Future<void> changePassword({
required String currentPassword,
required String newPassword,
}) async {}
@override
Future<void> forgotPassword(String email) async {}
}
Future<void> _boot(AuthRepository auth) async {
configureE2eTestEnvironment();
if (sl.isRegistered<AppConfig>()) await sl.reset();
sl.registerSingleton<AppConfig>(
const AppConfig(apiBaseUrl: 'http://localhost', isProduction: false, wsEnabled: false),
);
final locale = LocaleController();
SharedPreferences.setMockInitialValues({'app_locale': 'en'});
await locale.load();
sl.registerSingleton<LocaleController>(locale);
sl.registerLazySingleton<AuthRepository>(() => auth);
}
Widget _wrap(AuthRepository auth) {
return BlocProvider(
create: (_) => AuthBloc(auth),
child: MaterialApp(
theme: AppTheme.dark,
home: const TotpSetupScreen(),
),
);
}
void main() {
tearDown(() async {
if (sl.isRegistered<AppConfig>()) await sl.reset();
});
testWidgets('TOTP peeks cached enabled status without full-page spinner', (tester) async {
final hold = Completer<UserProfile>();
final auth = _ControllableAuth(
cached: const UserProfile(
email: 'ada@prediqo.test',
countryOfResidence: 'FR',
gender: 'female',
totpEnabled: true,
),
)..hold = hold;
await _boot(auth);
await tester.pumpWidget(_wrap(auth));
await tester.pump();
expect(find.textContaining('2FA is enabled'), findsOneWidget);
expect(find.byType(PredSkeletonCard), findsNothing);
expect(find.byType(CircularProgressIndicator), findsNothing);
hold.complete(auth.cached!);
await tester.pumpAndSettle();
expect(find.textContaining('2FA is enabled'), findsOneWidget);
});
testWidgets('TOTP shows skeleton while profile loads without cache', (tester) async {
final hold = Completer<UserProfile>();
final auth = _ControllableAuth()..hold = hold;
await _boot(auth);
await tester.pumpWidget(_wrap(auth));
await tester.pump();
expect(find.byType(PredSkeletonCard), findsWidgets);
hold.complete(const UserProfile(
email: 'ada@prediqo.test',
countryOfResidence: 'FR',
gender: 'female',
totpEnabled: false,
));
await tester.pumpAndSettle();
expect(find.text('Set up 2FA'), findsOneWidget);
expect(find.byType(PredSkeletonCard), findsNothing);
});
testWidgets('TOTP shows SectionError on profile failure', (tester) async {
final auth = _ControllableAuth()..failWith = Exception('down');
await _boot(auth);
await tester.pumpWidget(_wrap(auth));
await tester.pumpAndSettle();
expect(find.byType(SectionError), findsOneWidget);
});
testWidgets('TOTP retry does not launch parallel duplicate fetches', (tester) async {
final auth = _ControllableAuth()..failWith = Exception('down');
await _boot(auth);
await tester.pumpWidget(_wrap(auth));
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
expect(find.byType(SectionError), findsOneWidget);
final before = auth.getProfileCalls;
auth.failWith = null;
auth.hold = Completer<UserProfile>();
final retry = find.byKey(const Key('section_error_retry'));
expect(retry, findsOneWidget);
await tester.ensureVisible(retry);
await tester.tap(retry);
await tester.pump();
if (retry.evaluate().isNotEmpty) {
await tester.tap(retry);
await tester.pump();
}
expect(auth.getProfileCalls, before + 1);
auth.hold!.complete(const UserProfile(
email: 'ada@prediqo.test',
countryOfResidence: 'FR',
gender: 'female',
totpEnabled: false,
));
await tester.pumpAndSettle();
expect(find.text('Set up 2FA'), findsOneWidget);
});
}