Refactor tests for improved consistency and readability
- Updated test cases in `test_auth_endpoints.py` to ensure consistent formatting and style. - Enhanced `test_socket_endpoints.py` with consistent parameter formatting and improved readability. - Cleaned up `conftest.py` by ensuring consistent parameter formatting in fixtures. - Added comprehensive tests for API token dependencies in `test_api_token_dependencies.py`. - Refactored `test_auth_service.py` to maintain consistent parameter formatting. - Cleaned up `test_oauth_service.py` by removing unnecessary imports. - Improved `test_socket_service.py` with consistent formatting and readability. - Enhanced `test_cookies.py` by ensuring consistent formatting and readability. - Introduced new tests for token utilities in `test_token_utils.py` to validate token generation and expiration logic.
This commit is contained in:
@@ -11,14 +11,22 @@ from app.core.config import settings
|
||||
from app.core.dependencies import (
|
||||
get_auth_service,
|
||||
get_current_active_user,
|
||||
get_current_active_user_flexible,
|
||||
get_oauth_service,
|
||||
)
|
||||
from app.core.logging import get_logger
|
||||
from app.models.user import User
|
||||
from app.schemas.auth import UserLoginRequest, UserRegisterRequest, UserResponse
|
||||
from app.schemas.auth import (
|
||||
ApiTokenRequest,
|
||||
ApiTokenResponse,
|
||||
ApiTokenStatusResponse,
|
||||
UserLoginRequest,
|
||||
UserRegisterRequest,
|
||||
UserResponse,
|
||||
)
|
||||
from app.services.auth import AuthService
|
||||
from app.services.oauth import OAuthService
|
||||
from app.utils.auth import JWTUtils
|
||||
from app.utils.auth import JWTUtils, TokenUtils
|
||||
|
||||
router = APIRouter()
|
||||
logger = get_logger(__name__)
|
||||
@@ -131,7 +139,7 @@ async def login(
|
||||
|
||||
@router.get("/me")
|
||||
async def get_current_user_info(
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)],
|
||||
auth_service: Annotated[AuthService, Depends(get_auth_service)],
|
||||
) -> UserResponse:
|
||||
"""Get current user information."""
|
||||
@@ -426,3 +434,72 @@ async def exchange_oauth_token(
|
||||
user_id = token_data["user_id"]
|
||||
logger.info("OAuth tokens exchanged successfully for user: %s", user_id)
|
||||
return {"message": "Tokens set successfully", "user_id": str(user_id)}
|
||||
|
||||
|
||||
# API Token endpoints
|
||||
@router.post("/api-token")
|
||||
async def generate_api_token(
|
||||
request: ApiTokenRequest,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
auth_service: Annotated[AuthService, Depends(get_auth_service)],
|
||||
) -> ApiTokenResponse:
|
||||
"""Generate a new API token for the current user."""
|
||||
try:
|
||||
api_token = await auth_service.generate_api_token(
|
||||
current_user,
|
||||
expires_days=request.expires_days,
|
||||
)
|
||||
|
||||
# Refresh user to get updated token info
|
||||
await auth_service.session.refresh(current_user)
|
||||
|
||||
return ApiTokenResponse(
|
||||
api_token=api_token,
|
||||
expires_at=current_user.api_token_expires_at,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception(
|
||||
"Failed to generate API token for user: %s", current_user.email,
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to generate API token",
|
||||
) from e
|
||||
|
||||
|
||||
@router.get("/api-token/status")
|
||||
async def get_api_token_status(
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> ApiTokenStatusResponse:
|
||||
"""Get the current user's API token status."""
|
||||
has_token = current_user.api_token is not None
|
||||
is_expired = False
|
||||
|
||||
if has_token and current_user.api_token_expires_at:
|
||||
is_expired = TokenUtils.is_token_expired(current_user.api_token_expires_at)
|
||||
|
||||
return ApiTokenStatusResponse(
|
||||
has_token=has_token,
|
||||
expires_at=current_user.api_token_expires_at,
|
||||
is_expired=is_expired,
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/api-token")
|
||||
async def revoke_api_token(
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
auth_service: Annotated[AuthService, Depends(get_auth_service)],
|
||||
) -> dict[str, str]:
|
||||
"""Revoke the current user's API token."""
|
||||
try:
|
||||
await auth_service.revoke_api_token(current_user)
|
||||
except Exception as e:
|
||||
logger.exception(
|
||||
"Failed to revoke API token for user: %s", current_user.email,
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to revoke API token",
|
||||
) from e
|
||||
else:
|
||||
return {"message": "API token revoked successfully"}
|
||||
|
||||
Reference in New Issue
Block a user