From 4b33b17b2dd4f359eedbbb727ce631de93233034 Mon Sep 17 00:00:00 2001 From: Lars Date: Mon, 23 Nov 2026 14:34:15 +0100 Subject: [PATCH] Read the API key list before the next query replaces it. The exercise release screen asked which keys match and then looked up club roles on the same cursor. The key rows were discarded, so a valid key looked missing. Co-authored-by: Cursor --- backend/distribution_service.py | 4 +- backend/tests/test_distribution_contract.py | 46 ++++++++++++++++++++- backend/version.py | 11 ++++- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/backend/distribution_service.py b/backend/distribution_service.py index b948ed0..2e42c73 100644 --- a/backend/distribution_service.py +++ b/backend/distribution_service.py @@ -1538,6 +1538,8 @@ def release_keys_for_exercise(cur, exercise: dict, profile_id: int, role: str) - return [], denial if vis == "club" and exercise.get("club_id") is not None and not _api_enabled(cur, int(exercise["club_id"])): return [], "Die API-Schnittstelle ist für diesen Verein nicht freigegeben." + admin_ids = _club_ids_with_role(cur, profile_id, "club_admin") + member_ids = _club_ids_with_role(cur, profile_id, None) cur.execute( """ SELECT k.id, k.label, k.context_kind, k.context_club_id, k.context_profile_id, k.club_ids, @@ -1552,8 +1554,6 @@ def release_keys_for_exercise(cur, exercise: dict, profile_id: int, role: str) - """, (vis,), ) - admin_ids = _club_ids_with_role(cur, profile_id, "club_admin") - member_ids = _club_ids_with_role(cur, profile_id, None) keys = [] for row in _rows(cur): item = r2d(row) diff --git a/backend/tests/test_distribution_contract.py b/backend/tests/test_distribution_contract.py index 7262d10..fb99a4a 100644 --- a/backend/tests/test_distribution_contract.py +++ b/backend/tests/test_distribution_contract.py @@ -14,7 +14,7 @@ from fastapi.testclient import TestClient os.environ.setdefault("SKIP_DB_MIGRATE", "1") from auth import require_auth -from distribution_service import _lock_recipient +from distribution_service import _lock_recipient, release_keys_for_exercise from distribution_contract import ( InvalidCursor, actions_cover, @@ -237,6 +237,50 @@ def test_distribution_read_is_hidden_while_disabled(client: TestClient, monkeypa assert response.headers.get("cache-control") == "private, no-store" +class _DroppingCursor: + """Ein neues execute verwirft ein noch nicht gelesenes Ergebnis, wie psycopg2.""" + + def __init__(self, key: dict) -> None: + self._key = key + self._pending = None + + def execute(self, sql, _params=None): + if self._pending is not None: + raise AssertionError("Ergebnis wurde vor dem Lesen überschrieben") + self._pending = [self._key] if "FROM distribution_keys" in sql else [] + + def fetchall(self): + rows = self._pending + self._pending = None + return rows or [] + + def fetchone(self): + rows = self.fetchall() + return rows[0] if rows else None + + +def test_release_screen_keeps_the_application_key() -> None: + key = { + "id": "key-1", + "label": "Lara", + "context_kind": "user", + "context_club_id": None, + "context_profile_id": 7, + "club_ids": [3], + "recipient_id": "rec-1", + "api_enabled": True, + } + cur = _DroppingCursor(key) + keys, block = release_keys_for_exercise( + cur, + {"visibility": "official", "club_id": None, "created_by": 7}, + 7, + "superadmin", + ) + assert block is None + assert [item["label"] for item in keys] == ["Lara"] + + def test_channel_sequence_is_locked() -> None: assert "FOR UPDATE" in inspect.getsource(_lock_recipient) diff --git a/backend/version.py b/backend/version.py index e85fcef..32e890c 100644 --- a/backend/version.py +++ b/backend/version.py @@ -1,6 +1,6 @@ # Shinkan Jinkendo Version Information -APP_VERSION = "0.8.254" +APP_VERSION = "0.8.255" BUILD_DATE = "2026-11-23" DB_SCHEMA_VERSION = "20261123101" @@ -39,7 +39,7 @@ MODULE_VERSIONS = { "skill_profiles": "1.0.0", # Phase 3: gewichtetes Fähigkeiten-Profil + skill-discovery/suggestions "methods": "0.1.0", "exercises": "2.38.0", # Globale Übungen schreibt und gibt nur superadmin frei - "distribution": "1.3.0", # Ein Schlüssel je Anwendung, Rechte folgen der aktuellen Rolle, Schlüssel bleibt kopierbar + "distribution": "1.3.1", # Freigabe liest den Schlüssel, bevor weitere Abfragen denselben Cursor benutzen "planning_exercise_suggest": "0.23.8", # Progressionsgraph: Katalog-Kontext (Fokus/Stil/TT/ZG) im Match-Profil "training_units": "0.4.0", # POST .../publish-to-framework: Ablauf aus geplanter Einheit → Rahmen-Slot-Blueprint "training_programs": "0.1.0", @@ -55,6 +55,13 @@ MODULE_VERSIONS = { } CHANGELOG = [ + { + "version": "0.8.255", + "date": "2026-11-23", + "changes": [ + "Die Übungsfreigabe findet einen angelegten API-Schlüssel. Die Schlüsselabfrage wird gelesen, bevor die Rollenabfrage denselben Cursor überschreibt.", + ], + }, { "version": "0.8.254", "date": "2026-11-23",