Refactor auth dependencies and add comprehensive tests
Moved `auth` module from `dependencies.py` to `dependencies/auth.py` for better organization. Added extensive unit tests for authentication services and API dependencies to ensure robust verification of users, tokens, and permissions.
This commit is contained in:
0
backend/app/api/dependencies/__init__.py
Normal file
0
backend/app/api/dependencies/__init__.py
Normal file
@@ -1,4 +1,3 @@
|
||||
# app/api/dependencies/auth.py
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import Depends, HTTPException, status
|
||||
@@ -19,14 +18,14 @@ def get_current_user(
|
||||
) -> User:
|
||||
"""
|
||||
Get the current authenticated user.
|
||||
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
token: JWT token from request
|
||||
|
||||
|
||||
Returns:
|
||||
User: The authenticated user
|
||||
|
||||
|
||||
Raises:
|
||||
HTTPException: If authentication fails
|
||||
"""
|
||||
@@ -69,13 +68,13 @@ def get_current_active_user(
|
||||
) -> User:
|
||||
"""
|
||||
Check if the current user is active.
|
||||
|
||||
|
||||
Args:
|
||||
current_user: The current authenticated user
|
||||
|
||||
|
||||
Returns:
|
||||
User: The authenticated and active user
|
||||
|
||||
|
||||
Raises:
|
||||
HTTPException: If user is inactive
|
||||
"""
|
||||
@@ -92,13 +91,13 @@ def get_current_superuser(
|
||||
) -> User:
|
||||
"""
|
||||
Check if the current user is a superuser.
|
||||
|
||||
|
||||
Args:
|
||||
current_user: The current authenticated user
|
||||
|
||||
|
||||
Returns:
|
||||
User: The authenticated superuser
|
||||
|
||||
|
||||
Raises:
|
||||
HTTPException: If user is not a superuser
|
||||
"""
|
||||
@@ -117,11 +116,11 @@ def get_optional_current_user(
|
||||
"""
|
||||
Get the current user if authenticated, otherwise return None.
|
||||
Useful for endpoints that work with both authenticated and unauthenticated users.
|
||||
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
token: JWT token from request
|
||||
|
||||
|
||||
Returns:
|
||||
User or None: The authenticated user or None
|
||||
"""
|
||||
Reference in New Issue
Block a user