feat(auth): 登录异常改为居中弹窗
This commit is contained in:
1 parent
9b78e43702
commit
b0002abf82
14 files changed
+436
-10
No files matched your search
@@ -78,6 +78,7 @@ void main() {
|
||||
|
||||
expect(find.text('Enter your email'), findsOneWidget);
|
||||
expect(find.text('Welcome back'), findsOneWidget);
|
||||
expect(find.byKey(const Key('centered-error-dialog')), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
@@ -94,6 +95,80 @@ void main() {
|
||||
find.text('Password needs both letters and numbers'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.byKey(const Key('centered-error-dialog')), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('should_show_centered_dialog_when_login_api_returns_an_error', (
|
||||
tester,
|
||||
) async {
|
||||
final api = _FailingAuthApi(loginMessage: 'Invalid email or password');
|
||||
await pumpAuthPage(tester, api: api);
|
||||
await _enterValidCredentials(tester);
|
||||
|
||||
await tester.ensureVisible(find.byKey(const Key('auth-submit')));
|
||||
await tester.tap(find.byKey(const Key('auth-submit')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final dialog = find.byKey(const Key('centered-error-dialog'));
|
||||
expect(dialog, findsOneWidget);
|
||||
expect(find.text('Login failed'), findsOneWidget);
|
||||
expect(find.text('Invalid email or password'), findsOneWidget);
|
||||
expect(api.loginCalls, 1);
|
||||
expect(
|
||||
tester.getCenter(dialog),
|
||||
offsetMoreOrLessEquals(
|
||||
tester.getCenter(find.byType(Scaffold)),
|
||||
epsilon: 1,
|
||||
),
|
||||
);
|
||||
expect(
|
||||
tester
|
||||
.widget<FilledButton>(find.byKey(const Key('auth-submit')))
|
||||
.onPressed,
|
||||
isNotNull,
|
||||
);
|
||||
|
||||
await tester.tap(find.byKey(const Key('centered-error-confirm')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(dialog, findsNothing);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'should_show_registration_failed_title_when_register_api_returns_an_error',
|
||||
(tester) async {
|
||||
final api = _FailingAuthApi(registerMessage: 'Email already exists');
|
||||
await pumpAuthPage(tester, signUp: true, api: api);
|
||||
await _enterValidCredentials(tester);
|
||||
|
||||
await tester.ensureVisible(find.byKey(const Key('auth-submit')));
|
||||
await tester.tap(find.byKey(const Key('auth-submit')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byKey(const Key('centered-error-dialog')), findsOneWidget);
|
||||
expect(find.text('Registration failed'), findsOneWidget);
|
||||
expect(find.text('Email already exists'), findsOneWidget);
|
||||
expect(api.registerCalls, 1);
|
||||
|
||||
await tester.tapAt(const Offset(8, 8));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(const Key('centered-error-dialog')), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'should_show_network_error_when_login_throws_an_unknown_exception',
|
||||
(tester) async {
|
||||
final api = _FailingAuthApi(loginThrowsUnknown: true);
|
||||
await pumpAuthPage(tester, api: api);
|
||||
await _enterValidCredentials(tester);
|
||||
|
||||
await tester.ensureVisible(find.byKey(const Key('auth-submit')));
|
||||
await tester.tap(find.byKey(const Key('auth-submit')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byKey(const Key('centered-error-dialog')), findsOneWidget);
|
||||
expect(find.text('Network error, please try again'), findsOneWidget);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -342,6 +417,11 @@ void main() {
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _enterValidCredentials(WidgetTester tester) async {
|
||||
await tester.enterText(find.byKey(const Key('auth-email')), 'a@b.com');
|
||||
await tester.enterText(find.byKey(const Key('auth-password')), 'abcd1234');
|
||||
}
|
||||
|
||||
void _expectAboveKeyboard(WidgetTester tester, Key fieldKey, double inset) {
|
||||
final screenHeight =
|
||||
tester.view.physicalSize.height / tester.view.devicePixelRatio;
|
||||
@@ -351,6 +431,50 @@ void _expectAboveKeyboard(WidgetTester tester, Key fieldKey, double inset) {
|
||||
);
|
||||
}
|
||||
|
||||
class _FailingAuthApi extends AuthApi {
|
||||
_FailingAuthApi({
|
||||
this.loginMessage = 'login failed',
|
||||
this.registerMessage = 'register failed',
|
||||
this.loginThrowsUnknown = false,
|
||||
}) : super(
|
||||
ApiClient(
|
||||
baseUrl: 'http://127.0.0.1:1',
|
||||
adapter: FakeHttpAdapter((_) async => FakeHttpResponse('{}', 500)),
|
||||
),
|
||||
);
|
||||
|
||||
final String loginMessage;
|
||||
final String registerMessage;
|
||||
final bool loginThrowsUnknown;
|
||||
int loginCalls = 0;
|
||||
int registerCalls = 0;
|
||||
|
||||
@override
|
||||
Future<AuthSession> login({
|
||||
required String email,
|
||||
required String password,
|
||||
required String deviceId,
|
||||
String platform = '',
|
||||
String appVersion = '',
|
||||
}) async {
|
||||
loginCalls++;
|
||||
if (loginThrowsUnknown) throw StateError('unexpected');
|
||||
throw ApiException(401, loginMessage);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AuthSession> register({
|
||||
required String email,
|
||||
required String password,
|
||||
required String deviceId,
|
||||
String platform = '',
|
||||
String appVersion = '',
|
||||
}) async {
|
||||
registerCalls++;
|
||||
throw ApiException(409, registerMessage);
|
||||
}
|
||||
}
|
||||
|
||||
class _PendingAuthApi extends AuthApi {
|
||||
_PendingAuthApi()
|
||||
: super(
|
||||
|
||||
@@ -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