"""HTTP session helper for the mandatory suite.""" from __future__ import annotations ORIGIN = "http://testserver" PASSWORD = "Beispiel-Test-2026" def document(text: str) -> dict: return {"version": 1, "blocks": [{"type": "paragraph", "text": text}]} class Actor: def __init__(self, client, email: str, name: str): self.client = client self.email = email self.name = name self.password = PASSWORD self.csrf = "" self.scope = None def login(self) -> dict: response = self.client.post( "/api/v1/auth/login", json={"email": self.email, "password": self.password}, headers={"Origin": ORIGIN}, ) assert response.status_code == 200, response.text body = response.json() self.csrf = body["csrf_token"] self.scope = body["scope"] return body def use_room(self, room_id: str) -> dict: body = self.request("POST", "/api/v1/auth/session/scope", json={"kind": "organization", "id": room_id}) self.csrf = body["csrf_token"] self.scope = body["scope"] return body def request(self, method: str, path: str, *, json=None, params=None, data=None, files=None, status: int = 200): headers = {"Origin": ORIGIN, "X-CSRF-Token": self.csrf} if self.scope is not None: headers["X-Scope-Kind"] = self.scope["kind"] headers["X-Scope-Id"] = self.scope["id"] response = self.client.request(method, path, json=json, params=params, data=data, files=files, headers=headers) assert response.status_code == status, response.text if status == 204 or not response.content: return None return response.json() def register(conn, email: str, name: str, *, superadmin: bool = False): from app.auth.accounts import create_account account = create_account(conn, email, name, PASSWORD, is_superadmin=superadmin, origin="fixture") conn.commit() return account def case_of(actor: Actor, title: str) -> dict: return actor.request("POST", "/api/v1/cases", json={"title": title}, status=201) def matter_of(actor: Actor, title: str) -> dict: return actor.request("POST", "/api/v1/matters", json={"title": title}, status=201) def grant(actor: Actor, case_id: str, email: str, *, edit: bool = False, share: bool = False) -> None: actor.request( "POST", f"/api/v1/cases/{case_id}/grants", json={"email": email, "can_read": True, "can_edit": edit, "can_share": share}, status=201, ) def note_on(actor: Actor, title: str, text: str, case_id: str) -> dict: return actor.request( "POST", "/api/v1/notes", json={"title": title, "document": document(text), "context": {"kind": "case", "id": case_id}}, status=201, ) def bind_to(actor: Actor, element_id: str, source_id: str, target_id: str) -> dict: return actor.request( "POST", "/api/v1/bindings", json={ "element_id": element_id, "source_context": {"kind": "case", "id": source_id}, "target": {"kind": "case", "id": target_id}, }, status=201, ) def binding_for(case: dict, title: str) -> dict: for binding in case["bindings"]: if binding["element"]["title"] == title: return binding raise AssertionError(title) def texts(case: dict) -> dict[str, str]: found = {} for binding in case["bindings"]: blocks = binding["element"].get("document", {}).get("blocks", []) found[binding["element"]["title"]] = blocks[0]["text"] if blocks else "" return found