feat(app): 完善播放协议搜索与空状态
This commit is contained in:
1 parent
671e0ce143
commit
ef29fc5f15
85 files changed
+2976
-768
No files matched your search
@@ -53,70 +53,82 @@ void main() {
|
||||
);
|
||||
}
|
||||
|
||||
test('should_store_session_and_expose_signed_in_state_when_register_succeeds', () async {
|
||||
late AuthController auth;
|
||||
final adapter = FakeHttpAdapter((options) async {
|
||||
expect(options.uri.path, '/app/auth/register');
|
||||
expect(options.headers['X-Device-Id'], isNotEmpty);
|
||||
expect(options.headers.containsKey('Authorization'), isFalse);
|
||||
return FakeHttpResponse(tokenBody(accessToken: 'a1', refreshToken: 'r1'));
|
||||
});
|
||||
final store = AuthSessionStore();
|
||||
auth = AuthController(
|
||||
AuthApi(clientWith(() => auth, adapter: adapter)),
|
||||
store,
|
||||
);
|
||||
|
||||
expect(auth.isSignedIn, isFalse);
|
||||
await auth.register(email: 'a@b.com', password: 'abcd1234');
|
||||
expect(auth.isSignedIn, isTrue);
|
||||
expect(auth.accessToken, 'a1');
|
||||
expect(auth.user?.nickname, 'Tester');
|
||||
// 落盘:新建一个 store 也能读回来
|
||||
expect((await AuthSessionStore().read())?.refreshToken, 'r1');
|
||||
});
|
||||
|
||||
test('should_refresh_once_and_retry_with_new_token_when_response_code_is_401', () async {
|
||||
var calls = 0;
|
||||
late AuthController auth;
|
||||
final adapter = FakeHttpAdapter((options) async {
|
||||
calls++;
|
||||
if (options.uri.path == '/app/auth/refresh') {
|
||||
return FakeHttpResponse(tokenBody(accessToken: 'a2', refreshToken: 'r2'));
|
||||
}
|
||||
if (options.uri.path != '/app/me') {
|
||||
// 注册等其它请求:直接发第一版令牌
|
||||
return FakeHttpResponse(tokenBody(accessToken: 'a1', refreshToken: 'r1'));
|
||||
}
|
||||
final token = options.headers['Authorization'];
|
||||
if (token == 'Bearer a1') {
|
||||
test(
|
||||
'should_store_session_and_expose_signed_in_state_when_register_succeeds',
|
||||
() async {
|
||||
late AuthController auth;
|
||||
final adapter = FakeHttpAdapter((options) async {
|
||||
expect(options.uri.path, '/app/auth/register');
|
||||
expect(options.headers['X-Device-Id'], isNotEmpty);
|
||||
expect(options.headers.containsKey('Authorization'), isFalse);
|
||||
return FakeHttpResponse(
|
||||
jsonEncode({'code': 401, 'message': '未登录或登录已过期', 'data': null}),
|
||||
tokenBody(accessToken: 'a1', refreshToken: 'r1'),
|
||||
);
|
||||
}
|
||||
expect(token, 'Bearer a2');
|
||||
return FakeHttpResponse(
|
||||
jsonEncode({
|
||||
'code': 0,
|
||||
'message': 'ok',
|
||||
'data': {'hit': true},
|
||||
}),
|
||||
});
|
||||
final store = AuthSessionStore();
|
||||
auth = AuthController(
|
||||
AuthApi(clientWith(() => auth, adapter: adapter)),
|
||||
store,
|
||||
);
|
||||
});
|
||||
final store = AuthSessionStore();
|
||||
auth = AuthController(
|
||||
AuthApi(clientWith(() => auth, adapter: adapter)),
|
||||
store,
|
||||
);
|
||||
await auth.register(email: 'a@b.com', password: 'abcd1234');
|
||||
calls = 0;
|
||||
|
||||
final api = clientWith(() => auth, adapter: adapter);
|
||||
final data = await api.get('/app/me');
|
||||
expect((data as Map)['hit'], isTrue);
|
||||
expect(calls, 3); // 原请求(401) + 刷新 + 重试
|
||||
expect(auth.accessToken, 'a2');
|
||||
});
|
||||
expect(auth.isSignedIn, isFalse);
|
||||
await auth.register(email: 'a@b.com', password: 'abcd1234');
|
||||
expect(auth.isSignedIn, isTrue);
|
||||
expect(auth.accessToken, 'a1');
|
||||
expect(auth.user?.nickname, 'Tester');
|
||||
// 落盘:新建一个 store 也能读回来
|
||||
expect((await AuthSessionStore().read())?.refreshToken, 'r1');
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'should_refresh_once_and_retry_with_new_token_when_response_code_is_401',
|
||||
() async {
|
||||
var calls = 0;
|
||||
late AuthController auth;
|
||||
final adapter = FakeHttpAdapter((options) async {
|
||||
calls++;
|
||||
if (options.uri.path == '/app/auth/refresh') {
|
||||
return FakeHttpResponse(
|
||||
tokenBody(accessToken: 'a2', refreshToken: 'r2'),
|
||||
);
|
||||
}
|
||||
if (options.uri.path != '/app/me') {
|
||||
// 注册等其它请求:直接发第一版令牌
|
||||
return FakeHttpResponse(
|
||||
tokenBody(accessToken: 'a1', refreshToken: 'r1'),
|
||||
);
|
||||
}
|
||||
final token = options.headers['Authorization'];
|
||||
if (token == 'Bearer a1') {
|
||||
return FakeHttpResponse(
|
||||
jsonEncode({'code': 401, 'message': '未登录或登录已过期', 'data': null}),
|
||||
);
|
||||
}
|
||||
expect(token, 'Bearer a2');
|
||||
return FakeHttpResponse(
|
||||
jsonEncode({
|
||||
'code': 0,
|
||||
'message': 'ok',
|
||||
'data': {'hit': true},
|
||||
}),
|
||||
);
|
||||
});
|
||||
final store = AuthSessionStore();
|
||||
auth = AuthController(
|
||||
AuthApi(clientWith(() => auth, adapter: adapter)),
|
||||
store,
|
||||
);
|
||||
await auth.register(email: 'a@b.com', password: 'abcd1234');
|
||||
calls = 0;
|
||||
|
||||
final api = clientWith(() => auth, adapter: adapter);
|
||||
final data = await api.get('/app/me');
|
||||
expect((data as Map)['hit'], isTrue);
|
||||
expect(calls, 3); // 原请求(401) + 刷新 + 重试
|
||||
expect(auth.accessToken, 'a2');
|
||||
},
|
||||
);
|
||||
|
||||
test('should_clear_session_when_refresh_itself_fails', () async {
|
||||
late AuthController auth;
|
||||
@@ -127,7 +139,9 @@ void main() {
|
||||
);
|
||||
}
|
||||
if (options.uri.path == '/app/auth/register') {
|
||||
return FakeHttpResponse(tokenBody(accessToken: 'a1', refreshToken: 'r1'));
|
||||
return FakeHttpResponse(
|
||||
tokenBody(accessToken: 'a1', refreshToken: 'r1'),
|
||||
);
|
||||
}
|
||||
return FakeHttpResponse(
|
||||
jsonEncode({'code': 401, 'message': '未登录或登录已过期', 'data': null}),
|
||||
@@ -146,6 +160,40 @@ void main() {
|
||||
expect(await AuthSessionStore().read(), isNull);
|
||||
});
|
||||
|
||||
test(
|
||||
'should_keep_session_when_refresh_fails_because_server_is_down',
|
||||
() async {
|
||||
late AuthController auth;
|
||||
final adapter = FakeHttpAdapter((options) async {
|
||||
if (options.uri.path == '/app/auth/refresh') {
|
||||
return FakeHttpResponse(
|
||||
jsonEncode({'code': 500, 'message': '服务重启中', 'data': null}),
|
||||
);
|
||||
}
|
||||
if (options.uri.path == '/app/auth/register') {
|
||||
return FakeHttpResponse(
|
||||
tokenBody(accessToken: 'a1', refreshToken: 'r1'),
|
||||
);
|
||||
}
|
||||
return FakeHttpResponse(
|
||||
jsonEncode({'code': 401, 'message': '未登录或登录已过期', 'data': null}),
|
||||
);
|
||||
});
|
||||
final store = AuthSessionStore();
|
||||
auth = AuthController(
|
||||
AuthApi(clientWith(() => auth, adapter: adapter)),
|
||||
store,
|
||||
);
|
||||
await auth.register(email: 'a@b.com', password: 'abcd1234');
|
||||
|
||||
final api = clientWith(() => auth, adapter: adapter);
|
||||
await expectLater(api.get('/app/me'), throwsA(isA<ApiException>()));
|
||||
expect(auth.isSignedIn, isTrue);
|
||||
expect(auth.accessToken, 'a1');
|
||||
expect((await AuthSessionStore().read())?.refreshToken, 'r1');
|
||||
},
|
||||
);
|
||||
|
||||
test('should_clear_local_session_when_sign_out_hits_server_error', () async {
|
||||
late AuthController auth;
|
||||
final adapter = FakeHttpAdapter((options) async {
|
||||
@@ -183,7 +231,9 @@ void main() {
|
||||
final body = (options.data as Map).cast<String, dynamic>();
|
||||
expect(body['targetType'], 'PHONE');
|
||||
expect(body['code'], '123456');
|
||||
return FakeHttpResponse(tokenBody(accessToken: 'p1', refreshToken: 'pr1'));
|
||||
return FakeHttpResponse(
|
||||
tokenBody(accessToken: 'p1', refreshToken: 'pr1'),
|
||||
);
|
||||
});
|
||||
final store = AuthSessionStore();
|
||||
auth = AuthController(
|
||||
@@ -205,7 +255,9 @@ void main() {
|
||||
final body = (options.data as Map).cast<String, dynamic>();
|
||||
expect(body['targetType'], 'PHONE');
|
||||
expect(body['code'], '654321');
|
||||
return FakeHttpResponse(tokenBody(accessToken: 'p2', refreshToken: 'pr2'));
|
||||
return FakeHttpResponse(
|
||||
tokenBody(accessToken: 'p2', refreshToken: 'pr2'),
|
||||
);
|
||||
});
|
||||
auth = AuthController(
|
||||
AuthApi(clientWith(() => auth, adapter: adapter)),
|
||||
|
||||
Reference in new issue
Block a user