# SPDX-License-Identifier: AGPL-3.0-or-later """Phase 8c ohne Datenbank: PDF-Ausgaben, ODS, Vermerke, Ergebnis-Cookie.""" from __future__ import annotations import io import re import zipfile from decimal import Decimal from pathlib import Path from types import SimpleNamespace from xml.etree import ElementTree import pytest LOGO = Path(__file__).parents[1] / "surveybox" / "web" / "static" / "img" / "logo-128.png" def test_pdf_lists() -> None: pytest.importorskip("reportlab") from surveybox.forms import pdf_export as pdf sections = [pdf.Section("Schicht", [ pdf.Entry("Früh", 3, ["Anna ", sections, [("Stand", "25.11.2026")], LOGO.read_bytes(), pdf.Options(signature=True, page_per_entry=True)) assert data.startswith(b"%PDF") pages = len(re.findall(rb"/Type /Page\b(?!s)", data)) assert pages >= 4 # jeder Eintrag beginnt auf einer neuen Seite, "Spät" braucht zwei plain = pdf.participant_lists("Fest", [pdf.Section("", [pdf.Entry("Alle", None, ["A"])])], [], None, pdf.Options()) assert len(re.findall(rb"/Type /Page\b(?!s)", plain)) == 1 def test_printable_falls_back_to_base_letters() -> None: pytest.importorskip("reportlab") from surveybox.forms.pdf_export import printable assert printable("Jiří Őz\u0000\nŞahin") == "Jirí Oz Şahin" # í kennt die Schrift assert printable("Anna") == "Anna" def test_changed_labels_and_clean() -> None: pytest.importorskip("sqlalchemy") from surveybox.forms import notes q1, q2, q3 = (SimpleNamespace(id=i, label=f"Frage {i}") for i in (1, 2, 3)) before = [(q1, "Ja", []), (q2, "", [{"id": "a"}]), (q3, "x", [])] after = [(q1, "Ja", []), (q2, "", [{"id": "b"}]), (q3, "y", [])] assert notes.changed_labels(before, after) == ["Frage 2", "Frage 3"] assert notes.changed_labels(before, before) == [] assert notes.clean(" Zeile 1\r\nZeile 2\x07 ") == "Zeile 1\nZeile 2" def test_results_cookie_and_visibility() -> None: pytest.importorskip("sqlalchemy") from surveybox.forms import public_results form = SimpleNamespace(public_id="p1", share_token="tok" * 8, results_visibility="never") value = public_results.cookie_value("geheim", form) # type: ignore[arg-type] assert public_results.cookie_valid("geheim", form, value) # type: ignore[arg-type] assert not public_results.cookie_valid("anders", form, value) # type: ignore[arg-type] assert not public_results.cookie_valid("geheim", form, None) # type: ignore[arg-type] assert not public_results.visible(form, True) # type: ignore[arg-type] form.results_visibility = "after_submit" assert public_results.visible(form, True) # type: ignore[arg-type] assert not public_results.visible(form, False) # type: ignore[arg-type] def test_pdf_answers_table_fits_page_width() -> None: pytest.importorskip("reportlab") from surveybox.forms import pdf_export as pdf headers = ["Nr.", "Abgegeben", "Teilnehmer", "Was bringst du mit?", "E-Mail-Adresse"] rows = [[1, "25.11.2026 11:34", "Gast", "Muffin ", None], [2, "25.11.2026 11:36", "Gast", "Langer Text\nmit Umbruch " * 8, "a@b.de"]] widths = pdf.column_widths(headers, rows, 770.0, 9.0) assert widths is not None and abs(sum(widths) - 770.0) < 0.01 data = pdf.answers_table("Muster", headers, rows, [("Antworten", "2")], LOGO.read_bytes()) assert data.startswith(b"%PDF") and b"/MediaBox [ 0 0 841.8898 595.2756 ]" in data # Sehr viele Spalten: passt trotzdem auf die Seitenbreite (kleinere Schrift) wide = [f"Frage {i} mit langer Beschriftung" for i in range(30)] assert pdf.answers_table("Muster", wide, [["x" * 40] * 30], [], None).startswith(b"%PDF") def test_ods_document() -> None: from surveybox.forms import ods sheet = ods.Sheet("Antworten", [[], ["Titel"], ["Stand: heute"], []], ["Nr.", "Name", None], [[1, "=1+1", Decimal("2.5")], [2, "a\nb <&>", None]], widths=[6, 20, 10], title_rows={1}, image=ods.Image(LOGO.read_bytes(), 1.27, 1.27), page=ods.PageSetup(footer=("Seite ", " von "))) data = ods.document(sheet, "Fest") with zipfile.ZipFile(io.BytesIO(data)) as z: first = z.infolist()[0] assert first.filename == "mimetype" and first.compress_type == zipfile.ZIP_STORED assert z.read("mimetype") == b"application/vnd.oasis.opendocument.spreadsheet" for name in ("content.xml", "styles.xml", "meta.xml", "META-INF/manifest.xml"): ElementTree.fromstring(z.read(name)) # noqa: S314 (selbst erzeugt) content = z.read("content.xml").decode() styles = z.read("styles.xml").decode() assert "Pictures/logo.png" in z.namelist() assert "table:formula" not in content and "=1+1" in content assert "ab <&>" in content assert 'office:value="2.5"' in content and "" in content assert 'table:target-range-address="Antworten.A5:Antworten.C7"' in content assert 'style:print-orientation="landscape"' in styles and 'style:scale-to-X="1"' in styles assert "" in styles and "" in styles assert 'fo:margin-top="2.0cm"' in styles