"""Shared fixtures: a fake Radicale data tree on disk + pre-imported server module.""" import json import os import sys import textwrap from pathlib import Path import pytest REPO_ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(REPO_ROOT / "src")) def _write(path: Path, content: str) -> Path: path.parent.mkdir(parents=True, exist_ok=True) path.write_text(content, encoding="utf-8") return path def _cal_props(displayname: str = "", description: str = "") -> str: props = {"tag": "VCALENDAR"} if displayname: props["D:displayname"] = displayname if description: props["C:calendar-description"] = description return json.dumps(props) ICS_HEADER = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//mcp-ics-test//EN\n" ICS_FOOTER = "END:VCALENDAR\n" def _ics(body: str) -> str: return ICS_HEADER + body + ICS_FOOTER @pytest.fixture(scope="module") def radicale_tree(tmp_path_factory) -> Path: """A minimal Radicale collection-root layout covering edge cases.""" root = tmp_path_factory.mktemp("radicale") cr = root / "collections" / "collection-root" # user 'manuel', regular calendar cal = cr / "manuel" / "perso" _write(cal / ".Radicale.props", _cal_props("Perso manuel", "Agenda perso")) _write( cal / "abc.ics", ICS_HEADER + ( "BEGIN:VEVENT\n" "UID:evt-1@test\n" "DTSTAMP:20260101T000000Z\n" "DTSTART:20260315T090000\n" "DTEND:20260315T100000\n" "SUMMARY:Réunion équipe\n" "LOCATION:Bureau\n" "DESCRIPTION:Point hebdo\n" "END:VEVENT\n" ) + ICS_FOOTER, ) # recurring weekly event (10 occurrences) _write( cal / "recurring.ics", ICS_HEADER + ( "BEGIN:VEVENT\n" "UID:evt-rrule@test\n" "DTSTAMP:20260101T000000Z\n" "DTSTART:20260302T070000\n" "DTEND:20260302T080000\n" "RRULE:FREQ=WEEKLY;COUNT=10\n" "SUMMARY:Présence matinale\n" "END:VEVENT\n" ) + ICS_FOOTER, ) # birthday (filtered by default via BIRTHDAY_PATTERN) + excluded summary _write( cal / "special.ics", ICS_HEADER + ( "BEGIN:VEVENT\n" "UID:birth-1@test\n" "DTSTAMP:20260101T000000Z\n" "DTSTART;VALUE=DATE:20260410\n" "SUMMARY:Anniversaire Marc\n" "END:VEVENT\n" "BEGIN:VEVENT\n" "UID:trash-1@test\n" "DTSTAMP:20260101T000000Z\n" "DTSTART:20260316T180000\n" "SUMMARY:Poubelle verte\n" "END:VEVENT\n" ) + ICS_FOOTER, ) # VTODO on a second calendar cal2 = cr / "manuel" / "taches" _write(cal2 / ".Radicale.props", _cal_props("Tâches")) _write( cal2 / "todo.ics", ICS_HEADER + ( "BEGIN:VTODO\n" "UID:todo-1@test\n" "DTSTAMP:20260101T000000Z\n" "DUE:20260320T170000\n" "STATUS:NEEDS-ACTION\n" "SUMMARY:Ranger le garage\n" "END:VTODO\n" ) + ICS_FOOTER, ) # user 'robert' with a family calendar cal3 = cr / "robert" / "famille" _write(cal3 / ".Radicale.props", _cal_props("Famille")) _write(cal3 / "x.ics", ICS_HEADER + ICS_FOOTER) # empty calendar # VADDRESSBOOK (contacts) must be ignored _write(cr / "robert" / "contacts" / ".Radicale.props", '{"tag": "VADDRESSBOOK"}') # hidden dirs ignored (cr / ".stfolder").mkdir(parents=True) _write(cr / ".stfolder" / "x" / ".Radicale.props", '{"tag": "VCALENDAR"}') (cr / "manuel" / ".trash").mkdir(parents=True) _write(cr / "manuel" / ".trash" / "y.ics", ICS_HEADER + ICS_FOOTER) # corrupt props must be skipped with a warning _write(cr / "manuel" / "broken" / ".Radicale.props", "{not json") _write(cr / "manuel" / "broken" / "z.ics", ICS_HEADER + ICS_FOOTER) return root @pytest.fixture(scope="module") def server(radicale_tree): """Import the server module once per module with the fixture tree as data path.""" os.environ["CALDAV_DATA_PATH"] = str(radicale_tree) os.environ["EXCLUDE_PATTERN"] = "^Poubelle" os.environ["BIRTHDAY_PATTERN"] = "^Anniversaire|^Anniv" import server # noqa: E402 return server @pytest.fixture() def server_mod(server, monkeypatch, radicale_tree): """Reset mutable module state around each test.""" monkeypatch.setattr(server, "CALDAV_DATA_PATH", str(radicale_tree)) return server