From 16f3e3c6fd692ae334ca9441ada16565c6b40a18 Mon Sep 17 00:00:00 2001 From: Francesco Bartoli Date: Fri, 1 Nov 2024 19:44:14 +0100 Subject: [PATCH] Fix override of env variables during testing --- tests/conftest.py | 40 ++++++++++++++++++++++++++------------- tests/test_middlewares.py | 10 ++++------ 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b471ca2..f38f256 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -44,11 +44,13 @@ def _protected_app(): with mock.patch.dict( os.environ, { - "API_KEY_ENABLED": "true", - "PYGEOAPI_KEY_GLOBAL": "pygeoapi", - "JWKS_ENABLED": "false", - "OPA_ENABLED": "false", + "ENV_STATE": "dev", + "DEV_API_KEY_ENABLED": "true", + "DEV_PYGEOAPI_KEY_GLOBAL": "pygeoapi", + "DEV_JWKS_ENABLED": "false", + "DEV_OPA_ENABLED": "false", }, + clear=True, ): app = create_app() return app @@ -64,11 +66,13 @@ def _reverse_proxy_app(): with mock.patch.dict( os.environ, { - "API_KEY_ENABLED": "false", - "JWKS_ENABLED": "false", - "OPA_ENABLED": "false", - "FASTGEOAPI_REVERSE_PROXY": "true", + "ENV_STATE": "dev", + "DEV_API_KEY_ENABLED": "false", + "DEV_JWKS_ENABLED": "false", + "DEV_OPA_ENABLED": "false", + "DEV_FASTGEOAPI_REVERSE_PROXY": "true", }, + clear=True, ): app = create_app() return app @@ -84,12 +88,14 @@ def _protected_app(): with mock.patch.dict( os.environ, { - "API_KEY_ENABLED": "false", - "OAUTH2_JWKS_ENDPOINT": "https://76hxgq.logto.app/oidc/jwks", - "OAUTH2_TOKEN_ENDPOINT": "https://76hxgq.logto.app/oidc/token", - "JWKS_ENABLED": "true", - "OPA_ENABLED": "false", + "ENV_STATE": "dev", + "DEV_API_KEY_ENABLED": "false", + "DEV_OAUTH2_JWKS_ENDPOINT": "https://76hxgq.logto.app/oidc/jwks", + "DEV_OAUTH2_TOKEN_ENDPOINT": "https://76hxgq.logto.app/oidc/token", + "DEV_JWKS_ENABLED": "true", + "DEV_OPA_ENABLED": "false", }, + clear=True, ): app = create_app() return app @@ -113,6 +119,14 @@ def protected_bearer_schema(create_protected_with_bearer_app): return schemathesis.from_asgi("/geoapi/openapi?f=json", app=app) +@pytest.fixture +def reverse_proxy_enabled(create_app_with_reverse_proxy_enabled): + """Create a protected API key schema.""" + app = create_app_with_reverse_proxy_enabled() + + return app + + def get_access_token(): """Fetch an access token.""" with Client( diff --git a/tests/test_middlewares.py b/tests/test_middlewares.py index dd91fb7..324d901 100644 --- a/tests/test_middlewares.py +++ b/tests/test_middlewares.py @@ -1,19 +1,17 @@ """Test middlewares.""" import pytest +from httpx import ASGITransport from httpx import AsyncClient from app.config.app import configuration as cfg @pytest.mark.asyncio -async def test_pygeoapi_links_behind_proxy( - create_app_with_reverse_proxy_enabled, -) -> None: +async def test_pygeoapi_links_behind_proxy(reverse_proxy_enabled) -> None: """Test presence of reverse proxy base urls in links.""" - app = create_app_with_reverse_proxy_enabled() - - async with AsyncClient(app=app, timeout=30) as client: + transport = ASGITransport(app=reverse_proxy_enabled) + async with AsyncClient(transport=transport, timeout=30) as client: _proto = "https" _host = "proxy.example.com" response = await client.get(