games_app/prediqo_mobile/test/auth_submit_polish_test.dart

189 lines
5.4 KiB
Dart

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/forgot_password_screen.dart';
import 'package:prediqo_mobile/features/auth/presentation/login_screen.dart';
import 'package:prediqo_mobile/l10n/locale_controller.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'support/e2e_fixtures.dart';
class _FakeAuthRepo implements AuthRepository {
var loginCalls = 0;
var forgotCalls = 0;
@override
Future<LoginResult> login(
String email,
String password, {
String? totpCode,
String? captchaToken,
}) async {
loginCalls++;
throw Exception('should not be called');
}
@override
Future<void> forgotPassword(String email) async {
forgotCalls++;
}
@override
Future<RegisterResult> register(RegisterRequest request) async {
throw UnimplementedError();
}
@override
Future<void> resendVerificationEmail(String email) async {}
@override
Future<bool> isLoggedIn() async => false;
@override
Future<({String? userId, String? role})> getStoredUser() async =>
(userId: null, role: null);
@override
Future<void> logout() async {}
@override
Future<Map<String, dynamic>> setup2FA({String? password, String? totpCode}) async =>
{};
@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<List<CountryOption>> getCountries({bool force = false}) async => const [];
@override
List<CountryOption>? peekLastCountries() => null;
@override
void invalidateCountries() {}
@override
Future<UserProfile> getProfile({bool force = false}) async {
throw UnimplementedError();
}
@override
UserProfile? peekLastProfile() => null;
@override
void invalidateProfile() {}
@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<({bool valid, String? reason})> validateReferralCode(String code) async =>
(valid: true, reason: null);
}
Future<void> _boot(_FakeAuthRepo repo) 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>(() => repo);
sl.registerFactory<AuthBloc>(() => AuthBloc(repo));
}
void main() {
tearDown(() async {
if (sl.isRegistered<AppConfig>()) await sl.reset();
});
test('login emits submitting then error, never session loading', () async {
final repo = _FakeAuthRepo();
final bloc = AuthBloc(repo);
final statuses = <AuthStatus>[];
final sub = bloc.stream.listen((s) => statuses.add(s.status));
bloc.add(const AuthLoginRequested(email: 'a@b.c', password: 'x'));
await bloc.stream.firstWhere((s) => s.status == AuthStatus.error);
await sub.cancel();
expect(statuses, contains(AuthStatus.submitting));
expect(statuses, isNot(contains(AuthStatus.loading)));
expect(repo.loginCalls, 1);
await bloc.close();
});
testWidgets('login validates empty fields before API call', (tester) async {
final repo = _FakeAuthRepo();
await _boot(repo);
await tester.pumpWidget(
BlocProvider(
create: (_) => sl<AuthBloc>(),
child: MaterialApp(
theme: AppTheme.dark,
home: const LoginScreen(),
),
),
);
await tester.pump();
await tester.tap(find.byKey(const Key('login_submit')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
expect(repo.loginCalls, 0);
expect(find.textContaining('email and password'), findsOneWidget);
});
testWidgets('forgot password validates empty email', (tester) async {
final repo = _FakeAuthRepo();
await _boot(repo);
await tester.pumpWidget(
MaterialApp(
theme: AppTheme.dark,
home: const ForgotPasswordScreen(),
),
);
await tester.pump();
await tester.tap(find.byKey(const Key('forgot_password_submit')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
expect(repo.forgotCalls, 0);
expect(find.byKey(const Key('forgot_password_error')), findsOneWidget);
expect(find.textContaining('Email cannot be empty'), findsWidgets);
});
}