feat(mobile): login, password change and logout screens
P3 Task 3 移动端(对接 Task 2 api client + 本任务新增的后端契约): - 登录页实装:账号/密码表单(空值客户端校验)→ api.login(会话自动 持久化)→ pushReplacement /home;错误码本地化(AUTH_INVALID_ CREDENTIALS / AUTH_RATE_LIMITED(含稍后再试文案)/ 网络 / 校验); 提交加载态防重复提交。 - 修改密码页(设置页入口):当前/新/确认三字段,客户端校验(≥8 位、 两次一致)→ api.changePassword → toast + pop;成功后本地会话不变 (服务端保留当前会话);旧密码错误(401 AUTH_INVALID_CREDENTIALS) 显示「当前密码错误」且不触发 401 驱逐。 - 退出登录:设置页按钮 → api.logout 尽力而为 → 清会话 → 清栈回 /login;App 壳 initState 注册 onUnauthorized(业务 401 → 清栈 /login), 根 navigatorKey 接线;password-change 的 401 豁免会话驱逐(拦截器 豁免集合与令牌附加分离)。 - l10n:新增 16 组 zh/en 文案;错误码 → 文案解析扩展 (core/error_l10n.dart,errorCode→AppLocalizations)。 - 测试:ApiClient.changePassword 单测 ×2(Bearer+请求体;旧密码 401 不驱逐);widget 测试 ×12(登录 5:空值/成功跳转/凭据错误/限速文案/ 加载禁用;改密 4:过短/不一致/旧密码错误文案+会话保留/成功 toast+pop 且令牌不变;设置 2:改密入口跳转/登出清会话回登录;App 壳 1:401 清栈回 /login)。flutter analyze 0 issue;flutter test 81/81。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
/// App 壳 401 接线测试(P3 计划 Task 3)。
|
||||
///
|
||||
/// 覆盖:业务端点 401 → ApiClient 清会话并触发 onUnauthorized →
|
||||
/// 根导航清栈回 /login(PhotoLiveApp.initState 注册的回调)。
|
||||
library;
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:photo_live/app/app.dart';
|
||||
import 'package:photo_live/core/api_client.dart';
|
||||
import 'package:photo_live/core/error_codes.dart';
|
||||
import 'package:photo_live/core/session_store.dart';
|
||||
import 'package:photo_live/pages/login_page.dart';
|
||||
|
||||
import '../helpers/fakes.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('业务 401 → onUnauthorized 清栈回 /login', (tester) async {
|
||||
final store = FakeSessionStore();
|
||||
store.session = Session(
|
||||
token: 'tok-live-1',
|
||||
expiresAt: DateTime.utc(2099, 1, 1),
|
||||
account: 'staff01',
|
||||
);
|
||||
final adapter = FakeAdapter(
|
||||
(o) => (401, errorBody(ErrorCodes.authTokenExpired)),
|
||||
);
|
||||
final client = ApiClient(
|
||||
sessionStore: store,
|
||||
dio: Dio(BaseOptions(baseUrl: 'http://test.local'))
|
||||
..httpClientAdapter = adapter,
|
||||
);
|
||||
|
||||
// PhotoLiveApp.initState 读取被 override 的客户端并注册 onUnauthorized。
|
||||
await tester.pumpWidget(
|
||||
ProviderScope(
|
||||
overrides: [apiClientProvider.overrideWith((ref) => client)],
|
||||
child: const PhotoLiveApp(),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// 先抵 /home 再触发 401:验证清栈(回 /login 而非叠加路由)。
|
||||
rootNavigatorKey.currentState!
|
||||
.pushNamedAndRemoveUntil('/home', (route) => false);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// 触发 401 但不在测试体内 await(fake-async 下时钟由 pump 推进,
|
||||
// 阻塞等待会死锁);AppException 由 ignore 吞掉,断言在下方。
|
||||
client.listAlbums().ignore();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(store.session, isNull, reason: '业务 401 清空本地会话');
|
||||
expect(find.byType(LoginPage), findsOneWidget, reason: '清栈回登录页');
|
||||
});
|
||||
}
|
||||
@@ -509,4 +509,49 @@ void main() {
|
||||
expect(adapter.requests[1].method, 'DELETE');
|
||||
expect(adapter.requests[1].path, '/v1/photos/pho-1');
|
||||
});
|
||||
|
||||
test('changePassword:携带 Bearer 与双密码体;204 成功不动本地会话', () async {
|
||||
seedSession();
|
||||
final adapter = FakeAdapter((o) => (204, null));
|
||||
|
||||
await wired(adapter).changePassword('old-pass-1', 'brand-new-9');
|
||||
|
||||
final req = adapter.requests.single;
|
||||
expect(req.method, 'POST');
|
||||
expect(req.path, '/v1/auth/password-change');
|
||||
expect(req.headers['Authorization'], 'Bearer tok-live-1');
|
||||
expect(req.body, {'oldPassword': 'old-pass-1', 'newPassword': 'brand-new-9'});
|
||||
expect(store.session?.token, 'tok-live-1', reason: '当前会话保留');
|
||||
expect(unauthorizedCalls, 0);
|
||||
});
|
||||
|
||||
test('changePassword:旧密码错误(401 AUTH_INVALID_CREDENTIALS)→ 不驱逐会话不通知',
|
||||
() async {
|
||||
seedSession();
|
||||
final adapter = FakeAdapter(
|
||||
(o) => (
|
||||
401,
|
||||
{
|
||||
'code': 'AUTH_INVALID_CREDENTIALS',
|
||||
'message': 'old password wrong',
|
||||
'requestId': 'req-pc',
|
||||
}
|
||||
),
|
||||
);
|
||||
|
||||
final err = await captureError(
|
||||
() => wired(adapter).changePassword('wrong-old', 'brand-new-9'),
|
||||
);
|
||||
|
||||
expect(
|
||||
err,
|
||||
isA<AppException>().having(
|
||||
(e) => e.code,
|
||||
'code',
|
||||
ErrorCodes.authInvalidCredentials,
|
||||
),
|
||||
);
|
||||
expect(store.session?.token, 'tok-live-1', reason: '旧密码错误不清会话');
|
||||
expect(unauthorizedCalls, 0, reason: 'password-change 401 不驱逐');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/// Widget/单元测试共用替身(P3 计划 Task 3)。
|
||||
///
|
||||
/// 与 test/core/api_client_test.dart 中的手写打桩同款:FakeAdapter 为
|
||||
/// dio HttpClientAdapter,按路由分发预设响应并记录全部请求;未引入
|
||||
/// http_mock_adapter(零额外 dev 依赖,打桩面完全可控)。
|
||||
library;
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:photo_live/core/session_store.dart';
|
||||
|
||||
/// 内存会话存储(测试替身)。
|
||||
class FakeSessionStore implements SessionStore {
|
||||
Session? session;
|
||||
|
||||
@override
|
||||
Future<Session?> read() async => session;
|
||||
|
||||
@override
|
||||
Future<void> save(Session value) async => session = value;
|
||||
|
||||
@override
|
||||
Future<void> clear() async => session = null;
|
||||
}
|
||||
|
||||
/// 记录到适配器的请求快照。
|
||||
class RecordedRequest {
|
||||
const RecordedRequest({
|
||||
required this.method,
|
||||
required this.path,
|
||||
required this.headers,
|
||||
required this.body,
|
||||
});
|
||||
|
||||
final String method;
|
||||
final String path;
|
||||
final Map<String, dynamic> headers;
|
||||
final Object? body;
|
||||
}
|
||||
|
||||
/// 手写 dio 打桩适配器:按路由分发预设响应并记录全部请求。
|
||||
///
|
||||
/// [router] 返回 `(status, body)`(可为 async 以模拟慢响应/加载态);
|
||||
/// body 为 null 表示无响应体(204)。
|
||||
class FakeAdapter implements HttpClientAdapter {
|
||||
FakeAdapter(this.router);
|
||||
|
||||
final FutureOr<(int, Object?)> Function(RequestOptions options) router;
|
||||
|
||||
final List<RecordedRequest> requests = <RecordedRequest>[];
|
||||
|
||||
@override
|
||||
Future<ResponseBody> fetch(
|
||||
RequestOptions options,
|
||||
Stream? requestStream,
|
||||
Future? cancelFuture,
|
||||
) async {
|
||||
requests.add(
|
||||
RecordedRequest(
|
||||
method: options.method,
|
||||
path: options.path,
|
||||
headers: Map<String, dynamic>.from(options.headers),
|
||||
body: options.data,
|
||||
),
|
||||
);
|
||||
final (status, body) = await router(options);
|
||||
if (body == null) {
|
||||
// 204 等:空体且不带 JSON content-type(transformer 不解码)。
|
||||
return ResponseBody.fromString('', status);
|
||||
}
|
||||
return ResponseBody.fromString(
|
||||
jsonEncode(body),
|
||||
status,
|
||||
headers: {
|
||||
Headers.contentTypeHeader: [Headers.jsonContentType],
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void close({bool force = false}) {}
|
||||
}
|
||||
|
||||
/// 常用响应体:登录成功(openapi LoginResponse 形状)。
|
||||
Map<String, dynamic> loginOkBody({
|
||||
String token = 'tok-new',
|
||||
String expiresAt = '2026-09-20T12:00:00Z',
|
||||
}) => <String, dynamic>{
|
||||
'sessionToken': token,
|
||||
'expiresAt': expiresAt,
|
||||
};
|
||||
|
||||
/// 常用响应体:契约 Error 形状。
|
||||
Map<String, dynamic> errorBody(String code) => <String, dynamic>{
|
||||
'code': code,
|
||||
'message': 'stub message',
|
||||
'requestId': 'req-stub',
|
||||
};
|
||||
@@ -0,0 +1,155 @@
|
||||
/// 登录页 widget 测试(P3 计划 Task 3)。
|
||||
///
|
||||
/// 覆盖:空字段客户端校验(不发请求)、成功登录写会话并跳 /home、
|
||||
/// 凭据错误/限速(429)错误码本地化文案、提交中加载态(按钮禁用防重复提交)。
|
||||
/// API 层经 FakeAdapter(dio HttpClientAdapter)打桩,Provider 以 override
|
||||
/// 注入测试客户端(无真实网络)。
|
||||
library;
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:photo_live/core/api_client.dart';
|
||||
import 'package:photo_live/core/error_codes.dart';
|
||||
import 'package:photo_live/l10n/app_localizations.dart';
|
||||
import 'package:photo_live/pages/login_page.dart';
|
||||
|
||||
import '../helpers/fakes.dart';
|
||||
|
||||
void main() {
|
||||
late FakeSessionStore store;
|
||||
late int unauthorizedCalls;
|
||||
|
||||
setUp(() {
|
||||
store = FakeSessionStore();
|
||||
unauthorizedCalls = 0;
|
||||
});
|
||||
|
||||
ApiClient clientOf(FakeAdapter adapter) => ApiClient(
|
||||
sessionStore: store,
|
||||
dio: Dio(BaseOptions(baseUrl: 'http://test.local'))
|
||||
..httpClientAdapter = adapter,
|
||||
)..onUnauthorized = () => unauthorizedCalls++;
|
||||
|
||||
Future<void> pumpLoginPage(WidgetTester tester, ApiClient client) async {
|
||||
await tester.pumpWidget(
|
||||
ProviderScope(
|
||||
overrides: [apiClientProvider.overrideWith((ref) => client)],
|
||||
child: MaterialApp(
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: const LoginPage(),
|
||||
routes: {
|
||||
'/home': (context) =>
|
||||
const Scaffold(body: Center(child: Text('HOME_MARK'))),
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
Future<void> fillAndSubmit(WidgetTester tester) async {
|
||||
await tester.enterText(
|
||||
find.widgetWithText(TextFormField, 'Account'),
|
||||
'staff01',
|
||||
);
|
||||
await tester.enterText(
|
||||
find.widgetWithText(TextFormField, 'Password'),
|
||||
'pw-123456',
|
||||
);
|
||||
await tester.tap(find.text('Sign in'));
|
||||
await tester.pump(); // 触发提交(含 SnackBar 动画首帧)。
|
||||
}
|
||||
|
||||
testWidgets('空字段 → 客户端校验提示,不发请求', (tester) async {
|
||||
final adapter = FakeAdapter((_) => throw StateError('不应发起请求'));
|
||||
await pumpLoginPage(tester, clientOf(adapter));
|
||||
|
||||
await tester.tap(find.text('Sign in'));
|
||||
await tester.pump();
|
||||
|
||||
// 测试环境默认 locale=en:账号/密码两个必填提示。
|
||||
expect(find.text('This field is required'), findsNWidgets(2));
|
||||
expect(adapter.requests, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('成功 → 写会话并跳 /home', (tester) async {
|
||||
final adapter = FakeAdapter(
|
||||
(o) => o.path == '/v1/auth/password' ? (200, loginOkBody())
|
||||
: throw StateError('unexpected ${o.path}'),
|
||||
);
|
||||
await pumpLoginPage(tester, clientOf(adapter));
|
||||
|
||||
await fillAndSubmit(tester);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('HOME_MARK'), findsOneWidget);
|
||||
expect(store.session?.token, 'tok-new');
|
||||
expect(store.session?.account, 'staff01');
|
||||
expect(
|
||||
adapter.requests.single.body,
|
||||
{'account': 'staff01', 'password': 'pw-123456'},
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('凭据错误(401)→ 「账号或密码错误」文案,不驱逐通知', (tester) async {
|
||||
final adapter = FakeAdapter(
|
||||
(o) => o.path == '/v1/auth/password'
|
||||
? (401, errorBody(ErrorCodes.authInvalidCredentials))
|
||||
: throw StateError('unexpected ${o.path}'),
|
||||
);
|
||||
await pumpLoginPage(tester, clientOf(adapter));
|
||||
|
||||
await fillAndSubmit(tester);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Incorrect account or password'), findsOneWidget);
|
||||
expect(store.session, isNull, reason: '登录失败不落会话');
|
||||
expect(unauthorizedCalls, 0, reason: '登录 401 不触发驱逐');
|
||||
expect(find.text('HOME_MARK'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('限速(429 AUTH_RATE_LIMITED)→ 稍后再试文案', (tester) async {
|
||||
final adapter = FakeAdapter(
|
||||
(o) => o.path == '/v1/auth/password'
|
||||
? (429, errorBody(ErrorCodes.authRateLimited))
|
||||
: throw StateError('unexpected ${o.path}'),
|
||||
);
|
||||
await pumpLoginPage(tester, clientOf(adapter));
|
||||
|
||||
await fillAndSubmit(tester);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(
|
||||
find.text('Too many attempts. Please try again later'),
|
||||
findsOneWidget,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('提交中 → 加载指示且按钮禁用(防重复提交)', (tester) async {
|
||||
final gate = Completer<void>();
|
||||
final adapter = FakeAdapter((o) async {
|
||||
await gate.future;
|
||||
return (200, loginOkBody());
|
||||
});
|
||||
await pumpLoginPage(tester, clientOf(adapter));
|
||||
|
||||
await fillAndSubmit(tester);
|
||||
// dio 链路经真实定时器推进(fake-async 需步进时钟才抵达适配器)。
|
||||
await tester.pump(const Duration(milliseconds: 50));
|
||||
|
||||
expect(find.byType(CircularProgressIndicator), findsOneWidget);
|
||||
// 加载中再点按钮(onPressed=null)不产生第二个请求。
|
||||
await tester.tap(find.byType(FilledButton), warnIfMissed: false);
|
||||
await tester.pump(const Duration(milliseconds: 50));
|
||||
expect(adapter.requests, hasLength(1));
|
||||
|
||||
gate.complete();
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('HOME_MARK'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
/// 修改密码页 widget 测试(P3 计划 Task 3)。
|
||||
///
|
||||
/// 覆盖:客户端校验(新密码 ≥8、两次一致;不发请求)、旧密码错误
|
||||
/// (401 AUTH_INVALID_CREDENTIALS)→「当前密码错误」文案且本地会话保留
|
||||
/// (password-change 的 401 不驱逐)、成功 → toast + pop 回上级且
|
||||
/// 会话令牌不变(服务端保留当前会话语义)。
|
||||
library;
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:photo_live/core/api_client.dart';
|
||||
import 'package:photo_live/core/error_codes.dart';
|
||||
import 'package:photo_live/core/session_store.dart';
|
||||
import 'package:photo_live/l10n/app_localizations.dart';
|
||||
import 'package:photo_live/pages/password_change_page.dart';
|
||||
|
||||
import '../helpers/fakes.dart';
|
||||
|
||||
void main() {
|
||||
late FakeSessionStore store;
|
||||
late int unauthorizedCalls;
|
||||
|
||||
setUp(() {
|
||||
store = FakeSessionStore();
|
||||
unauthorizedCalls = 0;
|
||||
});
|
||||
|
||||
ApiClient clientOf(FakeAdapter adapter) => ApiClient(
|
||||
sessionStore: store,
|
||||
dio: Dio(BaseOptions(baseUrl: 'http://test.local'))
|
||||
..httpClientAdapter = adapter,
|
||||
)..onUnauthorized = () => unauthorizedCalls++;
|
||||
|
||||
void seedSession() {
|
||||
store.session = Session(
|
||||
token: 'tok-live-1',
|
||||
expiresAt: DateTime.utc(2099, 1, 1),
|
||||
account: 'staff01',
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> pumpOverMarker(WidgetTester tester, ApiClient client) async {
|
||||
await tester.pumpWidget(
|
||||
ProviderScope(
|
||||
overrides: [apiClientProvider.overrideWith((ref) => client)],
|
||||
child: MaterialApp(
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: const Scaffold(body: Center(child: Text('MARK'))),
|
||||
routes: {
|
||||
'/password-change': (context) => const PasswordChangePage(),
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
tester.state<NavigatorState>(find.byType(Navigator).first)
|
||||
.pushNamed('/password-change');
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
Future<void> fill(
|
||||
WidgetTester tester, {
|
||||
String oldPw = 'old-pass-123',
|
||||
String newPw = 'brand-new-pass-9',
|
||||
String confirmPw = 'brand-new-pass-9',
|
||||
}) async {
|
||||
await tester.enterText(
|
||||
find.widgetWithText(TextFormField, 'Current password'),
|
||||
oldPw,
|
||||
);
|
||||
await tester.enterText(
|
||||
find.widgetWithText(TextFormField, 'New password'),
|
||||
newPw,
|
||||
);
|
||||
await tester.enterText(
|
||||
find.widgetWithText(TextFormField, 'Confirm new password'),
|
||||
confirmPw,
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('新密码过短 → 客户端校验提示,不发请求', (tester) async {
|
||||
seedSession();
|
||||
final adapter = FakeAdapter((_) => throw StateError('不应发起请求'));
|
||||
await pumpOverMarker(tester, clientOf(adapter));
|
||||
|
||||
await fill(tester, newPw: 'short7', confirmPw: 'short7');
|
||||
await tester.tap(find.widgetWithText(FilledButton, 'Change password'));
|
||||
await tester.pump();
|
||||
|
||||
expect(
|
||||
find.text('New password must be at least 8 characters'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(adapter.requests, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('两次新密码不一致 → 校验提示,不发请求', (tester) async {
|
||||
seedSession();
|
||||
final adapter = FakeAdapter((_) => throw StateError('不应发起请求'));
|
||||
await pumpOverMarker(tester, clientOf(adapter));
|
||||
|
||||
await fill(tester, confirmPw: 'different-pass-9');
|
||||
await tester.tap(find.widgetWithText(FilledButton, 'Change password'));
|
||||
await tester.pump();
|
||||
|
||||
expect(
|
||||
find.text('New passwords do not match'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(adapter.requests, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('旧密码错误(401)→ 「当前密码错误」文案,会话保留不驱逐', (tester) async {
|
||||
seedSession();
|
||||
final adapter = FakeAdapter(
|
||||
(o) => o.path == '/v1/auth/password-change'
|
||||
? (401, errorBody(ErrorCodes.authInvalidCredentials))
|
||||
: throw StateError('unexpected ${o.path}'),
|
||||
);
|
||||
await pumpOverMarker(tester, clientOf(adapter));
|
||||
|
||||
await fill(tester);
|
||||
await tester.tap(find.widgetWithText(FilledButton, 'Change password'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Current password is incorrect'), findsOneWidget);
|
||||
expect(store.session?.token, 'tok-live-1', reason: '旧密码错误不清会话');
|
||||
expect(unauthorizedCalls, 0, reason: 'password-change 的 401 不驱逐');
|
||||
expect(find.text('MARK'), findsNothing, reason: '停留在修改密码页');
|
||||
});
|
||||
|
||||
testWidgets('成功 → toast + pop 回上级;会话令牌不变(当前会话保留)', (tester) async {
|
||||
seedSession();
|
||||
final adapter = FakeAdapter(
|
||||
(o) => o.path == '/v1/auth/password-change'
|
||||
? (204, null)
|
||||
: throw StateError('unexpected ${o.path}'),
|
||||
);
|
||||
await pumpOverMarker(tester, clientOf(adapter));
|
||||
|
||||
await fill(tester);
|
||||
await tester.tap(find.widgetWithText(FilledButton, 'Change password'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final req = adapter.requests.single;
|
||||
expect(req.method, 'POST');
|
||||
expect(req.path, '/v1/auth/password-change');
|
||||
expect(req.headers['Authorization'], 'Bearer tok-live-1');
|
||||
expect(req.body, {
|
||||
'oldPassword': 'old-pass-123',
|
||||
'newPassword': 'brand-new-pass-9',
|
||||
});
|
||||
|
||||
expect(find.text('Password changed'), findsOneWidget);
|
||||
expect(find.text('MARK'), findsOneWidget, reason: '成功后 pop 回上级');
|
||||
expect(
|
||||
store.session?.token,
|
||||
'tok-live-1',
|
||||
reason: '服务端保留当前会话:本地令牌无需变更',
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/// 设置页 widget 测试(P3 计划 Task 3)。
|
||||
///
|
||||
/// 覆盖:修改密码入口跳 /password-change;退出登录 → 调 api.logout
|
||||
/// (尽力而为)→ 清空本地会话 → 清栈回 /login。
|
||||
library;
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:photo_live/core/api_client.dart';
|
||||
import 'package:photo_live/core/session_store.dart';
|
||||
import 'package:photo_live/l10n/app_localizations.dart';
|
||||
import 'package:photo_live/pages/home_page.dart';
|
||||
import 'package:photo_live/pages/login_page.dart';
|
||||
import 'package:photo_live/pages/password_change_page.dart';
|
||||
|
||||
import '../helpers/fakes.dart';
|
||||
|
||||
void main() {
|
||||
late FakeSessionStore store;
|
||||
|
||||
setUp(() {
|
||||
store = FakeSessionStore();
|
||||
store.session = Session(
|
||||
token: 'tok-live-1',
|
||||
expiresAt: DateTime.utc(2099, 1, 1),
|
||||
account: 'staff01',
|
||||
);
|
||||
});
|
||||
|
||||
ApiClient clientOf(FakeAdapter adapter) => ApiClient(
|
||||
sessionStore: store,
|
||||
dio: Dio(BaseOptions(baseUrl: 'http://test.local'))
|
||||
..httpClientAdapter = adapter,
|
||||
);
|
||||
|
||||
Future<void> pumpHome(WidgetTester tester, ApiClient client) async {
|
||||
await tester.pumpWidget(
|
||||
ProviderScope(
|
||||
overrides: [apiClientProvider.overrideWith((ref) => client)],
|
||||
child: MaterialApp(
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: const HomePage(),
|
||||
routes: {
|
||||
'/login': (context) => const LoginPage(),
|
||||
'/password-change': (context) => const PasswordChangePage(),
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
// 切到设置 tab(底部导航第三项)。
|
||||
await tester.tap(find.byIcon(Icons.settings_outlined));
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
testWidgets('修改密码入口 → 跳 /password-change', (tester) async {
|
||||
final adapter = FakeAdapter((_) => throw StateError('不应发起请求'));
|
||||
await pumpHome(tester, clientOf(adapter));
|
||||
|
||||
await tester.tap(find.text('Change password'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(PasswordChangePage), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('退出登录 → api.logout + 清会话 + 清栈回 /login(服务端失败亦然)',
|
||||
(tester) async {
|
||||
// 服务端撤销失败(500)也照样本地登出——尽力而为语义。
|
||||
final adapter = FakeAdapter(
|
||||
(o) => o.path == '/v1/auth/logout'
|
||||
? (500, errorBody('INTERNAL_ERROR'))
|
||||
: throw StateError('unexpected ${o.path}'),
|
||||
);
|
||||
await pumpHome(tester, clientOf(adapter));
|
||||
|
||||
await tester.tap(find.text('Sign out'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(adapter.requests.single.path, '/v1/auth/logout');
|
||||
expect(adapter.requests.single.method, 'POST');
|
||||
expect(store.session, isNull, reason: '本地会话必须清除');
|
||||
expect(find.byType(LoginPage), findsOneWidget);
|
||||
expect(find.byType(HomePage), findsNothing, reason: '主壳已出栈');
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user