|
| 1 | +"""Tests for header-based filtering functionality. |
| 2 | +
|
| 3 | +This module tests the header filtering feature that allows stac-auth-proxy |
| 4 | +to pass allowed collections and geometries via HTTP headers. |
| 5 | +""" |
| 6 | + |
| 7 | +import json |
| 8 | + |
| 9 | +import pytest |
| 10 | +import pytest_asyncio |
| 11 | + |
| 12 | +from ..conftest import create_collection, create_item, delete_collections_and_items |
| 13 | + |
| 14 | +# Header names |
| 15 | +FILTER_COLLECTIONS_HEADER = "X-Filter-Collections" |
| 16 | +FILTER_GEOMETRY_HEADER = "X-Filter-Geometry" |
| 17 | + |
| 18 | + |
| 19 | +@pytest_asyncio.fixture(scope="function") |
| 20 | +async def multi_collection_ctx(txn_client, load_test_data): |
| 21 | + """Create multiple collections for testing header filtering.""" |
| 22 | + await delete_collections_and_items(txn_client) |
| 23 | + |
| 24 | + # Create test collections |
| 25 | + collections = [] |
| 26 | + for suffix in ["a", "b", "c"]: |
| 27 | + collection = load_test_data("test_collection.json").copy() |
| 28 | + collection["id"] = f"test-collection-{suffix}" |
| 29 | + await create_collection(txn_client, collection) |
| 30 | + collections.append(collection) |
| 31 | + |
| 32 | + # Create items in each collection |
| 33 | + items = [] |
| 34 | + for collection in collections: |
| 35 | + item = load_test_data("test_item.json").copy() |
| 36 | + item["id"] = f"test-item-{collection['id']}" |
| 37 | + item["collection"] = collection["id"] |
| 38 | + await create_item(txn_client, item) |
| 39 | + items.append(item) |
| 40 | + |
| 41 | + yield {"collections": collections, "items": items} |
| 42 | + |
| 43 | + await delete_collections_and_items(txn_client) |
| 44 | + |
| 45 | + |
| 46 | +class TestHeaderFilteringSearch: |
| 47 | + """Tests for search endpoints with header filtering.""" |
| 48 | + |
| 49 | + @pytest.mark.asyncio |
| 50 | + async def test_search_uses_header_collections( |
| 51 | + self, app_client, multi_collection_ctx |
| 52 | + ): |
| 53 | + """When X-Filter-Collections header is present, search only in those collections.""" |
| 54 | + # Search with header limiting to collection-a only |
| 55 | + response = await app_client.get( |
| 56 | + "/search", |
| 57 | + headers={FILTER_COLLECTIONS_HEADER: "test-collection-a"}, |
| 58 | + ) |
| 59 | + assert response.status_code == 200 |
| 60 | + data = response.json() |
| 61 | + |
| 62 | + # Should only return items from collection-a |
| 63 | + for feature in data["features"]: |
| 64 | + assert feature["collection"] == "test-collection-a" |
| 65 | + |
| 66 | + @pytest.mark.asyncio |
| 67 | + async def test_search_header_multiple_collections( |
| 68 | + self, app_client, multi_collection_ctx |
| 69 | + ): |
| 70 | + """Header with multiple collections filters to those collections.""" |
| 71 | + response = await app_client.get( |
| 72 | + "/search", |
| 73 | + headers={FILTER_COLLECTIONS_HEADER: "test-collection-a,test-collection-b"}, |
| 74 | + ) |
| 75 | + assert response.status_code == 200 |
| 76 | + data = response.json() |
| 77 | + |
| 78 | + # Should only return items from collection-a and collection-b |
| 79 | + for feature in data["features"]: |
| 80 | + assert feature["collection"] in ["test-collection-a", "test-collection-b"] |
| 81 | + |
| 82 | + @pytest.mark.asyncio |
| 83 | + async def test_search_no_header_returns_all(self, app_client, multi_collection_ctx): |
| 84 | + """Without header, search returns items from all collections.""" |
| 85 | + response = await app_client.get("/search") |
| 86 | + assert response.status_code == 200 |
| 87 | + data = response.json() |
| 88 | + |
| 89 | + # Should have items from all collections |
| 90 | + collections_in_response = {f["collection"] for f in data["features"]} |
| 91 | + assert "test-collection-a" in collections_in_response |
| 92 | + assert "test-collection-b" in collections_in_response |
| 93 | + assert "test-collection-c" in collections_in_response |
| 94 | + |
| 95 | + @pytest.mark.asyncio |
| 96 | + async def test_post_search_uses_header_collections( |
| 97 | + self, app_client, multi_collection_ctx |
| 98 | + ): |
| 99 | + """POST /search also respects the header.""" |
| 100 | + response = await app_client.post( |
| 101 | + "/search", |
| 102 | + json={}, |
| 103 | + headers={FILTER_COLLECTIONS_HEADER: "test-collection-b"}, |
| 104 | + ) |
| 105 | + assert response.status_code == 200 |
| 106 | + data = response.json() |
| 107 | + |
| 108 | + for feature in data["features"]: |
| 109 | + assert feature["collection"] == "test-collection-b" |
| 110 | + |
| 111 | + |
| 112 | +class TestHeaderFilteringCollections: |
| 113 | + """Tests for collections endpoint with header filtering.""" |
| 114 | + |
| 115 | + @pytest.mark.asyncio |
| 116 | + async def test_all_collections_filtered_by_header( |
| 117 | + self, app_client, multi_collection_ctx |
| 118 | + ): |
| 119 | + """GET /collections only returns collections from header.""" |
| 120 | + response = await app_client.get( |
| 121 | + "/collections", |
| 122 | + headers={FILTER_COLLECTIONS_HEADER: "test-collection-a,test-collection-c"}, |
| 123 | + ) |
| 124 | + assert response.status_code == 200 |
| 125 | + data = response.json() |
| 126 | + |
| 127 | + collection_ids = [c["id"] for c in data["collections"]] |
| 128 | + assert "test-collection-a" in collection_ids |
| 129 | + assert "test-collection-c" in collection_ids |
| 130 | + assert "test-collection-b" not in collection_ids |
| 131 | + |
| 132 | + @pytest.mark.asyncio |
| 133 | + async def test_get_collection_allowed_by_header( |
| 134 | + self, app_client, multi_collection_ctx |
| 135 | + ): |
| 136 | + """GET /collections/{id} works when collection is in header.""" |
| 137 | + response = await app_client.get( |
| 138 | + "/collections/test-collection-a", |
| 139 | + headers={FILTER_COLLECTIONS_HEADER: "test-collection-a,test-collection-b"}, |
| 140 | + ) |
| 141 | + assert response.status_code == 200 |
| 142 | + assert response.json()["id"] == "test-collection-a" |
| 143 | + |
| 144 | + @pytest.mark.asyncio |
| 145 | + async def test_get_collection_no_header_allowed( |
| 146 | + self, app_client, multi_collection_ctx |
| 147 | + ): |
| 148 | + """GET /collections/{id} works without header.""" |
| 149 | + response = await app_client.get("/collections/test-collection-a") |
| 150 | + assert response.status_code == 200 |
| 151 | + assert response.json()["id"] == "test-collection-a" |
| 152 | + |
| 153 | + |
| 154 | +class TestHeaderFilteringItems: |
| 155 | + """Tests for item endpoints with header filtering.""" |
| 156 | + |
| 157 | + @pytest.mark.asyncio |
| 158 | + async def test_item_collection_uses_header(self, app_client, multi_collection_ctx): |
| 159 | + """GET /collections/{id}/items respects header.""" |
| 160 | + response = await app_client.get( |
| 161 | + "/collections/test-collection-a/items", |
| 162 | + headers={FILTER_COLLECTIONS_HEADER: "test-collection-a"}, |
| 163 | + ) |
| 164 | + assert response.status_code == 200 |
| 165 | + |
| 166 | + @pytest.mark.asyncio |
| 167 | + async def test_get_item_with_header(self, app_client, multi_collection_ctx): |
| 168 | + """GET /collections/{id}/items/{item_id} works with header.""" |
| 169 | + response = await app_client.get( |
| 170 | + "/collections/test-collection-a/items/test-item-test-collection-a", |
| 171 | + headers={FILTER_COLLECTIONS_HEADER: "test-collection-a"}, |
| 172 | + ) |
| 173 | + assert response.status_code == 200 |
| 174 | + |
| 175 | + |
| 176 | +class TestGeometryHeaderFiltering: |
| 177 | + """Tests for geometry header filtering.""" |
| 178 | + |
| 179 | + @pytest.mark.asyncio |
| 180 | + async def test_search_with_geometry_header(self, app_client, ctx): |
| 181 | + """Search respects X-Filter-Geometry header.""" |
| 182 | + # Geometry that intersects with test item |
| 183 | + geometry = { |
| 184 | + "type": "Polygon", |
| 185 | + "coordinates": [ |
| 186 | + [ |
| 187 | + [149.0, -34.5], |
| 188 | + [149.0, -32.0], |
| 189 | + [151.5, -32.0], |
| 190 | + [151.5, -34.5], |
| 191 | + [149.0, -34.5], |
| 192 | + ] |
| 193 | + ], |
| 194 | + } |
| 195 | + |
| 196 | + response = await app_client.get( |
| 197 | + "/search", |
| 198 | + headers={FILTER_GEOMETRY_HEADER: json.dumps(geometry)}, |
| 199 | + ) |
| 200 | + assert response.status_code == 200 |
| 201 | + # Items should be filtered by geometry |
| 202 | + |
| 203 | + @pytest.mark.asyncio |
| 204 | + async def test_search_with_non_intersecting_geometry(self, app_client, ctx): |
| 205 | + """Search with non-intersecting geometry returns no items.""" |
| 206 | + # Geometry that doesn't intersect with test item |
| 207 | + geometry = { |
| 208 | + "type": "Polygon", |
| 209 | + "coordinates": [ |
| 210 | + [ |
| 211 | + [0.0, 0.0], |
| 212 | + [0.0, 1.0], |
| 213 | + [1.0, 1.0], |
| 214 | + [1.0, 0.0], |
| 215 | + [0.0, 0.0], |
| 216 | + ] |
| 217 | + ], |
| 218 | + } |
| 219 | + |
| 220 | + response = await app_client.get( |
| 221 | + "/search", |
| 222 | + headers={FILTER_GEOMETRY_HEADER: json.dumps(geometry)}, |
| 223 | + ) |
| 224 | + assert response.status_code == 200 |
| 225 | + data = response.json() |
| 226 | + assert len(data["features"]) == 0 |
0 commit comments