Die Raumfreigabe allein gab die Zugänge des Owners nicht weiter. Ein Leser im Raum sah die Akte, aber kein Original. Neu ist room_source_access: Wer im Raum teilen darf, schaltet für ein Mitglied „Originale über meine Zugänge“ ein. Das gilt für alle eigenen, mit dem Raum geteilten Einträge. Ausnahmen, Ende der Raumfreigabe und Verlassen des Raums schließen den Abruf. Beschluss R-01 ist entsprechend ergänzt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
179 lines
7.2 KiB
Python
179 lines
7.2 KiB
Python
"""Room members read linked originals through the owner's connector only when the owner lends it to them."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
import app.sources as sources
|
|
from app.auth.accounts import create_account
|
|
from tests.support import ORIGIN, PASSWORD, Actor
|
|
|
|
OWNER_TOKEN = "raum-owner-paperless-token"
|
|
UNAVAILABLE = "Das Original ist über die Freigabe nicht abrufbar."
|
|
|
|
|
|
def _second_client():
|
|
from app.main import app
|
|
|
|
return TestClient(app)
|
|
|
|
|
|
def _actor(client, db, key: str, email: str, *, main: bool = False) -> Actor:
|
|
actor = Actor(client if main else _second_client(), email, key.title())
|
|
create_account(db, actor.email, actor.name, PASSWORD)
|
|
db.commit()
|
|
actor.login()
|
|
return actor
|
|
|
|
|
|
def _id(db, email: str) -> str:
|
|
row = db.execute("SELECT id FROM auth_accounts WHERE email = %s", (email,)).fetchone()
|
|
db.rollback()
|
|
return str(row["id"])
|
|
|
|
|
|
def _raw(actor: Actor, path: str):
|
|
headers = {"Origin": ORIGIN, "X-CSRF-Token": actor.csrf}
|
|
if actor.scope is not None:
|
|
headers["X-Scope-Kind"] = actor.scope["kind"]
|
|
headers["X-Scope-Id"] = actor.scope["id"]
|
|
return actor.client.get(path, headers=headers)
|
|
|
|
|
|
def _lend(actor: Actor, room: str, subject: str, enabled: bool, status: int = 200):
|
|
return actor.request(
|
|
"PUT",
|
|
f"/api/v1/rooms/{room}/source-access/{subject}",
|
|
json={"enabled": enabled},
|
|
status=status,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def world(client, db, monkeypatch):
|
|
"""Ada owns the room and a case with a Paperless document. Ben is a member, Cem a reader, Dan outside."""
|
|
calls = []
|
|
|
|
def fake_file(base, token, path, max_bytes=40 * 1024 * 1024):
|
|
calls.append({"token": token, "path": path})
|
|
if path.endswith("/preview/"):
|
|
return (b"%PDF-1.4\nraum\n", "application/pdf", "vertrag.pdf")
|
|
return None
|
|
|
|
monkeypatch.setattr(sources, "_paperless_file", fake_file)
|
|
monkeypatch.setattr(sources, "_paperless_metadata", lambda *args, **kwargs: None)
|
|
|
|
actors = {
|
|
"ada": _actor(client, db, "ada", "ada-rq@example.test", main=True),
|
|
"ben": _actor(client, db, "ben", "ben-rq@example.test"),
|
|
"cem": _actor(client, db, "cem", "cem-rq@example.test"),
|
|
"dan": _actor(client, db, "dan", "dan-rq@example.test"),
|
|
}
|
|
ada = actors["ada"]
|
|
room = ada.request("POST", "/api/v1/rooms", json={"name": "Mandat"}, status=201)["id"]
|
|
ada.request("POST", f"/api/v1/rooms/{room}/members", json={"email": actors["ben"].email, "role": "member"})
|
|
ada.request("POST", f"/api/v1/rooms/{room}/members", json={"email": actors["cem"].email, "role": "reader"})
|
|
ada.use_room(room)
|
|
actors["cem"].use_room(room)
|
|
actors["ben"].use_room(room)
|
|
paper = ada.request(
|
|
"POST",
|
|
"/api/v1/connectors",
|
|
json={"kind": "paperless", "name": "Ada Archiv", "base_url": "https://paper.example.test", "token": OWNER_TOKEN},
|
|
status=201,
|
|
)["id"]
|
|
case = ada.request("POST", "/api/v1/cases", json={"title": "Mandatsakte"}, status=201)
|
|
document = ada.request(
|
|
"POST",
|
|
"/api/v1/external-refs",
|
|
json={
|
|
"title": "Vertrag",
|
|
"source": "paperless",
|
|
"external_id": "15",
|
|
"context": {"kind": "case", "id": case["id"]},
|
|
"detail": {"connector_id": paper},
|
|
},
|
|
status=201,
|
|
)
|
|
actors.update(
|
|
room=room,
|
|
case=case["id"],
|
|
path=f"/api/v1/external-refs/{document['id']}/content?context_kind=case&context_id={case['id']}",
|
|
calls=calls,
|
|
ids={key: _id(db, actors[key].email) for key in ("ada", "ben", "cem", "dan")},
|
|
)
|
|
return actors
|
|
|
|
|
|
def test_a_room_reader_reads_originals_only_after_the_owner_lends_them(world, db):
|
|
ada, cem, room, ids, calls = world["ada"], world["cem"], world["room"], world["ids"], world["calls"]
|
|
assert cem.request("GET", f"/api/v1/cases/{world['case']}")["rights"]["read"] is True
|
|
|
|
refused = _raw(cem, world["path"])
|
|
assert refused.status_code == 422
|
|
assert refused.json()["detail"] == UNAVAILABLE
|
|
assert calls == []
|
|
|
|
_lend(ada, room, ids["cem"], True)
|
|
members = ada.request("GET", f"/api/v1/rooms/{room}/members")["items"]
|
|
flags = {item["subject_id"]: item["my_sources"] for item in members}
|
|
assert flags[ids["cem"]] is True
|
|
assert flags[ids["ben"]] is False
|
|
share = ada.request("GET", f"/api/v1/cases/{world['case']}/room-share")
|
|
shown = {item["subject_id"]: item["sources"] for item in share["shares"][0]["members"]}
|
|
assert shown[ids["cem"]] is True
|
|
|
|
opened = _raw(cem, world["path"])
|
|
assert opened.status_code == 200, opened.text
|
|
assert opened.content.startswith(b"%PDF")
|
|
assert calls == [{"token": OWNER_TOKEN, "path": "/api/documents/15/preview/"}]
|
|
assert OWNER_TOKEN not in json.dumps(cem.request("GET", f"/api/v1/cases/{world['case']}"))
|
|
events = db.execute(
|
|
"SELECT result FROM domain_events WHERE action = 'source_read_mediated' AND actor_subject_id = %s",
|
|
(ids["cem"],),
|
|
).fetchall()
|
|
db.rollback()
|
|
assert [row["result"] for row in events] == ["ok"]
|
|
|
|
_lend(ada, room, ids["cem"], False)
|
|
assert _raw(cem, world["path"]).status_code == 422
|
|
|
|
|
|
def test_only_the_lending_person_decides_and_only_for_members(world):
|
|
ada, ben, cem, dan, room, ids = world["ada"], world["ben"], world["cem"], world["dan"], world["room"], world["ids"]
|
|
# Ben lends his own connectors to Cem. That does not open Ada's originals.
|
|
_lend(ben, room, ids["cem"], True)
|
|
assert _raw(cem, world["path"]).status_code == 422
|
|
# A reader cannot share into the room and so cannot lend.
|
|
_lend(cem, room, ids["ben"], True, status=403)
|
|
# Outsiders and non-members are not addressable.
|
|
_lend(dan, room, ids["cem"], True, status=404)
|
|
_lend(ada, room, ids["dan"], True, status=404)
|
|
_lend(ada, room, ids["ada"], True, status=422)
|
|
|
|
|
|
def test_exclusion_leaving_the_room_and_ending_the_share_close_the_access(world):
|
|
ada, cem, room, ids = world["ada"], world["cem"], world["room"], world["ids"]
|
|
_lend(ada, room, ids["cem"], True)
|
|
assert _raw(cem, world["path"]).status_code == 200
|
|
|
|
ada.request("PUT", f"/api/v1/cases/{world['case']}/room-share/people/{ids['cem']}", json={"excluded": True})
|
|
assert _raw(cem, world["path"]).status_code == 404
|
|
ada.request("PUT", f"/api/v1/cases/{world['case']}/room-share/people/{ids['cem']}", json={"excluded": False})
|
|
assert _raw(cem, world["path"]).status_code == 200
|
|
|
|
ada.request("DELETE", f"/api/v1/cases/{world['case']}/room-share/rooms/{room}", status=204)
|
|
assert _raw(cem, world["path"]).status_code == 404
|
|
ada.request("PUT", f"/api/v1/cases/{world['case']}/room-share/rooms/{room}", json={"access": "read"})
|
|
assert _raw(cem, world["path"]).status_code == 200
|
|
|
|
ada.request("DELETE", f"/api/v1/rooms/{room}/members/{ids['cem']}", status=204)
|
|
ada.request("POST", f"/api/v1/rooms/{room}/members", json={"email": cem.email, "role": "reader"})
|
|
cem.use_room(room)
|
|
assert _raw(cem, world["path"]).status_code == 422
|
|
flags = {item["subject_id"]: item["my_sources"] for item in ada.request("GET", f"/api/v1/rooms/{room}/members")["items"]}
|
|
assert flags[ids["cem"]] is False
|