refactor: consolidate linting tools into Ruff
Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
+28
-27
@@ -15,21 +15,23 @@ import pytest
|
||||
from tests.mock_oauth_server import MockOAuth2ServerContainer, create_test_userinfo
|
||||
|
||||
# Check if we should use real OAuth credentials from environment
|
||||
_REAL_OAUTH_AVAILABLE = all([
|
||||
os.environ.get("AUTHENTIK_CLIENT_ID") not in {"", "NOT_SET", "test-key", None},
|
||||
os.environ.get("AUTHENTIK_CLIENT_SECRET") not in {"", "NOT_SET", "test-key", None},
|
||||
os.environ.get("AUTHENTIK_CONFIG_URL") not in {"", "NOT_SET", "test-key", None},
|
||||
])
|
||||
_REAL_OAUTH_AVAILABLE = all(
|
||||
[
|
||||
os.environ.get("AUTHENTIK_CLIENT_ID") not in {"", "NOT_SET", "test-key", None},
|
||||
os.environ.get("AUTHENTIK_CLIENT_SECRET") not in {"", "NOT_SET", "test-key", None},
|
||||
os.environ.get("AUTHENTIK_CONFIG_URL") not in {"", "NOT_SET", "test-key", None},
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def use_real_oauth() -> bool:
|
||||
"""
|
||||
Determine if tests should use real OAuth credentials.
|
||||
|
||||
|
||||
Returns True if valid OAuth credentials are available in the environment
|
||||
(typically from GitHub Actions secrets).
|
||||
|
||||
|
||||
Returns:
|
||||
bool: True if real OAuth should be used, False for mock
|
||||
"""
|
||||
@@ -38,7 +40,7 @@ def use_real_oauth() -> bool:
|
||||
return True
|
||||
if os.environ.get("USE_MOCK_OAUTH", "").lower() in ("true", "1", "yes"):
|
||||
return False
|
||||
|
||||
|
||||
return _REAL_OAUTH_AVAILABLE
|
||||
|
||||
|
||||
@@ -46,10 +48,10 @@ def use_real_oauth() -> bool:
|
||||
def mock_oauth_server() -> Generator[MockOAuth2ServerContainer, None, None]:
|
||||
"""
|
||||
Provide a mock OAuth2/OIDC server for testing.
|
||||
|
||||
|
||||
This fixture starts a mock-oauth2-server container that provides
|
||||
a complete OIDC provider with all necessary endpoints.
|
||||
|
||||
|
||||
Yields:
|
||||
MockOAuth2ServerContainer: Running mock OAuth server
|
||||
"""
|
||||
@@ -57,7 +59,7 @@ def mock_oauth_server() -> Generator[MockOAuth2ServerContainer, None, None]:
|
||||
if not _REAL_OAUTH_AVAILABLE or os.environ.get("USE_MOCK_OAUTH", "").lower() in ("true", "1", "yes"):
|
||||
container = MockOAuth2ServerContainer()
|
||||
container.start()
|
||||
|
||||
|
||||
try:
|
||||
# Wait for the server to be ready
|
||||
container.wait_for_ready()
|
||||
@@ -72,13 +74,13 @@ def mock_oauth_server() -> Generator[MockOAuth2ServerContainer, None, None]:
|
||||
def oauth_config(mock_oauth_server: Optional[MockOAuth2ServerContainer], use_real_oauth: bool) -> Dict[str, str]:
|
||||
"""
|
||||
Provide OAuth configuration for tests.
|
||||
|
||||
|
||||
Returns either mock OAuth config or real OAuth config based on availability.
|
||||
|
||||
|
||||
Args:
|
||||
mock_oauth_server: Mock OAuth server fixture (may be None if using real)
|
||||
use_real_oauth: Whether to use real OAuth credentials
|
||||
|
||||
|
||||
Returns:
|
||||
Dictionary with OAuth configuration
|
||||
"""
|
||||
@@ -95,7 +97,7 @@ def oauth_config(mock_oauth_server: Optional[MockOAuth2ServerContainer], use_rea
|
||||
# Use mock OAuth server
|
||||
if mock_oauth_server is None:
|
||||
pytest.fail("Mock OAuth server not available and real credentials not configured")
|
||||
|
||||
|
||||
config = mock_oauth_server.get_config()
|
||||
return {
|
||||
"client_id": "test-client-id",
|
||||
@@ -114,7 +116,7 @@ def oauth_config(mock_oauth_server: Optional[MockOAuth2ServerContainer], use_rea
|
||||
def test_user_info() -> Dict:
|
||||
"""
|
||||
Provide test user information for OAuth flows.
|
||||
|
||||
|
||||
Returns:
|
||||
Dictionary with test user claims
|
||||
"""
|
||||
@@ -135,25 +137,25 @@ def oauth_test_token(
|
||||
) -> Optional[str]:
|
||||
"""
|
||||
Generate a test OAuth token.
|
||||
|
||||
|
||||
For mock mode: Creates a valid JWT from the mock server.
|
||||
For real mode: Skips (would need real authentication flow).
|
||||
|
||||
|
||||
Args:
|
||||
mock_oauth_server: Mock OAuth server
|
||||
test_user_info: User information to include in token
|
||||
use_real_oauth: Whether using real OAuth
|
||||
|
||||
|
||||
Returns:
|
||||
JWT token string or None if using real OAuth
|
||||
"""
|
||||
if use_real_oauth:
|
||||
# Can't generate tokens for real OAuth - would need actual auth flow
|
||||
return None
|
||||
|
||||
|
||||
if mock_oauth_server is None:
|
||||
pytest.fail("Mock OAuth server not available")
|
||||
|
||||
|
||||
# Create a token with the test user info
|
||||
return mock_oauth_server.create_token(
|
||||
subject=test_user_info["sub"],
|
||||
@@ -171,21 +173,20 @@ def oauth_test_token(
|
||||
def oauth_enabled_app(oauth_config: Dict[str, str]):
|
||||
"""
|
||||
Configure the FastAPI app with OAuth enabled for testing.
|
||||
|
||||
|
||||
This fixture temporarily enables OAuth and configures it with the
|
||||
test OAuth provider (mock or real).
|
||||
|
||||
|
||||
Args:
|
||||
oauth_config: OAuth configuration
|
||||
|
||||
|
||||
Yields:
|
||||
Configured test client
|
||||
"""
|
||||
import os
|
||||
|
||||
from app.main import app
|
||||
import app.auth as auth_module
|
||||
from app.auth import login, oauth_login, oauth_callback, auth, logout
|
||||
from app.auth import auth, login, logout, oauth_callback, oauth_login
|
||||
from app.main import app
|
||||
|
||||
# Save original state
|
||||
original_auth_enabled = auth_module.AUTH_ENABLED
|
||||
|
||||
Reference in New Issue
Block a user