Skip to content

Commit f8ae991

Browse files
committed
Update tests
1 parent 7cee884 commit f8ae991

File tree

5 files changed

+33
-21
lines changed

5 files changed

+33
-21
lines changed

.vscode/launch.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
],
1717
"jinja": true,
1818
"cwd": "${workspaceFolder}/src",
19+
"justMyCode": false
1920
}
2021
]
2122
}

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ dev = [
5050

5151
[tool.pytest.ini_options]
5252
asyncio_default_fixture_loop_scope = "function"
53-
asyncio_mode = "strict"
53+
asyncio_mode = "auto"

src/stac_auth_proxy/config.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ class Settings(BaseSettings):
4848
public_endpoints: EndpointMethods = {r"^/api.html$": ["GET"], r"^/api$": ["GET"]}
4949
openapi_spec_endpoint: Optional[str] = None
5050

51-
collections_filter: Optional[ClassInput] = None
52-
collections_filter_endpoints: Optional[EndpointMethods] = {
53-
"/collections": ["GET"],
54-
"/collections/{collection_id}": ["GET"],
55-
}
51+
# collections_filter: Optional[ClassInput] = None
52+
# collections_filter_endpoints: Optional[EndpointMethods] = {
53+
# r"^/collections$": ["GET"],
54+
# r"^/collections$/([^/]+)": ["GET"],
55+
# }
5656
items_filter: Optional[ClassInput] = None
5757
items_filter_endpoints: Optional[EndpointMethods] = {
5858
r"^/search$": ["POST"],

tests/conftest.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import json
44
import os
55
import threading
6-
from typing import Any, Generator
7-
from unittest.mock import AsyncMock, MagicMock, patch
6+
from typing import Any, AsyncGenerator
7+
from unittest.mock import AsyncMock, MagicMock, patch, DEFAULT
88

99
import pytest
1010
import uvicorn
@@ -144,12 +144,21 @@ def mock_env():
144144

145145

146146
@pytest.fixture
147-
def mock_upstream() -> Generator[MagicMock, None, None]:
147+
async def mock_upstream() -> AsyncGenerator[MagicMock, None]:
148148
"""Mock the HTTPX send method. Useful when we want to inspect the request is sent to upstream API."""
149+
150+
async def store_body(request, **kwargs):
151+
"""Exhaust and store the request body."""
152+
_streamed_body = b""
153+
async for chunk in request.stream:
154+
_streamed_body += chunk
155+
setattr(request, "_streamed_body", _streamed_body)
156+
return DEFAULT
157+
149158
with patch(
150159
"stac_auth_proxy.handlers.reverse_proxy.httpx.AsyncClient.send",
151160
new_callable=AsyncMock,
161+
side_effect=store_body,
162+
return_value=single_chunk_async_stream_response(b"{}"),
152163
) as mock_send_method:
153-
# Mock response from upstream API
154-
mock_send_method.return_value = single_chunk_async_stream_response(b"{}")
155164
yield mock_send_method

tests/test_filters_jinja2.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
id="simple_not_templated",
1919
),
2020
pytest.param(
21-
"{{ '(properties.private = false)' if token is none else true }}",
21+
"{{ '(properties.private = false)' if user is none else true }}",
2222
"true",
2323
"(properties.private = false)",
2424
id="simple_templated",
@@ -30,7 +30,7 @@
3030
id="complex_not_templated",
3131
),
3232
pytest.param(
33-
"""{{ '{"op": "=", "args": [{"property": "private"}, true]}' if token is none else true }}""",
33+
"""{{ '{"op": "=", "args": [{"property": "private"}, true]}' if user is none else true }}""",
3434
"true",
3535
"""{"op": "=", "args": [{"property": "private"}, true]}""",
3636
id="complex_templated",
@@ -146,11 +146,12 @@ def _build_client(
146146
return TestClient(app, headers=headers)
147147

148148

149-
def _get_upstream_request(mock_upstream: MagicMock):
149+
async def _get_upstream_request(mock_upstream: MagicMock):
150150
"""Fetches the raw body and query params from the single upstream request."""
151151
assert mock_upstream.call_count == 1
152152
[request] = cast(list[Request], mock_upstream.call_args[0])
153-
return request.read().decode(), dict(request.url.params)
153+
req_body = request._streamed_body
154+
return req_body.decode(), dict(request.url.params)
154155

155156

156157
@pytest.mark.parametrize(
@@ -159,7 +160,7 @@ def _get_upstream_request(mock_upstream: MagicMock):
159160
)
160161
@pytest.mark.parametrize("is_authenticated", [True, False], ids=["auth", "anon"])
161162
@pytest.mark.parametrize("input_query", SEARCH_POST_QUERIES)
162-
def test_search_post(
163+
async def test_search_post(
163164
mock_upstream,
164165
source_api_server,
165166
filter_template_expr,
@@ -179,7 +180,7 @@ def test_search_post(
179180
response.raise_for_status()
180181

181182
# Retrieve the JSON body that was actually sent upstream
182-
proxied_body_str = _get_upstream_request(mock_upstream)[0]
183+
proxied_body_str = (await _get_upstream_request(mock_upstream))[0]
183184
proxied_body = json.loads(proxied_body_str)
184185

185186
# Determine the expected combined filter
@@ -193,6 +194,7 @@ def test_search_post(
193194
expected_output = {
194195
**input_query,
195196
"filter": proxy_filter.to_json(),
197+
"filter-lang": "cql2-json",
196198
}
197199

198200
assert (
@@ -206,7 +208,7 @@ def test_search_post(
206208
)
207209
@pytest.mark.parametrize("is_authenticated", [True, False], ids=["auth", "anon"])
208210
@pytest.mark.parametrize("input_query", SEARCH_GET_QUERIES)
209-
def test_search_get(
211+
async def test_search_get(
210212
mock_upstream,
211213
source_api_server,
212214
filter_template_expr,
@@ -226,7 +228,7 @@ def test_search_get(
226228
response.raise_for_status()
227229

228230
# For GET, we expect the upstream body to be empty, but URL params to be appended
229-
proxied_body, upstream_query = _get_upstream_request(mock_upstream)
231+
proxied_body, upstream_query = await _get_upstream_request(mock_upstream)
230232
assert proxied_body == ""
231233

232234
# Determine the expected combined filter
@@ -253,7 +255,7 @@ def test_search_get(
253255
)
254256
@pytest.mark.parametrize("is_authenticated", [True, False], ids=["auth", "anon"])
255257
@pytest.mark.parametrize("input_query", ITEMS_LIST_QUERIES)
256-
def test_items_list(
258+
async def test_items_list(
257259
mock_upstream,
258260
source_api_server,
259261
filter_template_expr,
@@ -274,7 +276,7 @@ def test_items_list(
274276
response.raise_for_status()
275277

276278
# For GET items, we also expect an empty body and appended querystring
277-
proxied_body, proxied_query = _get_upstream_request(mock_upstream)
279+
proxied_body, proxied_query = await _get_upstream_request(mock_upstream)
278280
assert proxied_body == ""
279281

280282
# Only the appended filter (no input_filter merges in these particular tests),

0 commit comments

Comments
 (0)