from xml.etree import ElementTree import httpx from app.infra.geoserver import client as geoserver_client def test_project_workspace_sets_30_day_client_cache(monkeypatch): cache_updates: list[str] = [] def handler(request: httpx.Request) -> httpx.Response: path = request.url.path if request.method == "GET" and path.endswith("/rest/workspaces/demo.json"): return httpx.Response(404) if request.method == "GET" and "/gwc/rest/layers/" in path: return httpx.Response( 200, content=b"demo0", ) if request.method == "PUT" and "/gwc/rest/layers/" in path: root = ElementTree.fromstring(request.content) cache_updates.append(root.findtext("expireClients")) return httpx.Response(200) return httpx.Response(201) monkeypatch.setattr(geoserver_client.settings, "GEOSERVER_USERNAME", "admin") monkeypatch.setattr(geoserver_client.settings, "GEOSERVER_PASSWORD", "secret") monkeypatch.setattr( geoserver_client.settings, "GEOSERVER_CLIENT_CACHE_SECONDS", 2_592_000, ) http_client = httpx.Client( base_url="http://geoserver.example/geoserver", transport=httpx.MockTransport(handler), ) client = geoserver_client.GeoServerAdminClient(client=http_client) layers = client.create_project_workspace( workspace="demo", database_name="demo", map_bbox=(1.0, 2.0, 3.0, 4.0), ) assert layers == geoserver_client.PROJECT_LAYER_NAMES assert cache_updates == ["2592000"] * len(layers) def test_publish_feature_type_updates_existing_layer_and_cache(monkeypatch): requests: list[tuple[str, str]] = [] def handler(request: httpx.Request) -> httpx.Response: requests.append((request.method, request.url.path)) if request.method == "GET" and request.url.path.endswith("/featuretypes/demo_layer.json"): return httpx.Response(200, json={"featureType": {"name": "demo_layer"}}) if request.method == "GET" and "/gwc/rest/layers/" in request.url.path: return httpx.Response(200, content=b"demo") return httpx.Response(200) monkeypatch.setattr(geoserver_client.settings, "GEOSERVER_USERNAME", "admin") monkeypatch.setattr(geoserver_client.settings, "GEOSERVER_PASSWORD", "secret") monkeypatch.setattr(geoserver_client.settings, "GEOSERVER_CLIENT_CACHE_SECONDS", 2_592_000) http_client = httpx.Client( base_url="http://geoserver.example/geoserver", transport=httpx.MockTransport(handler), ) geoserver_client.GeoServerAdminClient(client=http_client).publish_feature_type( workspace="demo", datastore="demo", layer="demo_layer", title="Demo layer", map_bbox=(1.0, 2.0, 3.0, 4.0), ) assert ("PUT", "/geoserver/rest/workspaces/demo/datastores/demo/featuretypes/demo_layer") in requests assert ("PUT", "/geoserver/gwc/rest/layers/demo:demo_layer.xml") in requests