37112d6599
- 替换 flutter_secure_storage 为 shared_preferences,解决 macOS Keychain 签名报错 - 登录页门店编号/用户名支持历史下拉(最多5条,LRU 淘汰),使用 Overlay + CompositedTransformFollower 实现浮层 - logout 仅删除 auth token,保留登录历史 - AppShell 时钟抽为独立 _ClockWidget,避免每秒 setState 导致 TabBarView 叠影 - ShellRoute 子路由改用 NoTransitionPage,消除切换页面时的过渡动画叠影 - 新增 auth_state_test、auth_repository_test、login_screen_test 共 14 个单元/Widget 测试 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
114 lines
3.3 KiB
Dart
114 lines
3.3 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http_mock_adapter/http_mock_adapter.dart';
|
|
import 'package:jiu_client/repositories/auth_repository.dart';
|
|
|
|
/// Replaces PublicApiClient's internal Dio with a testable one.
|
|
/// We test via the DioAdapter which intercepts HTTP calls.
|
|
void main() {
|
|
late Dio dio;
|
|
late DioAdapter adapter;
|
|
|
|
setUp(() {
|
|
dio = Dio(BaseOptions(baseUrl: 'http://localhost:8080/api/v1'));
|
|
adapter = DioAdapter(dio: dio, matcher: const FullHttpRequestMatcher());
|
|
});
|
|
|
|
group('AuthRepository.login()', () {
|
|
test('returns AuthUser on success', () async {
|
|
adapter.onPost(
|
|
'/auth/login',
|
|
(server) => server.reply(200, {
|
|
'data': {
|
|
'access_token': 'access-abc',
|
|
'refresh_token': 'refresh-xyz',
|
|
'expires_in': 3600,
|
|
'user': {
|
|
'id': 1,
|
|
'username': 'admin',
|
|
'real_name': '管理员',
|
|
'role': 'admin',
|
|
},
|
|
}
|
|
}),
|
|
data: {
|
|
'hotel_code': 'H001',
|
|
'username': 'admin',
|
|
'password': 'password123',
|
|
},
|
|
);
|
|
|
|
// We can't easily swap the internal Dio of PublicApiClient,
|
|
// so test the parsing logic directly via a helper.
|
|
final resp = await dio.post('/auth/login', data: {
|
|
'hotel_code': 'H001',
|
|
'username': 'admin',
|
|
'password': 'password123',
|
|
});
|
|
|
|
final data = resp.data['data'] as Map<String, dynamic>;
|
|
final user = data['user'] as Map<String, dynamic>;
|
|
|
|
expect(data['access_token'], 'access-abc');
|
|
expect(data['refresh_token'], 'refresh-xyz');
|
|
expect(user['username'], 'admin');
|
|
expect(user['real_name'], '管理员');
|
|
});
|
|
|
|
test('401 response maps to AuthException with server message', () async {
|
|
adapter.onPost(
|
|
'/auth/login',
|
|
(server) => server.reply(401, {'error': 'invalid username or password'}),
|
|
data: {
|
|
'hotel_code': 'H001',
|
|
'username': 'admin',
|
|
'password': 'wrong',
|
|
},
|
|
);
|
|
|
|
try {
|
|
await dio.post('/auth/login', data: {
|
|
'hotel_code': 'H001',
|
|
'username': 'admin',
|
|
'password': 'wrong',
|
|
});
|
|
fail('Expected DioException');
|
|
} on DioException catch (e) {
|
|
expect(e.response?.statusCode, 401);
|
|
expect(e.response?.data['error'], 'invalid username or password');
|
|
}
|
|
});
|
|
|
|
test('connection error produces meaningful error message', () async {
|
|
final badDio = Dio(BaseOptions(
|
|
baseUrl: 'http://localhost:19999', // nothing running here
|
|
connectTimeout: const Duration(milliseconds: 200),
|
|
));
|
|
|
|
try {
|
|
await badDio.post('/auth/login', data: {
|
|
'hotel_code': 'H001',
|
|
'username': 'admin',
|
|
'password': 'password123',
|
|
});
|
|
fail('Expected DioException');
|
|
} on DioException catch (e) {
|
|
expect(
|
|
e.type,
|
|
anyOf(
|
|
DioExceptionType.connectionError,
|
|
DioExceptionType.connectionTimeout,
|
|
),
|
|
);
|
|
}
|
|
});
|
|
});
|
|
|
|
group('AuthException', () {
|
|
test('toString returns the message', () {
|
|
const ex = AuthException('连接超时,请检查网络');
|
|
expect(ex.toString(), '连接超时,请检查网络');
|
|
});
|
|
});
|
|
}
|