π OpenHands: AI-Driven Development
Fix authentication setup issues in unit tests (#10118)
Graham Neubig7ab2ad2c1bd8
diff --git a/tests/unit/test_get_repository_microagents.py b/tests/unit/test_get_repository_microagents.pyindex 1194ff6f6c..d4a87f3eed 100644--- a/tests/unit/test_get_repository_microagents.py+++ b/tests/unit/test_get_repository_microagents.py@@ -13,6 +13,7 @@ from openhands.integrations.service_types import (Repository,)from openhands.microagent.types import MicroagentContentResponse+from openhands.server.dependencies import check_session_api_keyfrom openhands.server.routes.git import app as git_appfrom openhands.server.user_auth import (get_access_token,@@ -49,10 +50,15 @@ def test_client():def mock_get_user_id():return 'test_user'+ def mock_check_session_api_key():+ # Mock session API key check to always pass for tests+ return None+# Override the dependencies in the appapp.dependency_overrides[get_provider_tokens] = mock_get_provider_tokensapp.dependency_overrides[get_access_token] = mock_get_access_tokenapp.dependency_overrides[get_user_id] = mock_get_user_id+ app.dependency_overrides[check_session_api_key] = mock_check_session_api_keyyield TestClient(app)diff --git a/tests/unit/test_middleware.py b/tests/unit/test_middleware.pyindex d83004e54f..2bdf2275fc 100644--- a/tests/unit/test_middleware.py+++ b/tests/unit/test_middleware.py@@ -46,24 +46,32 @@ def test_localhost_cors_middleware_init_without_env_var():def test_localhost_cors_middleware_is_allowed_origin_localhost(app):- """Test that localhost origins are allowed regardless of port."""- app.add_middleware(LocalhostCORSMiddleware)- client = TestClient(app)+ """Test that localhost origins are allowed regardless of port when no specific origins are configured."""+ # Test without setting PERMITTED_CORS_ORIGINS to trigger localhost behavior+ with patch.dict(os.environ, {}, clear=True):+ app.add_middleware(LocalhostCORSMiddleware)+ client = TestClient(app)- # Test with localhost- response = client.get('/test', headers={'Origin': 'http://localhost:8000'})- assert response.status_code == 200- assert response.headers['access-control-allow-origin'] == 'http://localhost:8000'+ # Test with localhost+ response = client.get('/test', headers={'Origin': 'http://localhost:8000'})+ assert response.status_code == 200+ assert (+ response.headers['access-control-allow-origin'] == 'http://localhost:8000'+ )- # Test with different port- response = client.get('/test', headers={'Origin': 'http://localhost:3000'})- assert response.status_code == 200- assert response.headers['access-control-allow-origin'] == 'http://localhost:3000'+ # Test with different port+ response = client.get('/test', headers={'Origin': 'http://localhost:3000'})+ assert response.status_code == 200+ assert (+ response.headers['access-control-allow-origin'] == 'http://localhost:3000'+ )- # Test with 127.0.0.1- response = client.get('/test', headers={'Origin': 'http://127.0.0.1:8000'})- assert response.status_code == 200- assert response.headers['access-control-allow-origin'] == 'http://127.0.0.1:8000'+ # Test with 127.0.0.1+ response = client.get('/test', headers={'Origin': 'http://127.0.0.1:8000'})+ assert response.status_code == 200+ assert (+ response.headers['access-control-allow-origin'] == 'http://127.0.0.1:8000'+ )def test_localhost_cors_middleware_is_allowed_origin_non_localhost(app):@@ -87,14 +95,15 @@ def test_localhost_cors_middleware_is_allowed_origin_non_localhost(app):def test_localhost_cors_middleware_missing_origin(app):"""Test behavior when Origin header is missing."""- app.add_middleware(LocalhostCORSMiddleware)- client = TestClient(app)-- # Test without Origin header- response = client.get('/test')- assert response.status_code == 200- # There should be no access-control-allow-origin header- assert 'access-control-allow-origin' not in response.headers+ with patch.dict(os.environ, {}, clear=True):+ app.add_middleware(LocalhostCORSMiddleware)+ client = TestClient(app)++ # Test without Origin header+ response = client.get('/test')+ assert response.status_code == 200+ # There should be no access-control-allow-origin header+ assert 'access-control-allow-origin' not in response.headersdef test_localhost_cors_middleware_inheritance():