fix(test): fix task __wrapped__ calls, auth imports, mock chains, async markers, and rate limit mock

Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-02-12 13:03:26 +00:00
parent c297577677
commit 604facf36f
6 changed files with 144 additions and 135 deletions
+2 -2
View File
@@ -205,8 +205,8 @@ def oauth_enabled_app(oauth_config: Dict[str, str]):
from fastapi.testclient import TestClient
from app.main import app
# Create test client
client = TestClient(app)
# Create test client with base_url to satisfy TrustedHostMiddleware
client = TestClient(app, base_url="http://localhost")
yield client
+23 -24
View File
@@ -163,7 +163,7 @@ class TestRequireLogin:
mock_request.session = {"user": {"id": "123"}}
mock_request.url = "http://localhost/test"
result = test_sync_endpoint(mock_request)
result = await test_sync_endpoint(mock_request)
assert result == {"message": "success"}
@@ -180,7 +180,7 @@ class TestRequireLogin:
mock_request.session = {}
mock_request.url = "http://localhost/protected"
result = test_sync_endpoint(mock_request)
result = await test_sync_endpoint(mock_request)
assert isinstance(result, RedirectResponse)
assert result.status_code == status.HTTP_302_FOUND
@@ -192,43 +192,42 @@ class TestOAuthConfiguration:
def test_oauth_not_configured_without_credentials(self):
"""Test OAuth is not configured when credentials are missing."""
with patch("app.auth.settings") as mock_settings:
mock_settings.auth_enabled = True
mock_settings.authentik_client_id = None
mock_settings.authentik_client_secret = None
import importlib
# Re-import to trigger configuration logic
import importlib
import app.auth
import app.auth
try:
with patch("app.config.settings") as mock_settings:
mock_settings.auth_enabled = True
mock_settings.authentik_client_id = None
mock_settings.authentik_client_secret = None
importlib.reload(app.auth)
assert app.auth.OAUTH_CONFIGURED is False
finally:
importlib.reload(app.auth)
from app.auth import OAUTH_CONFIGURED
assert OAUTH_CONFIGURED is False
def test_oauth_configured_with_credentials(self):
"""Test OAuth is configured when credentials are provided."""
with patch("app.auth.settings") as mock_settings:
with patch("app.auth.oauth") as mock_oauth:
import importlib
import app.auth
try:
with patch("app.config.settings") as mock_settings:
mock_settings.auth_enabled = True
mock_settings.authentik_client_id = "test_client_id"
mock_settings.authentik_client_secret = "test_secret"
mock_settings.authentik_config_url = "https://auth.example.com/.well-known/openid-configuration"
mock_settings.oauth_provider_name = "Test SSO"
# Re-import to trigger configuration logic
import importlib
import app.auth
importlib.reload(app.auth)
from app.auth import OAUTH_CONFIGURED, OAUTH_PROVIDER_NAME
assert OAUTH_CONFIGURED is True
assert OAUTH_PROVIDER_NAME == "Test SSO"
assert app.auth.OAUTH_CONFIGURED is True
assert app.auth.OAUTH_PROVIDER_NAME == "Test SSO"
finally:
importlib.reload(app.auth)
@pytest.mark.unit
+8
View File
@@ -64,6 +64,7 @@ class TestOAuthLoginFlow:
class TestOAuthCallback:
"""Test OAuth callback handling."""
@pytest.mark.asyncio
@patch("app.auth.oauth.authentik.authorize_access_token")
async def test_oauth_callback_with_valid_token(
self, mock_authorize, oauth_enabled_app: TestClient, test_user_info: dict
@@ -86,6 +87,7 @@ class TestOAuthCallback:
# Should redirect after successful login
assert response.status_code == 302
@pytest.mark.asyncio
@patch("app.auth.oauth.authentik.authorize_access_token")
async def test_oauth_callback_stores_user_in_session(
self, mock_authorize, oauth_enabled_app: TestClient, test_user_info: dict
@@ -108,6 +110,7 @@ class TestOAuthCallback:
# Should set session cookie
assert "set-cookie" in response.headers or response.status_code == 302
@pytest.mark.asyncio
@patch("app.auth.oauth.authentik.authorize_access_token")
async def test_oauth_callback_with_admin_user(
self, mock_authorize, oauth_enabled_app: TestClient
@@ -131,6 +134,7 @@ class TestOAuthCallback:
# Should successfully authenticate
assert response.status_code == 302
@pytest.mark.asyncio
@patch("app.auth.oauth.authentik.authorize_access_token")
async def test_oauth_callback_rejects_non_admin(
self, mock_authorize, oauth_enabled_app: TestClient
@@ -160,6 +164,7 @@ class TestOAuthCallback:
class TestOAuthSessionManagement:
"""Test session management with OAuth authentication."""
@pytest.mark.asyncio
@patch("app.auth.oauth.authentik.authorize_access_token")
async def test_authenticated_user_can_access_protected_routes(
self, mock_authorize, oauth_enabled_app: TestClient, test_user_info: dict
@@ -192,6 +197,7 @@ class TestOAuthSessionManagement:
if response.status_code == 302:
assert "/login" in response.headers.get("location", "")
@pytest.mark.asyncio
@patch("app.auth.oauth.authentik.authorize_access_token")
async def test_logout_clears_session(
self, mock_authorize, oauth_enabled_app: TestClient, test_user_info: dict
@@ -226,6 +232,7 @@ class TestOAuthErrorHandling:
# Should handle error gracefully
assert response.status_code in [302, 400]
@pytest.mark.asyncio
@patch("app.auth.oauth.authentik.authorize_access_token")
async def test_oauth_callback_with_invalid_token(
self, mock_authorize, oauth_enabled_app: TestClient
@@ -244,6 +251,7 @@ class TestOAuthErrorHandling:
location = response.headers.get("location", "")
assert "error" in location.lower() or "login" in location.lower()
@pytest.mark.asyncio
@patch("app.auth.oauth.authentik.authorize_access_token")
async def test_oauth_callback_without_userinfo(
self, mock_authorize, oauth_enabled_app: TestClient
+1 -1
View File
@@ -136,7 +136,7 @@ class TestRateLimitDecorators:
# Mock limiter to return a simple passthrough decorator
mock_limiter = MagicMock()
mock_limiter.exempt.return_value = lambda f: f
mock_limiter.exempt.side_effect = lambda f: f
mock_get_limiter.return_value = mock_limiter
# Decorate function
+16 -19
View File
@@ -44,8 +44,8 @@ class TestStepTimeout:
# Mock database session with proper query chain
mock_db = MagicMock()
# Set up the query chain to return empty list
mock_db.query.return_value.filter.return_value.filter.return_value.filter.return_value.all.return_value = []
# Set up the query chain to return empty list (single .filter() call with multiple conditions)
mock_db.query.return_value.filter.return_value.all.return_value = []
# Run function
count = mark_stalled_steps_as_failed(mock_db)
@@ -74,8 +74,8 @@ class TestStepTimeout:
# Mock database session
mock_db = MagicMock()
# Set up the query chain to return stalled steps
mock_db.query.return_value.filter.return_value.filter.return_value.filter.return_value.all.return_value = [step1, step2]
# Set up the query chain to return stalled steps (single .filter() call with multiple conditions)
mock_db.query.return_value.filter.return_value.all.return_value = [step1, step2]
# Run function
count = mark_stalled_steps_as_failed(mock_db)
@@ -105,8 +105,8 @@ class TestStepTimeout:
# Mock database session
mock_db = MagicMock()
# Set up the query chain to return stalled step
mock_db.query.return_value.filter.return_value.filter.return_value.filter.return_value.all.return_value = [step]
# Set up the query chain to return stalled step (single .filter() call with multiple conditions)
mock_db.query.return_value.filter.return_value.all.return_value = [step]
# Run function with 150 second timeout
count = mark_stalled_steps_as_failed(mock_db, timeout_seconds=150)
@@ -131,9 +131,8 @@ class TestStepTimeout:
# Mock database session with file filter
mock_db = MagicMock()
# Set up the query chain with file filter
mock_query_chain = mock_db.query.return_value.filter.return_value.filter.return_value.filter.return_value
mock_query_chain.filter.return_value.all.return_value = [step]
# Set up the query chain with file filter (first .filter() for conditions, second for file_id)
mock_db.query.return_value.filter.return_value.filter.return_value.all.return_value = [step]
# Run function for specific file
count = mark_stalled_steps_as_failed(mock_db, file_id=42)
@@ -158,8 +157,8 @@ class TestStepTimeout:
# Mock database session
mock_db = MagicMock()
# Set up the query chain to return stalled step
mock_db.query.return_value.filter.return_value.filter.return_value.filter.return_value.all.return_value = [step]
# Set up the query chain to return stalled step (single .filter() call with multiple conditions)
mock_db.query.return_value.filter.return_value.all.return_value = [step]
# Run function
count = mark_stalled_steps_as_failed(mock_db, timeout_seconds=600)
@@ -186,8 +185,8 @@ class TestStepTimeout:
# Mock database session
mock_db = MagicMock()
# Set up the query chain to return stalled step
mock_db.query.return_value.filter.return_value.filter.return_value.filter.return_value.all.return_value = [step]
# Set up the query chain to return stalled step (single .filter() call with multiple conditions)
mock_db.query.return_value.filter.return_value.all.return_value = [step]
# Run function
count = mark_stalled_steps_as_failed(mock_db)
@@ -212,9 +211,8 @@ class TestStepTimeout:
# Mock database session
mock_db = MagicMock()
# Set up the query chain with file filter
mock_query_chain = mock_db.query.return_value.filter.return_value.filter.return_value.filter.return_value
mock_query_chain.filter.return_value.all.return_value = [step]
# Set up the query chain with file filter (first .filter() for conditions, second for file_id)
mock_db.query.return_value.filter.return_value.filter.return_value.all.return_value = [step]
# Run function
result = check_and_recover_stalled_file(mock_db, 42)
@@ -229,9 +227,8 @@ class TestStepTimeout:
# Mock database session with no stalled steps
mock_db = MagicMock()
# Set up the query chain with file filter
mock_query_chain = mock_db.query.return_value.filter.return_value.filter.return_value.filter.return_value
mock_query_chain.filter.return_value.all.return_value = []
# Set up the query chain with file filter (first .filter() for conditions, second for file_id)
mock_db.query.return_value.filter.return_value.filter.return_value.all.return_value = []
# Run function
result = check_and_recover_stalled_file(mock_db, 42)