feat(auth): 登录异常改为居中弹窗
This commit is contained in:
1 parent
9b78e43702
commit
b0002abf82
14 files changed
+436
-10
No files matched your search
@@ -5,6 +5,7 @@ import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:fruitshorts/core/data/models/auth_session.dart';
|
||||
import 'package:fruitshorts/core/data/remote/api_client.dart';
|
||||
import 'package:fruitshorts/core/data/remote/auth_api.dart';
|
||||
import 'package:fruitshorts/core/data/services/auth_controller.dart';
|
||||
@@ -97,6 +98,64 @@ void main() {
|
||||
|
||||
expect(find.text('Enter a valid phone number'), findsOneWidget);
|
||||
expect(adapter.requests, isEmpty);
|
||||
expect(find.byKey(const Key('centered-error-dialog')), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('should_show_centered_dialog_when_otp_request_fails', (
|
||||
tester,
|
||||
) async {
|
||||
final api = _FailingPhoneAuthApi(sendOtpMessage: 'Too many OTP requests');
|
||||
final auth = AuthController(api, AuthSessionStore());
|
||||
await _pumpHost(tester, auth);
|
||||
await tester.tap(find.byKey(const Key('open-phone-sheet')));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('auth-phone-number')),
|
||||
'08123456789',
|
||||
);
|
||||
|
||||
await tester.tap(find.byKey(const Key('auth-send-code')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byKey(const Key('centered-error-dialog')), findsOneWidget);
|
||||
expect(find.text('Login failed'), findsOneWidget);
|
||||
expect(find.text('Too many OTP requests'), findsOneWidget);
|
||||
expect(api.sendOtpCalls, 1);
|
||||
expect(
|
||||
tester
|
||||
.widget<TextButton>(find.byKey(const Key('auth-send-code')))
|
||||
.onPressed,
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('should_show_centered_dialog_when_phone_login_fails', (
|
||||
tester,
|
||||
) async {
|
||||
final api = _FailingPhoneAuthApi(loginMessage: 'Invalid verification code');
|
||||
final auth = AuthController(api, AuthSessionStore());
|
||||
await _pumpHost(tester, auth);
|
||||
await tester.tap(find.byKey(const Key('open-phone-sheet')));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('auth-phone-number')),
|
||||
'08123456789',
|
||||
);
|
||||
await tester.enterText(find.byKey(const Key('auth-phone-code')), '123456');
|
||||
|
||||
await tester.tap(find.byKey(const Key('auth-phone-submit')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byKey(const Key('centered-error-dialog')), findsOneWidget);
|
||||
expect(find.text('Login failed'), findsOneWidget);
|
||||
expect(find.text('Invalid verification code'), findsOneWidget);
|
||||
expect(api.loginWithPhoneCalls, 1);
|
||||
expect(
|
||||
tester
|
||||
.widget<FilledButton>(find.byKey(const Key('auth-phone-submit')))
|
||||
.onPressed,
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('should_keep_code_above_ios_keyboard', (tester) async {
|
||||
@@ -132,6 +191,45 @@ void main() {
|
||||
});
|
||||
}
|
||||
|
||||
class _FailingPhoneAuthApi extends AuthApi {
|
||||
_FailingPhoneAuthApi({
|
||||
this.sendOtpMessage = 'otp failed',
|
||||
this.loginMessage = 'login failed',
|
||||
}) : super(
|
||||
ApiClient(
|
||||
baseUrl: 'http://127.0.0.1:1',
|
||||
adapter: FakeHttpAdapter((_) async => FakeHttpResponse('{}', 500)),
|
||||
),
|
||||
);
|
||||
|
||||
final String sendOtpMessage;
|
||||
final String loginMessage;
|
||||
int sendOtpCalls = 0;
|
||||
int loginWithPhoneCalls = 0;
|
||||
|
||||
@override
|
||||
Future<void> sendOtp({
|
||||
required String target,
|
||||
required String scene,
|
||||
required String deviceId,
|
||||
}) async {
|
||||
sendOtpCalls++;
|
||||
throw ApiException(429, sendOtpMessage);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AuthSession> loginWithPhone({
|
||||
required String target,
|
||||
required String code,
|
||||
required String deviceId,
|
||||
String platform = '',
|
||||
String appVersion = '',
|
||||
}) async {
|
||||
loginWithPhoneCalls++;
|
||||
throw ApiException(401, loginMessage);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _pumpHost(WidgetTester tester, AuthController auth) {
|
||||
return tester.pumpWidget(
|
||||
MultiProvider(
|
||||
|
||||
Reference in new issue
Block a user