Stelle bestehende Quellzuordnungen wieder her und halte Connectoren beim eigenen Konto.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
d6b70d8f92
commit
c3a07cee64
8 files changed
+250
-9
No files matched your search
@@ -58,7 +58,11 @@ def _prepare_source_storage(settings: Settings) -> None:
|
||||
|
||||
from app.media.port import ensure_legacy_instance
|
||||
from app.sourcebind import backfill_source_bindings
|
||||
from app.sources import _install_sources
|
||||
|
||||
# The process that migrates has not imported the application yet.
|
||||
# Without the modules, a legacy reference would stay unresolved.
|
||||
_install_sources()
|
||||
with psycopg.connect(settings.dsn(), row_factory=dict_row) as conn:
|
||||
backfill_source_bindings(conn)
|
||||
ensure_legacy_instance(conn, settings.media_root)
|
||||
|
||||
+11
-3
@@ -218,12 +218,20 @@ def _element_body(conn, actor: UUID, element: dict) -> dict:
|
||||
if revealed:
|
||||
external["external_id"] = ref["external_id"]
|
||||
payload["external"] = external
|
||||
from app.sourcebind import resolve_for_read
|
||||
from app.sourceregistry import get_source
|
||||
|
||||
if resolution in {"ambiguous", "unresolved"} and get_source(ref["source"]) is not None:
|
||||
payload["preview"] = {"available": False, "media": "unassigned", "source": ref["source"]}
|
||||
else:
|
||||
module = get_source(ref["source"])
|
||||
if module is None:
|
||||
payload["preview"] = _preview_meta("external_ref", None, ref["source"])
|
||||
else:
|
||||
resolved = resolve_for_read(conn, element["owner_subject_id"], ref["source"], ref, None)
|
||||
if resolved.code == "ok":
|
||||
payload["preview"] = _preview_meta("external_ref", None, ref["source"])
|
||||
elif actor == element["owner_subject_id"]:
|
||||
payload["preview"] = {"available": False, "media": "unassigned", "source": ref["source"]}
|
||||
else:
|
||||
payload["preview"] = {"available": False, "media": "none", "source": ref["source"]}
|
||||
return payload
|
||||
|
||||
|
||||
|
||||
@@ -146,7 +146,15 @@ def attach_new_reference(conn, element_id: UUID) -> None:
|
||||
|
||||
|
||||
def backfill_source_bindings(conn) -> None:
|
||||
"""Idempotent. Bound and ambiguous rows stay. Unique unresolved stock is bound once."""
|
||||
"""Idempotent. Unique stock is bound once. A stored id is never retargeted.
|
||||
|
||||
Startup calls this before the application has imported the connector
|
||||
modules. Register them first, otherwise every existing reference stays
|
||||
unresolved and the case or matter can no longer open its originals.
|
||||
"""
|
||||
from app.sources import _install_sources
|
||||
|
||||
_install_sources()
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE external_references
|
||||
@@ -160,7 +168,7 @@ def backfill_source_bindings(conn) -> None:
|
||||
FROM external_references er
|
||||
JOIN elements e ON e.id = er.element_id
|
||||
WHERE er.source <> 'url'
|
||||
AND er.resolution = 'unresolved'
|
||||
AND er.resolution IN ('unresolved', 'ambiguous')
|
||||
AND er.connector_id IS NULL
|
||||
"""
|
||||
).fetchall()
|
||||
|
||||
@@ -243,6 +243,9 @@ def test_ambiguous_legacy_stays_until_assignment(client, db, monkeypatch):
|
||||
db.commit()
|
||||
assert state["resolution"] == "ambiguous"
|
||||
assert state["connector_id"] is None
|
||||
opened = ada.request("GET", f"/api/v1/cases/{case['id']}")
|
||||
assert opened["bindings"][0]["element"]["id"] == loose
|
||||
assert opened["bindings"][0]["element"]["preview"]["media"] == "unassigned"
|
||||
blocked = _raw(ada, f"/api/v1/external-refs/{loose}/content")
|
||||
assert blocked.status_code == 422
|
||||
assert "eindeutiger Zugang" in blocked.json()["detail"]
|
||||
@@ -261,6 +264,100 @@ def test_ambiguous_legacy_stays_until_assignment(client, db, monkeypatch):
|
||||
assert _raw(ada, f"/api/v1/external-refs/{loose}/content").content.startswith(b"%PDF-token-beta")
|
||||
|
||||
|
||||
def test_legacy_stock_rebinds_and_case_and_matter_open(client, db, monkeypatch):
|
||||
from app.sourceregistry import get_source, unregister_source
|
||||
|
||||
register(db, "ada-keep@example.test", "Ada Keep")
|
||||
ada = Actor(client, "ada-keep@example.test", "Ada Keep")
|
||||
ada.login()
|
||||
case = _case(ada)
|
||||
matter = ada.request("POST", "/api/v1/matters", json={"title": "Bestand"}, status=201)
|
||||
kept = _paperless(ada, "Archiv", "token-kept")
|
||||
case_ref = _link(ada, case["id"], None, "15", "Vertragsakte")
|
||||
matter_ref = ada.request(
|
||||
"POST",
|
||||
"/api/v1/external-refs",
|
||||
json={
|
||||
"title": "Vertragsvorgang",
|
||||
"source": "paperless",
|
||||
"external_id": "16",
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
},
|
||||
status=201,
|
||||
)["id"]
|
||||
db.execute(
|
||||
"""
|
||||
UPDATE external_references
|
||||
SET connector_id = NULL, resolution = 'unresolved', detail = '{}'::jsonb
|
||||
WHERE element_id IN (%s, %s)
|
||||
""",
|
||||
(case_ref, matter_ref),
|
||||
)
|
||||
db.commit()
|
||||
unregister_source("paperless")
|
||||
unregister_source("imap")
|
||||
assert get_source("paperless") is None
|
||||
try:
|
||||
backfill_source_bindings(db)
|
||||
db.commit()
|
||||
for element_id in (case_ref, matter_ref):
|
||||
state = db.execute(
|
||||
"SELECT connector_id::text AS connector_id, resolution FROM external_references WHERE element_id = %s",
|
||||
(element_id,),
|
||||
).fetchone()
|
||||
assert state["resolution"] == "bound"
|
||||
assert state["connector_id"] == kept
|
||||
assert get_source("paperless") is not None
|
||||
case_page = ada.request("GET", f"/api/v1/cases/{case['id']}")
|
||||
matter_page = ada.request("GET", f"/api/v1/matters/{matter['id']}")
|
||||
assert case_page["bindings"][0]["element"]["preview"]["available"] is True
|
||||
assert case_page["bindings"][0]["element"]["preview"]["media"] == "pdf"
|
||||
assert matter_page["bindings"][0]["element"]["preview"]["available"] is True
|
||||
monkeypatch.setattr(
|
||||
"app.sources._paperless_bytes",
|
||||
lambda row, document_id: (f"%PDF-{row['secret']}-{document_id}\n".encode(), "application/pdf", "dokument.pdf"),
|
||||
)
|
||||
assert _raw(ada, f"/api/v1/external-refs/{case_ref}/content").content.startswith(b"%PDF-token-kept-15")
|
||||
assert _raw(ada, f"/api/v1/external-refs/{matter_ref}/content").content.startswith(b"%PDF-token-kept-16")
|
||||
finally:
|
||||
from app.sources import _install_sources
|
||||
|
||||
_install_sources()
|
||||
|
||||
|
||||
def test_connectors_remain_with_their_account(client, db, monkeypatch):
|
||||
calls = []
|
||||
monkeypatch.setattr(
|
||||
"app.sources._paperless_get",
|
||||
lambda *args, **kwargs: calls.append(args) or {"results": []},
|
||||
)
|
||||
register(db, "ada-own@example.test", "Ada Own")
|
||||
register(db, "ben-own@example.test", "Ben Own")
|
||||
ada = Actor(client, "ada-own@example.test", "Ada Own")
|
||||
ben = Actor(client, "ben-own@example.test", "Ben Own")
|
||||
ada.login()
|
||||
ada_paper = _paperless(ada, "Ada Archiv", "token-ada")
|
||||
ben.login()
|
||||
ben_paper = _paperless(ben, "Ben Archiv", "token-ben")
|
||||
listed = ben.request("GET", "/api/v1/connectors")
|
||||
ids = {item["id"] for item in listed["items"]}
|
||||
assert ben_paper in ids
|
||||
assert ada_paper not in ids
|
||||
assert all(item["name"] != "Ada Archiv" for item in listed["items"])
|
||||
ben.request("POST", f"/api/v1/connectors/{ada_paper}/probe", status=404)
|
||||
ben.request(
|
||||
"GET",
|
||||
"/api/v1/sources/paperless/documents",
|
||||
params={"q": "fremd", "connector_id": ada_paper},
|
||||
status=404,
|
||||
)
|
||||
assert calls == []
|
||||
ada.login()
|
||||
own = {item["id"] for item in ada.request("GET", "/api/v1/connectors")["items"]}
|
||||
assert ada_paper in own
|
||||
assert ben_paper not in own
|
||||
|
||||
|
||||
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")
|
||||
@@ -331,3 +428,112 @@ def test_credential_update_keeps_the_instance(client, db):
|
||||
paperless = next(item for item in caps["sources"] if item["kind"] == "paperless")
|
||||
assert paperless["search"]["query"] is True
|
||||
assert paperless["edit_content"] is False
|
||||
|
||||
|
||||
def test_legacy_reference_stays_bound_and_foreign_connectors_stay_hidden(client, db, database, monkeypatch):
|
||||
"""A startup without loaded modules must still keep the one existing connection."""
|
||||
from app.db.migrate import _prepare_source_storage
|
||||
from app.sourceregistry import unregister_source
|
||||
from app.sources import _install_sources
|
||||
|
||||
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)
|
||||
register(db, "ada-legacy@example.test", "Ada Legacy")
|
||||
register(db, "ben-legacy@example.test", "Ben Legacy")
|
||||
ada = Actor(client, "ada-legacy@example.test", "Ada Legacy")
|
||||
ben = Actor(client, "ben-legacy@example.test", "Ben Legacy")
|
||||
ada.login()
|
||||
case = _case(ada)
|
||||
matter = ada.request("POST", "/api/v1/matters", json={"title": "Altbestand"}, status=201)
|
||||
connector = _paperless(ada, "Archiv", "token-legacy")
|
||||
case_ref = _link(ada, case["id"], None, "15", "Akte")
|
||||
matter_ref = ada.request(
|
||||
"POST",
|
||||
"/api/v1/external-refs",
|
||||
json={
|
||||
"title": "Vorgang",
|
||||
"source": "paperless",
|
||||
"external_id": "16",
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
},
|
||||
status=201,
|
||||
)["id"]
|
||||
db.execute(
|
||||
"""
|
||||
UPDATE external_references
|
||||
SET connector_id = NULL, resolution = 'unresolved', detail = '{}'::jsonb
|
||||
WHERE element_id IN (%s, %s)
|
||||
""",
|
||||
(case_ref, matter_ref),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
opened = ada.request("GET", f"/api/v1/cases/{case['id']}")
|
||||
shown = next(item["element"] for item in opened["bindings"] if item["element"]["id"] == case_ref)
|
||||
assert shown["preview"]["media"] == "pdf"
|
||||
assert shown["preview"]["available"] is True
|
||||
assert _raw(ada, f"/api/v1/external-refs/{case_ref}/content?context_kind=case&context_id={case['id']}").content.startswith(
|
||||
b"%PDF-token-legacy"
|
||||
)
|
||||
matter_view = ada.request("GET", f"/api/v1/matters/{matter['id']}")
|
||||
matter_element = next(item["element"] for item in matter_view["bindings"] if item["element"]["id"] == matter_ref)
|
||||
assert matter_element["preview"]["media"] == "pdf"
|
||||
|
||||
unregister_source("paperless")
|
||||
unregister_source("imap")
|
||||
try:
|
||||
_prepare_source_storage(database)
|
||||
finally:
|
||||
_install_sources()
|
||||
state = db.execute(
|
||||
"SELECT connector_id::text AS connector_id, resolution FROM external_references WHERE element_id = %s",
|
||||
(case_ref,),
|
||||
).fetchone()
|
||||
assert state["connector_id"] == connector
|
||||
assert state["resolution"] == "bound"
|
||||
|
||||
ben.login()
|
||||
own = _paperless(ben, "Bens Archiv", "token-ben")
|
||||
listed = {item["id"] for item in ben.request("GET", "/api/v1/connectors")["items"]}
|
||||
assert listed == {own}
|
||||
ben.request("POST", f"/api/v1/connectors/{connector}/probe", status=404)
|
||||
ben.request(
|
||||
"GET",
|
||||
"/api/v1/sources/paperless/documents",
|
||||
params={"q": "Alt", "connector_id": connector},
|
||||
status=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()
|
||||
shared = ben.request("GET", f"/api/v1/matters/{matter['id']}")
|
||||
shared_element = next(item["element"] for item in shared["bindings"] if item["element"]["id"] == matter_ref)
|
||||
assert "connector_id" not in shared_element["external"]["detail"]
|
||||
assert "external_id" not in shared_element["external"]
|
||||
blocked = _raw(
|
||||
ben,
|
||||
f"/api/v1/external-refs/{matter_ref}/content?context_kind=matter&context_id={matter['id']}",
|
||||
)
|
||||
assert blocked.status_code == 422
|
||||
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": True},
|
||||
status=201,
|
||||
)
|
||||
ben.login()
|
||||
allowed = _raw(
|
||||
ben,
|
||||
f"/api/v1/external-refs/{matter_ref}/content?context_kind=matter&context_id={matter['id']}",
|
||||
)
|
||||
assert allowed.status_code == 200
|
||||
assert allowed.content.startswith(b"%PDF-token-legacy")
|
||||
@@ -294,6 +294,16 @@ def test_grantee_reads_linked_originals_only_through_the_owner(client, db, monke
|
||||
assert opened.headers["referrer-policy"] == "no-referrer"
|
||||
assert calls == [{"token": OWNER_TOKEN, "path": "/api/documents/15/preview/"}]
|
||||
assert GRANTEE_TOKEN not in json.dumps(calls)
|
||||
own_connectors = {item["id"] for item in ben.request("GET", "/api/v1/connectors")["items"]}
|
||||
assert paper not in own_connectors
|
||||
assert mailbox not in own_connectors
|
||||
ben.request("POST", f"/api/v1/connectors/{paper}/probe", status=404)
|
||||
ben.request(
|
||||
"GET",
|
||||
"/api/v1/sources/paperless/documents",
|
||||
params={"q": "Geheim", "connector_id": paper},
|
||||
status=404,
|
||||
)
|
||||
search = ben.request("GET", "/api/v1/sources/paperless/documents", params={"q": "Geheim"})
|
||||
assert "Geheimes Archivdokument" not in json.dumps(search)
|
||||
assert all(item["token"] != OWNER_TOKEN for item in calls if "query" in item)
|
||||
|
||||
Reference in new issue
Block a user