226 lines
8.4 KiB
Python
226 lines
8.4 KiB
Python
"""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)
|