diff --git a/news/+alarm-x-passthrough.bugfix.rst b/news/+alarm-x-passthrough.bugfix.rst new file mode 100644 index 0000000..b09f750 --- /dev/null +++ b/news/+alarm-x-passthrough.bugfix.rst @@ -0,0 +1 @@ +Where ``properties.passthrough`` is ``unsupported``, the X- properties on an alarm the backend keeps are reported as lost too, and ``verify=True`` no longer fails on a backend that drops them as it declares. diff --git a/src/calendaring/_mapping.py b/src/calendaring/_mapping.py index c368074..516d3e7 100644 --- a/src/calendaring/_mapping.py +++ b/src/calendaring/_mapping.py @@ -188,19 +188,24 @@ def apply() -> None: return [(Loss("VALARM", sent, None), apply)] +def _is_foreign_x(name: str) -> bool: + """An X- property that is not the library's own: what a backend without passthrough drops.""" + return name.startswith("X-") and name not in OWN_X_PROPERTIES + + def plan_losses(component: icalendar.Component, capabilities: Capabilities) -> list[Change]: """The losses writing ``component`` would cause, each with the edit that applies it.""" changes: list[Change] = _alarm_losses(component, capabilities) + drops_x = capabilities.level(Feature.PROPERTIES_PASSTHROUGH) is Support.UNSUPPORTED + if drops_x and capabilities.level(Feature.ALARMS) is not Support.UNSUPPORTED: # else the alarms go anyway + for alarm in (c for c in component.subcomponents if c.name == "VALARM"): + changes.extend((Loss(n, alarm[n], None), _drop(alarm, n)) for n in list(alarm) if _is_foreign_x(n)) is_task = component.name == "VTODO" table = ANY_COMPONENT | TASK_ONLY if is_task else ANY_COMPONENT for name in list(component): feature = table.get(name) if feature is None: - if ( - name.startswith("X-") - and name not in OWN_X_PROPERTIES - and capabilities.level(Feature.PROPERTIES_PASSTHROUGH) is Support.UNSUPPORTED - ): + if drops_x and _is_foreign_x(name): changes.append((Loss(name, component[name], None), _drop(component, name))) continue level = capabilities.level(feature) diff --git a/tests/_async/test_caldav_backend.py b/tests/_async/test_caldav_backend.py index 3074999..a7e8b80 100644 --- a/tests/_async/test_caldav_backend.py +++ b/tests/_async/test_caldav_backend.py @@ -338,13 +338,17 @@ async def test_a_token_from_elsewhere_is_refused(collection: AsyncCollection) -> @pytest.fixture async def radicale_collection(radicale_server: Any) -> AsyncIterator[AsyncCollection]: - """On Radicale, caldav filters every search result itself: it has no case-sensitive text search.""" + """Radicale, declared without case-sensitive text search, so caldav filters every search result itself. + + Radicale has it since 3.9.0, and caldav's hints say so from the release after 3.7.0. + """ + features = {**radicale_server.config["features"], "search.text.case-sensitive": {"support": "unsupported"}} backend = await AsyncBackend.open( radicale_server.url, kind="caldav", username=radicale_server.username, password=radicale_server.password, - features=radicale_server.config["features"], + features=features, ) async with backend: made = await backend.create_collection("backend-tests", components=(Component.TASK,)) diff --git a/tests/_async/test_memory_conformance.py b/tests/_async/test_memory_conformance.py index 81b283f..0f5b5f8 100644 --- a/tests/_async/test_memory_conformance.py +++ b/tests/_async/test_memory_conformance.py @@ -35,6 +35,7 @@ BackendError, BrokenObject, Component, + Event, Feature, Item, ListingLimitError, @@ -46,7 +47,8 @@ VerificationError, ) from calendaring._async.conformance import snapshot -from calendaring.loss import LossPolicy +from calendaring._mapping import OWN_X_PROPERTIES, Change +from calendaring.loss import ALLOW, LossPolicy from calendaring.memory import ( FEED, FULL, @@ -172,6 +174,87 @@ async def open_backend(self) -> AsyncBackend: return AsyncMemoryBackend(profile=MAPPED_KEEPING_UIDS) +NO_PASSTHROUGH = Profile( + "no-passthrough", {**FULL.capabilities, Feature.PROPERTIES_PASSTHROUGH: Support.UNSUPPORTED}, FULL.components +) + + +class StripsXProperties(AsyncMemoryCollection): + """Drops every X- property but the library's own, also from the alarms it keeps, as it declares.""" + + @staticmethod + def _stripped(calendar: icalendar.Calendar, wire: bytes | None) -> icalendar.Calendar: + calendar = icalendar.Calendar.from_ical(wire) if wire is not None else copy.deepcopy(calendar) + for component in calendar.walk(): + if component.name in ("VEVENT", "VTODO", "VJOURNAL", "VALARM"): + for name in [n for n in component if n.startswith("X-") and n not in OWN_X_PROPERTIES]: + del component[name] + return calendar + + async def _create(self, calendar: icalendar.Calendar, *, native_id: str | None, wire: bytes | None) -> StoredObject: + return await super()._create(self._stripped(calendar, wire), native_id=native_id, wire=None) + + async def _update( + self, native_id: str, calendar: icalendar.Calendar, *, etag: str | None, wire: bytes | None + ) -> StoredObject: + return await super()._update(native_id, self._stripped(calendar, wire), etag=etag, wire=None) + + +class StripsXPropertiesBackend(AsyncMemoryBackend): + collection_class = StripsXProperties + + +class TestNoPassthroughProfile(AsyncConformance): + """Keeps alarms but no X- properties, on them or on the item (properties.passthrough unsupported).""" + + async def open_backend(self) -> AsyncBackend: + return StripsXPropertiesBackend(profile=NO_PASSTHROUGH) + + +class PlansAlarmXPropertiesKept(StripsXProperties): + """Sends the X- properties on an alarm as they are, as the loss plan once did.""" + + def _plan_losses(self, component: icalendar.Component) -> list[Change]: + alarms = [c for c in component.subcomponents if c.name == "VALARM"] + return [ + (loss, apply) + for loss, apply in super()._plan_losses(component) + if not any(alarm.get(loss.property) is loss.sent for alarm in alarms) + ] + + +async def snoozed_event(collection: AsyncCollection) -> AsyncItem: + """An event stored with an alarm, which then gets an X- property of its own, unsaved.""" + event = Event.new(summary="snoozed", start=datetime(2026, 11, 3, 9, tzinfo=UTC)) + alarm = icalendar.Alarm() + alarm.add("ACTION", "DISPLAY") + alarm.add("DESCRIPTION", "soon") + alarm.add("TRIGGER", timedelta(minutes=-30)) + event.component.add_component(alarm) + stored = await collection.add(event, verify=True) + stored.component.subcomponents[0].add("X-SNOOZE", "PT5M") + return stored + + +async def test_an_update_drops_alarm_x_properties_as_declared() -> None: + collection = await StripsXPropertiesBackend(profile=NO_PASSTHROUGH).collection("default") + stored = await snoozed_event(collection) + await stored.save(loss=ALLOW, verify=True) + [alarm] = (await collection.reload(stored)).component.subcomponents + assert "X-SNOOZE" not in alarm + + +async def test_an_update_catches_alarm_x_properties_the_plan_kept() -> None: + """The backend strips on update as on create, so verify sees what a plan that misses alarms sends.""" + + class Backend(AsyncMemoryBackend): + collection_class = PlansAlarmXPropertiesKept + + stored = await snoozed_event(await Backend(profile=NO_PASSTHROUGH).collection("default")) + with pytest.raises(VerificationError): + await stored.save(verify=True) + + # Three more profiles, for the branches the four above leave out. EVENTS_ONLY = Profile( "events-only", diff --git a/tests/_sync/test_caldav_backend.py b/tests/_sync/test_caldav_backend.py index 053b526..b236abb 100644 --- a/tests/_sync/test_caldav_backend.py +++ b/tests/_sync/test_caldav_backend.py @@ -338,13 +338,17 @@ def test_a_token_from_elsewhere_is_refused(collection: Collection) -> None: @pytest.fixture def radicale_collection(radicale_server: Any) -> Iterator[Collection]: - """On Radicale, caldav filters every search result itself: it has no case-sensitive text search.""" + """Radicale, declared without case-sensitive text search, so caldav filters every search result itself. + + Radicale has it since 3.9.0, and caldav's hints say so from the release after 3.7.0. + """ + features = {**radicale_server.config["features"], "search.text.case-sensitive": {"support": "unsupported"}} backend = Backend.open( radicale_server.url, kind="caldav", username=radicale_server.username, password=radicale_server.password, - features=radicale_server.config["features"], + features=features, ) with backend: made = backend.create_collection("backend-tests", components=(Component.TASK,)) diff --git a/tests/_sync/test_memory_conformance.py b/tests/_sync/test_memory_conformance.py index 7f6eddc..d6d6fc5 100644 --- a/tests/_sync/test_memory_conformance.py +++ b/tests/_sync/test_memory_conformance.py @@ -35,6 +35,7 @@ BackendError, BrokenObject, Component, + Event, Feature, Item, ListingLimitError, @@ -46,7 +47,8 @@ VerificationError, ) from calendaring._sync.conformance import snapshot -from calendaring.loss import LossPolicy +from calendaring._mapping import OWN_X_PROPERTIES, Change +from calendaring.loss import ALLOW, LossPolicy from calendaring.memory import ( FEED, FULL, @@ -172,6 +174,87 @@ def open_backend(self) -> Backend: return MemoryBackend(profile=MAPPED_KEEPING_UIDS) +NO_PASSTHROUGH = Profile( + "no-passthrough", {**FULL.capabilities, Feature.PROPERTIES_PASSTHROUGH: Support.UNSUPPORTED}, FULL.components +) + + +class StripsXProperties(MemoryCollection): + """Drops every X- property but the library's own, also from the alarms it keeps, as it declares.""" + + @staticmethod + def _stripped(calendar: icalendar.Calendar, wire: bytes | None) -> icalendar.Calendar: + calendar = icalendar.Calendar.from_ical(wire) if wire is not None else copy.deepcopy(calendar) + for component in calendar.walk(): + if component.name in ("VEVENT", "VTODO", "VJOURNAL", "VALARM"): + for name in [n for n in component if n.startswith("X-") and n not in OWN_X_PROPERTIES]: + del component[name] + return calendar + + def _create(self, calendar: icalendar.Calendar, *, native_id: str | None, wire: bytes | None) -> StoredObject: + return super()._create(self._stripped(calendar, wire), native_id=native_id, wire=None) + + def _update( + self, native_id: str, calendar: icalendar.Calendar, *, etag: str | None, wire: bytes | None + ) -> StoredObject: + return super()._update(native_id, self._stripped(calendar, wire), etag=etag, wire=None) + + +class StripsXPropertiesBackend(MemoryBackend): + collection_class = StripsXProperties + + +class TestNoPassthroughProfile(SyncConformance): + """Keeps alarms but no X- properties, on them or on the item (properties.passthrough unsupported).""" + + def open_backend(self) -> Backend: + return StripsXPropertiesBackend(profile=NO_PASSTHROUGH) + + +class PlansAlarmXPropertiesKept(StripsXProperties): + """Sends the X- properties on an alarm as they are, as the loss plan once did.""" + + def _plan_losses(self, component: icalendar.Component) -> list[Change]: + alarms = [c for c in component.subcomponents if c.name == "VALARM"] + return [ + (loss, apply) + for loss, apply in super()._plan_losses(component) + if not any(alarm.get(loss.property) is loss.sent for alarm in alarms) + ] + + +def snoozed_event(collection: Collection) -> SyncItem: + """An event stored with an alarm, which then gets an X- property of its own, unsaved.""" + event = Event.new(summary="snoozed", start=datetime(2026, 11, 3, 9, tzinfo=UTC)) + alarm = icalendar.Alarm() + alarm.add("ACTION", "DISPLAY") + alarm.add("DESCRIPTION", "soon") + alarm.add("TRIGGER", timedelta(minutes=-30)) + event.component.add_component(alarm) + stored = collection.add(event, verify=True) + stored.component.subcomponents[0].add("X-SNOOZE", "PT5M") + return stored + + +def test_an_update_drops_alarm_x_properties_as_declared() -> None: + collection = StripsXPropertiesBackend(profile=NO_PASSTHROUGH).collection("default") + stored = snoozed_event(collection) + stored.save(loss=ALLOW, verify=True) + [alarm] = (collection.reload(stored)).component.subcomponents + assert "X-SNOOZE" not in alarm + + +def test_an_update_catches_alarm_x_properties_the_plan_kept() -> None: + """The backend strips on update as on create, so verify sees what a plan that misses alarms sends.""" + + class Backend(MemoryBackend): + collection_class = PlansAlarmXPropertiesKept + + stored = snoozed_event(Backend(profile=NO_PASSTHROUGH).collection("default")) + with pytest.raises(VerificationError): + stored.save(verify=True) + + # Three more profiles, for the branches the four above leave out. EVENTS_ONLY = Profile( "events-only", diff --git a/tests/test_mapping.py b/tests/test_mapping.py index 6432f61..82c1fad 100644 --- a/tests/test_mapping.py +++ b/tests/test_mapping.py @@ -432,3 +432,14 @@ def test_a_retired_x_pycal_property_is_lost_like_any_other_x_property() -> None: losses, component = planned(task, {Feature.PROPERTIES_PASSTHROUGH: Support.UNSUPPORTED}) assert losses == ["X-PYCAL-PLANNED-START"] assert "X-PYCAL-TIMER-START" in component + + +def test_x_properties_on_an_alarm_are_lost_where_x_properties_are_dropped() -> None: + """properties.passthrough covers the alarms too: a backend keeping them drops their X- properties.""" + event = with_alarm(Event.new(summary="x", start=datetime(2026, 11, 3, 9, tzinfo=UTC))) + event.component.subcomponents[0].add("X-SNOOZE", "PT5M") + losses, component = planned(event, {Feature.PROPERTIES_PASSTHROUGH: Support.UNSUPPORTED}) + assert losses == ["X-SNOOZE"] + [alarm] = component.subcomponents + assert "X-SNOOZE" not in alarm + assert "TRIGGER" in alarm