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:
@@ -19,7 +19,7 @@ from app.schemas.auth import (
|
||||
UserResponse,
|
||||
)
|
||||
from app.services.oauth import OAuthUserInfo
|
||||
from app.utils.auth import JWTUtils, PasswordUtils
|
||||
from app.utils.auth import JWTUtils, PasswordUtils, TokenUtils
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -123,6 +123,37 @@ class AuthService:
|
||||
|
||||
return user
|
||||
|
||||
async def get_user_by_api_token(self, api_token: str) -> User | None:
|
||||
"""Get a user by their API token."""
|
||||
return await self.user_repo.get_by_api_token(api_token)
|
||||
|
||||
async def generate_api_token(self, user: User, expires_days: int = 365) -> str:
|
||||
"""Generate a new API token for a user."""
|
||||
# Generate a secure random token
|
||||
api_token = TokenUtils.generate_api_token()
|
||||
|
||||
# Set expiration date
|
||||
expires_at = datetime.now(UTC) + timedelta(days=expires_days)
|
||||
|
||||
# Update user with new API token
|
||||
update_data = {
|
||||
"api_token": api_token,
|
||||
"api_token_expires_at": expires_at,
|
||||
}
|
||||
await self.user_repo.update(user, update_data)
|
||||
|
||||
logger.info("Generated new API token for user: %s", user.email)
|
||||
return api_token
|
||||
|
||||
async def revoke_api_token(self, user: User) -> None:
|
||||
"""Revoke a user's API token."""
|
||||
update_data = {
|
||||
"api_token": None,
|
||||
"api_token_expires_at": None,
|
||||
}
|
||||
await self.user_repo.update(user, update_data)
|
||||
logger.info("Revoked API token for user: %s", user.email)
|
||||
|
||||
def _create_access_token(self, user: User) -> TokenResponse:
|
||||
"""Create an access token for a user."""
|
||||
access_token_expires = timedelta(
|
||||
|
||||
@@ -104,9 +104,8 @@ class SocketManager:
|
||||
await self.sio.emit(event, data, room=room_id)
|
||||
logger.debug(f"Sent {event} to user {user_id} in room {room_id}")
|
||||
return True
|
||||
else:
|
||||
logger.warning(f"User {user_id} not found in any room")
|
||||
return False
|
||||
logger.warning(f"User {user_id} not found in any room")
|
||||
return False
|
||||
|
||||
async def broadcast_to_all(self, event: str, data: dict):
|
||||
"""Broadcast a message to all connected users."""
|
||||
|
||||
Reference in New Issue
Block a user