From ffaf200c0735f34524d1d4a84cb32b4dfc281fb8 Mon Sep 17 00:00:00 2001 From: Colin Maudry Date: Tue, 16 Jun 2026 17:03:14 +0200 Subject: [PATCH] api/v1/schema n'est plus auth, schema renvoie table schema, adaptation des tests #78 --- src/api/routes.py | 8 +++----- tests/api/test_endpoints_schema.py | 21 ++++++++++----------- tests/api/test_tracking.py | 6 +++--- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/api/routes.py b/src/api/routes.py index 7ac68d3..418492d 100644 --- a/src/api/routes.py +++ b/src/api/routes.py @@ -6,6 +6,7 @@ from src.api.auth import require_token from src.api.filters import FilterError, build_where from src.db import count_marches, query_marches from src.db import schema as duckdb_schema +from src.utils.data import DATA_SCHEMA bp = Blueprint( "api_v1", @@ -85,12 +86,9 @@ def health(): @bp.route("/schema") -@bp.doc(security=[{"BearerAuth": []}]) -@require_token def schema(): - """Liste des colonnes disponibles dans le dataset DECP.""" - cols = [{"name": name, "type": str(dtype)} for name, dtype in duckdb_schema.items()] - return {"columns": cols} + """Liste des champs disponibles dans le dataset DECP (format TableSchema).""" + return {"fields": list(DATA_SCHEMA.values())} @bp.route("/data") diff --git a/tests/api/test_endpoints_schema.py b/tests/api/test_endpoints_schema.py index 3d8438e..2cab204 100644 --- a/tests/api/test_endpoints_schema.py +++ b/tests/api/test_endpoints_schema.py @@ -1,19 +1,18 @@ -def test_schema_without_token_returns_401(api_client): +def test_schema_accessible_without_token(api_client): client, _ = api_client resp = client.get("/api/v1/schema") - assert resp.status_code == 401 + assert resp.status_code == 200 -def test_schema_returns_columns(api_client, valid_token_header): +def test_schema_returns_fields(api_client): client, _ = api_client - resp = client.get("/api/v1/schema", headers=valid_token_header) + resp = client.get("/api/v1/schema") assert resp.status_code == 200 data = resp.get_json() - assert "columns" in data - assert isinstance(data["columns"], list) - assert len(data["columns"]) > 0 - first = data["columns"][0] - assert set(first.keys()) >= {"name", "type"} - # uid doit être présent dans le schéma DECP de test - names = [c["name"] for c in data["columns"]] + assert "fields" in data + assert isinstance(data["fields"], list) + assert len(data["fields"]) > 0 + first = data["fields"][0] + assert set(first.keys()) >= {"name", "type", "title", "description"} + names = [f["name"] for f in data["fields"]] assert "uid" in names diff --git a/tests/api/test_tracking.py b/tests/api/test_tracking.py index 36e57a6..9a26ab5 100644 --- a/tests/api/test_tracking.py +++ b/tests/api/test_tracking.py @@ -29,7 +29,7 @@ def test_after_request_hook_increments_counter_async(api_client, valid_token_hea # Faire une requête (qui doit déclencher l'incrément) client.get("/api/v1/health") # pas authentifiée → ne compte pas - client.get("/api/v1/schema", headers=valid_token_header) + client.get("/api/v1/data", headers=valid_token_header) # /schema est public tracking.flush(timeout=2.0) @@ -49,7 +49,7 @@ def test_matomo_disabled_skips_call(monkeypatch, api_client, valid_token_header) "_post_matomo", lambda **kw: called.append(kw), ) - client.get("/api/v1/health") + client.get("/api/v1/data", headers=valid_token_header) # authentifiée → hook actif tracking.flush(timeout=2.0) assert called == [] @@ -69,7 +69,7 @@ def test_matomo_enabled_posts_event(monkeypatch, api_client, valid_token_header) ) client, _ = api_client - client.get("/api/v1/schema", headers=valid_token_header) + client.get("/api/v1/data", headers=valid_token_header) # /schema est public tracking.flush(timeout=2.0) assert len(captured) == 1