diff --git a/tests/test_guests.py b/tests/test_guests.py index 32704d3..84076c6 100644 --- a/tests/test_guests.py +++ b/tests/test_guests.py @@ -155,7 +155,8 @@ def test_edit_and_delete_with_personal_link(app: Any) -> None: csrf = token_for(first, f"{link}/edit") resp = first.post(f"{link}/edit", data={"csrf_token": csrf, f"q{qid}": late}) assert resp.status_code == 303 and resp.headers["location"].endswith("?done=saved") - assert sorted(b[:2] for b in bookings(qid)) == [(early, "confirmed"), (late, "confirmed")] + assert sorted(b[:2] for b in bookings(qid)) == sorted([(early, "confirmed"), + (late, "confirmed")]) # Löschen braucht die Bestätigung csrf = token_for(first, link) assert first.post(f"{link}/delete", data={"csrf_token": csrf}).status_code == 400 diff --git a/tests/test_security_matrix.py b/tests/test_security_matrix.py index ad7331b..db0f3a2 100644 --- a/tests/test_security_matrix.py +++ b/tests/test_security_matrix.py @@ -34,12 +34,22 @@ PROTECTED_PREFIXES = ("/admin", "/forms", "/account") DUMMY_ID = "00000000-0000-0000-0000-000000000000" +def _get(obj: Any, name: str) -> Any: + """Attribut oder Schlüssel (Einbindungs-Kontext kann beides sein).""" + if isinstance(obj, dict): + return obj.get(name) + return getattr(obj, name, None) + + def _dep_calls(obj: Any) -> set[Any]: - """Abhängigkeiten eines Endpunkts oder Routers (Depends(...) und aufgelöster Baum).""" + """Abhängigkeiten eines Endpunkts oder Routers (Depends(...) und aufgelöster Baum), + auch aus dem Einbindungs-Kontext (FastAPI ab 0.16x: include_router(dependencies=...)).""" calls: set[Any] = set() - for dep in getattr(obj, "dependencies", None) or (): - if getattr(dep, "dependency", None) is not None: - calls.add(dep.dependency) + for holder in (obj, _get(obj, "include_context")): + for dep in _get(holder, "dependencies") or () if holder is not None else (): + for name in ("dependency", "call"): + if getattr(dep, name, None) is not None: + calls.add(getattr(dep, name)) todo = [getattr(obj, "dependant", None)] while todo: node = todo.pop() @@ -56,6 +66,10 @@ def _is_endpoint(obj: Any) -> bool: return bool(getattr(obj, "methods", None)) and isinstance(getattr(obj, "path", None), str) +def _is_routing(obj: Any) -> bool: + return any(hasattr(obj, name) for name in ("path", "routes", "original_router")) + + def _children(obj: Any) -> list[Any]: """Untergeordnete Routen/Router, ohne sich auf eine bestimmte FastAPI-Version zu verlassen.""" found: list[Any] = [] @@ -67,8 +81,8 @@ def _children(obj: Any) -> list[Any]: "original_router")) for value in values: if isinstance(value, (list, tuple)): - found.extend(v for v in value if hasattr(v, "path") or hasattr(v, "routes")) - elif value is not None and (hasattr(value, "routes") or _is_endpoint(value)): + found.extend(v for v in value if _is_routing(v)) + elif value is not None and (_is_routing(value) and not isinstance(value, str)): found.append(value) return found @@ -83,7 +97,8 @@ def _routes(app: Any) -> list[tuple[str, Any, set[Any]]]: if depth > 8 or id(obj) in seen: return seen.add(id(obj)) - own = getattr(obj, "prefix", None) or "" + own = getattr(obj, "prefix", None) or _get(_get(obj, "include_context"), "prefix") or "" + own = own if isinstance(own, str) else "" if not _is_endpoint(obj) and not own and hasattr(obj, "routes"): own = getattr(obj, "path", "") or "" # Mount if own and not own.startswith(prefix or "\0"):