"""Nested chapters stay outline only: same object, no rights, no partial moves.""" from __future__ import annotations import psycopg import pytest from tests.support import Actor, binding_for, case_of, document, grant, matter_of, register def _note(actor: Actor, title: str, matter: dict, chapter_id: str | None = None) -> dict: body = { "title": title, "document": document(title), "context": {"kind": "matter", "id": matter["id"]}, } if chapter_id: body["chapter_id"] = chapter_id return actor.request("POST", "/api/v1/notes", json=body, status=201) def _detail(actor: Actor, matter_id: str) -> dict: return actor.request("GET", f"/api/v1/matters/{matter_id}") def _by_title(detail: dict) -> dict[str, dict]: return {item["title"]: item for item in detail["chapters"]} def test_flat_chapters_keep_identity_order_and_bindings(client, db): ada = Actor(client, "ada-flach@example.test", "Ada Flach") register(db, ada.email, ada.name) ada.login() matter = matter_of(ada, "Bestehender Vorgang") first = ada.request( "POST", f"/api/v1/matters/{matter['id']}/chapters", json={"title": "Schriftverkehr", "document_on": "2024-02-02"}, status=201, ) second = ada.request( "POST", f"/api/v1/matters/{matter['id']}/chapters", json={"title": "Nachweise", "document_on": "2024-01-01"}, status=201, ) _note(ada, "Altes Schreiben", matter, first["id"]) detail = _detail(ada, matter["id"]) assert [item["id"] for item in detail["chapters"]] == [second["id"], first["id"]] assert all(item["parent_chapter_id"] is None for item in detail["chapters"]) assert binding_for(detail, "Altes Schreiben")["chapter_id"] == first["id"] ada.login() again = _detail(ada, matter["id"]) assert [(item["id"], item["title"], item["parent_chapter_id"]) for item in again["chapters"]] == [ (second["id"], "Nachweise", None), (first["id"], "Schriftverkehr", None), ] assert binding_for(again, "Altes Schreiben")["chapter_id"] == first["id"] def test_three_levels_bulk_move_reorder_reparent_and_dissolve(client, db): ada = Actor(client, "ada-nest@example.test", "Ada Nest") register(db, ada.email, ada.name) ada.login() matter = matter_of(ada, "Nebenkostenabrechnung") root = ada.request( "POST", f"/api/v1/matters/{matter['id']}/chapters", json={"title": "Nebenkostenabrechnung 2025"}, status=201, ) sent = ada.request( "POST", f"/api/v1/matters/{matter['id']}/chapters", json={"title": "Übermittelte Abrechnungsunterlagen", "parent_chapter_id": root["id"]}, status=201, ) receipts = ada.request( "POST", f"/api/v1/matters/{matter['id']}/chapters", json={"title": "Einzelbelege", "parent_chapter_id": sent["id"]}, status=201, ) sibling = ada.request( "POST", f"/api/v1/matters/{matter['id']}/chapters", json={"title": "Weiterer Ordner", "parent_chapter_id": root["id"]}, status=201, ) _note(ada, "Anschreiben", matter, sent["id"]) _note(ada, "Abrechnung", matter, sent["id"]) _note(ada, "Rückfrage zur Abrechnung", matter, root["id"]) _note(ada, "Antwort des Vermieters", matter, root["id"]) for index in range(8): ada.request( "POST", "/api/v1/external-refs", json={ "title": f"Beleg {index + 1}", "source": "paperless", "external_id": str(700 + index), "context": {"kind": "matter", "id": matter["id"]}, "chapter_id": root["id"], }, status=201, ) detail = _detail(ada, matter["id"]) chapters = _by_title(detail) assert chapters["Übermittelte Abrechnungsunterlagen"]["parent_chapter_id"] == root["id"] assert chapters["Einzelbelege"]["parent_chapter_id"] == sent["id"] assert binding_for(detail, "Rückfrage zur Abrechnung")["chapter_id"] == root["id"] assert binding_for(detail, "Anschreiben")["chapter_id"] == sent["id"] receipt_ids = [binding_for(detail, f"Beleg {index + 1}")["id"] for index in range(8)] moved = ada.request( "POST", f"/api/v1/matters/{matter['id']}/bindings/chapters", json={"expected_version": detail["version"], "binding_ids": receipt_ids, "chapter_id": receipts["id"]}, ) detail = _detail(ada, matter["id"]) assert detail["version"] == moved["version"] assert all(binding_for(detail, f"Beleg {index + 1}")["chapter_id"] == receipts["id"] for index in range(8)) assert binding_for(detail, "Rückfrage zur Abrechnung")["chapter_id"] == root["id"] assert binding_for(detail, "Anschreiben")["chapter_id"] == sent["id"] stale = detail["version"] ada.request( "POST", f"/api/v1/matters/{matter['id']}/bindings/chapters", json={ "expected_version": stale, "binding_ids": [receipt_ids[0], "00000000-0000-4000-8000-000000000099"], "chapter_id": root["id"], }, status=404, ) detail = _detail(ada, matter["id"]) assert detail["version"] == stale assert binding_for(detail, "Beleg 1")["chapter_id"] == receipts["id"] ada.request( "POST", f"/api/v1/matters/{matter['id']}/chapters/order", json={"expected_version": detail["version"], "chapter_ids": [sibling["id"], sent["id"]]}, ) detail = _detail(ada, matter["id"]) ordered = [item["id"] for item in detail["chapters"] if item["parent_chapter_id"] == root["id"]] assert ordered == [sibling["id"], sent["id"]] assert _by_title(detail)["Einzelbelege"]["parent_chapter_id"] == sent["id"] ada.request( "POST", f"/api/v1/matters/{matter['id']}/chapters/order", json={"expected_version": detail["version"], "chapter_ids": [root["id"], sent["id"]]}, status=422, ) stale_parent = detail["version"] ada.request( "POST", f"/api/v1/chapters/{receipts['id']}/parent", json={"expected_version": stale_parent, "parent_chapter_id": root["id"]}, ) detail = _detail(ada, matter["id"]) assert _by_title(detail)["Einzelbelege"]["parent_chapter_id"] == root["id"] assert all(binding_for(detail, f"Beleg {index + 1}")["chapter_id"] == receipts["id"] for index in range(8)) ada.login() detail = _detail(ada, matter["id"]) assert _by_title(detail)["Einzelbelege"]["parent_chapter_id"] == root["id"] assert [item["id"] for item in detail["chapters"] if item["parent_chapter_id"] == root["id"]][:2] == [ sibling["id"], sent["id"], ] ada.request( "POST", f"/api/v1/chapters/{sent['id']}/parent", json={"expected_version": stale_parent, "parent_chapter_id": receipts["id"]}, status=409, ) other = matter_of(ada, "Fremder Vorgang") foreign = ada.request("POST", f"/api/v1/matters/{other['id']}/chapters", json={"title": "Fremd"}, status=201) detail = _detail(ada, matter["id"]) ada.request( "POST", f"/api/v1/chapters/{sent['id']}/parent", json={"expected_version": detail["version"], "parent_chapter_id": foreign["id"]}, status=422, ) ada.request( "POST", f"/api/v1/chapters/{sent['id']}/parent", json={"expected_version": detail["version"], "parent_chapter_id": sent["id"]}, status=422, ) ada.request( "POST", f"/api/v1/chapters/{root['id']}/parent", json={"expected_version": detail["version"], "parent_chapter_id": sent["id"]}, status=422, ) detail = _detail(ada, matter["id"]) assert _by_title(detail)["Übermittelte Abrechnungsunterlagen"]["parent_chapter_id"] == root["id"] dissolved = ada.request( "POST", f"/api/v1/chapters/{sent['id']}/delete", json={"expected_version": detail["version"]}, ) detail = _detail(ada, matter["id"]) assert detail["version"] == dissolved["version"] assert "Übermittelte Abrechnungsunterlagen" not in _by_title(detail) assert binding_for(detail, "Anschreiben")["chapter_id"] == root["id"] assert binding_for(detail, "Abrechnung")["chapter_id"] == root["id"] assert binding_for(detail, "Rückfrage zur Abrechnung")["chapter_id"] == root["id"] assert _by_title(detail)["Einzelbelege"]["parent_chapter_id"] == root["id"] assert binding_for(detail, "Beleg 8")["chapter_id"] == receipts["id"] def test_reclassifying_one_context_leaves_the_other_and_hidden_rows_alone(client, db): ada = Actor(client, "ada-zwei@example.test", "Ada Zwei") ben = Actor(client, "ben-zwei@example.test", "Ben Zwei") register(db, ada.email, ada.name) register(db, ben.email, ben.name) ada.login() case = case_of(ada, "Wohnung") left = matter_of(ada, "Vorgang A") right = matter_of(ada, "Vorgang B") ada.request("POST", f"/api/v1/matters/{left['id']}/case-links", json={"case_id": case["id"]}, status=201) chapter_a = ada.request("POST", f"/api/v1/matters/{left['id']}/chapters", json={"title": "Lage A"}, status=201) other_a = ada.request("POST", f"/api/v1/matters/{left['id']}/chapters", json={"title": "Ziel A"}, status=201) chapter_b = ada.request("POST", f"/api/v1/matters/{right['id']}/chapters", json={"title": "Lage B"}, status=201) shared = _note(ada, "Gemeinsame Notiz", left, chapter_a["id"]) ada.request( "POST", "/api/v1/bindings", json={ "element_id": shared["id"], "source_context": {"kind": "matter", "id": left["id"]}, "target": {"kind": "matter", "id": right["id"]}, "chapter_id": chapter_b["id"], }, status=201, ) hidden = _note(ada, "Verborgene Strategie", left, chapter_a["id"]) reply = _note(ada, "Antwort", left, chapter_a["id"]) ada.request( "POST", f"/api/v1/elements/{reply['id']}/references", json={"refers_to_element_id": shared["id"], "context": {"kind": "matter", "id": left["id"]}}, status=204, ) detail = _detail(ada, left["id"]) ada.request( "POST", f"/api/v1/matters/{left['id']}/bindings/chapters", json={ "expected_version": detail["version"], "binding_ids": [binding_for(detail, "Gemeinsame Notiz")["id"], binding_for(detail, "Antwort")["id"]], "chapter_id": other_a["id"], }, ) left_view = _detail(ada, left["id"]) right_view = _detail(ada, right["id"]) assert binding_for(left_view, "Gemeinsame Notiz")["chapter_id"] == other_a["id"] assert binding_for(right_view, "Gemeinsame Notiz")["chapter_id"] == chapter_b["id"] assert binding_for(left_view, "Gemeinsame Notiz")["element"]["excerpt"] == "Gemeinsame Notiz" assert binding_for(right_view, "Gemeinsame Notiz")["element"]["excerpt"] == "Gemeinsame Notiz" assert binding_for(left_view, "Antwort")["element"]["refers_to"][0]["title"] == "Gemeinsame Notiz" grant(ada, case["id"], ben.email, edit=False, share=False) ada.request( "PUT", "/api/v1/exceptions", json={ "resource_kind": "element", "resource_id": hidden["id"], "context": {"kind": "case", "id": case["id"]}, "remove_read": True, }, ) ben.login() visible = ben.request("GET", f"/api/v1/matters/{left['id']}?context_kind=case&context_id={case['id']}") titles = [item["element"]["title"] for item in visible["bindings"]] assert "Verborgene Strategie" not in titles assert "Gemeinsame Notiz" in titles assert "Lage B" not in str(visible) ben.request("GET", f"/api/v1/matters/{right['id']}", status=404) def test_child_matter_keeps_its_own_chapters_and_depth_is_guarded(client, db): ada = Actor(client, "ada-tiefe@example.test", "Ada Tiefe") register(db, ada.email, ada.name) ada.login() parent = matter_of(ada, "Hauptvorgang") child = ada.request( "POST", "/api/v1/matters", json={"title": "Belegeinsicht", "parent_matter_id": parent["id"]}, status=201, ) own = ada.request("POST", f"/api/v1/matters/{child['id']}/chapters", json={"title": "Nur hier"}, status=201) parent_view = _detail(ada, parent["id"]) child_view = _detail(ada, child["id"]) assert parent_view["chapters"] == [] assert child_view["chapters"][0]["id"] == own["id"] assert child_view["chapters"][0]["parent_chapter_id"] is None case = case_of(ada, "Akte") outer = ada.request("POST", f"/api/v1/cases/{case['id']}/chapters", json={"title": "Aktenkapitel"}, status=201) inner = ada.request( "POST", f"/api/v1/cases/{case['id']}/chapters", json={"title": "Unterlagen", "parent_chapter_id": outer["id"]}, status=201, ) case_view = ada.request("GET", f"/api/v1/cases/{case['id']}") assert {item["title"]: item["parent_chapter_id"] for item in case_view["chapters"]} == { "Aktenkapitel": None, "Unterlagen": outer["id"], } current = None for level in range(1, 33): body = {"title": f"Ebene {level}"} if current: body["parent_chapter_id"] = current created = ada.request("POST", f"/api/v1/matters/{parent['id']}/chapters", json=body, status=201) current = created["id"] ada.request( "POST", f"/api/v1/matters/{parent['id']}/chapters", json={"title": "Ebene 33", "parent_chapter_id": current}, status=422, ) stored = _detail(ada, parent["id"]) assert len(stored["chapters"]) == 32 root_id = next(item["id"] for item in stored["chapters"] if item["title"] == "Ebene 1") with pytest.raises(psycopg.Error): db.execute( "UPDATE chapters SET parent_chapter_id = %s WHERE id = %s", (current, root_id), )