Skip to content

Commit

Permalink
Fix override of env variables during testing
Browse files Browse the repository at this point in the history
  • Loading branch information
francbartoli committed Nov 1, 2024
1 parent fbcbf4c commit 16f3e3c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
40 changes: 27 additions & 13 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand Down
10 changes: 4 additions & 6 deletions tests/test_middlewares.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down

0 comments on commit 16f3e3c

Please sign in to comment.