test: 통합 테스트 오류 및 경고 수정
- 모든 서비스 메서드 시그니처를 실제 구현에 맞게 수정 - TestDataGenerator 제거하고 직접 객체 생성으로 변경 - 모델 필드명 및 타입 불일치 수정 - 불필요한 Either 패턴 사용 제거 - null safety 관련 이슈 해결 수정된 파일: - test/integration/screens/company_integration_test.dart - test/integration/screens/equipment_integration_test.dart - test/integration/screens/user_integration_test.dart - test/integration/screens/login_integration_test.dart
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../../../../core/constants/api_endpoints.dart';
|
||||
import '../../../../services/auth_service.dart';
|
||||
import '../../../../core/config/environment.dart';
|
||||
|
||||
/// 인증 인터셉터
|
||||
class AuthInterceptor extends Interceptor {
|
||||
@@ -15,7 +17,9 @@ class AuthInterceptor extends Interceptor {
|
||||
_authService ??= GetIt.instance<AuthService>();
|
||||
return _authService;
|
||||
} catch (e) {
|
||||
print('Failed to get AuthService in AuthInterceptor: $e');
|
||||
if (kDebugMode) {
|
||||
debugPrint('Failed to get AuthService in AuthInterceptor: $e');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -25,34 +29,50 @@ class AuthInterceptor extends Interceptor {
|
||||
RequestOptions options,
|
||||
RequestInterceptorHandler handler,
|
||||
) async {
|
||||
print('[AuthInterceptor] onRequest: ${options.method} ${options.path}');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] onRequest: ${options.method} ${options.path}');
|
||||
}
|
||||
|
||||
// 로그인, 토큰 갱신 요청은 토큰 없이 진행
|
||||
if (_isAuthEndpoint(options.path)) {
|
||||
print('[AuthInterceptor] Auth endpoint detected, skipping token attachment');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] Auth endpoint detected, skipping token attachment');
|
||||
}
|
||||
handler.next(options);
|
||||
return;
|
||||
}
|
||||
|
||||
// 저장된 액세스 토큰 가져오기
|
||||
final service = authService;
|
||||
print('[AuthInterceptor] AuthService available: ${service != null}');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] AuthService available: ${service != null}');
|
||||
}
|
||||
|
||||
if (service != null) {
|
||||
final accessToken = await service.getAccessToken();
|
||||
print('[AuthInterceptor] Access token retrieved: ${accessToken != null ? 'Yes (${accessToken.substring(0, 10)}...)' : 'No'}');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] Access token retrieved: ${accessToken != null ? 'Yes (${accessToken.substring(0, 10)}...)' : 'No'}');
|
||||
}
|
||||
|
||||
if (accessToken != null) {
|
||||
options.headers['Authorization'] = 'Bearer $accessToken';
|
||||
print('[AuthInterceptor] Authorization header set: Bearer ${accessToken.substring(0, 10)}...');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] Authorization header set: Bearer ${accessToken.substring(0, 10)}...');
|
||||
}
|
||||
} else {
|
||||
print('[AuthInterceptor] WARNING: No access token available for protected endpoint');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] WARNING: No access token available for protected endpoint');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
print('[AuthInterceptor] ERROR: AuthService not available from GetIt');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] ERROR: AuthService not available from GetIt');
|
||||
}
|
||||
}
|
||||
|
||||
print('[AuthInterceptor] Final headers: ${options.headers}');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] Final headers: ${options.headers}');
|
||||
}
|
||||
handler.next(options);
|
||||
}
|
||||
|
||||
@@ -61,30 +81,40 @@ class AuthInterceptor extends Interceptor {
|
||||
DioException err,
|
||||
ErrorInterceptorHandler handler,
|
||||
) async {
|
||||
print('[AuthInterceptor] onError: ${err.response?.statusCode} ${err.message}');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] onError: ${err.response?.statusCode} ${err.message}');
|
||||
}
|
||||
|
||||
// 401 Unauthorized 에러 처리
|
||||
if (err.response?.statusCode == 401) {
|
||||
// 인증 관련 엔드포인트는 재시도하지 않음
|
||||
if (_isAuthEndpoint(err.requestOptions.path)) {
|
||||
print('[AuthInterceptor] Auth endpoint 401 error, skipping retry');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] Auth endpoint 401 error, skipping retry');
|
||||
}
|
||||
handler.next(err);
|
||||
return;
|
||||
}
|
||||
|
||||
final service = authService;
|
||||
if (service != null) {
|
||||
print('[AuthInterceptor] Attempting token refresh...');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] Attempting token refresh...');
|
||||
}
|
||||
// 토큰 갱신 시도
|
||||
final refreshResult = await service.refreshToken();
|
||||
|
||||
final refreshSuccess = refreshResult.fold(
|
||||
(failure) {
|
||||
print('[AuthInterceptor] Token refresh failed: ${failure.message}');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] Token refresh failed: ${failure.message}');
|
||||
}
|
||||
return false;
|
||||
},
|
||||
(tokenResponse) {
|
||||
print('[AuthInterceptor] Token refresh successful');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] Token refresh successful');
|
||||
}
|
||||
return true;
|
||||
},
|
||||
);
|
||||
@@ -95,7 +125,9 @@ class AuthInterceptor extends Interceptor {
|
||||
final newAccessToken = await service.getAccessToken();
|
||||
|
||||
if (newAccessToken != null) {
|
||||
print('[AuthInterceptor] Retrying request with new token');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] Retrying request with new token');
|
||||
}
|
||||
err.requestOptions.headers['Authorization'] = 'Bearer $newAccessToken';
|
||||
|
||||
// dio 인스턴스를 통해 재시도
|
||||
@@ -104,7 +136,9 @@ class AuthInterceptor extends Interceptor {
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
print('[AuthInterceptor] Request retry failed: $e');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] Request retry failed: $e');
|
||||
}
|
||||
// 재시도 실패
|
||||
handler.next(err);
|
||||
return;
|
||||
@@ -112,7 +146,9 @@ class AuthInterceptor extends Interceptor {
|
||||
}
|
||||
|
||||
// 토큰 갱신 실패 시 로그인 화면으로 이동
|
||||
print('[AuthInterceptor] Clearing session due to auth failure');
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
debugPrint('[AuthInterceptor] Clearing session due to auth failure');
|
||||
}
|
||||
await service.clearSession();
|
||||
// TODO: Navigate to login screen
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user