Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/+alarm-x-passthrough.bugfix.rst
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 10 additions & 5 deletions src/calendaring/_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions tests/_async/test_caldav_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,))
Expand Down
85 changes: 84 additions & 1 deletion tests/_async/test_memory_conformance.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
BackendError,
BrokenObject,
Component,
Event,
Feature,
Item,
ListingLimitError,
Expand All @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 6 additions & 2 deletions tests/_sync/test_caldav_backend.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 84 additions & 1 deletion tests/_sync/test_memory_conformance.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading