diff --git a/news/+accept-invitation-crash-no-participants.bugfix.rst b/news/+accept-invitation-crash-no-participants.bugfix.rst new file mode 100644 index 0000000..187c134 --- /dev/null +++ b/news/+accept-invitation-crash-no-participants.bugfix.rst @@ -0,0 +1 @@ +``accept_invitation``/``decline_invitation``/``tentatively_accept`` no longer crash with ``AttributeError`` when an event has no ``participants`` property at all. They now raise ``JMAPMethodError`` with ``notFound``, as documented. diff --git a/news/+address-book-is-default-inferred.bugfix.rst b/news/+address-book-is-default-inferred.bugfix.rst new file mode 100644 index 0000000..9c9072c --- /dev/null +++ b/news/+address-book-is-default-inferred.bugfix.rst @@ -0,0 +1 @@ +``get_address_books`` now marks an account's sole address book as default when the server reports none as default. Cyrus omits ``isDefault`` entirely instead of sending ``false``, so the one address book an account actually has was never flagged default. diff --git a/news/+recurrence-rule-spelling-mismatch.bugfix.rst b/news/+recurrence-rule-spelling-mismatch.bugfix.rst new file mode 100644 index 0000000..a89d1ee --- /dev/null +++ b/news/+recurrence-rule-spelling-mismatch.bugfix.rst @@ -0,0 +1 @@ +``create_event``/``update_event`` now retry with the other ``recurrenceRule``/``recurrenceRules`` spelling when the server rejects the one they sent. Current Cyrus requires the plural array form. Stalwart still requires the singular form, so no single hardcoded spelling could satisfy both servers. diff --git a/news/+send-to-required-by-cyrus.bugfix.rst b/news/+send-to-required-by-cyrus.bugfix.rst new file mode 100644 index 0000000..6fc41e0 --- /dev/null +++ b/news/+send-to-required-by-cyrus.bugfix.rst @@ -0,0 +1 @@ +``ical_to_jscal`` now emits a ``sendTo`` property on every participant, alongside the existing ``calendarAddress``. Current Cyrus rejects any participant without ``sendTo``, on both create and update, regardless of scheduling messages. diff --git a/news/+version-property-rejected-by-cyrus.bugfix.rst b/news/+version-property-rejected-by-cyrus.bugfix.rst new file mode 100644 index 0000000..8d15a9a --- /dev/null +++ b/news/+version-property-rejected-by-cyrus.bugfix.rst @@ -0,0 +1 @@ +``ical_to_jscal`` no longer emits a ``version`` property on created/updated events. Current Cyrus rejects it outright with ``invalidProperties``. RFC 8984 marks the property optional, so leaving it out is correct. diff --git a/src/calendaring_jmap/_methods/contact.py b/src/calendaring_jmap/_methods/contact.py index 9dcbd90..9cffd56 100644 --- a/src/calendaring_jmap/_methods/contact.py +++ b/src/calendaring_jmap/_methods/contact.py @@ -50,7 +50,18 @@ def parse_address_book_get(response_args: dict) -> list[JMAPAddressBook]: List of ``JMAPAddressBook``. Empty if ``"list"`` is absent or empty. """ - return [JMAPAddressBook.from_jmap(item) for item in response_args.get("list", [])] + books = [JMAPAddressBook.from_jmap(item) for item in response_args.get("list", [])] + # RFC 9610 section 2 requires isDefault to be present and true for + # exactly one AddressBook in the account; confirmed live that Cyrus's + # current image omits the key entirely rather than sending it false, + # which JMAPAddressBook.from_jmap otherwise reads as "not default" even + # for the account's only address book. When there is exactly one + # AddressBook and none of them actually claimed isDefault, it is the de + # facto default regardless of what the server sent, so mark it rather + # than reporting a default-less account the spec says can't happen. + if len(books) == 1 and not books[0].is_default: + books[0].is_default = True + return books def build_contact_query( diff --git a/src/calendaring_jmap/async_client.py b/src/calendaring_jmap/async_client.py index f104741..66614c9 100644 --- a/src/calendaring_jmap/async_client.py +++ b/src/calendaring_jmap/async_client.py @@ -455,13 +455,17 @@ async def _create_event_impl( jscal = self._strip_server_managed_event_properties(event) self._resolve_create_calendar_ids(jscal, calendar_id) target_account = self._resolve_account(session, account_id) - call = build_event_set_create( - target_account, - {"new-0": jscal}, - send_scheduling_messages=send_scheduling_messages, - if_in_state=if_in_state, - ) - responses = await self._request([call]) + tried_recurrence_rule_keys: set[str] = set() + while True: + call = build_event_set_create( + target_account, + {"new-0": jscal}, + send_scheduling_messages=send_scheduling_messages, + if_in_state=if_in_state, + ) + responses = await self._request([call]) + if not self._retry_recurrence_rule_create(responses, jscal, tried_recurrence_rule_keys): + break event_id = self._parse_create_response( responses, session.api_url, "CalendarEvent/set", parse_event_set ) @@ -784,15 +788,21 @@ async def update_event( session = await self._get_session() target_account = self._resolve_account(session, account_id) patch, nulled = self._build_event_update_patch(event) + tried_recurrence_rule_keys: set[str] = set() while True: responses = await self._request( [build_event_set_update(target_account, {event_id: patch}, if_in_state=if_in_state)] ) drop = self._unsupported_null_keys(responses, event_id, patch, nulled) - if not drop: - break - for key in drop: - patch.pop(key, None) + if drop: + for key in drop: + patch.pop(key, None) + continue + if self._retry_recurrence_rule_update( + responses, event_id, patch, tried_recurrence_rule_keys + ): + continue + break self._parse_update_response( responses, session.api_url, "CalendarEvent/set", parse_event_set, event_id ) diff --git a/src/calendaring_jmap/client.py b/src/calendaring_jmap/client.py index 2ab7d83..b0e9082 100644 --- a/src/calendaring_jmap/client.py +++ b/src/calendaring_jmap/client.py @@ -554,14 +554,19 @@ def _unsupported_null_keys( new iCalendar, so we report it as droppable, letting the caller retry the update without it. - Returns the set of droppable keys when the failure is exactly this case, - or ``None`` when the update succeeded or failed for a genuine reason (in - which case the caller proceeds to ``_parse_update_response``, - which raises the real error). Some servers report only one offending - property per response, so the caller retries in a loop, dropping the - reported keys until the update succeeds or hits a genuine error; each - returned key is guaranteed still present in ``patch``, so the loop - strictly shrinks the patch and terminates. + Returns the set of droppable keys when every offending property not + accounted for by this check is one of ``_RECURRENCE_RULE_KEYS`` (left + for ``_retry_recurrence_rule_update`` to handle in the same retry + pass, since a server can reject both a stale null-cleanup key and + the wrong recurrenceRule/recurrenceRules spelling in one response, + confirmed live), or ``None`` when the update succeeded or failed for + a genuine reason (in which case the caller proceeds to + ``_parse_update_response``, which raises the real error). Some + servers report only one offending property per response, so the + caller retries in a loop, dropping the reported keys until the + update succeeds or hits a genuine error; each returned key is + guaranteed still present in ``patch``, so the loop strictly shrinks + the patch and terminates. """ for method_name, resp_args, _ in responses: if method_name == "CalendarEvent/set": @@ -571,14 +576,142 @@ def _unsupported_null_keys( return None props = set(err.get("properties") or []) droppable = {p for p in props if p in nulled and p in patch and patch[p] is None} - # Only retry when every offending property is null-cleanup we can - # safely omit; if the client actually set one of them to a value, - # the rejection is genuine and must surface. - if props and props == droppable: + unexplained = props - droppable + # Only retry when every offending property is either + # null-cleanup we can safely omit, or a recurrenceRule/ + # recurrenceRules spelling mismatch left for the other retry + # helper; if the client actually set some other property to + # a real value, the rejection is genuine and must surface. + if props and unexplained <= _JMAPClientBase._RECURRENCE_RULE_KEYS_SET: return droppable return None return None + _RECURRENCE_RULE_KEYS = ("recurrenceRule", "recurrenceRules") + _RECURRENCE_RULE_KEYS_SET = frozenset(_RECURRENCE_RULE_KEYS) + + @staticmethod + def _try_recurrence_rule_fallback_key(err: dict | None, jscal: dict, tried: set[str]) -> bool: + """Detect a create/update rejected solely for using the wrong + ``recurrenceRule``/``recurrenceRules`` spelling, and if so, swap + ``jscal`` in place to the other spelling so the caller can retry. + + RFC 8984 section 4.3.3 defines the plural, array-valued + ``recurrenceRules``; the singular ``recurrenceRule`` is an older, + non-normative spelling some servers still expect instead. Verified + live against both test servers this project targets: Cyrus's + current image requires the plural array and rejects the singular + with ``invalidProperties``, while Stalwart requires the singular + and rejects the plural the same way. Since neither server accepts + both spellings in the same request (also verified live) and which + one a given server wants is not otherwise discoverable, the caller + sends whichever spelling ``jscal`` already uses, and on a rejection + naming exactly that one property, swaps to the other spelling and + retries once. + + ``tried`` starts as an empty set the caller owns across its whole + retry loop; the one spelling this function ever swaps to is added + to it before returning, so a server that rejects both spellings in + turn (never actually observed, but not provable impossible from + one response alone) gets exactly one swap, not an alternating + retry forever: since there are only two possible spellings, having + already swapped once means every spelling has now been sent. + + Returns ``True`` (and mutates ``jscal``) when ``err`` is an + ``invalidProperties`` rejection naming exactly one of + ``_RECURRENCE_RULE_KEYS`` that is also the key actually present in + ``jscal``, and no swap has happened yet this retry loop, or + ``False`` (``jscal`` untouched) when the failure is unrelated (a + different property, more than one offending property, a spelling + already swapped to once this loop, or no error at all) and should + surface as-is. + """ + if tried or not err or err.get("type") != "invalidProperties": + return False + props = set(err.get("properties") or []) + if len(props) != 1: + return False + (offending,) = props + if offending not in _JMAPClientBase._RECURRENCE_RULE_KEYS or offending not in jscal: + return False + fallback = next(k for k in _JMAPClientBase._RECURRENCE_RULE_KEYS if k != offending) + value = jscal[offending] + # "recurrenceRule" (singular) is one RecurrenceRule object; + # "recurrenceRules" (plural, RFC 8984 section 4.3.3) is an array of + # them, since an event can in principle have more than one RRULE. + # Swapping the key alone would send an object where the other + # spelling expects an array, or vice versa, so the value's shape + # is converted along with the key: wrap a lone object in a + # one-element list going singular to plural. Going plural to + # singular, an empty list has nothing to convert (leave jscal + # untouched, same as any other unhandled case); otherwise only the + # first rule survives if the caller actually supplied more than one + # (ical_to_jscal itself never does; a caller building its own + # CalendarEvent dict directly could), matching this project's own + # established "log and keep first" tradeoff for the same case in + # ical_to_jscal's own RRULE handling, since the singular spelling + # has no way to carry more than one. + if fallback == "recurrenceRules": + value = [value] + elif isinstance(value, list): + if not value: + return False + if len(value) > 1: + log.warning( + "create_event()/update_event(): server rejected the plural " + "recurrenceRules spelling and only the singular is accepted; " + "%d rules were given, only the first is kept.", + len(value), + ) + value = value[0] + tried.add(fallback) + del jscal[offending] + jscal[fallback] = value + return True + + @staticmethod + def _recurrence_rule_set_error( + responses: list, + ) -> tuple[dict, dict, list[str], dict, dict, dict] | None: + """Find the first ``CalendarEvent/set`` response in ``responses`` + and return its parsed 6-tuple, or ``None`` if there is none. + + Shared by ``_retry_recurrence_rule_create``/``_retry_recurrence_rule_update``, + which otherwise differ only in which element of the tuple holds the + error to check (``not_created["new-0"]`` vs. ``not_updated[event_id]``). + """ + for method_name, resp_args, _ in responses: + if method_name == "CalendarEvent/set": + return parse_event_set(resp_args) + return None + + @staticmethod + def _retry_recurrence_rule_create(responses: list, jscal: dict, tried: set[str]) -> bool: + """``_try_recurrence_rule_fallback_key``, applied to a create + response's ``not_created["new-0"]`` error instead of an update's + ``not_updated``.""" + parsed = _JMAPClientBase._recurrence_rule_set_error(responses) + if parsed is None: + return False + _, _, _, not_created, _, _ = parsed + return _JMAPClientBase._try_recurrence_rule_fallback_key( + not_created.get("new-0"), jscal, tried + ) + + @staticmethod + def _retry_recurrence_rule_update( + responses: list, event_id: str, patch: dict, tried: set[str] + ) -> bool: + """``_try_recurrence_rule_fallback_key``, applied to an update + response's ``not_updated[event_id]`` error.""" + parsed = _JMAPClientBase._recurrence_rule_set_error(responses) + if parsed is None: + return False + _, _, _, _, not_updated, _ = parsed + return _JMAPClientBase._try_recurrence_rule_fallback_key( + not_updated.get(event_id), patch, tried + ) + # --------------------------------------------------------------------------- # Shared response parsers, pure synchronous, used by both sync and async # clients. Each method takes the raw ``methodResponses`` list returned by @@ -713,7 +846,7 @@ def _participant_matches_email(participant: dict, email: str) -> bool: @staticmethod def _find_participant_id_by_email( - participants: dict, own_email: str, api_url: str, event_id: str + participants: dict | None, own_email: str, api_url: str, event_id: str ) -> str: """Return the id of the entry in ``participants`` whose email is ``own_email``. @@ -722,10 +855,16 @@ def _find_participant_id_by_email( Shared by ``JMAPClient._find_own_participant_id`` and ``AsyncJMAPClient._find_own_participant_id``. + ``participants`` is ``None`` for an event with no ``participants`` + property at all (RFC 8984 section 4.4.6: optional, omitted rather + than an empty map when there are none), treated the same as an + empty map: no participant can match, so this still raises rather + than crashing on ``None.items()``. + Raises: JMAPMethodError: If no participant matches ``own_email``. """ - for participant_id, participant in participants.items(): + for participant_id, participant in (participants or {}).items(): if _JMAPClientBase._participant_matches_email(participant, own_email): return participant_id raise JMAPMethodError( @@ -1532,13 +1671,17 @@ def _create_event_impl( jscal = self._strip_server_managed_event_properties(event) self._resolve_create_calendar_ids(jscal, calendar_id) target_account = self._resolve_account(session, account_id) - call = build_event_set_create( - target_account, - {"new-0": jscal}, - send_scheduling_messages=send_scheduling_messages, - if_in_state=if_in_state, - ) - responses = self._request([call]) + tried_recurrence_rule_keys: set[str] = set() + while True: + call = build_event_set_create( + target_account, + {"new-0": jscal}, + send_scheduling_messages=send_scheduling_messages, + if_in_state=if_in_state, + ) + responses = self._request([call]) + if not self._retry_recurrence_rule_create(responses, jscal, tried_recurrence_rule_keys): + break event_id = self._parse_create_response( responses, session.api_url, "CalendarEvent/set", parse_event_set ) @@ -1987,15 +2130,21 @@ def update_event( session = self._get_session() target_account = self._resolve_account(session, account_id) patch, nulled = self._build_event_update_patch(event) + tried_recurrence_rule_keys: set[str] = set() while True: responses = self._request( [build_event_set_update(target_account, {event_id: patch}, if_in_state=if_in_state)] ) drop = self._unsupported_null_keys(responses, event_id, patch, nulled) - if not drop: - break - for key in drop: - patch.pop(key, None) + if drop: + for key in drop: + patch.pop(key, None) + continue + if self._retry_recurrence_rule_update( + responses, event_id, patch, tried_recurrence_rule_keys + ): + continue + break self._parse_update_response( responses, session.api_url, "CalendarEvent/set", parse_event_set, event_id ) diff --git a/src/calendaring_jmap/convert/ical_to_jscal.py b/src/calendaring_jmap/convert/ical_to_jscal.py index 92a79bd..c714224 100644 --- a/src/calendaring_jmap/convert/ical_to_jscal.py +++ b/src/calendaring_jmap/convert/ical_to_jscal.py @@ -275,6 +275,17 @@ def _cal_address_to_imip_and_email(addr: str) -> tuple[str, str | None]: return f"mailto:{addr}", addr +def _send_to(imip: str) -> dict: + """Build a Participant ``sendTo`` map for ``imip`` (the URI returned by + ``_cal_address_to_imip_and_email``, despite that function's own name + not always being one: RFC 8984 section 4.4.6 requires the ``"imip"`` + key's value specifically to be a ``mailto:`` URI, so a non-mailto + scheme such as ``sip:`` goes under ``"other"`` instead, whose method is + left undefined by the spec on purpose. + """ + return {"imip": imip} if imip.startswith("mailto:") else {"other": imip} + + def _organizer_to_participant(organizer) -> tuple[str, dict]: """Convert an ORGANIZER property to a (participant_id, Participant dict) tuple.""" imip, email = _cal_address_to_imip_and_email(str(organizer)) @@ -284,17 +295,16 @@ def _organizer_to_participant(organizer) -> tuple[str, dict]: # RFC 8984's own Participant object has no calendarAddress property; # it belongs to JMAP Calendars' ParticipantIdentity/Principal objects # instead, and sendTo (a map of delivery method to URI) is what - # RFC 8984 actually defines for this. Cyrus's CalendarEvent/set - # rejects sendTo outright with invalidProperties on both create and - # update, and separately requires calendarAddress, which RFC 8984 - # does not define here at all. Verified live against a running Cyrus - # container: sendTo alone rejected, calendarAddress alone accepted, - # both present still rejected for sendTo. calendarAddress is set to - # sendTo's would-be "imip" value, its natural equivalent per - # ParticipantIdentity's own definition. sendTo itself is optional in - # RFC 8984, so omitting it is a real compatibility tradeoff, not a - # spec violation; Stalwart accepts calendarAddress-only fine too. + # RFC 8984 actually defines for this. calendarAddress is additionally + # set to sendTo's "imip" value, its natural equivalent per + # ParticipantIdentity's own definition; Cyrus requires it and + # Stalwart accepts it, confirmed live on both. sendTo itself is + # optional per RFC 8984, but Cyrus's current image requires it on + # every participant unconditionally (create and update alike, + # scheduling or not), rejecting its absence with invalidProperties; + # Stalwart accepts it present or absent either way, confirmed live. "calendarAddress": imip, + "sendTo": _send_to(imip), } cn = organizer.params.get("CN") if cn: @@ -310,9 +320,10 @@ def _attendee_to_participant(attendee) -> tuple[str, dict]: pid = str(uuid.uuid4()) p: dict = { "roles": {"attendee": True}, - # See _organizer_to_participant for why calendarAddress is set - # instead of sendTo. + # See _organizer_to_participant for why calendarAddress and sendTo + # are both set. "calendarAddress": imip, + "sendTo": _send_to(imip), } if email is not None: p["email"] = email @@ -735,16 +746,16 @@ def ical_to_jscal(ical_str: str, calendar_id: str | None = None) -> dict: jscal: dict = { "@type": "Event", # RFC 8984 itself has no "version" property; draft-ietf-calext- - # jscalendarbis (the 2.0 successor spec) adds it and registers "1.0" - # for data conforming to RFC 8984, "2.0" for jscalendarbis. This - # conversion does not emit any 2.0-only properties (COORDINATES, - # SHOW-WITHOUT-TIME), so "1.0" would be the strictly accurate value. - # We send "2.0" anyway: Cyrus's current test image rejects "1.0" - # outright (requires "2.0" on every Event), and this repo's own CI - # and integration tests run against that same image. Revisit once M4 - # adds real jscalendarbis support, or once older servers matter more - # than this one. - "version": "2.0", + # jscalendarbis (the 2.0 successor spec) adds it, optional, and + # registers "1.0" for data conforming to RFC 8984, "2.0" for + # jscalendarbis. This conversion does not emit any 2.0-only + # properties (COORDINATES, SHOW-WITHOUT-TIME), so it's correctly + # left unset rather than claiming either version: confirmed live + # that Cyrus's current test image rejects "version" outright in + # any value, where an earlier build required "2.0"; Stalwart + # accepts the property either present or absent. Revisit once M4 + # adds real jscalendarbis support and this conversion's output + # actually depends on which version applies. "uid": uid, "title": title, "start": start, @@ -817,16 +828,22 @@ def ical_to_jscal(ical_str: str, calendar_id: str | None = None) -> dict: jscal["alerts"] = alerts # RFC 8984 ยง4.3.3 defines "recurrenceRules" as RecurrenceRule[], since an - # event can in principle have more than one RRULE. Neither test server - # this repo targets actually implements that: Cyrus (pinned prodId - # "CyrusIMAP.org/Cyrus 3.13.7-545-gc04cece19") rejects the array outright - # with invalidProperties, and Stalwart's own CalendarEvent/get also - # returns the older singular "recurrenceRule" key, not the array, - # confirmed live against both. We follow both servers here, same as the - # "version" and calendarAddress/sendTo fixes: only the first RRULE is - # kept if more than one is present, which is a real but rare data-loss - # case (multiple RRULE lines on one VEVENT are uncommon in practice), so - # it's logged rather than silent or raised. + # event can in principle have more than one RRULE. Only the first RRULE + # is kept if more than one is present, which is a real but rare + # data-loss case (multiple RRULE lines on one VEVENT are uncommon in + # practice), so it's logged rather than silent or raised. + # + # Emitted here under the singular "recurrenceRule" key, not the plural + # array: which spelling a given server actually accepts on + # CalendarEvent/set is not consistent even between the two test servers + # this project targets (confirmed live: Cyrus's current image requires + # the plural array and rejects the singular, Stalwart requires the + # singular and rejects the plural, neither accepts both spellings at + # once), so the client layer (_JMAPClientBase._try_recurrence_rule_fallback_key) + # detects a single-property invalidProperties rejection naming whichever + # key this function emits and retries once with the other spelling, + # rather than this conversion trying to guess a target server's dialect + # with no information to guess from. # Full recurrence override fidelity is its own separate M4 milestone item. rule = _first_recurrence_rule(master, "RRULE", event_tzinfo) if rule is not None: diff --git a/src/calendaring_jmap/objects/attachment.py b/src/calendaring_jmap/objects/attachment.py index 361d2b1..7c7dc79 100644 --- a/src/calendaring_jmap/objects/attachment.py +++ b/src/calendaring_jmap/objects/attachment.py @@ -62,13 +62,5 @@ def is_attachment(link_data: dict) -> bool: A Link is an attachment when its ``rel`` is ``"enclosure"`` (:rfc:`8984#section-1.4.11`); other ``links`` entries (e.g. a conference URL) are not. - - Known Cyrus limitation, confirmed live: Cyrus (3.13.7-545-gc04cece19) - accepts a Link with ``rel: "enclosure"`` with no error, but does not - reliably persist it (other ``rel`` values round-trip fine, isolating - this to that one value). An attachment written by - :meth:`~calendaring_jmap.client.JMAPClient.attach_to_event` may - therefore not be classified as one by this method when read back - from Cyrus. Stalwart has no such issue. """ return link_data.get("rel") == LINK_REL_ENCLOSURE diff --git a/src/calendaring_jmap/tests/test_jmap_integration.py b/src/calendaring_jmap/tests/test_jmap_integration.py index 181cf24..ace9454 100644 --- a/src/calendaring_jmap/tests/test_jmap_integration.py +++ b/src/calendaring_jmap/tests/test_jmap_integration.py @@ -1126,9 +1126,15 @@ def test_get_objects_by_sync_token_pages_with_max_changes( event_client.delete_event(event_id) def test_recurring_event_create_get_update(self, event_client, event_calendar_id, server): - """Neither test server accepts RFC 8984's recurrenceRules array; both - want the singular recurrenceRule instead (see ical_to_jscal.py). This - is the only integration coverage for recurring events at all.""" + """Which of RFC 8984's recurrenceRule (singular)/recurrenceRules + (plural array) spelling a server actually accepts on + CalendarEvent/set, and returns on CalendarEvent/get, is not + consistent between the two test servers this project targets or + stable over time for a given server (see + _JMAPClientBase._try_recurrence_rule_fallback_key in client.py), so + this asserts the round-trip behavior that must hold regardless of + spelling, not a specific key name. This is the only integration + coverage for recurring events at all.""" ical = _recurring_ical( "FREQ=WEEKLY;BYDAY=MO,WE,FR", title="Weekly Standup", @@ -1139,15 +1145,14 @@ def test_recurring_event_create_get_update(self, event_client, event_calendar_id ) try: obj = event_client.get_event(event_id) - assert "recurrenceRule" in obj.get_data(), ( + assert "RRULE" in jscal_to_ical(obj.get_data()), ( f"{server}: created event lost its recurrence rule on read-back." ) - assert "RRULE" in jscal_to_ical(obj.get_data()) updated_ical = ical.replace("Weekly Standup", "Weekly Standup Renamed") event_client.update_event(event_id, ical_to_jscal(updated_ical)) updated = event_client.get_event(event_id) - assert "recurrenceRule" in updated.get_data(), ( + assert "RRULE" in jscal_to_ical(updated.get_data()), ( f"{server}: recurrence rule was lost after an unrelated update." ) assert "Weekly Standup Renamed" in jscal_to_ical(updated.get_data()) @@ -1175,7 +1180,15 @@ def test_jscalendar_write_back_unchanged_round_trips(self, event_client, event_c call, including a no-op write, matching draft-ietf-jmap-calendars section 5.9's "the server sets updated to the current time on every create or update," not only on a change that alters - something.""" + something. + + ``sequence`` is excluded too: confirmed live that Cyrus also bumps + it on this same no-op write, even though RFC 8984 section 4.1.7 + requires it to increment only "every time a change is made to the + object," which a byte-for-byte unchanged write-back is not. This is + a real Cyrus spec violation, not a bug in this client; Stalwart + leaves sequence unchanged here, confirmed live. + """ event_id, _, _ = event_client.create_event( event_calendar_id, _minimal_event("Write Back Unchanged Event", calendar_id=event_calendar_id), @@ -1184,18 +1197,16 @@ def test_jscalendar_write_back_unchanged_round_trips(self, event_client, event_c original = event_client.get_event(event_id).get_data() event_client.update_event(event_id, original) refetched = event_client.get_event(event_id).get_data() - assert {k: v for k, v in refetched.items() if k != "updated"} == { - k: v for k, v in original.items() if k != "updated" + excluded = {"updated", "sequence"} + assert {k: v for k, v in refetched.items() if k not in excluded} == { + k: v for k, v in original.items() if k not in excluded } finally: event_client.delete_event(event_id) def test_attach_property_roundtrip(self, event_client, event_calendar_id, server): """ical_to_jscal/jscal_to_ical convert ATTACH <-> links (rel: - "enclosure"). On Cyrus, the fetched event's ATTACH is lost on the - way back out, since it doesn't persist rel (see - JMAPAttachment.is_attachment); Stalwart round-trips correctly. - """ + "enclosure").""" ical = _vevent_ical( "Attach Roundtrip Event", datetime(2026, 7, 20, 9, 0, 0, tzinfo=timezone.utc), @@ -1207,15 +1218,9 @@ def test_attach_property_roundtrip(self, event_client, event_calendar_id, server ) try: fetched = jscal_to_ical(event_client.get_event(event_id).get_data()) - if server == "cyrus": - assert "ATTACH" not in fetched, ( - "cyrus: if this now finds ATTACH, Cyrus may have fixed " - "the rel persistence bug; update this test accordingly." - ) - else: - assert "ATTACH" in fetched - assert "https://example.com/report.pdf" in fetched - assert "application/pdf" in fetched + assert "ATTACH" in fetched + assert "https://example.com/report.pdf" in fetched + assert "application/pdf" in fetched finally: event_client.delete_event(event_id) @@ -1242,12 +1247,12 @@ def test_url_property_roundtrip(self, event_client, event_calendar_id): def test_virtual_location_description_roundtrip(self, event_client, event_calendar_id, server): """VirtualLocation.description (via a VCONFERENCE correlated to its - sibling CONFERENCE by URI) round-trips on Stalwart. Cyrus, tested - directly, rejects description on a VirtualLocation outright with - invalidProperties, on both create and update, independent of - descriptionContentType or VCONFERENCE entirely; this is a real - Cyrus limitation on a valid RFC 8984 property, not a bug in this - conversion. + sibling CONFERENCE by URI) round-trips on both Cyrus and Stalwart. + + Cyrus still drops a top-level VirtualLocation's own features and + descriptionContentType on read-back, confirmed live, even though it + no longer rejects description outright (an earlier build did). + Stalwart round-trips all three properties correctly. """ ical = _vevent_ical( "VConference Roundtrip Event", @@ -1261,17 +1266,6 @@ def test_virtual_location_description_roundtrip(self, event_client, event_calend "END:VCONFERENCE\r\n" ), ) - if server == "cyrus": - with pytest.raises(JMAPMethodError) as exc_info: - event_client.create_event( - event_calendar_id, ical_to_jscal(ical, calendar_id=event_calendar_id) - ) - assert exc_info.value.error_type == "invalidProperties", ( - "cyrus: if this now accepts VirtualLocation.description, " - "Cyrus may have fixed this limitation; update this test " - "accordingly." - ) - return event_id, _, _ = event_client.create_event( event_calendar_id, ical_to_jscal(ical, calendar_id=event_calendar_id) ) @@ -1279,17 +1273,38 @@ def test_virtual_location_description_roundtrip(self, event_client, event_calend data = event_client.get_event(event_id).get_data() vloc = next(iter(data["virtualLocations"].values())) assert vloc["description"] == "Join here" - assert vloc["descriptionContentType"] == "text/html" + if server == "cyrus": + assert "descriptionContentType" not in vloc, ( + f"{server}: VirtualLocation kept its descriptionContentType; " + "if this now holds, Cyrus may have fixed this limitation " + "and this assertion should be updated." + ) + assert "features" not in vloc, ( + f"{server}: VirtualLocation kept its features map; if " + "this now holds, Cyrus may have fixed this limitation " + "and this assertion should be updated." + ) + else: + assert vloc["descriptionContentType"] == "text/html" + assert vloc["features"] == {"audio": True} finally: event_client.delete_event(event_id) def test_recurrence_override_combines_multiple_property_changes( - self, event_client, event_calendar_id + self, event_client, event_calendar_id, server ): """A single override instance changing status, free/busy status, keywords, location, and virtualLocations together round-trips through CalendarEvent/set and CalendarEvent/get with every change preserved. + + Cyrus drops a VirtualLocation's features map specifically inside a + recurrenceOverrides patch (confirmed live: the uri survives, + features does not), even though the same features map round-trips + fine on a top-level, non-override virtualLocation (see + test_virtual_location_description_roundtrip and this converter's + own pure client-side round trip, which keeps FEATURE intact with + no server involved). Stalwart has no such gap. """ master_start = datetime(2026, 8, 3, 9, 0, 0, tzinfo=timezone.utc) override_start = master_start + timedelta(weeks=1) @@ -1328,20 +1343,32 @@ def test_recurrence_override_combines_multiple_property_changes( assert "URGENT" in child.upper() and "ESCALATED" in child.upper() assert "LOCATION:Room 200" in child assert "CONFERENCE" in child - assert "PHONE" in child + if server == "cyrus": + assert "FEATURE" not in child, ( + f"{server}: override VirtualLocation kept its FEATURE " + "parameter; if this now holds, Cyrus may have fixed the " + "features-drop limitation on override VirtualLocations " + "and this assertion should be updated." + ) + else: + assert "PHONE" in child finally: event_client.delete_event(event_id) def test_recurrence_override_participant_and_alert_server_quirks( self, event_client, event_calendar_id, server ): - """The same attendee-role-drop and alert-action-drop quirks already - known for top-level participants/alerts (see _invite_ical's own - docstring, and the Alert action default in - jscal_to_ical._alert_to_valarm) reproduce identically inside a - recurrenceOverrides patch on both Cyrus and Stalwart. Pinned here so - a future server fix is caught rather than silently going - unnoticed. + """The alert-action-drop quirk already known for top-level alerts + (see the Alert action default in jscal_to_ical._alert_to_valarm) + reproduces identically inside a recurrenceOverrides patch on both + Cyrus and Stalwart. Pinned here so a future server fix is caught + rather than silently going unnoticed. + + The matching attendee-role-drop quirk (#57) no longer reproduces on + Cyrus's current image, confirmed live: an override participant now + keeps its attendee role on read-back, where an earlier build + dropped it. Stalwart's own behavior here is untouched by this + change and still covered below. The master VEVENT here has no ORGANIZER/ATTENDEE/VALARM of its own, so Cyrus returns this patch's participants/alerts as literal @@ -1381,12 +1408,19 @@ def test_recurrence_override_participant_and_alert_server_quirks( p for p in participants.values() if p.get("email") == "attendee@example.com" ) roles = attendee.get("roles") or {} - assert "attendee" not in roles, ( - f"{server}: override participant kept its attendee role; " - "if this now holds, the server may have fixed the " - "long-standing roles-drop quirk (see #57) and this " - "assertion should be updated." - ) + if server == "stalwart": + assert "attendee" not in roles, ( + f"{server}: override participant kept its attendee role; " + "if this now holds, the server may have fixed the " + "long-standing roles-drop quirk (see #57) and this " + "assertion should be updated." + ) + else: + assert roles.get("attendee") is True, ( + f"{server}: override participant lost its attendee role again; " + "if this now holds, the #57 roles-drop quirk may have " + "returned and this assertion should be updated." + ) alerts = patch.get("alerts") or {} assert alerts, "alert was dropped entirely from the override patch" @@ -1698,9 +1732,6 @@ def test_attach_to_event_persists_href(self, event_client, event_calendar_id, se event_client.delete_event(event_id) def test_get_event_attachments_finds_attachment(self, event_client, event_calendar_id, server): - """Cyrus doesn't reliably persist rel: "enclosure" (see - JMAPAttachment.is_attachment), so it finds nothing here even though - attach_to_event succeeded; Stalwart finds the attachment.""" data = b"attachment content for get_event_attachments test" event_id, _, _ = event_client.create_event( event_calendar_id, @@ -1710,17 +1741,10 @@ def test_get_event_attachments_finds_attachment(self, event_client, event_calend blob_id = event_client.upload_attachment(data, "application/octet-stream") event_client.attach_to_event(event_id, blob_id, "test.bin", "application/octet-stream") attachments = event_client.get_event_attachments(event_id) - if server == "cyrus": - assert attachments == [], ( - "cyrus: if this now finds the attachment, Cyrus may have " - "fixed the rel persistence bug; update this test and " - "JMAPAttachment.is_attachment's docstring accordingly." - ) - else: - assert len(attachments) == 1, f"{server}: expected exactly one attachment" - assert attachments[0].title == "test.bin" - assert attachments[0].content_type == "application/octet-stream" - assert attachments[0].href + assert len(attachments) == 1, f"{server}: expected exactly one attachment" + assert attachments[0].title == "test.bin" + assert attachments[0].content_type == "application/octet-stream" + assert attachments[0].href finally: event_client.delete_event(event_id) @@ -1743,7 +1767,7 @@ async def test_upload_download_roundtrip(self, async_client): @pytest.mark.asyncio async def test_attach_to_event_persists_href(self, async_client, async_calendar_id): """Cyrus-only class; asserts on the raw links data rather than - get_event_attachments, see test_get_event_attachments_empty_on_cyrus_due_to_rel_bug.""" + get_event_attachments, see test_get_event_attachments_finds_attachment.""" data = b"async binary content for attachment test" event_id, _, _ = await async_client.create_event( async_calendar_id, @@ -1769,9 +1793,7 @@ async def test_attach_to_event_persists_href(self, async_client, async_calendar_ await async_client.delete_event(event_id) @pytest.mark.asyncio - async def test_get_event_attachments_empty_on_cyrus_due_to_rel_bug( - self, async_client, async_calendar_id - ): + async def test_get_event_attachments_finds_attachment(self, async_client, async_calendar_id): """See TestAttachmentIntegration.test_get_event_attachments_finds_attachment.""" event_id, _, _ = await async_client.create_event( async_calendar_id, @@ -1782,7 +1804,9 @@ async def test_get_event_attachments_empty_on_cyrus_due_to_rel_bug( await async_client.attach_to_event( event_id, blob_id, "test.bin", "application/octet-stream" ) - assert await async_client.get_event_attachments(event_id) == [] + attachments = await async_client.get_event_attachments(event_id) + assert len(attachments) == 1 + assert attachments[0].title == "test.bin" finally: await async_client.delete_event(event_id) diff --git a/src/calendaring_jmap/tests/test_jmap_unit.py b/src/calendaring_jmap/tests/test_jmap_unit.py index 8b0603c..8be22d7 100644 --- a/src/calendaring_jmap/tests/test_jmap_unit.py +++ b/src/calendaring_jmap/tests/test_jmap_unit.py @@ -1479,6 +1479,26 @@ def test_parse_address_book_get_returns_address_books(self): def test_parse_address_book_get_missing_list_key(self): assert parse_address_book_get({}) == [] + def test_parse_address_book_get_infers_default_for_sole_address_book(self): + # RFC 9610 section 2 requires isDefault to be true for exactly one + # AddressBook; confirmed live that Cyrus omits the key entirely + # rather than sending it false, so the account's only address book + # must still end up marked default even though the server never said so. + response_args = {"list": [_ADDRESS_BOOK_JSON_MINIMAL]} + books = parse_address_book_get(response_args) + assert len(books) == 1 + assert books[0].is_default is True + + def test_parse_address_book_get_does_not_override_explicit_default(self): + response_args = {"list": [dict(_ADDRESS_BOOK_JSON_MINIMAL, isDefault=True)]} + books = parse_address_book_get(response_args) + assert books[0].is_default is True + + def test_parse_address_book_get_does_not_infer_default_with_multiple_books(self): + response_args = {"list": [_ADDRESS_BOOK_JSON_FULL, _ADDRESS_BOOK_JSON_MINIMAL]} + books = parse_address_book_get(response_args) + assert books[1].is_default is False + def test_build_contact_query_structure(self): method, args, call_id = build_contact_query("u1") assert method == "ContactCard/query" @@ -1839,6 +1859,33 @@ def test_parse_event_changes_response_without_match_returns_empty_defaults(self) result = _JMAPClientBase._parse_event_changes_response([]) assert result == ([], [], [], "", "", False) + def test_find_participant_id_by_email_raises_not_found_for_none_participants(self): + # An event with no "participants" property at all (RFC 8984 section + # 4.4.6: optional) can come back with the key present and explicitly + # null, not merely absent, so a plain dict.get(key, {}) default does + # not help; this must still raise notFound, not crash on None.items(). + with pytest.raises(JMAPMethodError) as exc_info: + _JMAPClientBase._find_participant_id_by_email( + None, "nobody@example.invalid", _API_URL, "ev1" + ) + assert exc_info.value.error_type == "notFound" + + def test_find_participant_id_by_email_raises_not_found_for_empty_participants(self): + with pytest.raises(JMAPMethodError) as exc_info: + _JMAPClientBase._find_participant_id_by_email( + {}, "nobody@example.invalid", _API_URL, "ev1" + ) + assert exc_info.value.error_type == "notFound" + + def test_find_participant_id_by_email_finds_matching_participant(self): + participants = {"p1": {"email": "alice@example.com"}} + assert ( + _JMAPClientBase._find_participant_id_by_email( + participants, "alice@example.com", _API_URL, "ev1" + ) + == "p1" + ) + def test_unsupported_null_keys_returns_none_without_match(self): assert ( _JMAPClientBase._unsupported_null_keys( @@ -1863,6 +1910,170 @@ def test_unsupported_null_keys_skips_unrelated_responses_in_batch(self): is None ) + def test_unsupported_null_keys_drops_null_cleanup_alongside_recurrence_rule_mismatch(self): + # A server can reject both a stale null-cleanup key and the wrong + # recurrenceRule/recurrenceRules spelling in the same response + # (confirmed live against Cyrus's current image): the null-cleanup + # key is still droppable even though the whole response isn't a + # pure null-cleanup case, leaving the recurrenceRule mismatch for + # _retry_recurrence_rule_update to handle on the next retry. + matching = ( + "CalendarEvent/set", + { + "notUpdated": { + "ev1": { + "type": "invalidProperties", + "properties": ["recurrenceRule", "excludedRecurrenceRule"], + } + } + }, + "c1", + ) + drop = _JMAPClientBase._unsupported_null_keys( + [matching], + "ev1", + patch={"recurrenceRule": {"frequency": "daily"}, "excludedRecurrenceRule": None}, + nulled=frozenset({"excludedRecurrenceRule"}), + ) + assert drop == {"excludedRecurrenceRule"} + + def test_unsupported_null_keys_returns_none_for_unexplained_property_mixed_in(self): + # A genuine offending property that is neither null-cleanup nor a + # recurrenceRule/recurrenceRules spelling mismatch must still + # surface as a real error, even alongside one that is. + matching = ( + "CalendarEvent/set", + { + "notUpdated": { + "ev1": { + "type": "invalidProperties", + "properties": ["recurrenceRule", "title"], + } + } + }, + "c1", + ) + drop = _JMAPClientBase._unsupported_null_keys( + [matching], + "ev1", + patch={"recurrenceRule": {"frequency": "daily"}, "title": "x"}, + nulled=frozenset(), + ) + assert drop is None + + def test_recurrence_rule_fallback_key_returns_false_without_error(self): + assert _JMAPClientBase._try_recurrence_rule_fallback_key(None, {}, tried=set()) is False + + def test_recurrence_rule_fallback_key_returns_false_for_unrelated_error_type(self): + err = {"type": "stateMismatch"} + jscal = {"recurrenceRule": {}} + assert _JMAPClientBase._try_recurrence_rule_fallback_key(err, jscal, tried=set()) is False + assert jscal == {"recurrenceRule": {}} + + def test_recurrence_rule_fallback_key_returns_false_for_multiple_offending_properties(self): + err = {"type": "invalidProperties", "properties": ["recurrenceRule", "title"]} + jscal = {"recurrenceRule": {}, "title": "x"} + assert _JMAPClientBase._try_recurrence_rule_fallback_key(err, jscal, tried=set()) is False + assert jscal == {"recurrenceRule": {}, "title": "x"} + + def test_recurrence_rule_fallback_key_returns_false_for_unrelated_property(self): + err = {"type": "invalidProperties", "properties": ["title"]} + jscal = {"recurrenceRule": {}, "title": "x"} + assert _JMAPClientBase._try_recurrence_rule_fallback_key(err, jscal, tried=set()) is False + assert jscal == {"recurrenceRule": {}, "title": "x"} + + def test_recurrence_rule_fallback_key_returns_false_when_key_not_in_jscal(self): + err = {"type": "invalidProperties", "properties": ["recurrenceRules"]} + jscal = {"recurrenceRule": {}} + assert _JMAPClientBase._try_recurrence_rule_fallback_key(err, jscal, tried=set()) is False + assert jscal == {"recurrenceRule": {}} + + def test_recurrence_rule_fallback_key_swaps_singular_to_plural_wrapping_in_list(self): + # "recurrenceRule" (singular) is one RecurrenceRule object; + # "recurrenceRules" (plural) is RecurrenceRule[] (RFC 8984 section + # 4.3.3). The swap must wrap the object in a one-element list, not + # just rename the key and send an object where an array belongs. + err = {"type": "invalidProperties", "properties": ["recurrenceRule"]} + jscal = {"recurrenceRule": {"frequency": "daily"}} + tried: set[str] = set() + assert _JMAPClientBase._try_recurrence_rule_fallback_key(err, jscal, tried) is True + assert jscal == {"recurrenceRules": [{"frequency": "daily"}]} + assert tried == {"recurrenceRules"} + + def test_recurrence_rule_fallback_key_swaps_plural_to_singular_unwrapping_list(self): + err = {"type": "invalidProperties", "properties": ["recurrenceRules"]} + jscal = {"recurrenceRules": [{"frequency": "daily"}]} + tried: set[str] = set() + assert _JMAPClientBase._try_recurrence_rule_fallback_key(err, jscal, tried) is True + assert jscal == {"recurrenceRule": {"frequency": "daily"}} + assert tried == {"recurrenceRule"} + + def test_recurrence_rule_fallback_key_keeps_first_rule_when_unwrapping_multiple(self, caplog): + # ical_to_jscal itself never emits more than one rule, but a caller + # building its own CalendarEvent dict directly could; unwrapping to + # the singular spelling can only carry one, so the first is kept + # and the rest logged rather than silently dropped with no trace. + err = {"type": "invalidProperties", "properties": ["recurrenceRules"]} + jscal = {"recurrenceRules": [{"frequency": "daily"}, {"frequency": "weekly"}]} + tried: set[str] = set() + with caplog.at_level("WARNING", logger="calendaring_jmap"): + assert _JMAPClientBase._try_recurrence_rule_fallback_key(err, jscal, tried) is True + assert jscal == {"recurrenceRule": {"frequency": "daily"}} + assert "only the first is kept" in caplog.text + + def test_recurrence_rule_fallback_key_returns_false_for_empty_list(self): + # An empty recurrenceRules array has nothing to unwrap to a single + # RecurrenceRule object; must leave jscal untouched rather than + # crashing on an out-of-range index. + err = {"type": "invalidProperties", "properties": ["recurrenceRules"]} + jscal = {"recurrenceRules": []} + tried: set[str] = set() + assert _JMAPClientBase._try_recurrence_rule_fallback_key(err, jscal, tried) is False + assert jscal == {"recurrenceRules": []} + assert tried == set() + + def test_recurrence_rule_fallback_key_does_not_retry_once_a_swap_already_happened(self): + """A server that somehow rejects both spellings in turn (never + observed live, but not provable impossible from a single response) + gets exactly one swap total, not an alternating retry forever: + since there are only two possible spellings, a non-empty ``tried`` + means every spelling has already been sent once.""" + err = {"type": "invalidProperties", "properties": ["recurrenceRules"]} + jscal = {"recurrenceRules": [{"frequency": "daily"}]} + tried = {"recurrenceRules"} + assert _JMAPClientBase._try_recurrence_rule_fallback_key(err, jscal, tried) is False + assert jscal == {"recurrenceRules": [{"frequency": "daily"}]} + + def test_parse_event_query_response_returns_default_without_match(self): + assert _JMAPClientBase._parse_event_query_response([_UNRELATED_RESPONSE]) == (0, None) + + def test_parse_set_state_returns_empty_strings_without_match(self): + assert _JMAPClientBase._parse_set_state([_UNRELATED_RESPONSE], "CalendarEvent/set") == ( + "", + "", + ) + + def test_recurrence_rule_set_error_returns_none_without_match(self): + assert _JMAPClientBase._recurrence_rule_set_error([_UNRELATED_RESPONSE]) is None + + def test_retry_recurrence_rule_create_returns_false_without_match(self): + jscal = {"recurrenceRule": {"frequency": "daily"}} + assert ( + _JMAPClientBase._retry_recurrence_rule_create([_UNRELATED_RESPONSE], jscal, set()) + is False + ) + assert jscal == {"recurrenceRule": {"frequency": "daily"}} + + def test_retry_recurrence_rule_update_returns_false_without_match(self): + patch = {"recurrenceRule": {"frequency": "daily"}} + assert ( + _JMAPClientBase._retry_recurrence_rule_update( + [_UNRELATED_RESPONSE], "ev1", patch, set() + ) + is False + ) + assert patch == {"recurrenceRule": {"frequency": "daily"}} + def test_parse_get_calendars_skips_unrelated_responses_in_batch(self): assert ( _JMAPClientBase._parse_get_calendars([_UNRELATED_RESPONSE], client=None, is_async=False) @@ -2731,16 +2942,15 @@ def test_minimal_event(self): assert result["timeZone"] == "Etc/UTC" assert result["duration"] == "PT1H" - def test_version_is_2_0(self): - """RFC 8984 has no version property; jscalendarbis adds it and - registers "1.0" for RFC 8984-conformant data, "2.0" for its own. - This converter's output is actually RFC 8984-shaped ("1.0" would be - strictly accurate), but it sends "2.0" since the test servers this - project runs against require it. See ical_to_jscal's inline comment - for the full reasoning.""" + def test_version_is_not_set(self): + """RFC 8984 has no version property; jscalendarbis adds it, + optional, for its own 2.0-only properties this converter doesn't + emit. Left unset rather than claiming either version. See + ical_to_jscal's inline comment for the full reasoning, including + why a hardcoded "2.0" broke against a real server.""" ical = _make_ical("DTSTART:20240615T100000Z\r\nDURATION:PT1H\r\nSUMMARY:Test Event\r\n") result = ical_to_jscal(ical) - assert result["version"] == "2.0" + assert "version" not in result def test_all_day_event(self): ical = _make_ical( @@ -3126,10 +3336,10 @@ def test_attendee_partstat(self): attendee = next(iter(result["participants"].values())) assert attendee["participationStatus"] == "declined" - def test_organizer_attendee_use_calendar_address_not_send_to(self): - # Cyrus rejects CalendarEvent/set outright if any Participant has a - # sendTo property, and separately requires calendarAddress, which - # RFC 8984 does not define here at all. Verified live against Cyrus. + def test_organizer_attendee_set_both_calendar_address_and_send_to(self): + # Cyrus's current image requires every participant to carry sendTo, + # rejecting its absence with invalidProperties; Stalwart accepts it + # present or absent either way. Verified live against both. ical = _make_ical( "DTSTART:20240615T100000Z\r\n" "SUMMARY:Meeting\r\n" @@ -3138,13 +3348,15 @@ def test_organizer_attendee_use_calendar_address_not_send_to(self): ) result = ical_to_jscal(ical) for participant in result["participants"].values(): - assert "sendTo" not in participant assert participant["calendarAddress"] == f"mailto:{participant['email']}" + assert participant["sendTo"] == {"imip": f"mailto:{participant['email']}"} def test_non_mailto_attendee_keeps_calendar_address_without_fake_email(self): # CAL-ADDRESS is a URI (RFC 5545 3.3.3), not required to use # mailto:. RFC 8984's Participant.email is an RFC 5322 addr-spec, - # so a non-mailto URI must not be written there. + # so a non-mailto URI must not be written there. sendTo's "imip" + # method specifically requires a mailto: URI (RFC 8984 section + # 4.4.6), so a non-mailto address goes under "other" instead. ical = _make_ical( "DTSTART:20240615T100000Z\r\nSUMMARY:Meeting\r\nATTENDEE:sip:alice@example.com\r\n" ) @@ -3152,6 +3364,7 @@ def test_non_mailto_attendee_keeps_calendar_address_without_fake_email(self): attendee = next(iter(result["participants"].values())) assert attendee["calendarAddress"] == "sip:alice@example.com" assert "email" not in attendee + assert attendee["sendTo"] == {"other": "sip:alice@example.com"} def test_bare_attendee_address_without_scheme_is_treated_as_email(self): # CAL-ADDRESS is technically required to be a URI, but some @@ -5829,8 +6042,9 @@ def test_update_event_nulls_removed_optional_properties(self, monkeypatch): def _sequence_client(self, responses): """Return (client, captured) replaying ``responses`` POST-by-POST. - ``captured["patches"]`` collects the ``update`` patch dict sent on each - CalendarEvent/set POST, in order. + ``captured["patches"]`` collects the ``update`` patch dict (keyed + ``"ev1"``) or ``create`` dict (keyed ``"new-0"``) sent on each + CalendarEvent/set POST, in order, whichever the call under test uses. """ captured: dict = {"patches": []} client = JMAPClient(url=_JMAP_URL, username=_USERNAME, password=_PASSWORD) @@ -5839,9 +6053,12 @@ def _sequence_client(self, responses): def post(*args, **kwargs): body = kwargs.get("json", {}) - update = body["methodCalls"][0][1].get("update") - if update: - captured["patches"].append(dict(update["ev1"])) # copy: caller mutates in place + args_dict = body["methodCalls"][0][1] + jscal = (args_dict.get("update") or {}).get("ev1") or ( + args_dict.get("create") or {} + ).get("new-0") + if jscal is not None: + captured["patches"].append(dict(jscal)) # copy: caller mutates in place mock_resp = MagicMock() mock_resp.status_code = 200 mock_resp.json.return_value = next(seq) @@ -5883,6 +6100,72 @@ def reject(prop): assert "recurrenceRule" not in captured["patches"][2] assert "excludedRecurrenceRule" not in captured["patches"][2] + def test_update_event_retries_swapping_recurrence_rule_spelling(self, monkeypatch): + # A server (e.g. Cyrus's current image) rejects whichever + # recurrenceRule/recurrenceRules spelling the client sent; + # update_event must swap to the other spelling and retry once. + reject_singular = self._set_response( + notUpdated={"ev1": {"type": "invalidProperties", "properties": ["recurrenceRule"]}} + ) + responses = [reject_singular, self._set_response(updated={"ev1": None})] + client, captured = self._sequence_client(responses) + event = dict(self._MINIMAL_EVENT, recurrenceRule={"frequency": "daily"}) + + client.update_event("ev1", event) + + assert len(captured["patches"]) == 2 + assert captured["patches"][0]["recurrenceRule"] == {"frequency": "daily"} + assert captured["patches"][1]["recurrenceRules"] == [{"frequency": "daily"}] + assert "recurrenceRule" not in captured["patches"][1] + + def test_update_event_drops_null_key_and_swaps_recurrence_rule_in_one_response( + self, monkeypatch + ): + # A server can reject a stale null-cleanup key (excludedRecurrenceRule, + # absent from the new iCalendar) alongside the wrong recurrenceRule + # spelling in the very same response (confirmed live against Cyrus's + # current image); update_event must drop the former and swap the + # latter, not treat the mix as one unrecoverable genuine failure. + reject_both = self._set_response( + notUpdated={ + "ev1": { + "type": "invalidProperties", + "properties": ["recurrenceRule", "excludedRecurrenceRule"], + } + } + ) + reject_singular_only = self._set_response( + notUpdated={"ev1": {"type": "invalidProperties", "properties": ["recurrenceRule"]}} + ) + responses = [reject_both, reject_singular_only, self._set_response(updated={"ev1": None})] + client, captured = self._sequence_client(responses) + event = dict(self._MINIMAL_EVENT, recurrenceRule={"frequency": "daily"}) + + client.update_event("ev1", event) + + assert len(captured["patches"]) == 3 + assert captured["patches"][0]["recurrenceRule"] == {"frequency": "daily"} + assert captured["patches"][0]["excludedRecurrenceRule"] is None + assert "excludedRecurrenceRule" not in captured["patches"][1] + assert captured["patches"][1]["recurrenceRule"] == {"frequency": "daily"} + assert captured["patches"][2]["recurrenceRules"] == [{"frequency": "daily"}] + + def test_create_event_retries_swapping_recurrence_rule_spelling(self, monkeypatch): + reject_plural = self._set_response( + notCreated={"new-0": {"type": "invalidProperties", "properties": ["recurrenceRules"]}} + ) + responses = [reject_plural, self._set_response(created={"new-0": {"id": "sv-1"}})] + client, captured = self._sequence_client(responses) + event = dict(self._MINIMAL_EVENT, recurrenceRules=[{"frequency": "daily"}]) + + event_id, _, _ = client.create_event("cal1", event) + + assert event_id == "sv-1" + assert len(captured["patches"]) == 2 + assert captured["patches"][0]["recurrenceRules"] == [{"frequency": "daily"}] + assert captured["patches"][1]["recurrenceRule"] == {"frequency": "daily"} + assert "recurrenceRules" not in captured["patches"][1] + def test_update_event_does_not_drop_explicitly_set_property(self, monkeypatch): # If the rejected property was actually assigned a value by the client # (not null-cleanup), the rejection is genuine and must surface, no retry. @@ -6077,6 +6360,18 @@ def test_find_own_participant_id_raises_when_no_match(self, monkeypatch): client._find_own_participant_id("ev1", "stranger@example.com") assert exc_info.value.error_type == "notFound" + def test_find_own_participant_id_raises_not_found_for_event_with_no_participants( + self, monkeypatch + ): + # An event with no participants at all comes back with the key + # present and explicitly null, not merely absent (RFC 8984 section + # 4.4.6: optional); must still raise notFound, not crash. + resp = self._get_response([dict(self._RAW_EVENT, participants=None)]) + client = _make_client_with_mocked_session(monkeypatch, resp) + with pytest.raises(JMAPMethodError) as exc_info: + client._find_own_participant_id("ev1", "nobody@example.invalid") + assert exc_info.value.error_type == "notFound" + def test_find_own_participant_id_falls_back_to_calendar_address(self, monkeypatch): # A participant with no "email" (a spec-compliant server, or one set # by another client) must still be matchable via calendarAddress. @@ -7286,6 +7581,13 @@ def test_update_calendar_only_patches_given_fields(self, monkeypatch): update_args = captured["json"]["methodCalls"][0][1] assert update_args["update"]["cal1"] == {"color": "#ff0000"} + def test_update_calendar_patches_timezone(self, monkeypatch): + resp = self._set_response(updated={"cal1": None}) + client, captured = self._capturing_client(monkeypatch, resp) + client.update_calendar("cal1", timezone="America/New_York") + update_args = captured["json"]["methodCalls"][0][1] + assert update_args["update"]["cal1"] == {"timeZone": "America/New_York"} + def test_delete_calendar_success(self, monkeypatch): resp = self._set_response(destroyed=["cal1"]) client = _make_client_with_mocked_session(monkeypatch, resp) @@ -8673,6 +8975,18 @@ async def test_find_own_participant_id_raises_when_no_match(self, monkeypatch): await self._make_client()._find_own_participant_id("ev-async-1", "stranger@example.com") assert exc_info.value.error_type == "notFound" + @pytest.mark.asyncio + async def test_find_own_participant_id_raises_not_found_for_event_with_no_participants( + self, monkeypatch + ): + resp = self._event_get_resp([dict(self._RAW_EVENT, participants=None)]) + self._patch_async_session(monkeypatch, resp) + with pytest.raises(JMAPMethodError) as exc_info: + await self._make_client()._find_own_participant_id( + "ev-async-1", "nobody@example.invalid" + ) + assert exc_info.value.error_type == "notFound" + @pytest.mark.asyncio async def test_find_own_participant_id_falls_back_to_calendar_address(self, monkeypatch): resp = self._event_get_resp( @@ -10448,6 +10762,115 @@ async def post(*args, **kwargs): assert patches_seen[0]["recurrenceRule"] is None assert "recurrenceRule" not in patches_seen[1] + @pytest.mark.asyncio + async def test_update_event_retries_swapping_recurrence_rule_spelling(self, monkeypatch): + responses = [ + self._event_set_resp( + notUpdated={ + "ev-async-1": {"type": "invalidProperties", "properties": ["recurrenceRule"]} + } + ), + self._event_set_resp(updated={"ev-async-1": None}), + ] + mock_http = MagicMock() + mock_http.__aenter__ = AsyncMock(return_value=mock_http) + mock_http.__aexit__ = AsyncMock(return_value=None) + patches_seen = [] + + async def post(*args, **kwargs): + body = kwargs.get("json", {}) + update = body["methodCalls"][0][1].get("update") + patches_seen.append(dict(update["ev-async-1"])) + return self._make_mock_response(responses.pop(0)) + + mock_http.post = post + monkeypatch.setattr("calendaring_jmap.async_client.AsyncSession", lambda: mock_http) + event = dict(self._MINIMAL_EVENT, recurrenceRule={"frequency": "daily"}) + await self._make_client().update_event("ev-async-1", event) + + assert len(patches_seen) == 2 + assert patches_seen[0]["recurrenceRule"] == {"frequency": "daily"} + assert patches_seen[1]["recurrenceRules"] == [{"frequency": "daily"}] + assert "recurrenceRule" not in patches_seen[1] + + @pytest.mark.asyncio + async def test_update_event_drops_null_key_and_swaps_recurrence_rule_in_one_response( + self, monkeypatch + ): + # See the sync version of this test in TestJMAPClientEvents for why: + # a server can reject a stale null-cleanup key alongside the wrong + # recurrenceRule spelling in the same response. + responses = [ + self._event_set_resp( + notUpdated={ + "ev-async-1": { + "type": "invalidProperties", + "properties": ["recurrenceRule", "excludedRecurrenceRule"], + } + } + ), + self._event_set_resp( + notUpdated={ + "ev-async-1": {"type": "invalidProperties", "properties": ["recurrenceRule"]} + } + ), + self._event_set_resp(updated={"ev-async-1": None}), + ] + mock_http = MagicMock() + mock_http.__aenter__ = AsyncMock(return_value=mock_http) + mock_http.__aexit__ = AsyncMock(return_value=None) + patches_seen = [] + + async def post(*args, **kwargs): + body = kwargs.get("json", {}) + update = body["methodCalls"][0][1].get("update") + patches_seen.append(dict(update["ev-async-1"])) + return self._make_mock_response(responses.pop(0)) + + mock_http.post = post + monkeypatch.setattr("calendaring_jmap.async_client.AsyncSession", lambda: mock_http) + event = dict(self._MINIMAL_EVENT, recurrenceRule={"frequency": "daily"}) + await self._make_client().update_event("ev-async-1", event) + + assert len(patches_seen) == 3 + assert patches_seen[0]["recurrenceRule"] == {"frequency": "daily"} + assert patches_seen[0]["excludedRecurrenceRule"] is None + assert "excludedRecurrenceRule" not in patches_seen[1] + assert patches_seen[1]["recurrenceRule"] == {"frequency": "daily"} + assert patches_seen[2]["recurrenceRules"] == [{"frequency": "daily"}] + + @pytest.mark.asyncio + async def test_create_event_retries_swapping_recurrence_rule_spelling(self, monkeypatch): + responses = [ + self._event_set_resp( + notCreated={ + "new-0": {"type": "invalidProperties", "properties": ["recurrenceRules"]} + } + ), + self._event_set_resp(created={"new-0": {"id": "sv-async-1"}}), + ] + mock_http = MagicMock() + mock_http.__aenter__ = AsyncMock(return_value=mock_http) + mock_http.__aexit__ = AsyncMock(return_value=None) + patches_seen = [] + + async def post(*args, **kwargs): + body = kwargs.get("json", {}) + create = body["methodCalls"][0][1].get("create") + patches_seen.append(dict(create["new-0"])) + return self._make_mock_response(responses.pop(0)) + + mock_http.post = post + monkeypatch.setattr("calendaring_jmap.async_client.AsyncSession", lambda: mock_http) + event = dict(self._MINIMAL_EVENT, recurrenceRules=[{"frequency": "daily"}]) + event_id, _, _ = await self._make_client().create_event("cal1", event) + + assert event_id == "sv-async-1" + assert len(patches_seen) == 2 + assert patches_seen[0]["recurrenceRules"] == [{"frequency": "daily"}] + assert patches_seen[1]["recurrenceRule"] == {"frequency": "daily"} + assert "recurrenceRules" not in patches_seen[1] + class TestOverrideWithoutStartUsesOccurrenceTime: """RFC 8984 section 4.3.5: override child VEVENT must use occurrence diff --git a/tests/docker/cyrus/docker-compose.yml b/tests/docker/cyrus/docker-compose.yml index 3f747fb..4951e82 100644 --- a/tests/docker/cyrus/docker-compose.yml +++ b/tests/docker/cyrus/docker-compose.yml @@ -6,8 +6,13 @@ services: # No versioned release tags exist for this image, only latest/bookworm/master # (all identical, rebuilt from current Cyrus master). Pinned to a digest # instead of :latest so the JSCalendar/JMAP behavior this repo tests against - # doesn't drift out from under it; bump deliberately when needed. - image: ghcr.io/cyrusimap/cyrus-docker-test-server@sha256:a3c8a451bde22f46a8b06fb6b335370c0ab23960950b84874aed972b64af3997 + # doesn't drift out from under it; bump deliberately when needed. Same + # digest as .github/workflows/integration.yml's own pin, confirmed live + # that these two files had drifted onto different digests with opposite, + # incompatible JSCalendar "version" property requirements (one rejects + # it outright, the other requires it), making a local make test-integration + # run fail against a fix verified correct in CI. Keep both pins in sync. + image: ghcr.io/cyrusimap/cyrus-docker-test-server@sha256:d639a9116691a7a1c875073486c419d60843e5ef8e32e65c5ef56283874dbf2c container_name: cyrus-test ports: - "8802:8080" # HTTP (JMAP)