forked from eatvio/eatvio_app
feat(auth): align password policy and handle 429 responses
- map 429 Retry-After to a localized toast in AuthCubit - enforce the 8-character minimum on registration v1 and add the hint - surface errors.password in registration v2 - show password errors from 422 responses in the reset flow - add ru/en localization strings and regenerate l10n - add a regression test for the 429/Retry-After path
This commit is contained in:
88
test/auth_cubit_429_test.dart
Normal file
88
test/auth_cubit_429_test.dart
Normal file
@@ -0,0 +1,88 @@
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:eatvio_app/core/firebase_api.dart';
|
||||
import 'package:eatvio_app/core/prefs.dart';
|
||||
import 'package:eatvio_app/core/session/session_lifecycle_service.dart';
|
||||
import 'package:eatvio_app/features/auth/presentation/bloc/auth_cubit/auth_cubit.dart';
|
||||
import 'package:eatvio_app/features/auth/presentation/bloc/auth_cubit/auth_state.dart';
|
||||
import 'package:eatvio_app/features/auth/repositories/auth_repository.dart';
|
||||
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
|
||||
import 'package:eatvio_app/generated/l10n.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class _MockAuthRepository extends Mock implements AuthRepository {}
|
||||
|
||||
class _MockPrefs extends Mock implements Prefs {}
|
||||
|
||||
class _MockProfileRepository extends Mock implements ProfileRepository {}
|
||||
|
||||
class _MockFirebaseApi extends Mock implements FirebaseApi {}
|
||||
|
||||
class _MockSessionLifecycleService extends Mock
|
||||
implements SessionLifecycleService {}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
const toastChannel = MethodChannel('PonnamKarthik/fluttertoast');
|
||||
|
||||
setUpAll(() async {
|
||||
await S.delegate.load(const Locale('ru'));
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(toastChannel, (call) async => true);
|
||||
});
|
||||
|
||||
test('AuthCubit maps 429 Retry-After to localized message', () async {
|
||||
final request = RequestOptions(path: '/api/login');
|
||||
final response = Response(
|
||||
requestOptions: request,
|
||||
statusCode: 429,
|
||||
headers: Headers.fromMap({
|
||||
'retry-after': ['55'],
|
||||
}),
|
||||
data: {
|
||||
'message': 'Слишком много неудачных попыток.',
|
||||
'error': {
|
||||
'code': 'too_many_login_attempts',
|
||||
'retry_after': 55,
|
||||
},
|
||||
},
|
||||
);
|
||||
final exception = DioException(
|
||||
requestOptions: request,
|
||||
response: response,
|
||||
type: DioExceptionType.badResponse,
|
||||
);
|
||||
|
||||
final repository = _MockAuthRepository();
|
||||
when(
|
||||
() => repository.getTokenByCredentials(
|
||||
'browser.check@example.com',
|
||||
'wrong-password',
|
||||
),
|
||||
).thenThrow(exception);
|
||||
|
||||
final cubit = AuthCubit(
|
||||
authRepository: repository,
|
||||
prefs: _MockPrefs(),
|
||||
profileRepository: _MockProfileRepository(),
|
||||
firebaseApi: _MockFirebaseApi(),
|
||||
sessionLifecycleService: _MockSessionLifecycleService(),
|
||||
);
|
||||
|
||||
final result = await cubit.getTokenByCredentials(
|
||||
'browser.check@example.com',
|
||||
'wrong-password',
|
||||
);
|
||||
|
||||
expect(result, isFalse);
|
||||
expect(cubit.state, isA<AuthStateError>());
|
||||
expect(
|
||||
(cubit.state as AuthStateError).errorMessage,
|
||||
contains('Повторите через 55 сек.'),
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user