refactor(frontend): adapt to RESTful API routes

This commit is contained in:
qzl
2026-02-26 14:28:58 +08:00
parent 5b8b584013
commit d635d9a5e0
16 changed files with 210 additions and 115 deletions
@@ -34,16 +34,13 @@ void main() {
});
group('AuthRepositoryImpl', () {
test('signupStart calls api and returns response', () async {
when(() => mockApi.signupStart(any())).thenAnswer(
(_) async => const SignupStartResponse(
status: 'pending_verification',
email: 'test@example.com',
message: 'Verification code sent',
),
test('createVerification calls api and returns response', () async {
when(() => mockApi.createVerification(any())).thenAnswer(
(_) async =>
const VerificationCreateResponse(email: 'test@example.com'),
);
final result = await repository.signupStart(
final result = await repository.createVerification(
const SignupStartRequest(
username: 'testuser',
email: 'test@example.com',
@@ -51,12 +48,12 @@ void main() {
),
);
expect(result.status, 'pending_verification');
verify(() => mockApi.signupStart(any())).called(1);
expect(result.email, 'test@example.com');
verify(() => mockApi.createVerification(any())).called(1);
});
test('login calls api and saves tokens', () async {
when(() => mockApi.login(any())).thenAnswer(
test('createSession calls api and saves tokens', () async {
when(() => mockApi.createSession(any())).thenAnswer(
(_) async => AuthResponse(
accessToken: 'access_token',
refreshToken: 'refresh_token',
@@ -72,7 +69,7 @@ void main() {
),
).thenAnswer((_) async {});
final result = await repository.login(
final result = await repository.createSession(
const LoginRequest(email: 'test@example.com', password: 'password123'),
);
@@ -85,21 +82,24 @@ void main() {
).called(1);
});
test('logout calls api with refresh token and clears storage', () async {
when(
() => mockStorage.getRefreshToken(),
).thenAnswer((_) async => 'refresh_token');
when(() => mockApi.logout(any())).thenAnswer((_) async {});
when(() => mockStorage.clear()).thenAnswer((_) async {});
test(
'deleteSession calls api with refresh token and clears storage',
() async {
when(
() => mockStorage.getRefreshToken(),
).thenAnswer((_) async => 'refresh_token');
when(() => mockApi.deleteSession(any())).thenAnswer((_) async {});
when(() => mockStorage.clear()).thenAnswer((_) async {});
await repository.logout();
await repository.deleteSession();
verify(() => mockApi.logout(any())).called(1);
verify(() => mockStorage.clear()).called(1);
});
verify(() => mockApi.deleteSession(any())).called(1);
verify(() => mockStorage.clear()).called(1);
},
);
test('refresh saves new tokens', () async {
when(() => mockApi.refresh(any())).thenAnswer(
test('refreshSession saves new tokens', () async {
when(() => mockApi.refreshSession(any())).thenAnswer(
(_) async => AuthResponse(
accessToken: 'new_access',
refreshToken: 'new_refresh',
@@ -115,7 +115,7 @@ void main() {
),
).thenAnswer((_) async {});
final result = await repository.refresh('old_refresh');
final result = await repository.refreshSession('old_refresh');
expect(result.accessToken, 'new_access');
verify(
@@ -41,7 +41,7 @@ void main() {
when(
() => mockRepository.getRefreshToken(),
).thenAnswer((_) async => 'valid_refresh');
when(() => mockRepository.refresh('valid_refresh')).thenAnswer(
when(() => mockRepository.refreshSession('valid_refresh')).thenAnswer(
(_) async => AuthResponse(
accessToken: 'new_access',
refreshToken: 'new_refresh',
@@ -63,9 +63,9 @@ void main() {
() => mockRepository.getRefreshToken(),
).thenAnswer((_) async => 'expired_refresh');
when(
() => mockRepository.refresh('expired_refresh'),
() => mockRepository.refreshSession('expired_refresh'),
).thenThrow(Exception('Invalid refresh token'));
when(() => mockRepository.logout()).thenAnswer((_) async {});
when(() => mockRepository.deleteSession()).thenAnswer((_) async {});
return authBloc;
},
act: (bloc) => bloc.add(AuthStarted()),
@@ -86,7 +86,7 @@ void main() {
blocTest<AuthBloc, AuthState>(
'emits [AuthUnauthenticated] when AuthLoggedOut',
build: () {
when(() => mockRepository.logout()).thenAnswer((_) async {});
when(() => mockRepository.deleteSession()).thenAnswer((_) async {});
return authBloc;
},
seed: () => AuthAuthenticated(
@@ -65,12 +65,8 @@ void main() {
password: const Password.dirty('password123'),
),
setUp: () {
when(() => mockRepository.signupStart(any())).thenAnswer(
(_) async => SignupStartResponse(
status: 'ok',
email: 'test@example.com',
message: 'Code sent',
),
when(() => mockRepository.createVerification(any())).thenAnswer(
(_) async => VerificationCreateResponse(email: 'test@example.com'),
);
},
act: (c) => c.sendCodeSilently(),
@@ -85,7 +81,7 @@ void main() {
),
],
verify: (_) {
verify(() => mockRepository.signupStart(any())).called(1);
verify(() => mockRepository.createVerification(any())).called(1);
},
);
@@ -99,7 +95,7 @@ void main() {
),
setUp: () {
when(
() => mockRepository.signupStart(any()),
() => mockRepository.createVerification(any()),
).thenThrow(ServerException('Network error'));
},
act: (c) => c.sendCodeSilently(),
@@ -113,12 +109,12 @@ void main() {
),
],
verify: (_) {
verify(() => mockRepository.signupStart(any())).called(1);
verify(() => mockRepository.createVerification(any())).called(1);
},
);
blocTest<RegisterCubit, RegisterState>(
'does not call signupStart when input is invalid',
'does not call createVerification when input is invalid',
build: () => cubit,
seed: () => RegisterState(
username: const Username.dirty(''),
@@ -128,7 +124,7 @@ void main() {
act: (c) => c.sendCodeSilently(),
expect: () => [],
verify: (_) {
verifyNever(() => mockRepository.signupStart(any()));
verifyNever(() => mockRepository.createVerification(any()));
},
);
@@ -144,7 +140,7 @@ void main() {
act: (c) => c.sendCodeSilently(),
expect: () => [],
verify: (_) {
verifyNever(() => mockRepository.signupStart(any()));
verifyNever(() => mockRepository.createVerification(any()));
},
);
});
@@ -161,7 +157,7 @@ void main() {
.having((s) => s.errorMessage, 'errorMessage', '验证码发送失败,请返回上一步重试'),
],
verify: (_) {
verifyNever(() => mockRepository.signupResend(any()));
verifyNever(() => mockRepository.resendVerification(any()));
},
);
@@ -171,8 +167,8 @@ void main() {
seed: () => RegisterState(pendingEmail: 'test@example.com'),
setUp: () {
when(
() => mockRepository.signupResend(any()),
).thenAnswer((_) async => SignupResendResponse(message: 'Code sent'));
() => mockRepository.resendVerification(any()),
).thenAnswer((_) async {});
},
act: (c) => c.resendCode(),
expect: () => [
@@ -188,7 +184,7 @@ void main() {
),
],
verify: (_) {
verify(() => mockRepository.signupResend(any())).called(1);
verify(() => mockRepository.resendVerification(any())).called(1);
},
);
@@ -198,7 +194,7 @@ void main() {
seed: () => RegisterState(pendingEmail: 'test@example.com'),
setUp: () {
when(
() => mockRepository.signupResend(any()),
() => mockRepository.resendVerification(any()),
).thenThrow(ServerException('Network error'));
},
act: (c) => c.resendCode(),