From bfa5e5e58e9f431600fc452886031be3568e6bff Mon Sep 17 00:00:00 2001 From: Tobias Brox Date: Mon, 9 Nov 2026 13:07:47 +0100 Subject: [PATCH 1/5] test: sweep test calendars off external servers The test runs kept accumulating new calendars on the server. That can be an annoyance and can also cause tests to fail; some providers allow only four or five on a free account. The async fixtures now share the sync suite's cal_ids and display names. Where calendars can be recreated, every pythoncaldav-* calendar on an external server is deleted for good when a session first uses the server and again when it ends. Where a plain DELETE leaves the id taken (Nextcloud's trashbin), the tests attempt to skip the trashbin too (that's a header-field that may be Nextcloud-specific). Prompts: > for ecloud there are now 11 calendars coming from the caldav tests. That's > too many. Every test should clean up after itself, and it's needed with some > cleanup-magic also at the start of a test to remove old obsoleted test > calendars. Some calendar providers only allow for 4-5 calendars on the > free account. > The tests should not create calendars with UUIDs anymore (if > calendaring-client is doing it, it needs to be fixed there). The ecloud > server is only used for testing, so it's safe to delete calendars there. > I thought "sweep" meant "delete content of calendar, but not the calendar itself"? > I think it's needed to do some testing here. We have the > empty-thrashbin-script (sic), if running that one for each test (but I don't > want it into the public repo - this is a hack for testing of ecloud only), > would it suffice to avoid the rate limits? If not, we need to reuse > calendars. If the test run needs to create 6-8 calendars but still needs > every test to be run serially, then something is wrong with the tests. > (on bare-UUID leftovers not matching the prefix) The sweep in the test suite > should not touch those. That said, it's OK to delete such calendars for my > ecloud account. If such calendars reappear after deleting them, it's needed > to do more research. > (same finding, after the research found the server-tester creating them) [add a calendar task for the fix in caldav-server-tester] > (on deleting past the trashbin wherever the tests delete a calendar) ,,, this > is NextCloud-specific, but harmless for other servers, and only in tests ... > so let's do it > Take a step back. The [edited] commit message below reads better. It's not an > ecloud problem, I was clearing away A LOT of junk calendars from my zimbra > the other day. Assisted-By: Claude Opus 5.5 Reviewed-by: Tobias Brox --- docs/design/TODO.md | 4 +- tests/README.md | 12 ++ tests/conftest.py | 7 + tests/fixture_helpers.py | 177 +++++++++++++++++++++- tests/test_async_integration.py | 47 ++++-- tests/test_caldav.py | 26 +++- tests/test_fixture_helpers.py | 260 ++++++++++++++++++++++++++++++++ 7 files changed, 508 insertions(+), 25 deletions(-) diff --git a/docs/design/TODO.md b/docs/design/TODO.md index ba5b508c..64fe22ee 100644 --- a/docs/design/TODO.md +++ b/docs/design/TODO.md @@ -70,8 +70,8 @@ On Nextcloud a deleted calendar moves to the trashbin, keeps its cal_id occupied ## A recognisable cal_id for every test calendar The tests and the caldav-server-tester leave calendars behind, and on servers that cap the number of calendars per principal (ecloud, see above) that eventually blocks all creation. On 2026-10-23 ecloud held 15 empty calendars with bare-uuid cal_ids and the uuid as display name; which code made them was not found. Every calendar the tests or the tester create should carry a cal_id prefix, like the caldav-server-tester's `csc_` UIDs, so a cleanup step can delete leftovers safely - with the purge from the section above where the server has a trashbin. -* caldav tests: there is no common cal_id prefix on every calendar, and no sweep. Several tests create calendars by name only (`test_caldav.py` "CalConfigA"/"CalConfigB", "XandikosMetaCal"/"RadicaleMetaCal", `test_get_calendars_sections.py`, `test_examples.py`), so `make_calendar()` gives them a bare uuid. Give all of them a `pythoncaldav-test` prefixed cal_id and add a session-level sweep. -* caldav-server-tester: has `PROBE_CALENDAR_PREFIXES` and a sweep matching cal_id or display name, but `CheckDuplicateUID` creates its second calendar by name only, so its cal_id is a bare uuid that the sweep cannot recognise when the server drops the display name. Pass a prefixed cal_id. Its sweep also only moves calendars to the Nextcloud trashbin. +* caldav tests: a session-level sweep now deletes every live `pythoncaldav-` calendar on external servers (`tests/fixture_helpers.py`), but not every calendar carries the prefix. Several tests create calendars by name only (`test_caldav.py` "CalConfigA"/"CalConfigB", "XandikosMetaCal"/"RadicaleMetaCal", `test_get_calendars_sections.py`, `test_examples.py`), so `make_calendar()` gives them a bare uuid. Give all of them a `pythoncaldav-test` prefixed cal_id, so the sweep covers them. +* caldav-server-tester: has `PROBE_CALENDAR_PREFIXES` and a sweep matching cal_id or display name, but `CheckDuplicateUID` and the MOVE check both create their second calendar by name only (`make_calendar(name=...)`), so its cal_id is a bare uuid that the sweep cannot recognise when the server drops the display name. On 2026-11-12 every compatibility run against ecloud left two such calendars behind, their display name reading back as the uuid. Pass a prefixed cal_id and delete by it. Its sweep also only moves calendars to the Nextcloud trashbin. ## An installed caldav.testing looks for the checkout in site-packages diff --git a/tests/README.md b/tests/README.md index d641fafc..9320e2d6 100644 --- a/tests/README.md +++ b/tests/README.md @@ -210,6 +210,18 @@ test-servers: - no-sync-token ``` +Most test calendars have ids starting with `pythoncaldav-`. On an external +server every such calendar is deleted when a test session first uses the +server and again when the session ends, bypassing the Nextcloud trashbin. Do +not keep calendars of your own under that prefix. Nothing is deleted when the +server cannot create or cannot delete calendars, when `test-calendar` in the +features names a calendar for the tests, or when its `cleanup-regime` is +`none`. Calendars already in the Nextcloud trashbin are not listed, so the +sweep does not reach them; the tests themselves delete past the trashbin +where a plain DELETE would leave the id taken. The scheduling tests' calendars (`schedulingnosetestcalendar*`, +`asyncschedtest*`) are kept apart from this on purpose: they are reused from +run to run, with their contents wiped. + ## Test Structure ### Unit Tests (no server required) diff --git a/tests/conftest.py b/tests/conftest.py index abfe5890..06e94435 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,6 +15,13 @@ def pytest_configure(config): os.environ["PYTHON_CALDAV_DEBUGMODE"] = "DEBUG_PDB" +def pytest_sessionfinish(session, exitstatus): + """Remove the test calendars from the external servers this session used.""" + from .fixture_helpers import sweep_used_servers + + sweep_used_servers() + + @pytest.fixture def process_tz(request, monkeypatch): """Run the test with the Python process in the time zone given by diff --git a/tests/fixture_helpers.py b/tests/fixture_helpers.py index d4ebcf37..fb2daf56 100644 --- a/tests/fixture_helpers.py +++ b/tests/fixture_helpers.py @@ -8,9 +8,13 @@ import asyncio import inspect import logging +import time from typing import Any from urllib.parse import unquote, urlparse +from caldav.compatibility_hints import write_delay +from caldav.lib.error import DAVError, NotFoundError + async def _maybe_await(result: Any) -> Any: """Await if result is awaitable, otherwise return as-is.""" @@ -430,14 +434,17 @@ async def cleanup_calendar_objects(calendar: Any) -> None: pass -async def adelete_calendar_if_present(principal: Any, cal_id: str) -> None: +async def adelete_calendar_if_present(principal: Any, cal_id: str, client: Any = None) -> None: """Best-effort removal of a leftover test calendar from a previous run. A test that recreates a calendar with a fixed ``cal_id`` must first clear any leftover, or the recreate MKCALENDAR 405s ("resource already exists"). + Given the ``client``, the leftover goes as adelete_test_calendar() deletes + it: past the trashbin where a plain DELETE would keep the id taken. Only ``NotFoundError`` (the calendar isn't there) is swallowed - everything - else propagates. A previous incarnation wrapped this in a bare + else propagates, except on that past-the-trashbin path, where a refused + DELETE is logged rather than raised. A previous incarnation wrapped this in a bare ``except Exception: pass``, which silently hid a real bug (async ``principal.calendar()`` raising ``TypeError``), so the cleanup never ran and calendars leaked. Keep the catch narrow so that can't recur. @@ -447,8 +454,172 @@ async def adelete_calendar_if_present(principal: Any, cal_id: str) -> None: calendar = await _maybe_await(principal.calendar(cal_id=cal_id)) await cleanup_calendar_objects(calendar) try: - await _maybe_await(calendar.delete()) + if client is None: + await _maybe_await(calendar.delete()) + else: + await adelete_test_calendar(client, calendar) except error.NotFoundError: # Already gone, which is exactly what this function wants. Nothing # wider is caught on purpose - see the docstring. pass + + +## Most calendars the functional tests create have an id starting with this. +TEST_CALENDAR_PREFIX = "pythoncaldav-" + +## Nextcloud moves a deleted calendar to a trashbin, where it still counts +## towards the account's calendar limit and still holds the id; this header +## deletes it for good. Other servers ignore it. +NO_TRASHBIN_HEADERS = {"X-NC-CalDAV-No-Trashbin": "1"} + +## Servers already swept in this pytest session, by registry name. +_swept_servers: set[str] = set() + + +def sweep_test_calendars(client: Any, principal: Any) -> list[str]: + """Delete every test calendar (id starting with ``pythoncaldav-``) for good. + + The fixtures leave calendars behind on purpose where deleting them would + not free the id (wipe-calendar regime), and an interrupted run leaves them + behind anywhere. On accounts allowing only a handful of calendars that + adds up, so the session sweeps them away at its start and end. + + Nothing is touched where the calendars could not be recreated: without + calendar creation or deletion, with a calendar configured for the tests, or + with cleanup switched off. Returns the ids deleted. + """ + if not (_supports(client, "create-calendar") and _supports(client, "delete-calendar")): + return [] + test_cal_info = client.features.is_supported("test-calendar", return_type=dict) + if test_cal_info.get("cleanup-regime") == "none" or any( + k in test_cal_info for k in ("name", "cal_id", "cal_url") + ): + return [] + deleted = [ + cal_id + for cal in principal.get_calendars() + if (cal_id := _cal_id(cal)).startswith(TEST_CALENDAR_PREFIX) + and delete_calendar_for_good(client, cal) + ] + ## A server deleting calendars asynchronously (Infomaniak) answers before + ## the calendar is gone; a fixture re-creating the id right after would + ## meet the old one, as Calendar.delete() knows. + delay = write_delay(client.features, "synchronous-write.delete-calendar") + deadline = time.monotonic() + delay + while deleted and delay: + if not any(_cal_id(cal) in deleted for cal in principal.get_calendars()): + break + if time.monotonic() >= deadline: + logging.warning("sweep: %s still listed after %ss", deleted, delay) + break + time.sleep(0.5) + return deleted + + +def _cal_id(cal: Any) -> str: + return unquote(str(cal.url).rstrip("/").rsplit("/", 1)[-1]) + + +def delete_calendar_for_good(client: Any, cal: Any) -> bool: + """DELETE a calendar past the Nextcloud trashbin; False if refused. + + A refusal is logged, not raised: the callers clean up, and one calendar + the server will not let go of must not stop them. + """ + try: + response = client.delete(str(cal.url), headers=NO_TRASHBIN_HEADERS) + except DAVError as e: + return _deleted_for_good(cal, e) + return _deleted_for_good(cal, response) + + +async def adelete_calendar_for_good(client: Any, cal: Any) -> bool: + """Async twin of delete_calendar_for_good().""" + try: + response = await _maybe_await(client.delete(str(cal.url), headers=NO_TRASHBIN_HEADERS)) + except DAVError as e: + return _deleted_for_good(cal, e) + return _deleted_for_good(cal, response) + + +def delete_test_calendar(client: Any, cal: Any) -> None: + """Delete a calendar the tests made, freeing its id where that takes more. + + Where a plain DELETE leaves the id taken (``delete-calendar.free-namespace`` + unsupported: Nextcloud's trashbin), the calendar goes for good, so that it + neither counts towards the account's calendar limit nor blocks the next + MKCALENDAR of the same id. Elsewhere Calendar.delete() does it, with its + retries and its wait for an asynchronous deletion, or by wiping the + objects where the server refuses to delete calendars (Synology). + """ + if _plain_delete_will_do(client): + cal.delete() + else: + delete_calendar_for_good(client, cal) + + +async def adelete_test_calendar(client: Any, cal: Any) -> None: + """Async twin of delete_test_calendar().""" + if _plain_delete_will_do(client): + await _maybe_await(cal.delete()) + else: + await adelete_calendar_for_good(client, cal) + + +def _plain_delete_will_do(client: Any) -> bool: + return not _supports(client, "delete-calendar") or _supports( + client, "delete-calendar.free-namespace" + ) + + +def _deleted_for_good(cal: Any, outcome: Any) -> bool: + """Read a DELETE's response, or the error it raised; log a refusal.""" + if isinstance(outcome, NotFoundError): + return True + if isinstance(outcome, DAVError): + logging.warning("DELETE %s refused: %s", cal.url, outcome) + return False + if outcome.status in (200, 204, 404): + return True + logging.warning("DELETE %s answered %s", cal.url, outcome.status) + return False + + +def sweep_server(server_name: str) -> None: + """Sweep the test calendars off an external test server. + + Docker and embedded servers are thrown away after the run anyway. Best + effort: a sweep that fails must not fail a test. + """ + from caldav.testing.base import ExternalTestServer + from caldav.testing.registry import get_registry + + server = get_registry().get(server_name) + if not isinstance(server, ExternalTestServer): + return + try: + with server.get_sync_client() as client: + deleted = sweep_test_calendars(client, client.principal()) + except Exception as e: + logging.warning("sweep of %s failed: %s", server_name, e) + return + if deleted: + logging.info("sweep of %s deleted %s", server_name, deleted) + + +def sweep_server_once(server_name: str | None) -> None: + """Sweep a server the first time a test in this session uses it. + + Clears leftovers from interrupted runs; conftest sweeps the same servers + again when the session ends. + """ + if not server_name or server_name in _swept_servers: + return + _swept_servers.add(server_name) + sweep_server(server_name) + + +def sweep_used_servers() -> None: + """Sweep every server swept at its first use, at the end of the session.""" + for server_name in sorted(_swept_servers): + sweep_server(server_name) diff --git a/tests/test_async_integration.py b/tests/test_async_integration.py index e683120e..702b3ce2 100644 --- a/tests/test_async_integration.py +++ b/tests/test_async_integration.py @@ -197,8 +197,11 @@ def test_server(cls) -> TestServer: fixtures written as plain instance methods (each test gets a fresh instance, so per-instance state set here would not be visible anyway). """ + from .fixture_helpers import sweep_server_once + server = cls.server server.start() + sweep_server_once(server.name) yield server # Stop the server to free the port for other test modules server.stop() @@ -256,10 +259,16 @@ async def _afixture_calendar( *, cal_id: str, what: str, + name: str | None = None, supported_calendar_component_set: list[str] | None = None, ) -> Any: """Shared body of the calendar fixtures below. + The cal_ids and display names are the ones the sync suite's + ``_fixCalendar`` uses, so both suites share one set of calendars + (fewer on accounts with a calendar limit) and a calendar one suite + left behind looks like the one the other expects. + An async generator (not a fixture itself) so each fixture can keep its own name, docstring and cal_id while the create/wipe/teardown logic lives in exactly one place - see fixture_helpers.afix_calendar. @@ -281,7 +290,7 @@ async def _afixture_calendar( async_client, principal, cal_id=cal_id, - calendar_name=cal_id, + calendar_name=name or cal_id, supported_calendar_component_set=supported_calendar_component_set, ) if calendar is None: @@ -299,7 +308,7 @@ async def async_calendar(self, async_client: Any) -> Any: handles the server-capability bookkeeping (see its docstring). """ async for calendar in self._afixture_calendar( - async_client, cal_id="pythoncaldav-async-test", what="a calendar for testing" + async_client, cal_id="pythoncaldav-test", what="a calendar for testing", name="Yep" ): yield calendar @@ -325,7 +334,7 @@ async def async_task_list(self, async_client: Any) -> Any: component_set = None if self.is_supported("save-load.todo.mixed-calendar") else ["VTODO"] async for calendar in self._afixture_calendar( async_client, - cal_id="pythoncaldav-async-test-tasks", + cal_id="pythoncaldav-test-tasks", what="a task list for testing", supported_calendar_component_set=component_set, ): @@ -336,8 +345,9 @@ async def async_calendar2(self, async_client: Any) -> Any: """Create or find a stable second test calendar for tests needing two calendars.""" async for calendar in self._afixture_calendar( async_client, - cal_id="pythoncaldav-async-test-2", + cal_id="pythoncaldav-test2", what="a second calendar for testing", + name="Yapp", ): yield calendar @@ -346,7 +356,7 @@ async def async_journal_list(self, async_client: Any) -> Any: """Create or find a stable VJOURNAL calendar, wiping it before and after use.""" async for calendar in self._afixture_calendar( async_client, - cal_id="pythoncaldav-async-journal", + cal_id="pythoncaldav-test-journals", what="a journal list for testing", supported_calendar_component_set=["VJOURNAL"], ): @@ -397,11 +407,15 @@ async def test_principal_make_calendar(self, async_client: Any) -> None: from caldav.aio import AsyncCalendarSet, AsyncPrincipal from caldav.lib.error import AuthorizationError, MkcalendarError, NotFoundError - from .fixture_helpers import adelete_calendar_if_present, cleanup_calendar_objects + from .fixture_helpers import ( + adelete_calendar_if_present, + adelete_test_calendar, + cleanup_calendar_objects, + ) ## A cal_id of its own: this test deletes the calendar it creates, and ## the async_calendar fixture hands the same collection to other tests. - ## Sharing "pythoncaldav-async-test" with the fixture meant the delete + ## Sharing the fixture's cal_id meant the delete ## below raced the fixture's next MKCALENDAR, and the following test got ## a calendar that was not there (403/404 on its first PUT against ## Robur). Only reachable since delete-calendar became supported there @@ -420,7 +434,7 @@ async def test_principal_make_calendar(self, async_client: Any) -> None: # don't accumulate calendars (some servers enforce a quota). try: principal = await AsyncPrincipal.create(async_client) - await adelete_calendar_if_present(principal, cal_id) + await adelete_calendar_if_present(principal, cal_id, client=async_client) calendar = await principal.make_calendar(name="Async Test", cal_id=cal_id) except (MkcalendarError, AuthorizationError): # Calendar exists and can't be (re)created (e.g. no delete support @@ -449,7 +463,7 @@ async def test_principal_make_calendar(self, async_client: Any) -> None: # Always clean up so calendars don't accumulate (quota safety). try: if self.is_supported("delete-calendar"): - await calendar.delete() + await adelete_test_calendar(async_client, calendar) else: # Can't delete the calendar, just wipe its objects await cleanup_calendar_objects(calendar) @@ -1924,8 +1938,8 @@ async def test_create_delete_calendar(self, async_client: Any) -> None: await adelete_calendar_if_present(principal, cal_id) else: ## Deleting doesn't free the id (Nextcloud's trashbin), so a fixed - ## id can be created only once; a fresh id per run instead, as in - ## testCreateDeleteCalendar + ## id can be created only once; a fresh id per run instead, purged + ## from the trashbin after the test, as in testCreateDeleteCalendar cal_id = f"pythoncaldav-async-createdelete-{uuid.uuid4().hex[:8]}" c = await principal.make_calendar(name="Yep", cal_id=cal_id) @@ -1935,6 +1949,11 @@ async def test_create_delete_calendar(self, async_client: Any) -> None: assert len(events) == 0 finally: await c.delete() + if not self.is_supported("delete-calendar.free-namespace"): + ## The trashbin entry sits at the same URL + from .fixture_helpers import adelete_calendar_for_good + + assert await adelete_calendar_for_good(async_client, c) @pytest.mark.asyncio async def test_calendar_by_full_url(self, async_calendar: Any, async_client: Any) -> None: @@ -2356,15 +2375,15 @@ async def test_utf8_event(self, async_client: Any) -> None: ## keep_name=True: the non-ASCII *display name* is half of what this test ## checks, so it is sent unconditionally - exactly as the sync - ## testUtf8Event does by passing name="Yølp" to _fixCalendar (which only + ## testUtf8Event does by passing name="Yølp UTF-8" to _fixCalendar (which only ## applies its name-dropping rule when the caller supplied no name). ## Servers that relocate a named calendar (Zimbra, OX) are fine: the ## library re-points to the canonical URL. c, created = await afix_calendar( async_client, principal, - cal_id="pythoncaldav-async-utf8-test", - calendar_name="Yølp", + cal_id="pythoncaldav-test-utf8", + calendar_name="Yølp UTF-8", keep_name=True, ) if c is None: diff --git a/tests/test_caldav.py b/tests/test_caldav.py index a977c6d9..15e44b9d 100644 --- a/tests/test_caldav.py +++ b/tests/test_caldav.py @@ -1449,6 +1449,9 @@ def setup_method(self): self.principal = self.caldav.principal() + from .fixture_helpers import sweep_server_once + + sweep_server_once(self.server_params.get("name")) self._cleanup("pre") logging.debug("##############################") @@ -1557,13 +1560,15 @@ def _cleanup(self, mode=None): self._teardownCalendar(cal_id=name) def _teardownCalendar(self, name=None, cal_id=None): + from .fixture_helpers import delete_test_calendar + try: cal = self.principal.calendar(name=name, cal_id=cal_id) - cal.delete() + delete_test_calendar(self.caldav, cal) except: pass try: - cal.delete() + delete_test_calendar(self.caldav, cal) except: pass @@ -2101,8 +2106,8 @@ def testCreateDeleteCalendar(self): ## Deleting doesn't free the id (Nextcloud's trashbin), so a fixed ## id can be created only once, and deleting the shared test ## calendar would take it away from the tests reusing it. A - ## fresh id per run instead, leaving one calendar per run in the - ## trashbin; its own name keeps it apart from the shared "Yep". + ## fresh id per run instead, purged from the trashbin after the + ## test; its own name keeps it apart from the shared "Yep". cal_id = f"pythoncaldav-test-{uuid.uuid4().hex[:8]}" name = "Yep-createdelete" c = self.principal.make_calendar(name=name, cal_id=cal_id) @@ -2114,6 +2119,11 @@ def testCreateDeleteCalendar(self): assert len(events) == 0 finally: c.delete() + if not self.is_supported("delete-calendar.free-namespace"): + ## The trashbin entry sits at the same URL + from .fixture_helpers import delete_calendar_for_good + + assert delete_calendar_for_good(self.caldav, c) if not self.is_supported("create-calendar.auto"): # Two separate pytest.raises blocks: with both probes in a single @@ -2287,8 +2297,10 @@ def testNamedCalendarUrlIsUsable(self): assert fetched.url == stored.url assert fetched.icalendar_component["uid"] == uid finally: + from .fixture_helpers import delete_test_calendar + try: - cal.delete() + delete_test_calendar(self.caldav, cal) except Exception: logging.warning(f"unable to delete calendar {cal.url} with name {name}") self._teardownCalendar(cal_id=cal_id) @@ -4209,8 +4221,10 @@ def testSetCalendarProperties(self): if self.cleanup_regime in ("light", "pre"): self._teardownCalendar(cal_id=self.testcal_id2) cc = self._fixCalendar(name="Yep", cal_id=self.testcal_id2) + from .fixture_helpers import delete_test_calendar + try: - cc.delete() + delete_test_calendar(self.caldav, cc) except error.DeleteError: if not self.is_supported("delete-calendar"): raise diff --git a/tests/test_fixture_helpers.py b/tests/test_fixture_helpers.py index 838bf2d7..a123e2d7 100644 --- a/tests/test_fixture_helpers.py +++ b/tests/test_fixture_helpers.py @@ -20,9 +20,15 @@ from .fixture_helpers import ( _get_or_create_impl, + adelete_calendar_if_present, + adelete_test_calendar, afix_calendar, arelease_calendar, component_set_unobtainable, + sweep_server, + sweep_server_once, + sweep_test_calendars, + sweep_used_servers, ) from .test_caldav import RepeatedFunctionalTestsBaseClass @@ -639,3 +645,257 @@ def test_unobtainable_component_sets_per_profile(profile: str, component: str) - client = FakeClient(resolve_features(profile)) unobtainable = bool(component_set_unobtainable(client, [component])) assert unobtainable is ((profile, component) in UNOBTAINABLE) + + +class SweepResponse: + def __init__(self, status: int) -> None: + self.status = status + + +class SweepClient(FakeClient): + """Sync client that records the DELETEs a sweep sends.""" + + def __init__(self, hints: dict[str, Any] | None = None, status: int = 204) -> None: + super().__init__(hints) + self.status = status + self.deletes: list[tuple[str, dict[str, str] | None]] = [] + + def delete(self, url: str, headers: dict[str, str] | None = None) -> SweepResponse: + self.deletes.append((url, headers)) + if self.status in (401, 403): + ## The real client raises on these rather than returning them. + raise error.AuthorizationError(url=url, reason=str(self.status)) + return SweepResponse(self.status) + + +class SweepPrincipal: + def __init__(self, *cal_ids: str, lingering: int = 0) -> None: + self.cal_ids = list(cal_ids) + ## Listings that still show a deleted calendar, as on a server + ## deleting asynchronously. + self.lingering = lingering + self.listings = 0 + + def get_calendars(self) -> list[FakeCalendar]: + self.listings += 1 + if self.listings > 1 and self.lingering: + self.lingering -= 1 + elif self.listings > 1: + self.cal_ids = [c for c in self.cal_ids if not c.startswith("pythoncaldav-")] + return [FakeCalendar(url=f"http://dav.example.com/cals/{c}/") for c in self.cal_ids] + + +def test_sweep_deletes_only_test_calendars_and_skips_the_trashbin() -> None: + """Leftover pythoncaldav-* calendars go, for good; the user's calendars stay. + + On Nextcloud a plain DELETE moves the calendar to a trashbin that still + counts towards the account's calendar limit, hence the header. + """ + client = SweepClient() + principal = SweepPrincipal( + "personal", "pythoncaldav-test", "pythoncaldav-test-tasks", "9b0c663d-b3b0", "testcal" + ) + + deleted = sweep_test_calendars(client, principal) + + assert deleted == ["pythoncaldav-test", "pythoncaldav-test-tasks"] + assert [url for url, _ in client.deletes] == [ + "http://dav.example.com/cals/pythoncaldav-test/", + "http://dav.example.com/cals/pythoncaldav-test-tasks/", + ] + assert all(h == {"X-NC-CalDAV-No-Trashbin": "1"} for _, h in client.deletes) + + +@pytest.mark.parametrize( + "hints", + [ + ## Nothing can be recreated: whatever is there was set up by hand. + {"create-calendar": {"support": "unsupported"}}, + {"delete-calendar": {"support": "unsupported"}}, + ## A calendar configured for the tests must survive the sweep. + {"test-calendar": {"cal_id": "pythoncaldav-test"}}, + {"test-calendar": {"cleanup-regime": "none"}}, + ], +) +def test_sweep_leaves_calendars_it_cannot_recreate(hints: dict[str, Any]) -> None: + client = SweepClient(hints) + + assert sweep_test_calendars(client, SweepPrincipal("pythoncaldav-test")) == [] + assert client.deletes == [] + + +def test_sweep_does_not_count_a_refused_delete() -> None: + client = SweepClient(status=403) + + assert sweep_test_calendars(client, SweepPrincipal("pythoncaldav-test")) == [] + + +def test_sweep_goes_on_past_a_refused_delete() -> None: + """A 403 on one calendar must not leave the others behind.""" + client = SweepClient(status=403) + + sweep_test_calendars(client, SweepPrincipal("pythoncaldav-test", "pythoncaldav-test2")) + + assert len(client.deletes) == 2 + + +def test_sweep_matches_the_decoded_prefix_only() -> None: + """Percent-encoded ids are decoded; a lookalike or a name is no match.""" + client = SweepClient() + principal = SweepPrincipal("pythoncaldav%2Dtest", "xpythoncaldav-test", "my-pythoncaldav-test") + + assert sweep_test_calendars(client, principal) == ["pythoncaldav-test"] + + +def test_sweep_waits_for_an_asynchronous_deletion(monkeypatch: pytest.MonkeyPatch) -> None: + """A fixture re-creating the id must not meet the calendar on its way out.""" + import tests.fixture_helpers as fh + + monkeypatch.setattr(fh.time, "sleep", lambda s: None) + client = SweepClient( + {"synchronous-write.delete-calendar": {"support": "unsupported", "delay": 20}} + ) + principal = SweepPrincipal("personal", "pythoncaldav-test", lingering=2) + + sweep_test_calendars(client, principal) + + ## the listing to sweep, two still showing it, one without it + assert principal.listings == 4 + + +class FakeRegistry: + def __init__(self, servers: dict[str, Any]) -> None: + self.servers = servers + + def get(self, name: str) -> Any: + return self.servers.get(name) + + +@pytest.fixture +def swept(monkeypatch: pytest.MonkeyPatch) -> list[str]: + """Record which servers get swept, with the registry and sweep faked.""" + import caldav.testing.registry + import tests.fixture_helpers as fh + from caldav.testing.base import ExternalTestServer + + class External(ExternalTestServer): + def __init__(self, name: str) -> None: + self.name = name + + def get_sync_client(self) -> Any: + name = self.name + + class Client: + def __enter__(self) -> Any: + return self + + def __exit__(self, *a: Any) -> None: + pass + + def principal(self) -> str: + return name + + return Client() + + class Docker: + def get_sync_client(self) -> Any: + raise AssertionError("a docker server must not be swept") + + registry = FakeRegistry( + {"ecloud": External("ecloud"), "gmx": External("gmx"), "baikal": Docker()} + ) + monkeypatch.setattr(caldav.testing.registry, "get_registry", lambda: registry) + monkeypatch.setattr(fh, "_swept_servers", set()) + calls: list[str] = [] + monkeypatch.setattr( + fh, "sweep_test_calendars", lambda client, principal: calls.append(principal) or [] + ) + return calls + + +def test_sweep_server_leaves_non_external_servers_alone(swept: list[str]) -> None: + sweep_server("baikal") + sweep_server("not-registered") + + assert swept == [] + + +def test_sweep_once_per_server_then_again_at_session_end(swept: list[str]) -> None: + for name in ("ecloud", "gmx", "ecloud", None): + sweep_server_once(name) + assert swept == ["ecloud", "gmx"] + + sweep_used_servers() + assert swept == ["ecloud", "gmx", "ecloud", "gmx"] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("frees", [True, False]) +async def test_delete_test_calendar_skips_the_trashbin_where_it_would_keep_the_id( + frees: bool, +) -> None: + """Where a plain DELETE leaves the id taken (Nextcloud's trashbin), the + calendar goes for good; elsewhere Calendar.delete() does it, with its + retries and its wait for an asynchronous deletion.""" + client = SweepClient({"delete-calendar.free-namespace": frees}) + cal = FakeCalendar(url="http://dav.example.com/cals/pythoncaldav-test2/") + + await adelete_test_calendar(client, cal) + + assert cal.deleted is frees + assert client.deletes == ( + [] + if frees + else [("http://dav.example.com/cals/pythoncaldav-test2/", {"X-NC-CalDAV-No-Trashbin": "1"})] + ) + + +@pytest.mark.asyncio +async def test_delete_if_present_skips_the_trashbin_given_the_client() -> None: + """A leftover cleared before re-creating its id must not land in the + trashbin, or the MKCALENDAR that follows meets the id still taken.""" + url = "http://dav.example.com/cals/pythoncaldav-async-mkcalendar/" + principal = FakePrincipal({"pythoncaldav-async-mkcalendar": FakeCalendar(url=url)}) + client = SweepClient({"delete-calendar.free-namespace": False}) + + await adelete_calendar_if_present(principal, "pythoncaldav-async-mkcalendar", client=client) + + assert client.deletes == [(url, {"X-NC-CalDAV-No-Trashbin": "1"})] + + +@pytest.mark.asyncio +async def test_delete_test_calendar_wipes_where_deletion_is_unsupported() -> None: + """Synology refuses the DELETE; Calendar.delete() wipes the objects there.""" + client = SweepClient( + {"delete-calendar": {"support": "unsupported"}, "delete-calendar.free-namespace": False} + ) + cal = FakeCalendar(url="http://dav.example.com/cals/pythoncaldav-test2/") + + await adelete_test_calendar(client, cal) + + assert cal.deleted + assert client.deletes == [] + + +def test_sweep_does_not_count_a_delete_answered_500() -> None: + client = SweepClient(status=500) + + assert ( + sweep_test_calendars(client, SweepPrincipal("pythoncaldav-test", "pythoncaldav-test2")) + == [] + ) + assert len(client.deletes) == 2 + + +def test_sweep_server_survives_a_failing_sweep( + swept: list[str], monkeypatch: pytest.MonkeyPatch +) -> None: + """Best effort: a server that breaks the sweep must not fail the session.""" + import tests.fixture_helpers as fh + + def boom(client: Any, principal: Any) -> list[str]: + raise error.DAVError("server fell over") + + monkeypatch.setattr(fh, "sweep_test_calendars", boom) + + sweep_server("ecloud") From 50cd6a270f00a7246bf191224399be2f0158b125 Mon Sep 17 00:00:00 2001 From: Tobias Brox Date: Thu, 12 Nov 2026 21:51:02 +0100 Subject: [PATCH 2/5] test: let sync ecloud runs send a request a second Two ecloud test runs at one request per two seconds created 22 calendars in three hours and got no 429, while a full run took over two hours. The proactive pace in the ecloud hint, which only the sync test suite applies, is doubled; the 429 handling stays as it was. Prompts (comments made inline in the document while reviewing it): > (on the proposal to look at the ecloud rate-limit hint, interval 2, count 1, since no 429 was observed) agreed, lower it a bit, with a comment, and we can revisit it later Assisted-By: Claude Opus 5.5 Reviewed-by: Tobias Brox --- caldav/compatibility_hints.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/caldav/compatibility_hints.py b/caldav/compatibility_hints.py index fcb9b4bd..c2286fc7 100644 --- a/caldav/compatibility_hints.py +++ b/caldav/compatibility_hints.py @@ -1864,9 +1864,14 @@ def compare(self, observed): #'search.text.case-sensitive': {'support': 'unsupported'}, ## observed to work at 4bc0de765a2b53e6f223e0b9ac51c653bac11fb7 (caldav) / 3cae24cf99da1702b851b5a74a9b88c8e5317dad (server checker) 'delete-calendar.free-namespace': False, ## TODO: this applies only to test runs, not to ordinary usage + ## 2026-11-12: two test runs at one request per 2 s made 22 calendars in + ## 3 hours (up to 9 in one hour) and got no 429 at all, so the pace is + ## doubled to one request per second. Only the sync suite paces by + ## interval/count. Revisit: lower it further while no 429 shows up, + ## raise it again if one does. 'rate-limit': { 'enable': True, - 'interval': 2, + 'interval': 1, 'count': 1, 'default_sleep': 4, 'max_sleep': 120, From 646475a344399513633d37153b584ae5cfb29ab7 Mon Sep 17 00:00:00 2001 From: Tobias Brox Date: Mon, 16 Nov 2026 19:14:51 +0100 Subject: [PATCH 3/5] feat: create_calendar_per_hour rate-limit key Nextcloud lets an account make only so many calendars in an hour (rateLimitCalendarCreation, 10 by default) and answers MKCALENDAR beyond that with 429. A suite that makes a calendar per test, like calendaring's conformance suite, fails most of its tests on ecloud. The rate-limit client-feature gets an extra key for it, create_calendar_per_hour, set to 10 for ecloud, so such a client can reuse its calendars instead. It is not a server-feature: it is not probed, and what it asks of a client depends on the client. Prompts: > [pick from the task list: "calendaring: conformance follow-up - > decisions and push after 2026-11-11 session"] > (on ecloud's calendar-creation rate limit making 174 tests fail) I > thought we already had a fix here, reusing calendars? > (on the options: a caldav hint, keying on the server name, or skipping > ecloud) We go for [a caldav calendar-creation rate-limiting hint], > probably. I don't think we should have a probe for this. This means > it's not a "server-feature", but either a "client-feature" or > "server-observation" or something like that. > please fix it now > (on the new key being spelled with hyphens while its neighbours use > underscores) Consistency matters - it seems like I've (relatively?) > consistently been choosing dash in the feature names and underscore in > the extra keys. Assisted-By: Claude Opus 5.5 Reviewed-by: Tobias Brox --- caldav/compatibility_hints.py | 7 ++++++- changes/+create-calendar-rate-limit.added.md | 1 + tests/test_compatibility_hints.py | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 changes/+create-calendar-rate-limit.added.md diff --git a/caldav/compatibility_hints.py b/caldav/compatibility_hints.py index c2286fc7..9374792c 100644 --- a/caldav/compatibility_hints.py +++ b/caldav/compatibility_hints.py @@ -343,7 +343,8 @@ class FeatureSet: "interval": "Rate limiting window, in seconds", "count": "Max number of requests to send within the interval", "max_sleep": "Max sleep when hitting a 429 or 503 with retry-after, in seconds", - "default_sleep": "Sleep for this long when hitting a 429, in seconds" + "default_sleep": "Sleep for this long when hitting a 429, in seconds", + "create_calendar_per_hour": "How many calendars the account may make within an hour (Nextcloud's default period); MKCALENDAR beyond that gets a 429 (Nextcloud's rateLimitCalendarCreation, observed as about 10 on ecloud). A client that makes many calendars, like a test suite, should reuse them rather than make new ones. Read on its own, whatever 'enable' says: a server with only this limit needs no pacing or sleep-and-retry", }}, "search-cache": { "type": "server-peculiarity", @@ -1875,6 +1876,10 @@ def compare(self, observed): 'count': 1, 'default_sleep': 4, 'max_sleep': 120, + ## Nextcloud's own default (rateLimitCalendarCreation); 2026-11-11 a + ## dozen creations within the hour got a 429, 2026-11-12 nine in one + ## hour did not. + 'create_calendar_per_hour': 10, 'description': "It's needed to manually empty trashbin frequently when running tests. Since this operation takes some time and/or there are some caches, it's needed to run tests slowly, even when hammering the 'empty thrashbin' frequently", }, 'auto-connect.url': { diff --git a/changes/+create-calendar-rate-limit.added.md b/changes/+create-calendar-rate-limit.added.md new file mode 100644 index 00000000..2bdb7de7 --- /dev/null +++ b/changes/+create-calendar-rate-limit.added.md @@ -0,0 +1 @@ +New extra key `create_calendar_per_hour` on the `rate-limit` client-feature: how many calendars the account may make within an hour before MKCALENDAR gets a 429. Set to 10 for ecloud, Nextcloud's default. A client making many calendars, like a test suite, can reuse them instead. diff --git a/tests/test_compatibility_hints.py b/tests/test_compatibility_hints.py index ed49e08b..47c945fc 100644 --- a/tests/test_compatibility_hints.py +++ b/tests/test_compatibility_hints.py @@ -1258,3 +1258,22 @@ def test_task_modes_unknown_unless_probed(self) -> None: """RFC 10051: absent means unsupported, but an unprobed server is not known to lack it.""" assert FeatureSet({}).is_supported("supported-task-modes", str) == "unknown" assert FeatureSet(ch.ox).is_supported("supported-task-modes", str) == "unsupported" + + +class TestCalendarCreationRateLimit: + """Nextcloud limits how many calendars an account may make in an hour. + + A client making many, like a test suite, reuses them instead: the limit is + an extra key on the rate-limit client-feature, not a probed server-feature. + """ + + KEY = "create_calendar_per_hour" + + def test_the_key_is_documented(self) -> None: + assert self.KEY in FeatureSet.FEATURES["rate-limit"]["extra_keys"] + + def test_ecloud_declares_its_limit(self) -> None: + assert FeatureSet(ch.ecloud).is_supported("rate-limit", dict)[self.KEY] == 10 + + def test_absent_on_a_server_without_one(self) -> None: + assert self.KEY not in FeatureSet(ch.baikal).is_supported("rate-limit", dict) From 85047383c3e0c5476b2067f250c085e0276a75e8 Mon Sep 17 00:00:00 2001 From: Tobias Brox Date: Wed, 18 Nov 2026 06:14:42 +0100 Subject: [PATCH 4/5] docs: Synology calendars can go through DSM's API The synology hint said DSM manages calendars only through its own UI and API, leaving a reader with no way to clear leftover test calendars. DSM's web API deletes them with the account's own credentials, also those created over CalDAV, which its list method does not show. Prompt: (on whether to mention the DSM delete in the synology hint) please do Assisted-By: Claude Opus 5.5 Reviewed-by: Tobias Brox --- caldav/compatibility_hints.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/caldav/compatibility_hints.py b/caldav/compatibility_hints.py index 9374792c..1ea394c1 100644 --- a/caldav/compatibility_hints.py +++ b/caldav/compatibility_hints.py @@ -2540,7 +2540,14 @@ def __getattr__(name: str) -> Any: ## through its own UI/API and refuses a CalDAV DELETE on the collection, ## where upstream DAViCal allows it. Consequence for callers: Calendar ## .delete() degrades to wiping the objects, so a cal_id is never freed for - ## reuse and a later MKCALENDAR at the same id gets 405. + ## reuse and a later MKCALENDAR at the same id gets 405. DSM's own web + ## API does delete them, with the account's CalDAV credentials: log in + ## at /webapi/entry.cgi (api=SYNO.API.Auth, version=6, method=login, + ## session=Calendar, format=sid), then api=SYNO.Cal.Cal, version=1, + ## method=delete, cal_id='"///"' (a JSON string) with the + ## sid as _sid. It works on calendars made over CalDAV too, although + ## SYNO.Cal.Cal's list method does not show those (2026-11-18, 384 + ## leftover test calendars deleted). 'delete-calendar': False, ## sync-token (fragile, inherited from davical) and sync-token.delete ## both came out full in a server-tester run on 2026-10-23. From 091b38e95c5b7525fb6789ff226eee463870e4c1 Mon Sep 17 00:00:00 2001 From: Tobias Brox Date: Wed, 18 Nov 2026 06:46:33 +0100 Subject: [PATCH 5/5] test: let the sweep fake call its parent __init__ Code scanning flagged the fake ExternalTestServer in the sweep tests for not calling the parent __init__. The parent needs no config, so the fake now passes its name through it instead of setting it alone. Prompts: > rebase, write a comment in the code why the parent `__init__` is > skipped, and dismiss the QA-issue. [the code-scanning finding > "Missing call to superclass __init__" on > https://github.com/python-caldav/caldav/pull/871; review then showed > the skip was not needed, so the code calls it rather than explaining] Assisted-By: Claude Opus 5.5 Reviewed-by: Tobias Brox --- tests/test_fixture_helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_fixture_helpers.py b/tests/test_fixture_helpers.py index a123e2d7..f39e6fdf 100644 --- a/tests/test_fixture_helpers.py +++ b/tests/test_fixture_helpers.py @@ -780,7 +780,7 @@ def swept(monkeypatch: pytest.MonkeyPatch) -> list[str]: class External(ExternalTestServer): def __init__(self, name: str) -> None: - self.name = name + super().__init__({"name": name}) def get_sync_client(self) -> Any: name = self.name