"""Markdown-Export (→ KONZEPT.md 18).""" import io import subprocess import zipfile import pytest from test_app import ADMIN, tenant_package, env # noqa: F401 (Fixture) from hugin_app import export def test_export_content(env, tmp_path): env.example("pub-munin-nutzen/1.4.0") env.example("pub-munin-nutzen/1.5.0") tenant_package(env) with env.hugin.database.sessions() as s: d = export.files(s) assert "public/pub-munin-nutzen/1.4.0/referenz/beispiele.md" in d assert "public/pub-munin-nutzen/1.5.0/SKILL.md" in d assert "shop/shop-intern/1.0.0/RULES.md" in d assert b"version: 1.5.0" in d["public/pub-munin-nutzen/1.5.0/hugin.yaml"] package_md = d["public/pub-munin-nutzen/PACKAGE.md"].decode() assert "| [1.5.0](1.5.0/) | approved | stable | text |" in package_md and "pub-pruefer" in package_md index = d["INDEX.md"].decode() assert index.index("## Öffentlich") < index.index("## shop") def test_export_folder_and_git(env, tmp_path): env.example("pub-hausregeln/1.0.0") target = tmp_path / "export" target.mkdir() subprocess.run(["git", "init", "-q", str(target)], check=True) subprocess.run(["git", "-C", str(target), "config", "user.email", "test@hugin"], check=True) subprocess.run(["git", "-C", str(target), "config", "user.name", "Test"], check=True) with env.hugin.database.sessions() as s: first = export.write(s, target, commit=True) second = export.write(s, target, commit=True) assert first["written"] > 0 and second == {"written": 0, "unchanged": first["written"], "removed": 0} log = subprocess.run(["git", "-C", str(target), "log", "--oneline"], capture_output=True, text=True).stdout assert len(log.strip().splitlines()) == 1 # zweiter Lauf ohne Änderung: kein Commit (target / "public" / "alt.md").write_text("deprecated") with env.hugin.database.sessions() as s: assert export.write(s, target)["removed"] == 1 def test_export_protects_foreign_folder(env, tmp_path): (tmp_path / "wichtig.txt").write_text("nicht löschen") with env.hugin.database.sessions() as s, pytest.raises(ValueError): export.write(s, tmp_path) assert (tmp_path / "wichtig.txt").exists() def test_export_as_zip(env): env.example("pub-hausregeln/1.0.0") assert env.client.get("/api/v1/admin/export", headers=env.h("shop-admin")).status_code == 403 response = env.client.get("/api/v1/admin/export", headers=ADMIN) with zipfile.ZipFile(io.BytesIO(response.content)) as z: assert "hugin-export/public/pub-hausregeln/1.0.0/RULES.md" in z.namelist()