Schließe die Connector-Kapselung ab, damit Paperless, IMAP und weitere Quellen über dieselbe Anbindung laufen.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
75a3949f7c
commit
d6b70d8f92
75 files changed
+9477
-414
No files matched your search
@@ -0,0 +1,182 @@
|
||||
"""Third source used only to prove that registration is enough.
|
||||
|
||||
This module is not a product connector and is not imported by the application.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from app.access.policy import mediated_source_owner
|
||||
from app.core.errors import not_found, unprocessable
|
||||
from app.retrieval import FilePayload, RetrievalFailure
|
||||
from app.sourcebind import resolve_for_read
|
||||
from app.sourceregistry import SourceModule, register_source, unregister_source
|
||||
from app.sources import ORIGINAL_UNAVAILABLE, _audit_mediated, _connector_owner
|
||||
|
||||
KIND = "sample"
|
||||
|
||||
CATALOG = {
|
||||
"alpha": {
|
||||
"brief": {
|
||||
"id": "brief",
|
||||
"title": "Probenbrief",
|
||||
"text": "Der Probenbrief bleibt lesbar.",
|
||||
},
|
||||
"notiz": {
|
||||
"id": "notiz",
|
||||
"title": "Randnotiz",
|
||||
"text": "Die Randnotiz bleibt in der Quelle.",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
CAPABILITIES = {
|
||||
"kind": KIND,
|
||||
"navigate": False,
|
||||
"read_original": True,
|
||||
"read_preview": True,
|
||||
"read_attachment": False,
|
||||
"edit_content": False,
|
||||
"edit_metadata": False,
|
||||
"search": {
|
||||
"query": True,
|
||||
"title": False,
|
||||
"correspondent": False,
|
||||
"document_type": False,
|
||||
"tags": False,
|
||||
"document_range": False,
|
||||
"sort": False,
|
||||
"continuation": False,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def parse_config(payload: dict) -> tuple[str, dict, str]:
|
||||
config = payload.get("config") or {}
|
||||
catalog = str(config.get("catalog") or "").strip()
|
||||
name = str(payload.get("name") or "").strip() or catalog
|
||||
if catalog not in CATALOG:
|
||||
raise unprocessable("Der Katalog dieser Beispielquelle ist unbekannt.")
|
||||
return name, {"catalog": catalog}, ""
|
||||
|
||||
|
||||
def probe(row) -> dict:
|
||||
items = _items(row)
|
||||
return {"ok": True, "detail": f"Beispielquelle antwortet. {len(items)} Einträge sichtbar."}
|
||||
|
||||
|
||||
def find(conn, owner_id: UUID, connector_id: UUID | None, query: str, options: dict) -> dict:
|
||||
del options
|
||||
row = _connector(conn, owner_id, connector_id)
|
||||
needle = query.casefold()
|
||||
items = list(_items(row).values())
|
||||
if needle:
|
||||
items = [
|
||||
item for item in items
|
||||
if needle in item["title"].casefold() or needle in item["text"].casefold()
|
||||
]
|
||||
return {"items": items, "connector_id": str(row["id"])}
|
||||
|
||||
|
||||
def preview(conn, owner_id: UUID, connector_id: UUID, options: dict) -> dict:
|
||||
row = _connector(conn, owner_id, connector_id)
|
||||
item = _items(row).get(str(options.get("item") or ""))
|
||||
if item is None:
|
||||
raise unprocessable("Dieser Eintrag ist in der Beispielquelle nicht vorhanden.")
|
||||
return {"id": item["id"], "title": item["title"], "text": item["text"], "media": "text"}
|
||||
|
||||
|
||||
def open_original(conn, actor_id: UUID, element: dict, context, ref) -> tuple[bytes, str, str]:
|
||||
external_id = str((ref or {}).get("external_id") or "")
|
||||
stored = ref.get("connector_id") if ref else None
|
||||
stored_owner = _connector_owner(conn, stored)
|
||||
if element["owner_subject_id"] == actor_id or stored_owner == actor_id:
|
||||
result = resolve_for_read(conn, actor_id, KIND, ref, None)
|
||||
if result.row is None:
|
||||
raise unprocessable(result.message)
|
||||
return _bytes(result.row, external_id)
|
||||
owner_id = mediated_source_owner(conn, actor_id, element, context)
|
||||
if owner_id is None:
|
||||
raise unprocessable(ORIGINAL_UNAVAILABLE)
|
||||
result = resolve_for_read(conn, owner_id, KIND, ref, None)
|
||||
if result.row is None:
|
||||
_audit_mediated(conn, actor_id, element, context, KIND, stored, "unavailable")
|
||||
raise unprocessable(ORIGINAL_UNAVAILABLE)
|
||||
try:
|
||||
data, media, name = _bytes(result.row, external_id)
|
||||
except Exception:
|
||||
_audit_mediated(conn, actor_id, element, context, KIND, result.row["id"], "unavailable")
|
||||
raise unprocessable(ORIGINAL_UNAVAILABLE) from None
|
||||
_audit_mediated(conn, actor_id, element, context, KIND, result.row["id"], "ok")
|
||||
return data, media, name
|
||||
|
||||
|
||||
def export_original(conn, actor_id: UUID, external_id: str, detail: dict, assigned, ref):
|
||||
del detail
|
||||
result = resolve_for_read(conn, actor_id, KIND, ref, assigned)
|
||||
if result.row is None:
|
||||
return RetrievalFailure(result.code, result.message, result.choices)
|
||||
try:
|
||||
data, media, name = _bytes(result.row, str(external_id))
|
||||
except Exception as exc:
|
||||
return RetrievalFailure("missing", str(getattr(exc, "detail", exc)))
|
||||
return FilePayload(name, media, data, "")
|
||||
|
||||
|
||||
def install_sample_source() -> None:
|
||||
register_source(SourceModule(
|
||||
kind=KIND,
|
||||
label="Beispielquelle",
|
||||
representation="text",
|
||||
capabilities=CAPABILITIES,
|
||||
parse_config=parse_config,
|
||||
probe=probe,
|
||||
find=find,
|
||||
open_original=open_original,
|
||||
export_original=export_original,
|
||||
navigate=None,
|
||||
preview=preview,
|
||||
))
|
||||
|
||||
|
||||
def remove_sample_source() -> None:
|
||||
unregister_source(KIND)
|
||||
|
||||
|
||||
def _connector(conn, owner_id: UUID, connector_id: UUID | None):
|
||||
if connector_id is not None:
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT id, owner_id, kind, name, settings, secret
|
||||
FROM connectors WHERE id = %s AND owner_id = %s
|
||||
""",
|
||||
(connector_id, owner_id),
|
||||
).fetchone()
|
||||
if row is None or row["kind"] != KIND:
|
||||
raise not_found()
|
||||
return row
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT id, owner_id, kind, name, settings, secret
|
||||
FROM connectors
|
||||
WHERE owner_id = %s AND kind = %s
|
||||
ORDER BY name, id
|
||||
""",
|
||||
(owner_id, KIND),
|
||||
).fetchall()
|
||||
if len(rows) != 1:
|
||||
raise unprocessable("Für diese Quelle ist kein eindeutiger Zugang zugeordnet.")
|
||||
return rows[0]
|
||||
|
||||
|
||||
def _items(row) -> dict:
|
||||
catalog = (row["settings"] or {}).get("catalog")
|
||||
return CATALOG.get(catalog) or {}
|
||||
|
||||
|
||||
def _bytes(row, external_id: str) -> tuple[bytes, str, str]:
|
||||
item = _items(row).get(external_id)
|
||||
if item is None:
|
||||
raise unprocessable("Dieser Eintrag ist in der Beispielquelle nicht vorhanden.")
|
||||
return item["text"].encode("utf-8"), "text/plain", f"{item['id']}.txt"
|
||||
@@ -0,0 +1,333 @@
|
||||
"""Section A: source binding and the storage port, with today's behaviour kept."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
|
||||
from app.core.errors import Problem
|
||||
from app.media.port import (
|
||||
LEGACY_GENERATION,
|
||||
LocalAdapter,
|
||||
MemoryAdapter,
|
||||
check_object_key,
|
||||
instance_for_write,
|
||||
publish,
|
||||
read_clear_bytes,
|
||||
)
|
||||
from app.sourcebind import backfill_source_bindings
|
||||
from tests.support import ORIGIN, Actor, register
|
||||
|
||||
PDF = b"%PDF-1.4\n1 0 obj<<>>endobj\ntrailer<<>>\n%%EOF\n"
|
||||
|
||||
|
||||
def _raw(actor: Actor, path: str):
|
||||
return actor.client.get(
|
||||
path,
|
||||
headers={
|
||||
"Origin": ORIGIN,
|
||||
"X-CSRF-Token": actor.csrf,
|
||||
"X-Scope-Kind": actor.scope["kind"],
|
||||
"X-Scope-Id": actor.scope["id"],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _case(actor: Actor) -> dict:
|
||||
return actor.request("POST", "/api/v1/cases", json={"title": "Bestand"}, status=201)
|
||||
|
||||
|
||||
def _paperless(actor: Actor, name: str, token: str) -> str:
|
||||
row = actor.request(
|
||||
"POST",
|
||||
"/api/v1/connectors",
|
||||
json={"kind": "paperless", "name": name, "base_url": "https://paper.example.test", "token": token},
|
||||
status=201,
|
||||
)
|
||||
return row["id"]
|
||||
|
||||
|
||||
def _link(actor: Actor, case_id: str, connector_id: str | None, external_id: str, title: str) -> str:
|
||||
body = {
|
||||
"title": title,
|
||||
"source": "paperless",
|
||||
"external_id": external_id,
|
||||
"context": {"kind": "case", "id": case_id},
|
||||
}
|
||||
if connector_id:
|
||||
body["detail"] = {"connector_id": connector_id}
|
||||
created = actor.request("POST", "/api/v1/external-refs", json=body, status=201)
|
||||
return created["id"]
|
||||
|
||||
|
||||
def test_storage_adapters_roundtrip_and_reject_escape(tmp_path):
|
||||
payload = b"opaque-bytes"
|
||||
key = str(uuid4())
|
||||
revision = "rev-1"
|
||||
for adapter in (LocalAdapter(tmp_path), MemoryAdapter()):
|
||||
publish(adapter, key, payload)
|
||||
assert adapter.open(key) == payload
|
||||
assert adapter.describe(key)["byte_size"] == len(payload)
|
||||
assert adapter.open(key) == payload
|
||||
assert revision == "rev-1"
|
||||
memory = MemoryAdapter()
|
||||
memory.fail_put = True
|
||||
with pytest.raises(Problem) as failed:
|
||||
publish(memory, str(uuid4()), payload)
|
||||
assert failed.value.code == "storage_unavailable"
|
||||
assert memory._items == {}
|
||||
with pytest.raises(Problem):
|
||||
check_object_key("../secret")
|
||||
with pytest.raises(Problem):
|
||||
check_object_key("/etc/passwd")
|
||||
with pytest.raises(Problem):
|
||||
LocalAdapter(tmp_path).open("dispatch/../../" + str(uuid4()))
|
||||
|
||||
|
||||
def test_changed_root_does_not_retarget_legacy(db, database):
|
||||
legacy = db.execute(
|
||||
"SELECT id, root_path FROM storage_instances WHERE generation = %s",
|
||||
(LEGACY_GENERATION,),
|
||||
).fetchone()
|
||||
key = str(uuid4())
|
||||
original = b"%PDF-legacy-root"
|
||||
publish(LocalAdapter(legacy["root_path"]), key, original)
|
||||
other = Path(database.media_root).parent / f"other-root-{uuid4().hex}"
|
||||
other.mkdir()
|
||||
moved = instance_for_write(db, str(other))
|
||||
assert moved["id"] != legacy["id"]
|
||||
assert moved["generation"] != LEGACY_GENERATION
|
||||
publish(LocalAdapter(moved["root_path"]), key, b"%PDF-other-root")
|
||||
seen = read_clear_bytes(
|
||||
db,
|
||||
instance_id=legacy["id"],
|
||||
generation=LEGACY_GENERATION,
|
||||
object_key=key,
|
||||
media_root=str(other),
|
||||
protection_profile="legacy-plaintext",
|
||||
expected_sha256=None,
|
||||
)
|
||||
assert seen == original
|
||||
db.rollback()
|
||||
|
||||
|
||||
def test_existing_file_keeps_id_bytes_and_revision(client, db):
|
||||
register(db, "ada-file@example.test", "Ada File")
|
||||
ada = Actor(client, "ada-file@example.test", "Ada File")
|
||||
ada.login()
|
||||
case = _case(ada)
|
||||
upload = ada.request(
|
||||
"POST",
|
||||
"/api/v1/files",
|
||||
data={"title": "Scan", "context_kind": "case", "context_id": case["id"]},
|
||||
files={"upload": ("scan.pdf", PDF, "application/pdf")},
|
||||
status=201,
|
||||
)
|
||||
stored = db.execute(
|
||||
"""
|
||||
SELECT a.id, a.object_key, a.content_revision, a.storage_generation, a.protection_profile,
|
||||
s.root_path
|
||||
FROM media_assets a
|
||||
JOIN storage_instances s ON s.id = a.storage_instance_id
|
||||
WHERE a.element_id = %s
|
||||
""",
|
||||
(upload["id"],),
|
||||
).fetchone()
|
||||
assert stored["protection_profile"] == "legacy-plaintext"
|
||||
assert stored["object_key"] == str(stored["id"])
|
||||
assert Path(stored["root_path"]) == Path(client.app.state.settings.media_root).resolve()
|
||||
assert stored["storage_generation"]
|
||||
first = stored["content_revision"]
|
||||
opened = _raw(ada, f"/api/v1/files/{upload['id']}/content")
|
||||
assert opened.status_code == 200
|
||||
assert opened.content == PDF
|
||||
again = db.execute(
|
||||
"SELECT content_revision FROM media_assets WHERE element_id = %s",
|
||||
(upload["id"],),
|
||||
).fetchone()
|
||||
assert again["content_revision"] == first
|
||||
refused = ada.client.patch(
|
||||
f"/api/v1/notes/{upload['id']}",
|
||||
json={"expected_version": 1, "document": {"version": 1, "blocks": [{"type": "paragraph", "text": "nein"}]}},
|
||||
headers={
|
||||
"Origin": ORIGIN,
|
||||
"X-CSRF-Token": ada.csrf,
|
||||
"X-Scope-Kind": ada.scope["kind"],
|
||||
"X-Scope-Id": ada.scope["id"],
|
||||
},
|
||||
)
|
||||
assert refused.status_code == 422
|
||||
assert refused.json()["code"] == "unsupported"
|
||||
|
||||
|
||||
def test_two_paperless_instances_stay_bound_when_order_changes(client, db, monkeypatch):
|
||||
register(db, "ada-two@example.test", "Ada Two")
|
||||
ada = Actor(client, "ada-two@example.test", "Ada Two")
|
||||
ada.login()
|
||||
case = _case(ada)
|
||||
older = _paperless(ada, "Zweitens", "token-older")
|
||||
newer = _paperless(ada, "Erstens", "token-newer")
|
||||
assert older != newer
|
||||
|
||||
def fake_bytes(row, document_id):
|
||||
return f"%PDF-{row['secret']}-{document_id}\n".encode(), "application/pdf", "dokument.pdf"
|
||||
|
||||
monkeypatch.setattr("app.sources._paperless_bytes", fake_bytes)
|
||||
left = _link(ada, case["id"], older, "15", "Altbestand")
|
||||
right = _link(ada, case["id"], newer, "15", "Zweiter Bestand")
|
||||
db.execute("UPDATE connectors SET created_at = now() - interval '2 days' WHERE id = %s", (newer,))
|
||||
db.execute("UPDATE connectors SET created_at = now() WHERE id = %s", (older,))
|
||||
db.commit()
|
||||
assert _raw(ada, f"/api/v1/external-refs/{left}/content").content.startswith(b"%PDF-token-older-15")
|
||||
assert _raw(ada, f"/api/v1/external-refs/{right}/content").content.startswith(b"%PDF-token-newer-15")
|
||||
calls = []
|
||||
|
||||
def fake_get(*args, **kwargs):
|
||||
calls.append(args)
|
||||
return {"results": [{"id": 15, "title": "Treffer", "created": "2024-01-02"}]}
|
||||
|
||||
monkeypatch.setattr("app.sources._paperless_get", fake_get)
|
||||
headers = {
|
||||
"Origin": ORIGIN,
|
||||
"X-CSRF-Token": ada.csrf,
|
||||
"X-Scope-Kind": ada.scope["kind"],
|
||||
"X-Scope-Id": ada.scope["id"],
|
||||
}
|
||||
refused = ada.client.get("/api/v1/sources/paperless/documents", params={"q": "Treffer"}, headers=headers)
|
||||
assert refused.status_code == 422
|
||||
assert calls == []
|
||||
found = ada.request(
|
||||
"GET",
|
||||
"/api/v1/sources/paperless/documents",
|
||||
params={"q": "Treffer", "connector_id": older},
|
||||
)
|
||||
assert found["items"][0]["title"] == "Treffer"
|
||||
assert found["connector_id"] == older
|
||||
assert found["capabilities"]["search"]["correspondent"] is False
|
||||
assert "correspondent" in found["unsupported"]
|
||||
filtered = ada.client.get(
|
||||
"/api/v1/sources/paperless/documents",
|
||||
params={"q": "Treffer", "connector_id": older, "correspondent": "Haus"},
|
||||
headers=headers,
|
||||
)
|
||||
assert filtered.status_code == 422
|
||||
assert filtered.json()["code"] == "unsupported"
|
||||
assert "Haus" not in json.dumps(filtered.json())
|
||||
|
||||
|
||||
def test_ambiguous_legacy_stays_until_assignment(client, db, monkeypatch):
|
||||
register(db, "ada-old@example.test", "Ada Old")
|
||||
ada = Actor(client, "ada-old@example.test", "Ada Old")
|
||||
ada.login()
|
||||
case = _case(ada)
|
||||
loose = _link(ada, case["id"], None, "15", "Ohne Instanz")
|
||||
_paperless(ada, "Alpha", "token-alpha")
|
||||
second = _paperless(ada, "Beta", "token-beta")
|
||||
db.execute(
|
||||
"""
|
||||
UPDATE external_references
|
||||
SET connector_id = NULL, resolution = 'unresolved', detail = '{}'::jsonb
|
||||
WHERE element_id = %s
|
||||
""",
|
||||
(loose,),
|
||||
)
|
||||
backfill_source_bindings(db)
|
||||
state = db.execute(
|
||||
"SELECT connector_id, resolution FROM external_references WHERE element_id = %s",
|
||||
(loose,),
|
||||
).fetchone()
|
||||
db.commit()
|
||||
assert state["resolution"] == "ambiguous"
|
||||
assert state["connector_id"] is None
|
||||
blocked = _raw(ada, f"/api/v1/external-refs/{loose}/content")
|
||||
assert blocked.status_code == 422
|
||||
assert "eindeutiger Zugang" in blocked.json()["detail"]
|
||||
|
||||
def fake_bytes(row, document_id):
|
||||
del document_id
|
||||
return f"%PDF-{row['secret']}\n".encode(), "application/pdf", "dokument.pdf"
|
||||
|
||||
monkeypatch.setattr("app.sources._paperless_bytes", fake_bytes)
|
||||
ada.request(
|
||||
"POST",
|
||||
f"/api/v1/external-refs/{loose}/assignment",
|
||||
json={"connector_id": second},
|
||||
)
|
||||
_paperless(ada, "Gamma", "token-gamma")
|
||||
assert _raw(ada, f"/api/v1/external-refs/{loose}/content").content.startswith(b"%PDF-token-beta")
|
||||
|
||||
|
||||
def test_locked_profile_serves_metadata_not_cleartext(client, db):
|
||||
register(db, "ada-lock@example.test", "Ada Lock")
|
||||
ada = Actor(client, "ada-lock@example.test", "Ada Lock")
|
||||
ada.login()
|
||||
case = _case(ada)
|
||||
upload = ada.request(
|
||||
"POST",
|
||||
"/api/v1/files",
|
||||
data={"title": "Geschuetzt", "context_kind": "case", "context_id": case["id"]},
|
||||
files={"upload": ("geheim.pdf", PDF, "application/pdf")},
|
||||
status=201,
|
||||
)
|
||||
dummy = b"cipher-dummy-not-clear"
|
||||
asset = db.execute(
|
||||
"SELECT id, object_key, storage_instance_id FROM media_assets WHERE element_id = %s",
|
||||
(upload["id"],),
|
||||
).fetchone()
|
||||
root = db.execute(
|
||||
"SELECT root_path FROM storage_instances WHERE id = %s",
|
||||
(asset["storage_instance_id"],),
|
||||
).fetchone()["root_path"]
|
||||
path = Path(root) / asset["object_key"]
|
||||
path.write_bytes(dummy)
|
||||
db.execute(
|
||||
"""
|
||||
UPDATE media_assets
|
||||
SET protection_profile = 'client-encrypted',
|
||||
key_responsibility = 'client',
|
||||
sha256 = %s
|
||||
WHERE element_id = %s
|
||||
""",
|
||||
(hashlib.sha256(dummy).hexdigest(), upload["id"]),
|
||||
)
|
||||
db.commit()
|
||||
detail = ada.request("GET", f"/api/v1/cases/{case['id']}")
|
||||
rendered = json.dumps(detail)
|
||||
assert "client-encrypted" in rendered
|
||||
assert "Geschuetzt" in rendered
|
||||
assert dummy.decode() not in rendered
|
||||
opened = _raw(ada, f"/api/v1/files/{upload['id']}/content")
|
||||
assert opened.status_code == 422
|
||||
assert opened.json()["code"] == "unsupported"
|
||||
assert path.read_bytes() == dummy
|
||||
|
||||
|
||||
def test_credential_update_keeps_the_instance(client, db):
|
||||
register(db, "ada-key@example.test", "Ada Key")
|
||||
ada = Actor(client, "ada-key@example.test", "Ada Key")
|
||||
ada.login()
|
||||
first = _paperless(ada, "Archiv", "token-first-1")
|
||||
second = _paperless(ada, "Anderes Archiv", "token-second-2")
|
||||
renewed = ada.request(
|
||||
"PATCH",
|
||||
f"/api/v1/connectors/{first}",
|
||||
json={"kind": "paperless", "name": "Archiv", "base_url": "https://paper.example.test", "token": "token-renewed"},
|
||||
)
|
||||
assert renewed["id"] == first
|
||||
secrets = {
|
||||
str(row["id"]): row["secret"]
|
||||
for row in db.execute("SELECT id, secret FROM connectors").fetchall()
|
||||
}
|
||||
assert secrets[first] == "token-renewed"
|
||||
assert secrets[second] == "token-second-2"
|
||||
caps = ada.request("GET", "/api/v1/sources/capabilities")
|
||||
assert caps["editors"]["note"] is True
|
||||
assert caps["editors"]["markdown_file"] is False
|
||||
assert caps["protection_profiles"]["client-encrypted"] == "locked"
|
||||
paperless = next(item for item in caps["sources"] if item["kind"] == "paperless")
|
||||
assert paperless["search"]["query"] is True
|
||||
assert paperless["edit_content"] is False
|
||||
@@ -230,7 +230,7 @@ def test_dispatch_keeps_the_secured_stand_and_distinct_mail_parts(client, db, mo
|
||||
"Fachlicher Zusatz: die Frist bleibt der 1. Juni.",
|
||||
[("rechnung.pdf", PDF_A), ("lage.pdf", PDF_B), ("konto.pdf", PDF_C), ("lage.pdf", PDF_D)],
|
||||
)
|
||||
monkeypatch.setattr("app.retrieval.pull_rfc822", lambda *args, **kwargs: raw)
|
||||
monkeypatch.setattr("app.sources.pull_rfc822", lambda *args, **kwargs: raw)
|
||||
ada = Actor(client, "ada-versand@example.test", "Ada Versand")
|
||||
register(db, ada.email, ada.name)
|
||||
ada.login()
|
||||
|
||||
@@ -141,9 +141,9 @@ def test_handover_package_follows_the_confirmed_selection(client, db, monkeypatc
|
||||
"Der vollständige Brieftext bleibt erhalten.",
|
||||
[("lage.pdf", PDF_A), ("lage.pdf", PDF_B)],
|
||||
)
|
||||
monkeypatch.setattr("app.retrieval.pull_rfc822", lambda *args, **kwargs: raw)
|
||||
monkeypatch.setattr("app.sources.pull_rfc822", lambda *args, **kwargs: raw)
|
||||
monkeypatch.setattr(
|
||||
"app.retrieval.pull_paperless_original",
|
||||
"app.sources.pull_paperless_original",
|
||||
lambda *args, **kwargs: (PDF_B, "application/pdf", "archiv.pdf"),
|
||||
)
|
||||
ada = Actor(client, "ada-export@example.test", "Ada Export")
|
||||
@@ -491,7 +491,7 @@ def test_export_names_gaps_and_hides_unreadable_counterparts(client, db, monkeyp
|
||||
calls.append(args)
|
||||
return _eml("<stored@example.test>", "darf nicht ersatzweise kommen", [])
|
||||
|
||||
monkeypatch.setattr("app.retrieval.pull_rfc822", pull)
|
||||
monkeypatch.setattr("app.sources.pull_rfc822", pull)
|
||||
context = {"kind": "case", "id": case["id"]}
|
||||
tree = ada.request(
|
||||
"GET",
|
||||
@@ -547,7 +547,7 @@ def test_export_names_gaps_and_hides_unreadable_counterparts(client, db, monkeyp
|
||||
assert {item["name"] for item in pending["issues"][0]["choices"]} == {"Neu", "Weiter"}
|
||||
monkeypatch.setattr("app.retrieval.EXPORT_BYTE_LIMIT", 40)
|
||||
huge = _eml("<offen@example.test>", "GEKÜRZT-VERBOTEN " * 20, [])
|
||||
monkeypatch.setattr("app.retrieval.pull_rfc822", lambda *args, **kwargs: huge)
|
||||
monkeypatch.setattr("app.sources.pull_rfc822", lambda *args, **kwargs: huge)
|
||||
assigned = ada.request(
|
||||
"POST",
|
||||
f"/api/v1/matters/{matter['id']}/export/review",
|
||||
@@ -557,7 +557,7 @@ def test_export_names_gaps_and_hides_unreadable_counterparts(client, db, monkeyp
|
||||
assert "GEKÜRZT-VERBOTEN" not in json.dumps(assigned)
|
||||
wrong = _eml("<andere@example.test>", "FALSCHE-NACHRICHT", [])
|
||||
monkeypatch.setattr("app.retrieval.EXPORT_BYTE_LIMIT", 80 * 1024 * 1024)
|
||||
monkeypatch.setattr("app.retrieval.pull_rfc822", lambda *args, **kwargs: wrong)
|
||||
monkeypatch.setattr("app.sources.pull_rfc822", lambda *args, **kwargs: wrong)
|
||||
identity = ada.request(
|
||||
"POST",
|
||||
f"/api/v1/matters/{matter['id']}/export/review",
|
||||
@@ -609,7 +609,7 @@ def test_nested_chapter_export_keeps_path_and_complete_mail(client, db, monkeypa
|
||||
"Die Antwort bleibt vollständig.",
|
||||
[("lage.pdf", PDF_A), ("konto.pdf", PDF_B)],
|
||||
)
|
||||
monkeypatch.setattr("app.retrieval.pull_rfc822", lambda *args, **kwargs: raw)
|
||||
monkeypatch.setattr("app.sources.pull_rfc822", lambda *args, **kwargs: raw)
|
||||
ada = Actor(client, "ada-nested-export@example.test", "Ada Verschachtelt")
|
||||
register(db, ada.email, ada.name)
|
||||
ada.login()
|
||||
@@ -844,7 +844,7 @@ def test_chapter_entries_share_one_numbering_sequence(client, db, monkeypatch):
|
||||
files={"upload": ("drittes.pdf", PDF_C, "application/pdf")},
|
||||
status=201,
|
||||
)
|
||||
monkeypatch.setattr("app.retrieval.pull_rfc822", lambda *args, **kwargs: raw)
|
||||
monkeypatch.setattr("app.sources.pull_rfc822", lambda *args, **kwargs: raw)
|
||||
tree = ada.request("GET", f"/api/v1/matters/{matter['id']}/export-tree")["tree"]
|
||||
exportable = {"binding", "annotation", "attachment", "dispatch_item", "evidence"}
|
||||
selected = [node["id"] for node in _walk(tree) if node["exportable"] and node["kind"] in exportable]
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
"""A third source appears only by registering its module."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import subprocess
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.sample_source import install_sample_source, remove_sample_source
|
||||
from tests.support import ORIGIN, Actor, document, register
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
TEXT = "Der Probenbrief bleibt lesbar."
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_source():
|
||||
install_sample_source()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
remove_sample_source()
|
||||
|
||||
|
||||
def test_registered_sample_source_links_reads_and_exports(client, db, sample_source):
|
||||
del sample_source
|
||||
ada = Actor(client, "ada-sample@example.test", "Ada Probe")
|
||||
ben = Actor(client, "ben-sample@example.test", "Ben Probe")
|
||||
register(db, ada.email, ada.name)
|
||||
register(db, ben.email, ben.name)
|
||||
ada.login()
|
||||
refused = ada.request(
|
||||
"POST",
|
||||
"/api/v1/connectors",
|
||||
json={"kind": "missing", "name": "Unbekannt"},
|
||||
status=422,
|
||||
)
|
||||
assert refused["code"] == "unprocessable"
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/connectors",
|
||||
json={"kind": "sample", "name": "Ohne Katalog", "config": {"catalog": "kein-katalog"}},
|
||||
status=422,
|
||||
)
|
||||
connector_id = ada.request(
|
||||
"POST",
|
||||
"/api/v1/connectors",
|
||||
json={"kind": "sample", "name": "Katalog Alpha", "config": {"catalog": "alpha"}},
|
||||
status=201,
|
||||
)["id"]
|
||||
probed = ada.request("POST", f"/api/v1/connectors/{connector_id}/probe")
|
||||
assert probed["ok"] is True
|
||||
assert "2 Einträge" in probed["detail"]
|
||||
blocked = ada.request(
|
||||
"GET",
|
||||
"/api/v1/sources/sample/search",
|
||||
params={"q": "Probe", "correspondent": "geheim-wert"},
|
||||
status=422,
|
||||
)
|
||||
assert blocked["code"] == "unsupported"
|
||||
assert "geheim-wert" not in blocked["detail"]
|
||||
ada.request("GET", "/api/v1/sources/sample/navigate", params={"connector_id": connector_id}, status=422)
|
||||
found = ada.request("GET", "/api/v1/sources/sample/search", params={"q": "Proben", "connector_id": connector_id})
|
||||
assert [item["id"] for item in found["items"]] == ["brief"]
|
||||
preview = ada.request(
|
||||
"GET",
|
||||
"/api/v1/sources/sample/preview",
|
||||
params={"connector_id": connector_id, "item": "brief"},
|
||||
)
|
||||
assert preview["text"] == TEXT
|
||||
matter = ada.request("POST", "/api/v1/matters", json={"title": "Probenvorgang"}, status=201)
|
||||
linked = ada.request(
|
||||
"POST",
|
||||
"/api/v1/external-refs",
|
||||
json={
|
||||
"title": "Probenbrief",
|
||||
"source": "sample",
|
||||
"external_id": "brief",
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
"detail": {"connector_id": connector_id},
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
opened = _raw(ada, f"/api/v1/external-refs/{linked['id']}/content?context_kind=matter&context_id={matter['id']}")
|
||||
assert opened.status_code == 200
|
||||
assert opened.content == TEXT.encode("utf-8")
|
||||
assert opened.headers["content-type"].startswith("text/plain")
|
||||
detail = ada.request("GET", f"/api/v1/matters/{matter['id']}")
|
||||
element = next(item["element"] for item in detail["bindings"] if item["element"]["id"] == linked["id"])
|
||||
assert element["preview"]["media"] == "text"
|
||||
written = ada.request(
|
||||
"PATCH",
|
||||
f"/api/v1/notes/{linked['id']}",
|
||||
json={
|
||||
"expected_version": element["version"],
|
||||
"document": document("Das darf nicht geschrieben werden."),
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
},
|
||||
status=422,
|
||||
)
|
||||
assert written["code"] == "unsupported"
|
||||
tree = ada.request("GET", f"/api/v1/matters/{matter['id']}/export-tree")["tree"]
|
||||
selected = [node["id"] for node in _walk(tree) if node["kind"] == "binding" and node.get("exportable")]
|
||||
body = {"context": {"kind": "matter", "id": matter["id"]}, "selected": selected}
|
||||
review = ada.request("POST", f"/api/v1/matters/{matter['id']}/export/review", json=body)
|
||||
assert review["ready"] is True, review
|
||||
package = _post_zip(ada, matter["id"], body)
|
||||
assert package.status_code == 200, package.text
|
||||
archive = zipfile.ZipFile(io.BytesIO(package.content))
|
||||
assert any(archive.read(name) == TEXT.encode("utf-8") for name in archive.namelist())
|
||||
|
||||
ben.login()
|
||||
ben.request(
|
||||
"GET",
|
||||
"/api/v1/sources/sample/search",
|
||||
params={"connector_id": connector_id, "q": "Proben"},
|
||||
status=404,
|
||||
)
|
||||
hidden = _raw(ben, f"/api/v1/external-refs/{linked['id']}/content?context_kind=matter&context_id={matter['id']}")
|
||||
assert hidden.status_code == 404
|
||||
ada.login()
|
||||
ada.request(
|
||||
"POST",
|
||||
f"/api/v1/matters/{matter['id']}/grants",
|
||||
json={"email": ben.email, "can_read": True, "can_edit": False, "can_share": False, "use_owner_sources": False},
|
||||
status=201,
|
||||
)
|
||||
ben.login()
|
||||
denied = _raw(ben, f"/api/v1/external-refs/{linked['id']}/content?context_kind=matter&context_id={matter['id']}")
|
||||
assert denied.status_code == 422
|
||||
assert denied.json()["detail"] == "Das Original ist über die Freigabe nicht abrufbar."
|
||||
ada.login()
|
||||
grant_id = next(item["id"] for item in ada.request("GET", f"/api/v1/matters/{matter['id']}")["grants"])
|
||||
ada.request("DELETE", f"/api/v1/grants/{grant_id}", status=204)
|
||||
ada.request(
|
||||
"POST",
|
||||
f"/api/v1/matters/{matter['id']}/grants",
|
||||
json={"email": ben.email, "can_read": True, "can_edit": False, "can_share": False, "use_owner_sources": True},
|
||||
status=201,
|
||||
)
|
||||
ben.login()
|
||||
shared = _raw(ben, f"/api/v1/external-refs/{linked['id']}/content?context_kind=matter&context_id={matter['id']}")
|
||||
assert shared.status_code == 200
|
||||
assert shared.content == TEXT.encode("utf-8")
|
||||
shown = ben.request("GET", f"/api/v1/matters/{matter['id']}")
|
||||
visible = next(item["element"] for item in shown["bindings"] if item["element"]["id"] == linked["id"])
|
||||
assert "connector_id" not in (visible["external"].get("detail") or {})
|
||||
ada.login()
|
||||
grant_id = next(item["id"] for item in ada.request("GET", f"/api/v1/matters/{matter['id']}")["grants"])
|
||||
ada.request("DELETE", f"/api/v1/grants/{grant_id}", status=204)
|
||||
ben.login()
|
||||
revoked = _raw(ben, f"/api/v1/external-refs/{linked['id']}/content?context_kind=matter&context_id={matter['id']}")
|
||||
assert revoked.status_code == 404
|
||||
|
||||
|
||||
def test_sample_registration_does_not_edit_generic_modules():
|
||||
forbidden = [
|
||||
"backend/app/api/schemas.py",
|
||||
"backend/app/exporting.py",
|
||||
"backend/app/present.py",
|
||||
"frontend/src/pages/Cases.jsx",
|
||||
"frontend/src/pages/Matters.jsx",
|
||||
"frontend/src/pages/ReadingPane.jsx",
|
||||
"frontend/src/pages/ExportPackage.jsx",
|
||||
"frontend/src/App.jsx",
|
||||
"frontend/src/pages/Sources.jsx",
|
||||
]
|
||||
for relative in forbidden:
|
||||
text = (ROOT / relative).read_text(encoding="utf-8")
|
||||
assert '"sample"' not in text and "'sample'" not in text, relative
|
||||
completed = subprocess.run(
|
||||
[
|
||||
"node",
|
||||
"--input-type=module",
|
||||
"-e",
|
||||
(
|
||||
"import { installSampleSource, removeSampleSource } from './src/sources/sample-source.js';"
|
||||
"import { sourceModules } from './src/sources/registry-core.js';"
|
||||
"installSampleSource();"
|
||||
"if (!sourceModules().some((item) => item.kind === 'sample' && item.representation === 'text')) process.exit(2);"
|
||||
"removeSampleSource();"
|
||||
"if (sourceModules().some((item) => item.kind === 'sample')) process.exit(3);"
|
||||
),
|
||||
],
|
||||
cwd=ROOT / "frontend",
|
||||
check=False,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
assert completed.returncode == 0, completed.stderr
|
||||
|
||||
|
||||
def _raw(actor: Actor, path: str):
|
||||
return actor.client.get(
|
||||
path,
|
||||
headers={
|
||||
"Origin": ORIGIN,
|
||||
"X-CSRF-Token": actor.csrf,
|
||||
"X-Scope-Kind": actor.scope["kind"],
|
||||
"X-Scope-Id": actor.scope["id"],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _post_zip(actor: Actor, matter_id: str, body: dict):
|
||||
return actor.client.post(
|
||||
f"/api/v1/matters/{matter_id}/export",
|
||||
json=body,
|
||||
headers={
|
||||
"Origin": ORIGIN,
|
||||
"X-CSRF-Token": actor.csrf,
|
||||
"Content-Type": "application/json",
|
||||
"X-Scope-Kind": actor.scope["kind"],
|
||||
"X-Scope-Id": actor.scope["id"],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _walk(node):
|
||||
yield node
|
||||
for child in node.get("children") or []:
|
||||
yield from _walk(child)
|
||||
Reference in new issue
Block a user