Skip to content

Commit 166ca41

Browse files
committed
Add functional test for CQL2 filter
1 parent 7021043 commit 166ca41

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

tests/test_filters_jinja2.py

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
"""Tests for Jinja2 CQL2 filter."""
22

3+
from dataclasses import dataclass
4+
from typing import Generator
5+
from unittest.mock import AsyncMock, MagicMock, patch
6+
from urllib.parse import parse_qs
7+
8+
import httpx
39
import pytest
410
from fastapi.testclient import TestClient
511
from utils import AppFactory
@@ -10,24 +16,58 @@
1016
)
1117

1218

13-
def test_collections_filter_contained_by_token(source_api_server, token_builder):
14-
""""""
19+
@pytest.fixture
20+
def mock_send() -> Generator[MagicMock, None, None]:
21+
"""Mock the HTTPX send method. Useful when we want to inspect the request is sent to upstream API."""
22+
with patch(
23+
"stac_auth_proxy.handlers.reverse_proxy.httpx.AsyncClient.send",
24+
new_callable=AsyncMock,
25+
) as mock_send_method:
26+
yield mock_send_method
27+
28+
29+
@dataclass
30+
class SingleChunkAsyncStream(httpx.AsyncByteStream):
31+
"""Mock async stream that returns a single chunk of data."""
32+
33+
body: bytes
34+
35+
async def __aiter__(self):
36+
"""Return a single chunk of data."""
37+
yield self.body
38+
39+
40+
def test_collections_filter_contained_by_token(
41+
mock_send, source_api_server, token_builder
42+
):
43+
"""Test that the collections filter is applied correctly."""
44+
# Mock response from upstream API
45+
mock_send.return_value = httpx.Response(
46+
200,
47+
stream=SingleChunkAsyncStream(b"{}"),
48+
headers={"content-type": "application/json"},
49+
)
50+
1551
app = app_factory(
1652
upstream_url=source_api_server,
1753
collections_filter={
1854
"cls": "stac_auth_proxy.filters.Template",
1955
"args": [
20-
"A_CONTAINEDBY(id, ( '{{ token.collections | join(\"', '\") }}' ))"
56+
"A_CONTAINEDBY(id, ('{{ token.collections | join(\"', '\") }}' ))"
2157
],
2258
},
2359
)
60+
61+
auth_token = token_builder({"collections": ["foo", "bar"]})
2462
client = TestClient(
2563
app,
26-
headers={
27-
"Authorization": f"Bearer {token_builder({"collections": ["foo", "bar"]})}"
28-
},
64+
headers={"Authorization": f"Bearer {auth_token}"},
2965
)
66+
3067
response = client.get("/collections")
3168
assert response.status_code == 200
32-
33-
# TODO: We need to verify that the upstream API was called with an applied filter
69+
assert mock_send.call_count == 1
70+
[r] = mock_send.call_args[0]
71+
assert parse_qs(r.url.query.decode()) == {
72+
"filter": ["a_containedby(id, ('foo', 'bar'))"]
73+
}

0 commit comments

Comments
 (0)