diff --git a/caldav/calendarobjectresource.py b/caldav/calendarobjectresource.py index 05a628e9..9922260a 100644 --- a/caldav/calendarobjectresource.py +++ b/caldav/calendarobjectresource.py @@ -57,6 +57,10 @@ Precondition = Literal["auto", "etag"] ## The "*" header save(no_overwrite=True) or save(no_create=True) sends Wildcard = Literal["if-none-match", "if-match"] +## How save(no_overwrite=True) or save(no_create=True) checks before its PUT +SaveCheck = Literal["search", "get"] +## save.no-uid-conflict levels at which the server refuses a duplicate UID +UID_CONFLICT_REFUSED = frozenset({"full", "quirk", "ungraceful"}) class _NotGiven(enum.Enum): @@ -1204,34 +1208,53 @@ def _put( precondition: Precondition = "auto", wildcard: Wildcard | None = None, ) -> "None | Coroutine[Any, Any, None]": - ## TODO: quite much overlapping with _async_put, should consolidate ## TODO: this is low-level http-communication - shouldn't it be in the davclient file rather than in calendarobjectresource.py? headers = self._put_headers(precondition, wildcard) if self.is_async_client: - return self._async_put(headers, retry_on_failure, precondition, wildcard) + return self._async_send_put( + str(self.data), headers, retry_on_failure, precondition, wildcard + ) + return self._send_put(self.data, headers, retry_on_failure, precondition, wildcard) + + def _send_put( + self, body, headers, retry_on_failure, precondition: Precondition, wildcard: Wildcard | None + ) -> None: + """The PUT of ``_put()`` and ``_put_raw()``, with what the answer + says about a UID conflict (``_raise_uid_conflict``, + ``_bare_uid_conflict``).""" try: - r = self.client.put(self.url, self.data, headers) + r = self.client.put(str(self.url), body, headers) except error.AuthorizationError as e: self._raise_uid_conflict(e, wildcard is not None) raise - return self._post_put(r, retry_on_failure, precondition, wildcard) + try: + return self._post_put(r, retry_on_failure, precondition, wildcard) + except error.DAVError as e: + if not isinstance(e, error.UidConflictError) and self._bare_uid_conflict( + e.status, precondition, wildcard + ): + self._raise_bare_uid_conflict(e, wildcard, self._get_status(self.url)) + raise - async def _async_put( - self, - headers, - retry_on_failure=True, - precondition: Precondition = "auto", - wildcard: Wildcard | None = None, - ): + async def _async_send_put( + self, body, headers, retry_on_failure, precondition: Precondition, wildcard: Wildcard | None + ) -> None: try: - r = await self.client.put(str(self.url), str(self.data), headers | ICALH) + r = await self.client.put(str(self.url), body, headers) except error.AuthorizationError as e: self._raise_uid_conflict(e, wildcard is not None) raise - result = self._post_put(r, retry_on_failure, precondition, wildcard) - if result is not None: - # _post_put returned a retry coroutine (self._put(False) for async client) - await result + try: + result = self._post_put(r, retry_on_failure, precondition, wildcard) + if result is not None: + # _post_put returned a retry coroutine (self._put(False) for async client) + await result + except error.DAVError as e: + if not isinstance(e, error.UidConflictError) and self._bare_uid_conflict( + e.status, precondition, wildcard + ): + self._raise_bare_uid_conflict(e, wildcard, await self._get_status(self.url)) + raise def _update_tag_props(self, r) -> None: """Capture the ETag / Schedule-Tag response headers into self.props. @@ -1382,15 +1405,99 @@ def _raise_uid_conflict(e: error.DAVError, consistency: bool = False) -> None: for the UID - it is a ConsistencyError, which is what the search raised.""" if error.UID_CONFLICT_PRECONDITIONS.intersection(e.preconditions): - if consistency: - cls = error.UID_CONFLICT_CONSISTENCY_ERRORS.get( - type(e), error.UidConflictConsistencyError - ) - else: - cls = error.UID_CONFLICT_ERRORS.get(type(e), error.UidConflictError) - raise cls( - e.url, e.reason, preconditions=e.preconditions, body=e.body, status=e.status - ) from e + raise CalendarObjectResource._as_uid_conflict(e, consistency) from e + + @classmethod + def _raise_uid_conflict_as_consistency( + cls, e: error.UidConflictError, check: "SaveCheck | None" + ) -> None: + """Where ``_save_plan`` planned a check (a search, or a GET for + no_overwrite), a UID conflict the server still refuses is the + ConsistencyError the search raises, also where no wildcard went + out: the search missed it (a race, a lazy index), or a GET only + guarded the URL. An If-Match refusal keeps its mismatch class + (issue #756).""" + if check is not None and not isinstance( + e, (error.ConsistencyError, error.ETagMismatchError, error.ScheduleTagMismatchError) + ): + raise cls._as_uid_conflict(e, True) from e + + @staticmethod + def _as_uid_conflict(e: error.DAVError, consistency: bool) -> error.UidConflictError: + """The UidConflictError to raise for ``e`` (see ``_raise_uid_conflict``).""" + if consistency: + cls = next( + ( + c + for base, c in error.UID_CONFLICT_CONSISTENCY_ERRORS.items() + if isinstance(e, base) + ), + error.UidConflictConsistencyError, + ) + else: + cls = error.UID_CONFLICT_ERRORS.get(type(e), error.UidConflictError) + return cls(e.url, e.reason, preconditions=e.preconditions, body=e.body, status=e.status) + + def _bare_uid_conflict( + self, status: int | None, precondition: Precondition, wildcard: Wildcard | None + ) -> bool: + """May a failed PUT answered with ``status`` be the server refusing a taken UID without the + precondition element? That is where save.no-uid-conflict (or its + .if-none-match child, for a PUT carrying If-None-Match: *) is a + 'quirk' with a bare ``status`` of this PUT's. + + A bare status may mean something else as well, so it is only + considered where the PUT carried no condition on the object at + the URL (If-Match, If-Schedule-Tag-Match), and never for 403, + which is also what a read-only calendar answers. Even then a GET + has to find nothing at the URL (``_raise_bare_uid_conflict``), + which only a server answering 404 for a missing object can say + (non-existing-raises-not-found.object).""" + if status in (None, 403): + return False + sent = {h.lower() for h in self._put_headers(precondition, wildcard)} + if sent & {"if-match", "if-schedule-tag-match"}: + return False + features = self.client.features if self.client is not None else None + if features is None or not features.is_supported("non-existing-raises-not-found.object"): + return False + feature = ( + "save.no-uid-conflict.if-none-match" + if wildcard == "if-none-match" + else "save.no-uid-conflict" + ) + node = features.is_supported(feature, return_type=dict) + return ( + node.get("support") == "quirk" + and not node.get("precondition") + and node.get("status") == status + ) + + @classmethod + def _raise_bare_uid_conflict( + cls, e: error.DAVError, wildcard: Wildcard | None, get_status: int | None + ) -> None: + """Raise UidConflictError for a bare refusal ``e`` (see + ``_bare_uid_conflict``) if a GET of the URL answered 404: the PUT + was not about an object there. Otherwise ``e`` stands - with + If-None-Match: *, the URL is taken.""" + if get_status == 404: + raise cls._as_uid_conflict(e, wildcard is not None) from e + + def _get_status(self, url) -> "int | None | Coroutine[Any, Any, int | None]": + """The status of a GET of ``url``, None where it failed.""" + if self.is_async_client: + return self._async_get_status(url) + try: + return self.client.request(str(url), "GET").status + except error.DAVError: + return None + + async def _async_get_status(self, url) -> int | None: + try: + return (await self.client.request(str(url), "GET")).status + except error.DAVError: + return None def _post_put( self, @@ -1413,12 +1520,15 @@ def _post_put( ) self._raise_uid_conflict(error.error_from_response(cls, r), wildcard is not None) if r.status == 412 and wildcard == "if-none-match": - ## If-None-Match: * failed - there is an object at this URL + ## If-None-Match: * failed - there is an object at this URL, or + ## (save.no-uid-conflict.if-none-match) the UID is taken elsewhere + what = "object already exists at" + if self._uid_conflict_refused("save.no-uid-conflict.if-none-match"): + what = "the UID is taken, or an object already exists at" raise error.error_from_response( error.ConsistencyError, r, - f"no_overwrite flag was set, but object already exists at {self.url} " - f"({error.errmsg(r)})", + f"no_overwrite flag was set, but {what} {self.url} ({error.errmsg(r)})", ) if r.status == 412 and wildcard == "if-match": ## If-Match: * failed - there is no object at this URL @@ -1438,7 +1548,8 @@ def _post_put( ## No retry with a wildcard: an object this very PUT may have ## stored (a 5xx after writing) would refuse If-None-Match: *, ## and the failure is reported as it was for If-Match: * too. - if wildcard: + ## Nor where it may be a taken UID: cleaning up the data does not free it. + if wildcard or self._bare_uid_conflict(r.status, precondition, wildcard): retry_on_failure = False if retry_on_failure: try: @@ -1474,23 +1585,8 @@ def _put_raw( headers = self._put_headers(precondition, wildcard) body = WireBytes(body) if self.is_async_client: - return self._async_put_raw(body, headers, precondition, wildcard) - try: - r = self.client.put(str(self.url), body, headers) - except error.AuthorizationError as e: - self._raise_uid_conflict(e, wildcard is not None) - raise - return self._post_put(r, False, precondition, wildcard) - - async def _async_put_raw( - self, body: bytes, headers, precondition: Precondition, wildcard: Wildcard | None = None - ) -> None: - try: - r = await self.client.put(str(self.url), body, headers) - except error.AuthorizationError as e: - self._raise_uid_conflict(e, wildcard is not None) - raise - self._post_put(r, False, precondition, wildcard) + return self._async_send_put(body, headers, False, precondition, wildcard) + return self._send_put(body, headers, False, precondition, wildcard) @staticmethod def _check_raw_data(raw_data: Any) -> bytes | None: @@ -1796,17 +1892,19 @@ def get_self(): ## raw bytes are never merged into a master merging = raw_data is None - wildcard, search = self._save_plan( + wildcard, check = self._save_plan( no_overwrite, no_create, only_this_recurrence if merging else False, all_recurrences and merging, precondition, ) - if search: + if check == "search": uid = self.id or self.icalendar_component.get("uid") existing = get_self() self._validate_save_constraints(existing, uid, no_overwrite, no_create) + elif check == "get" and (target := self._target_url()) is not None: + self._refuse_taken_url(self._get_status(target)) ## Note: RFC 4791 ยง9.6.5 permits servers to omit RECURRENCE-ID on the initial ## expanded instance. If this object is such an instance (no RECURRENCE-ID but @@ -1817,8 +1915,6 @@ def get_self(): and (only_this_recurrence is not False or all_recurrences) and "RECURRENCE-ID" in self.icalendar_component ): - from caldav.lib import error - obj = get_self() if obj is None: if only_this_recurrence is True: @@ -1840,12 +1936,16 @@ def get_self(): precondition=precondition, ) - if raw_data is not None: - self._put_raw(raw_data, precondition, wildcard) - return self - self._maybe_increment_sequence(increase_seqno) - path = self.url.path if self.url else None - self._create(id=self.id, path=path, precondition=precondition, wildcard=wildcard) + try: + if raw_data is not None: + self._put_raw(raw_data, precondition, wildcard) + return self + self._maybe_increment_sequence(increase_seqno) + path = self.url.path if self.url else None + self._create(id=self.id, path=path, precondition=precondition, wildcard=wildcard) + except error.UidConflictError as e: + self._raise_uid_conflict_as_consistency(e, check) + raise return self def _save_plan( @@ -1855,11 +1955,12 @@ def _save_plan( only_this_recurrence: bool | None, all_recurrences: bool, precondition: Precondition, - ) -> tuple[Wildcard | None, bool]: + ) -> tuple[Wildcard | None, SaveCheck | None]: """How save() enforces no_overwrite and no_create. - Returns (the header to send with the value ``*``, if any; whether - to search for the UID first). + Returns (the header to send with the value ``*``, if any; the + check before the PUT, if any: ``"search"`` for the UID, or + ``"get"`` of the URL). A wildcard precondition makes the server enforce the flag in the same request as the PUT, so there is no window between a check @@ -1871,7 +1972,10 @@ def _save_plan( save.if-none-match says the server overwrites anyway. It only guards the URL: the search also finds the same UID under another resource name, so it is left out only where the server is known - to refuse that (save.no-uid-conflict). + to refuse that (save.no-uid-conflict, recognisably or not). + Where If-None-Match: * is not sent, a GET of the URL guards it + instead of the search, if the URL is one the server keeps + (save-load.stable-url). no_create: If-Match: * (section 13.1.1), unless save-load.mutable.if-match-wildcard says the server gets it @@ -1884,31 +1988,59 @@ def _save_plan( Both flags at once cannot hold; the search refuses either way. """ if not (no_overwrite or no_create): - return None, False + return None, None if no_overwrite and no_create: - return None, True + return None, "search" features = self.client.features if self.client is not None else None def supported(feature: str) -> bool: return features is not None and features.is_supported(feature) + uid_conflict_refused = self._uid_conflict_refused + merges_recurrence = bool( (only_this_recurrence is not False or all_recurrences) and "RECURRENCE-ID" in self.icalendar_component ) if no_overwrite: - if not (features is None or supported("save.if-none-match")): - return None, True - return "if-none-match", merges_recurrence or not supported("save.no-uid-conflict") + if features is None or supported("save.if-none-match"): + guarded = uid_conflict_refused("save.no-uid-conflict.if-none-match") + return "if-none-match", None if guarded and not merges_recurrence else "search" + if ( + uid_conflict_refused("save.no-uid-conflict") + and supported("save-load.stable-url") + and not merges_recurrence + ): + return None, "get" + return None, "search" if self._precondition_headers(precondition) or not ( features is None or supported("save-load.mutable.if-match-wildcard") ): - return None, True + return None, "search" if not (self.id or self.icalendar_component.get("uid")): from caldav.lib import error raise error.ConsistencyError("no_create flag was set, but no ID given") - return "if-match", merges_recurrence + return "if-match", "search" if merges_recurrence else None + + def _uid_conflict_refused(self, feature: str) -> bool: + """Does ``feature`` (save.no-uid-conflict or a child) say the + server refuses a duplicate UID, recognisably or not?""" + features = self.client.features if self.client is not None else None + return ( + features is not None + and features.is_supported(feature, return_type=str) in UID_CONFLICT_REFUSED + ) + + def _target_url(self): + """The URL save() will PUT to, None where it is still to be made up.""" + return self.url or (self._generate_url() if self.id else None) + + @staticmethod + def _refuse_taken_url(get_status: int | None) -> None: + """For save(no_overwrite=True) checking with a GET (``_save_plan``).""" + if get_status is not None and 200 <= get_status < 300: + raise error.ConsistencyError("no_overwrite flag was set, but object already exists") def _validate_save_constraints(self, existing, uid, no_overwrite, no_create): """Raise ConsistencyError if no_overwrite/no_create constraints are violated.""" @@ -1919,7 +2051,7 @@ def _validate_save_constraints(self, existing, uid, no_overwrite, no_create): if no_overwrite and existing: ## The same UID under another URL is what the server's ## CALDAV:no-uid-conflict precondition would refuse (issue #806) - target = self.url or (self._generate_url() if uid else None) + target = self._target_url() if existing.url and target and existing.url != target: raise error.UidConflictConsistencyError( "no_overwrite flag was set, but another object holds the UID" @@ -2042,17 +2174,19 @@ async def get_self(): ## raw bytes are never merged into a master merging = raw_data is None - wildcard, search = self._save_plan( + wildcard, check = self._save_plan( no_overwrite, no_create, only_this_recurrence if merging else False, all_recurrences and merging, precondition, ) - if search: + if check == "search": uid = self.id or self.icalendar_component.get("uid") existing = await get_self() self._validate_save_constraints(existing, uid, no_overwrite, no_create) + elif check == "get" and (target := self._target_url()) is not None: + self._refuse_taken_url(await self._get_status(target)) if ( raw_data is None @@ -2080,12 +2214,16 @@ async def get_self(): precondition=precondition, ) - if raw_data is not None: - await self._put_raw(raw_data, precondition, wildcard) - return self - self._maybe_increment_sequence(increase_seqno) - path = self.url.path if self.url else None - await self._create(id=self.id, path=path, precondition=precondition, wildcard=wildcard) + try: + if raw_data is not None: + await self._put_raw(raw_data, precondition, wildcard) + return self + self._maybe_increment_sequence(increase_seqno) + path = self.url.path if self.url else None + await self._create(id=self.id, path=path, precondition=precondition, wildcard=wildcard) + except error.UidConflictError as e: + self._raise_uid_conflict_as_consistency(e, check) + raise return self def is_loaded(self): diff --git a/caldav/collection.py b/caldav/collection.py index 04224a58..e6b064a7 100644 --- a/caldav/collection.py +++ b/caldav/collection.py @@ -15,6 +15,7 @@ import re import uuid import warnings +from collections.abc import Generator from dataclasses import dataclass from datetime import date as _date from datetime import datetime, timezone @@ -40,6 +41,7 @@ FreeBusy, Journal, Todo, + _quote_uid, ) from .compatibility_hints import ( at_literal_is_refused, @@ -2147,11 +2149,65 @@ def get_object_by_uid( comp_class: filter by component type (Event, Todo, Journal) comp_filter: for backward compatibility. Don't use! + Unless the ``search.text.uid`` feature says the server handles + UID searches, a failing search (iCloud answers 412, issue #783) + makes this fetch the object as ``.ics``, the name ``save()`` + gives it. If the server is known not to handle UID searches + (``search.text.uid`` unsupported, broken or ungraceful), the search is + skipped. Should the object not be found that way, and the + search failed with 412 or is known to fail, the object is found + among all objects of the type - a download of the whole calendar + for every missing UID - unless the server is known to name every + object ``.ics`` (``forces_uid_ics`` on ``save-load.stable-url``). + Returns: CalendarObjectResource (Event, Todo, or Journal) """ + gen = self._uid_lookup(uid, comp_filter, comp_class) if self.is_async_client: - return self._async_get_object_by_uid(uid, comp_filter, comp_class) + return self._async_run_uid_lookup(gen) + try: + step = next(gen) + while True: + try: + result = self._uid_lookup_step(*step) + except error.DAVError as e: + step = gen.throw(e) + else: + step = gen.send(result) + except StopIteration as done: + return done.value + + async def _async_run_uid_lookup(self, gen: Generator) -> "CalendarObjectResource": + """Async driver for _uid_lookup(), see get_object_by_uid().""" + try: + step = next(gen) + while True: + try: + result = await self._uid_lookup_step(*step) + except error.DAVError as e: + step = gen.throw(e) + else: + step = gen.send(result) + except StopIteration as done: + return done.value + + def _uid_lookup_step(self, action: str, arg: Any) -> Any: + """Execute one step of _uid_lookup(). Returns a coroutine with + an async client.""" + if action == "search": + return self.search(**arg) + return self.client.request(str(arg)) + + def _uid_lookup( + self, + uid: str, + comp_filter: cdav.CompFilter | None, + comp_class: Optional["CalendarObjectResource"], + ) -> Generator: + """The logic of get_object_by_uid(), as a generator yielding + ``("search", kwargs)`` and ``("request", url)`` for the driver to + execute, and getting the results (or the DAVError) back.""" ## Use self.search() rather than CalDAVSearcher directly, so that any ## monkey-patching of Calendar.search (e.g. the search-cache delay that ## tests/test_caldav.py wraps it with for servers with lazy search @@ -2161,38 +2217,116 @@ def get_object_by_uid( ## search(uid=...) does substring matching on the server side, so we ## apply an exact match filter afterwards to preserve the semantics of ## this method (see testObjectByUID). - items_found = self.search( - uid=uid, - comp_class=comp_class, - xml=comp_filter, - post_filter=True, - _hacks="insist", - include_completed=True, + ## + ## No return_defaults: an unconfigured server is not known to be fine. + uid_search = self.client.features.is_supported( + "search.text.uid", return_type=str, return_defaults=False ) - items_found = [o for o in items_found if o.id == uid] + known_to_fail = uid_search in ("unsupported", "broken", "ungraceful") + ## The deprecated comp_filter has no fallback. + fallback = comp_filter is None and uid_search not in ("full", "quirk") + scan = False + absent = False + search_error = None + if not (fallback and known_to_fail): + try: + items_found = yield ( + "search", + dict( + uid=uid, + comp_class=comp_class, + xml=comp_filter, + post_filter=True, + _hacks="insist", + include_completed=True, + ), + ) + return self._uid_match(items_found, uid) + except (error.NotFoundError, error.RateLimitError, error.AuthorizationError): + ## the fallback won't help with these + raise + except error.DAVError as e: + if not fallback: + raise + scan = e.status == 412 + search_error = e + log.debug(f"Search by UID failed ({e}), trying GET {uid}.ics") + else: + scan = True + url = self._uid_fallback_url(uid) + if url is not None: + try: + response = yield ("request", url) + found = self._uid_fallback_object(url, response, uid, comp_class) + if ( + found is None + and response.status in (404, 410) + and self.client.features.is_supported( + "save-load.stable-url", return_type=dict + ).get("forces_uid_ics") + ): + ## the server names every object .ics: nothing to scan for + scan = False + absent = True + except error.RateLimitError: + raise + except error.DAVError: + found = None + if found is not None: + return found + items_found = [] + if search_error is not None and not (scan or absent): + ## the search failed and nothing says the object is not there: + ## a NotFoundError would be a false answer + raise search_error + if scan: + try: + items_found = yield ( + "search", + dict(comp_class=comp_class, include_completed=True), + ) + except error.DAVError as e: + if search_error is None: + raise + raise e from search_error + return self._uid_match(items_found, uid) - if not items_found: - raise error.NotFoundError("%s not found on server" % uid) - error.assert_(len(items_found) == 1) - return items_found[0] + def _uid_fallback_url(self, uid: str) -> "URL | None": + """Return the URL save() would use for this UID, or None if GET + by URL is unsupported.""" + if not self.client.features.is_supported("save-load.get-by-url"): + return None + return self.url.join(_quote_uid(uid, self.client.features) + ".ics") - async def _async_get_object_by_uid( + def _uid_fallback_object( self, + url: "URL", + response: Any, uid: str, - comp_filter: cdav.CompFilter | None = None, - comp_class: Optional["CalendarObjectResource"] = None, - ) -> "Event": - """Async helper for get_object_by_uid().""" - items_found = await self.search( - uid=uid, - comp_class=comp_class, - xml=comp_filter, - post_filter=True, - _hacks="insist", - include_completed=True, - ) - items_found = [o for o in items_found if o.id == uid] + comp_class: Optional["CalendarObjectResource"], + ) -> "CalendarObjectResource | None": + """Return the object in a GET response if it has this UID and + type, else None.""" + if not 200 <= response.status < 300: + return None + found_class = self._calendar_comp_class_by_data(response.raw) + if found_class is CalendarObjectResource: ## not calendar data + return None + if comp_class is not None and found_class is not comp_class: + return None + obj = found_class(self.client, url=url, data=response.raw, parent=self) + try: + if obj.id != uid: + return None + except ValueError: ## unparsable data + return None + obj._keep_raw_data(getattr(response, "_raw_body", None)) + obj._update_tag_props(response) + return obj + def _uid_match(self, items_found: list, uid: str) -> "CalendarObjectResource": + """Return the single object in items_found with exactly this UID.""" + items_found = [o for o in items_found if o.id == uid] if not items_found: raise error.NotFoundError("%s not found on server" % uid) error.assert_(len(items_found) == 1) diff --git a/caldav/compatibility_hints.py b/caldav/compatibility_hints.py index 2429645d..fd17f270 100644 --- a/caldav/compatibility_hints.py +++ b/caldav/compatibility_hints.py @@ -521,6 +521,9 @@ class FeatureSet: }, "save-load.stable-url": { "description": "The server reports a calendar object resource under the same URL the client used to store it, so that a client may compare a searched object's URL with one it constructed as '/.ics'. When 'unsupported' it may not, and has to read the URL off the response. Two different things make it 'unsupported'. The server may assign the resource a name of its own choosing. Or the *collection* may not stay at the address it was created at, in which case a constructed object URL is only usable from the address the server reports objects under - and a client is handed either address depending on how it got the Calendar: make_calendar adopts the canonical URL, a calendar listing reports it, while the cal_id shortcut is pure URL arithmetic and never round-trips. That comes in two flavours. OX App Suite exposes a calendar both at the requested cal_id and at an opaque internal 'cal://0/NNN' path, a direct GET on either working, so a constructed URL resolves but is not the URL the server reports. Zimbra instead relocates the collection to a display-name-derived path and leaves an alias at the cal_id that answers PROPFIND but 404s on child objects, so a constructed URL under the cal_id does not resolve at all. That second case is 'create-calendar.stable-url' seen from the object end, and it is read from there rather than measured again here - measuring it made this feature's verdict depend on which address the probe run happened to be holding.", + "extra_keys": { + "forces_uid_ics": "True when the server names every object .ics whatever name it was PUT to, so a GET of .ics that misses means the object is not there. get_object_by_uid() then does not scan the calendar after a failed UID search.", + }, "default": {"support": "full"}, }, "save-load.reuse-deleted-uid": { @@ -706,6 +709,11 @@ class FeatureSet: "description": "According to RFC4791 the search done should be a substring search. The search.text.substring feature is set if the calendar server does this (as opposed to only return full matches). Substring matches does not always make sense, but it's mandated by the RFC. When a server does a substring match on some properties but an exact match on others, the support should be marked as fragile. Except for categories, which are handled in search.text.category.substring", "links": ["https://datatracker.ietf.org/doc/html/rfc4791#section-9.7.5"], }, + "search.text.uid": { + "description": "A calendar-query with a UID prop-filter works. 'unsupported' when the filter is ignored and the whole calendar comes back; 'ungraceful' or 'broken' when the query fails (iCloud answers any UID filter with 412) or answers wrongly. A server may do text searches on other properties and still fail on UID, so it can be set on its own; where it is not, it takes the value of search.text, as sub-features do. See https://github.com/python-caldav/caldav/issues/783", + "default": {"support": "full"}, + "links": ["https://datatracker.ietf.org/doc/html/rfc4791#section-9.7"], + }, "search.text.category": { "description": "Search for category should work. This is not explicitly specified in RFC4791, but covered in section 9.7.5. No examples targets categories explicitly, but there are some text match examples in section 7.8.6 and following sections", ## Independent feature (directly probed): the default marks it so the @@ -896,8 +904,69 @@ class FeatureSet: "links": ["https://datatracker.ietf.org/doc/html/rfc4791#section-5.3.2", "https://datatracker.ietf.org/doc/html/rfc4791#section-2", "https://datatracker.ietf.org/doc/html/rfc2616#section-14.26", "https://datatracker.ietf.org/doc/html/rfc9110#section-13.1.2"], }, "save.no-uid-conflict": { - "description": "A PUT of an object whose UID another object resource in the same calendar already holds is refused (RFC 4791 section 5.3.2.1, precondition CALDAV:no-uid-conflict; the library raises UidConflictError). The RFC requires it, but whether a server does is only known where it was observed, hence the default 'unknown'. 'ungraceful': refused, but without a precondition the library recognises, so it cannot tell the refusal from any other failed PUT. When 'full' and save.if-none-match holds, save(no_overwrite=True) skips the UID search before the PUT: If-None-Match: * covers the same URL, and this covers the same UID under another URL.", + "description": ( + "The server refuses to store two objects with the same UID in one calendar. RFC 4791 " + "section 5.3.2.1 says it MUST: a PUT of an object whose UID another object in the " + "calendar already has is refused, with the precondition CALDAV:no-uid-conflict in the " + "error body (for a scheduling object RFC 6638 section 3.2.4.1's " + "CALDAV:unique-scheduling-object-resource does as well), and the library raises " + "UidConflictError. Graded by a plain PUT, without conditional headers; for one " + "carrying If-None-Match: *, see the child feature. " + "'full': refused with 403 or 409 and the precondition. " + "'quirk': refused otherwise, but in a way the library still recognises. The extra key " + "'status' is the HTTP status, 'precondition' whether the body names the precondition. " + "With the precondition (412 from Xandikos and Stalwart) the library goes by the body. " + "Without it, the status alone could mean something else, so the library takes it for " + "a UID conflict only where the PUT carried no conditional header except " + "If-None-Match: *, and a GET then finds nothing at the URL - which takes a server " + "answering 404 for a missing object (non-existing-raises-not-found.object), and a " + "status other than 403. The caldav-server-tester " + "grades a bare status 'quirk' only after checking that nothing was stored at the URL, " + "and that the same PUT with another UID goes through. " + "'ungraceful': refused, but not recognisably - a bare 403, which a read-only calendar " + "answers too, or a refusal the tester could not confirm. The library then raises " + "what the status gives, not UidConflictError. " + "'unsupported': the duplicate is stored. " + "Where the duplicate is refused at all ('full', 'quirk' or 'ungraceful'), " + "save(no_overwrite=True) only has to guard the URL, and leaves out its search for the " + "UID, which may download the whole calendar: it sends If-None-Match: * (where " + "save.if-none-match holds), or else GETs the URL first (where save-load.stable-url " + "holds). " + "The default is 'unknown' rather than the 'full' the RFC asks for, since a wrong " + "'full' is harmful: save(no_overwrite=True) would leave out the search, and on one of " + "the several servers that store duplicates, silently store one." + ), "default": {"support": "unknown"}, + "extra_keys": { + "status": "quirk: the HTTP status the server refuses with", + "precondition": "quirk: whether the error body names the precondition", + }, + "links": ["https://datatracker.ietf.org/doc/html/rfc4791#section-5.3.2.1", "https://datatracker.ietf.org/doc/html/rfc6638#section-3.2.4.1"], + }, + "save.no-uid-conflict.if-none-match": { + "description": ( + "save.no-uid-conflict for a PUT carrying If-None-Match: *, as save(no_overwrite=True) " + "sends it, with the same grades and extra keys. A server may answer it otherwise: " + "Bedework refuses a plain PUT with 403 CALDAV:no-uid-conflict, but this one with a bare " + "412. Where it is not stated, it is what save.no-uid-conflict says." + ), + "default": {"support": "unknown"}, + "extra_keys": { + "status": "quirk: the HTTP status the server refuses with", + "precondition": "quirk: whether the error body names the precondition", + }, + }, + "save.no-uid-conflict.href-given": { + "description": ( + "Where the server refuses a duplicate UID with the precondition, the precondition " + "element names the object holding the UID in a DAV:href, as RFC 4791 section 5.3.2.1 " + "says it SHOULD; UidConflictError.href gives it, and is None where it is not given. " + "Nothing in the library depends on it. Only probed where the precondition is given. " + "Where a profile states the parent, this one inherits the parent's level rather than " + "its own default, so state it too ('unknown' where the refusal has no " + "precondition to read an href from)." + ), + "default": {"support": "full"}, "links": ["https://datatracker.ietf.org/doc/html/rfc4791#section-5.3.2.1"], }, "delete": {"description": "Deleting a calendar object resource. Deleting a whole calendar is delete-calendar."}, @@ -1536,9 +1605,9 @@ def compare(self, observed): "support": "broken", "behaviour": "the reply is cut at DAV:limit without a 507, and the token skips the rest", }, - ## A second object with a UID already in the calendar is refused with 412 - ## CALDAV:no-uid-conflict (testNoOverwriteSameUidOtherUrl, 2026-10-19). - "save.no-uid-conflict": {"support": "full"}, + ## Measured with caldav-server-tester, 2026-10-28. + "save.no-uid-conflict": {"support": "quirk", "status": 412, "precondition": True, "behaviour": "refused with 412 and the precondition"}, + "save.no-uid-conflict.href-given": {"support": "unsupported"}, ## Probed 2026-10-21 by caldav-server-tester "calendar-color": {"support": "full"}, "calendar-order": {"support": "full"}, @@ -1565,9 +1634,9 @@ def compare(self, observed): "support": "unsupported", "behaviour": "If-Match: * is compared as a literal etag: an overwrite of an existing object is refused with 412", }, - ## A second object with a UID already in the calendar is refused with 409 - ## CALDAV:no-uid-conflict (testNoOverwriteSameUidOtherUrl on 3.8.3, 2026-10-19). + ## Measured with caldav-server-tester, 2026-10-28. "save.no-uid-conflict": {"support": "full"}, + "save.no-uid-conflict.href-given": {"support": "unsupported"}, "search.recurrences.includes-implicit.todo.pending": {"support": "fragile", "behaviour": "inconsistent results between runs"}, "search.recurrences.expanded.todo": {"support": "unsupported"}, "search.recurrences.expanded.exception": {"support": "full"}, @@ -1702,6 +1771,8 @@ def compare(self, observed): ## Zimbra is not very good at it's caldav support zimbra = { + ## Measured with caldav-server-tester, 2026-10-27: the UID filter works although search.text does not. + 'search.text.uid': {'support': 'full'}, ## Measured with caldav-server-tester, 2026-10-19. "save.no-uid-conflict": {"support": "full"}, ## Observed by testCancelSingleRecurrence, 2026-10-20 @@ -1828,6 +1899,8 @@ def compare(self, observed): ## 5.x, hence the version-stamped name - a plain `bedework` would be claiming ## far more than we have measured. bedework_3_10_3 = { + ## Measured with caldav-server-tester, 2026-10-27: the UID filter works although search.text does not. + "search.text.uid": {"support": "full"}, ## Probed 2026-10-26 by caldav-server-tester: RFC 4791 section 5.2.3 makes ## storing a component type outside the set an error. "supported-components.honoured": {"support": "broken", "behaviour": "the restriction is advertised but not enforced: a VTODO-only calendar reports ['VTODO'], and a VEVENT can be saved to it"}, @@ -1835,8 +1908,10 @@ def compare(self, observed): "create-calendar.with-supported-component-types.multiple": {"support": "broken", "behaviour": "asked for VEVENT and VTODO, the calendar reports one concatenated name, ['VEVENTVTODO']"}, ## Probed 2026-10-26 by caldav-server-tester "save-load.icalendar.participant": {"support": "ungraceful", "behaviour": "an event with a PARTICIPANT is refused (403)"}, - ## Measured with caldav-server-tester, 2026-10-19. - "save.no-uid-conflict": {"support": "ungraceful", "behaviour": "refused with a bare 412 when the PUT carries If-None-Match: *"}, + ## Measured with caldav-server-tester, 2026-10-28. + "save.no-uid-conflict": {"support": "full"}, + "save.no-uid-conflict.href-given": {"support": "unsupported"}, + "save.no-uid-conflict.if-none-match": {"support": "quirk", "status": 412, "precondition": False}, ## If tests are yielding unexpected results, try to increase this: 'search-cache': {'behaviour': 'delay', 'delay': 3}, 'scheduling.calendar-user-address-set': {'support': 'full'}, @@ -1906,8 +1981,12 @@ def compare(self, observed): ## deletes calendars, its sync-token and text search behave differently - so ## nothing is inherited from that profile. bedework_5_0_0 = { - ## Measured with caldav-server-tester, 2026-10-19. - "save.no-uid-conflict": {"support": "ungraceful", "behaviour": "refused with a bare 412 when the PUT carries If-None-Match: *"}, + ## Measured with caldav-server-tester, 2026-10-27: the UID filter works although search.text does not. + "search.text.uid": {"support": "full"}, + ## Measured with caldav-server-tester, 2026-10-28. + "save.no-uid-conflict": {"support": "full"}, + "save.no-uid-conflict.href-given": {"support": "unsupported"}, + "save.no-uid-conflict.if-none-match": {"support": "quirk", "status": 412, "precondition": False}, ## Writes are asynchronous: a read-back issued immediately after a PUT may ## 404 or hand back the pre-write copy. However, in some cases the ## delay is small and varies, sometimes the caldav-server-tester tool will @@ -2151,6 +2230,10 @@ def compare(self, observed): ## The 401 offers 'X-MobileMe-AuthToken realm="MMCalDav", Basic realm="MMCalDav"' ## - Basic, with an app-specific password, is what works. icloud = { + ## Measured with caldav-server-tester, 2026-10-28 (by Rusich90, PR 826): a plain + ## PUT and one carrying If-None-Match: * both get a bare 412. + "save.no-uid-conflict": {"support": "quirk", "status": 412, "precondition": False, "behaviour": "refused with a bare 412"}, + "save.no-uid-conflict.href-given": {"support": "unknown"}, 'auto-connect.url': { 'scheme': 'https', 'domain': 'caldav.icloud.com', @@ -2176,6 +2259,7 @@ def compare(self, observed): }, 'save.duplicate-uid.cross-calendar': {'support': 'ungraceful', 'behaviour': 'PUT refused'}, 'scheduling.schedule-tag': {'support': 'unsupported', 'behaviour': 'no Schedule-Tag returned on GET or via PROPFIND'}, + 'search.text.uid': {'support': 'ungraceful', 'behaviour': 'any UID filter in a calendar-query is answered with 412 (#783)'}, ## 2026-10-14: on another account, is-not-defined.category/class and ## search.text.case-* found nothing (an empty 207) in two tester runs, and ## SUMMARY and CATEGORIES filters by hand did not either, also 30 minutes @@ -2262,8 +2346,9 @@ def compare(self, observed): } sogo = { - ## Measured with caldav-server-tester, 2026-10-19. - "save.no-uid-conflict": {"support": "ungraceful", "behaviour": "refused with 409, without the CALDAV:no-uid-conflict precondition"}, + ## Measured with caldav-server-tester, 2026-10-28. + "save.no-uid-conflict": {"support": "quirk", "status": 409, "precondition": False, "behaviour": "refused with a bare 409"}, + "save.no-uid-conflict.href-given": {"support": "unknown"}, "scheduling.schedule-tag": False, "scheduling.mailbox.inbox-delivery": False, ## SOGo rejects calendar-color set as a colour name, but takes a hex @@ -2386,6 +2471,8 @@ def compare(self, observed): #] robur = { + ## Measured with caldav-server-tester, 2026-10-27. + "search.text.uid": {"support": "unsupported", "behaviour": "objects not matching the UID come back"}, ## Probed 2026-10-26 by caldav-server-tester "save-load.icalendar.participant": {"support": "ungraceful", "behaviour": "an event with a PARTICIPANT is refused (400)"}, ## Measured with caldav-server-tester, 2026-10-19. @@ -2534,8 +2621,9 @@ def compare(self, observed): ## Probed 2026-10-26 by caldav-server-tester "save-load.icalendar.participant": {"support": "ungraceful", "behaviour": "an event with a PARTICIPANT is refused (403)"}, "save-load.icalendar.vtimezone": {"support": "unsupported", "behaviour": "the event comes back with TZID=Europe/Berlin but no VTIMEZONE for it"}, - ## Measured with caldav-server-tester, 2026-10-19. + ## Measured with caldav-server-tester, 2026-10-28. "save.no-uid-conflict": {"support": "full"}, + "save.no-uid-conflict.href-given": {"support": "unsupported"}, ## Probed 2026-10-13: DAV:limit is refused with a 507 and ## number-of-matches-within-limits ("Report limit not supported"), as ## RFC 6578 section 3.7 says @@ -2601,8 +2689,10 @@ def compare(self, observed): ## Probed 2026-10-26 by caldav-server-tester: RFC 4791 section 5.2.3 makes ## storing a component type outside the set an error. "supported-components.honoured": {"support": "broken", "behaviour": "the restriction is advertised but not enforced: a VTODO-only calendar reports ['VTODO'], and a VEVENT can be saved to it"}, - ## Measured with caldav-server-tester, 2026-10-19. - "save.no-uid-conflict": {"support": "full"}, + ## Measured with caldav-server-tester, 2026-10-28. + "save.no-uid-conflict": {"support": "quirk", "status": 412, "precondition": True, "behaviour": "refused with 412 and the precondition"}, + ## Spelled out: unset, it would be the parent's 'quirk'. + "save.no-uid-conflict.href-given": {"support": "full"}, ## Probed 2026-10-11. calendaring-sync found that it also answers a token ## whose history is gone with a partial report and no error. "sync-token.invalid-token": {"support": "broken", "behaviour": "'http://example.invalid/never-issued': 207, no error; '0000000000000000000000000000000000000000': 207, no error"}, @@ -2715,6 +2805,7 @@ def compare(self, observed): purelymail = { ## Measured with caldav-server-tester, 2026-10-19. "save.no-uid-conflict": {"support": "ungraceful", "behaviour": "refused with 403, with no-uid-conflict in the CardDAV namespace"}, + "save.no-uid-conflict.href-given": {"support": "unknown"}, ## Purelymail claims that the search indexes are "lazily" populated, ## so search works some minutes after the event was created/edited. ## A plain listing reflects a new, edited, completed or deleted object at @@ -2759,6 +2850,7 @@ def compare(self, observed): "save-load.icalendar.participant": {"support": "ungraceful", "behaviour": "an event with a PARTICIPANT is refused (500, ical4j casts the Participant to a VAlarm)"}, ## Measured with caldav-server-tester, 2026-10-19. "save.no-uid-conflict": {"support": "full"}, + "save.no-uid-conflict.href-given": {"support": "unsupported"}, 'auto-connect.url': { 'scheme': 'https', 'domain': 'caldav.gmx.net', @@ -2856,8 +2948,9 @@ def compare(self, observed): "save-load.icalendar.unknown-property": {"support": "unsupported", "behaviour": "ESTIMATED-DURATION is dropped"}, "save-load.todo.attendee": {"support": "broken", "behaviour": "the task is accepted, then gone (404)"}, "save-load.todo.completed-without-status": {"support": "unsupported", "behaviour": "COMPLETED is dropped"}, - ## Measured with caldav-server-tester, 2026-10-19. + ## Measured with caldav-server-tester, 2026-10-28. "save.no-uid-conflict": {"support": "full"}, + "save.no-uid-conflict.href-given": {"support": "unsupported"}, ## Probed 2026-10-11 "sync-token.invalid-token": {"support": "broken", "behaviour": "'0000000000000000000000000000000000000000': 207, no error"}, ## Renaming a calendar after creation via PROPPATCH is not supported, but diff --git a/changes/+forces-uid-ics.added.md b/changes/+forces-uid-ics.added.md new file mode 100644 index 00000000..5e7e6372 --- /dev/null +++ b/changes/+forces-uid-ics.added.md @@ -0,0 +1 @@ +The extra key `forces_uid_ics` on the `save-load.stable-url` feature tells that a server names every object `.ics` whatever name it was PUT to. `get_object_by_uid()` then treats a missing `.ics` as conclusive and skips the full scan. diff --git a/changes/+no-overwrite-without-search.changed.md b/changes/+no-overwrite-without-search.changed.md new file mode 100644 index 00000000..6b6d718f --- /dev/null +++ b/changes/+no-overwrite-without-search.changed.md @@ -0,0 +1 @@ +`save(no_overwrite=True)` no longer searches the calendar for the UID before the PUT wherever the server is known to refuse a duplicate UID at all - also where it does so unrecognisably (`save.no-uid-conflict` `ungraceful`, e.g. a bare 403), and on iCloud and Bedework. On a server ignoring `If-None-Match: *` (SOGo), a GET of the URL replaces the search, where `save-load.stable-url` holds. Where the server refuses the duplicate unrecognisably, the error raised for a UID taken under another URL is then whatever that refusal gives - still a `ConsistencyError` for a 412 to `If-None-Match: *` (its message then says the UID or the URL is taken), but a `PutError` or `AuthorizationError` for other statuses - rather than `UidConflictConsistencyError`. diff --git a/changes/+search-text-uid.added.md b/changes/+search-text-uid.added.md new file mode 100644 index 00000000..47073100 --- /dev/null +++ b/changes/+search-text-uid.added.md @@ -0,0 +1 @@ +New feature `search.text.uid`: a calendar-query with a UID filter works. iCloud answers it with 412 (https://github.com/python-caldav/caldav/issues/783), and its profile sets it to `ungraceful`. Where it is not set, it is what `search.text` says: a feature list with `search.text` unsupported, broken or ungraceful therefore skips the search by UID, fetching `.ics` and, failing that, all objects instead, so set `search.text.uid: full` if a UID search works on that server. diff --git a/changes/+uid-conflict-href-given.added.md b/changes/+uid-conflict-href-given.added.md new file mode 100644 index 00000000..cc0c8749 --- /dev/null +++ b/changes/+uid-conflict-href-given.added.md @@ -0,0 +1 @@ +The feature `save.no-uid-conflict.href-given` says whether a server refusing a duplicate UID names the object holding it (`UidConflictError.href`), as RFC 4791 section 5.3.2.1 says it SHOULD. diff --git a/changes/+uid-conflict-quirk.changed.md b/changes/+uid-conflict-quirk.changed.md new file mode 100644 index 00000000..0d275f06 --- /dev/null +++ b/changes/+uid-conflict-quirk.changed.md @@ -0,0 +1 @@ +`save.no-uid-conflict` is graded by a plain PUT, and a server refusing a duplicate UID in a way the RFC does not prescribe can be a `quirk` the library works around, with the extra keys `status` and `precondition`. With the precondition but another status than 403 or 409 (Xandikos, Stalwart: 412) the library goes by the body, as before. With a bare status (SOGo: 409), `save()` now raises `UidConflictError` (`UidConflictConsistencyError` with `no_overwrite` or `no_create`) where a GET finds nothing at the URL (only on a server answering 404 for a missing object, `non-existing-raises-not-found.object`); a bare 403 is not taken for a UID conflict, since a read-only calendar answers it too. The new `save.no-uid-conflict.if-none-match` does the same for a PUT carrying `If-None-Match: *`, which some servers answer otherwise (Bedework: 403 with the precondition to a plain PUT, a bare 412 to this one). iCloud refuses both with a bare 412, so there `save()` raises `UidConflictError` too. diff --git a/changes/783.fixed.md b/changes/783.fixed.md new file mode 100644 index 00000000..1c303536 --- /dev/null +++ b/changes/783.fixed.md @@ -0,0 +1 @@ +`get_object_by_uid()` and `get_event_by_uid()`, `get_todo_by_uid()`, ... failed on servers that refuse the search by UID, such as iCloud (412), and so did `save()` of a single recurrence, `save(no_overwrite=True)` and, with the `icloud` profile, `save(no_create=True)`. Unless `search.text.uid` is configured as supported, a failing search now falls back to fetching `.ics`, and on a 412 (or if `search.text.uid` is unsupported, broken or ungraceful, in which case the search is skipped) further to listing all objects of the type. On such servers, looking up a UID that is missing or not stored as `.ics` downloads all objects of the type, on every call. Servers that always name an object `.ics` can say so with `forces_uid_ics` on `save-load.stable-url`, and then a missing UID does not cost a full download. diff --git a/docs/design/FEATURE_COMPLETE_ROADMAP.md b/docs/design/FEATURE_COMPLETE_ROADMAP.md index 14c71d0e..a8aafa10 100644 --- a/docs/design/FEATURE_COMPLETE_ROADMAP.md +++ b/docs/design/FEATURE_COMPLETE_ROADMAP.md @@ -309,7 +309,7 @@ change notifications. Requested by the proposal authors themselves. **Tasks:** - [x] Robust ETag-based collision detection โ€” **done in v3.2.0**: the ETag from PUT/GET responses is cached in `self.props` and `ETagMismatchError` is raised on 412 -- [x] Proper `If-Match` / `If-None-Match` header usage โ€” `If-Match` is sent when an ETag is cached (and `If-Schedule-Tag-Match` takes precedence when a Schedule-Tag is) on save, and on delete with `delete(if_match=True)` (https://github.com/python-caldav/caldav/issues/740); `save(no_overwrite=True)` sends `If-None-Match: *` (not on servers where `save.if-none-match` is unsupported), and leaves out its UID search where `save.no-uid-conflict` is full. `save(no_create=True)` sends `If-Match: *` instead of searching, where `save-load.mutable.if-match-wildcard` is full and no ETag or Schedule-Tag is cached +- [x] Proper `If-Match` / `If-None-Match` header usage โ€” `If-Match` is sent when an ETag is cached (and `If-Schedule-Tag-Match` takes precedence when a Schedule-Tag is) on save, and on delete with `delete(if_match=True)` (https://github.com/python-caldav/caldav/issues/740); `save(no_overwrite=True)` sends `If-None-Match: *` (not on servers where `save.if-none-match` is unsupported), and leaves out its UID search where the server refuses a duplicate UID (`save.no-uid-conflict`, and its `.if-none-match` child, full, quirk or ungraceful) - with a GET of the URL in place of the header where `save.if-none-match` is unsupported. `save(no_create=True)` sends `If-Match: *` instead of searching, where `save-load.mutable.if-match-wildcard` is full and no ETag or Schedule-Tag is cached - [ ] Handle UID vs path name mismatches - [ ] Race condition mitigation - [ ] Clear error messages for conflicts diff --git a/pyproject.toml b/pyproject.toml index 478964fc..a8c55de5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -189,6 +189,7 @@ known-first-party = ["caldav"] [tool.pytest.ini_options] asyncio_mode = "strict" +asyncio_default_fixture_loop_scope = "function" filterwarnings = [ # Treat all warnings as errors by default "error", diff --git a/tests/test_caldav.py b/tests/test_caldav.py index 06d5bce7..8e412f92 100644 --- a/tests/test_caldav.py +++ b/tests/test_caldav.py @@ -2403,20 +2403,26 @@ def testNoOverwriteSameUidOtherUrl(self): """ save(no_overwrite=True) refuses an object whose UID is already in the calendar under another resource name, not only one at the - same URL. Where save.no-uid-conflict is full the library leaves - that to the server (CALDAV:no-uid-conflict, RFC 4791 section - 5.3.2.1) and skips its own UID search. + same URL. Where the server is known to refuse a duplicate UID + (save.no-uid-conflict, RFC 4791 section 5.3.2.1), the library + leaves that to the server and skips its own UID search; either + way it raises UidConflictConsistencyError - unless the server's + refusal is 'ungraceful', when it is whatever the refusal gives. """ self.skip_unless_support("save-load.event") c = self._fixCalendar() ev1_now = near_now_ics(ev1) c.add_event(ev1_now) other = Event(client=self.caldav, parent=c, data=ev1_now, url=c.url.join("other-name.ics")) - with pytest.raises(error.ConsistencyError) as exc_info: + ## The server's refusal to the PUT save(no_overwrite=True) sends - + ## also where the search runs, since it may miss the duplicate + if self.is_supported("save.if-none-match"): + level = self.is_supported("save.no-uid-conflict.if-none-match", str) + else: + level = self.is_supported("save.no-uid-conflict", str) + expected = error.DAVError if level == "ungraceful" else error.UidConflictConsistencyError + with pytest.raises(expected): other.save(no_overwrite=True) - if self.is_supported("save.no-uid-conflict") and self.is_supported("save.if-none-match"): - ## refused by the server, not by the library's UID search - assert isinstance(exc_info.value, error.UidConflictError) assert len(c.get_events()) == 1 def testRawDataRoundTrip(self): diff --git a/tests/test_caldav_unit.py b/tests/test_caldav_unit.py index 5c040c20..d24c8c02 100755 --- a/tests/test_caldav_unit.py +++ b/tests/test_caldav_unit.py @@ -2823,10 +2823,10 @@ async def fake_async_put(*args, **kwargs): nonlocal saved saved = True - todo._async_put = fake_async_put + todo._async_send_put = fake_async_put asyncio.run(todo.uncomplete()) - assert saved, "uncomplete() did not call _async_put for async client" + assert saved, "uncomplete() did not PUT for async client" assert todo.icalendar_component.get("STATUS") == "NEEDS-ACTION" assert "COMPLETED" not in todo.icalendar_component @@ -8006,7 +8006,14 @@ def base(self, request): return AsyncDAVClient def _client( - self, base, put_status=201, put_body=b"", features=None, exc=None, report=EMPTY_MULTISTATUS + self, + base, + put_status=201, + put_body=b"", + features=None, + exc=None, + report=EMPTY_MULTISTATUS, + get_status=None, ): sent = [] client = base(url="http://cal.example.com/dav/", features=features) @@ -8018,6 +8025,8 @@ def request(url, method="GET", body_=None, headers=None): if exc is not None: raise exc status, body = put_status, put_body + elif method == "GET" and get_status is not None: + status, body = get_status, b"" else: status, body = 207, report resp = mock.MagicMock() @@ -8194,6 +8203,16 @@ def test_search_skipped_where_no_uid_conflict_holds(self, base) -> None: assert [m for m, _ in sent] == ["PUT"] assert sent[-1][1]["if-none-match"] == "*" + @pytest.mark.parametrize("support", ["unsupported", "broken", "ungraceful"]) + def test_uid_lookup_kept_where_uid_search_fails(self, base, support) -> None: + """If-None-Match only guards the URL: without no-uid-conflict a + UID under another filename must still be looked for, by GET and + a scan where the UID search itself fails.""" + client, sent = self._client(base, features={"search.text.uid": {"support": support}}) + self._run(self._event(client).save(no_overwrite=True)) + assert [m for m, _ in sent] == ["GET", "REPORT", "PUT"] + assert sent[-1][1]["if-none-match"] == "*" + def test_plain_save_sends_no_if_none_match(self, base) -> None: client, sent = self._client(base, features=self.NO_UID_CONFLICT) self._run(self._event(client).save()) @@ -8267,16 +8286,122 @@ def test_uid_found_at_own_url_by_search_is_plain_consistency_error(self, base) - def test_server_ignoring_if_none_match_keeps_search(self, base) -> None: """Where If-None-Match: * is known to overwrite (Zimbra, SOGo) the - old search-then-PUT is kept, and the header is not sent.""" + header is not sent. Where the server may also take a duplicate + UID, the old search-then-PUT is kept.""" features = { "save.if-none-match": {"support": "unsupported"}, - "save.no-uid-conflict": {"support": "full"}, + "save.no-uid-conflict": {"support": "unsupported"}, } client, sent = self._client(base, features=features) self._run(self._event(client).save(no_overwrite=True)) assert [m for m, _ in sent] == ["REPORT", "PUT"] assert "if-none-match" not in sent[-1][1] + IGNORES_IF_NONE_MATCH = {"save.if-none-match": {"support": "unsupported"}} + + @pytest.mark.parametrize("support", ["full", "quirk", "ungraceful"]) + def test_server_ignoring_if_none_match_gets_url(self, base, support) -> None: + """Where the server refuses a duplicate UID, only the URL needs + guarding, and a GET of it does that instead of the search.""" + features = self.IGNORES_IF_NONE_MATCH | {"save.no-uid-conflict": {"support": support}} + client, sent = self._client(base, features=features, get_status=404) + self._run(self._event(client).save(no_overwrite=True)) + assert [m for m, _ in sent] == ["GET", "PUT"] + assert "if-none-match" not in sent[-1][1] + + def test_server_ignoring_if_none_match_url_taken(self, base) -> None: + features = self.IGNORES_IF_NONE_MATCH | self.NO_UID_CONFLICT + client, sent = self._client(base, features=features, get_status=200) + with pytest.raises(error.ConsistencyError) as exc_info: + self._run(self._event(client).save(no_overwrite=True)) + assert not isinstance(exc_info.value, error.UidConflictError) + assert [m for m, _ in sent] == ["GET"] + + def test_server_ignoring_if_none_match_unstable_url_keeps_search(self, base) -> None: + """Zimbra: a GET of a constructed URL may 404 on an existing object.""" + features = ( + self.IGNORES_IF_NONE_MATCH + | self.NO_UID_CONFLICT + | {"save-load.stable-url": {"support": "unsupported"}} + ) + client, sent = self._client(base, features=features) + self._run(self._event(client).save(no_overwrite=True)) + assert [m for m, _ in sent] == ["REPORT", "PUT"] + + @pytest.mark.parametrize("get_status", [403, 500]) + def test_server_ignoring_if_none_match_get_fails(self, base, get_status) -> None: + """A GET that does not find the object does not stop the save.""" + features = self.IGNORES_IF_NONE_MATCH | self.NO_UID_CONFLICT + client, sent = self._client(base, features=features, get_status=get_status) + self._run(self._event(client).save(no_overwrite=True)) + assert [m for m, _ in sent] == ["GET", "PUT"] + + def test_server_ignoring_if_none_match_recurrence_instance_searches(self, base) -> None: + features = self.IGNORES_IF_NONE_MATCH | self.NO_UID_CONFLICT + client, sent = self._client(base, features=features, get_status=404) + instance = ev1.replace("DTSTART", "RECURRENCE-ID:20060714T170000Z\nDTSTART", 1) + self._run( + self._event(client, data=instance).save(no_overwrite=True, only_this_recurrence=None) + ) + assert [m for m, _ in sent][0] == "REPORT" + assert "GET" not in [m for m, _ in sent] + + def test_no_create_etag_uid_conflict_stays_etag_mismatch(self, base) -> None: + """Issue https://github.com/python-caldav/caldav/issues/756: a 412 + no-uid-conflict to If-Match is still an ETagMismatchError.""" + client, sent = self._client( + base, + put_status=412, + put_body=NO_UID_CONFLICT_BODY, + features=self.NO_UID_CONFLICT, + report=EV1_MULTISTATUS, + ) + event = self._event(client, etag='"etag-a"') + with pytest.raises(error.UidConflictError) as exc_info: + self._run(event.save(no_create=True)) + assert isinstance(exc_info.value, error.ETagMismatchError) + + def test_search_missing_a_duplicate_server_refusal_is_consistency_error(self, base) -> None: + """Zimbra: no wildcard and an unstable URL, so the search runs. + A duplicate it misses (a race, a lazy index) is refused by the + server, and that is still the ConsistencyError no_overwrite gives.""" + features = ( + self.IGNORES_IF_NONE_MATCH + | self.NO_UID_CONFLICT + | {"save-load.stable-url": {"support": "unsupported"}} + ) + client, sent = self._client( + base, put_status=409, put_body=NO_UID_CONFLICT_BODY, features=features + ) + with pytest.raises(error.UidConflictConsistencyError): + self._run(self._event(client).save(no_overwrite=True)) + assert [m for m, _ in sent] == ["REPORT", "PUT"] + + def test_no_create_search_path_uid_conflict_is_consistency_error(self, base) -> None: + """save(no_create=True) where If-Match: * cannot be used searches; + the server's refusal of the UID is a ConsistencyError there too.""" + features = {"save-load.mutable.if-match-wildcard": {"support": "unsupported"}} + client, sent = self._client( + base, + put_status=409, + put_body=NO_UID_CONFLICT_BODY, + features=features, + report=EV1_MULTISTATUS, + ) + with pytest.raises(error.UidConflictConsistencyError): + self._run(self._event(client).save(no_create=True)) + assert [m for m, _ in sent] == ["REPORT", "PUT"] + + def test_server_ignoring_if_none_match_uid_conflict_is_consistency_error(self, base) -> None: + """No wildcard goes out, but no_overwrite was asked for: the + server's refusal is still the ConsistencyError the search gave.""" + features = self.IGNORES_IF_NONE_MATCH | self.NO_UID_CONFLICT + client, sent = self._client( + base, put_status=409, put_body=NO_UID_CONFLICT_BODY, features=features, get_status=404 + ) + with pytest.raises(error.UidConflictConsistencyError): + self._run(self._event(client).save(no_overwrite=True)) + def test_recurrence_instance_keeps_search(self, base) -> None: """A recurrence instance is merged into its master, which the PUT then overwrites - only the search can refuse that.""" @@ -8343,6 +8468,186 @@ def test_plain_save_failure_still_retried(self, base) -> None: ).encode() +class TestUidConflictBareStatus(_SaveRequests): + """A server refusing a duplicate UID without the CALDAV:no-uid-conflict + precondition (RFC 4791 section 5.3.2.1), but with a status the tester + has confirmed (save.no-uid-conflict 'quirk', extra keys 'status' and + 'precondition'). Such a status alone is no proof, so the library + reads it as a UID conflict only when a GET finds nothing at the URL.""" + + ## Bedework: 403 CALDAV:no-uid-conflict to a plain PUT, a bare 412 + ## when it carries If-None-Match: * + BEDEWORK = { + "save.no-uid-conflict": {"support": "full"}, + "save.no-uid-conflict.if-none-match": { + "support": "quirk", + "status": 412, + "precondition": False, + }, + } + BARE_409 = {"save.no-uid-conflict": {"support": "quirk", "status": 409, "precondition": False}} + + def test_search_skipped(self, base) -> None: + client, sent = self._client(base, features=self.BEDEWORK) + self._run(self._event(client).save(no_overwrite=True)) + assert [m for m, _ in sent] == ["PUT"] + assert sent[-1][1]["if-none-match"] == "*" + + def test_search_skipped_where_ungraceful(self, base) -> None: + """The server refuses the duplicate, if not recognisably: no + duplicate can be stored, so the search adds nothing.""" + features = {"save.no-uid-conflict": {"support": "ungraceful"}} + client, sent = self._client(base, features=features) + self._run(self._event(client).save(no_overwrite=True)) + assert [m for m, _ in sent] == ["PUT"] + + def test_bare_412_and_no_object_at_url_is_uid_conflict(self, base) -> None: + client, sent = self._client(base, put_status=412, features=self.BEDEWORK, get_status=404) + with pytest.raises(error.UidConflictConsistencyError) as exc_info: + self._run(self._event(client).save(no_overwrite=True)) + assert exc_info.value.status == 412 + assert [m for m, _ in sent] == ["PUT", "GET"] + + def test_bare_412_and_object_at_url_is_url_taken(self, base) -> None: + client, sent = self._client(base, put_status=412, features=self.BEDEWORK, get_status=200) + with pytest.raises(error.ConsistencyError) as exc_info: + self._run(self._event(client).save(no_overwrite=True)) + assert not isinstance(exc_info.value, error.UidConflictError) + + def test_plain_save_bare_409_is_uid_conflict(self, base) -> None: + client, sent = self._client(base, put_status=409, features=self.BARE_409, get_status=404) + with pytest.raises(error.UidConflictError) as exc_info: + self._run(self._event(client).save()) + assert not isinstance(exc_info.value, error.ConsistencyError) + assert exc_info.value.status == 409 + ## No retry with cleaned-up data: that cannot free the UID + assert [m for m, _ in sent] == ["PUT", "GET"] + + def test_plain_save_bare_409_and_object_at_url_one_get(self, base) -> None: + client, sent = self._client(base, put_status=409, features=self.BARE_409, get_status=200) + with pytest.raises(error.PutError): + self._run(self._event(client).save()) + assert [m for m, _ in sent] == ["PUT", "GET"] + + def test_raw_data_bare_409_is_uid_conflict(self, base) -> None: + client, sent = self._client(base, put_status=409, features=self.BARE_409, get_status=404) + event = self._event(client) + with pytest.raises(error.UidConflictError): + self._run(event.save(raw_data=ev1.encode())) + assert [m for m, _ in sent] == ["PUT", "GET"] + + def test_icloud_profile_skips_search(self, base) -> None: + """Only the .if-none-match child is known for iCloud.""" + from caldav import compatibility_hints + + client, sent = self._client(base, features=compatibility_hints.icloud) + self._run(self._event(client).save(no_overwrite=True)) + assert [m for m, _ in sent] == ["PUT"] + + def test_icloud_profile_plain_save_bare_412_is_uid_conflict(self, base) -> None: + """iCloud refuses a plain PUT of a taken UID with a bare 412 too + (measured by caldav-server-tester, PR 826).""" + from caldav import compatibility_hints + + client, sent = self._client( + base, put_status=412, features=compatibility_hints.icloud, get_status=404 + ) + with pytest.raises(error.UidConflictError) as exc_info: + self._run(self._event(client).save()) + assert not isinstance(exc_info.value, error.ConsistencyError) + assert [m for m, _ in sent] == ["PUT", "GET"] + + def test_bare_412_message_does_not_claim_the_url(self, base) -> None: + """Where the server refuses duplicates unrecognisably, a 412 to + If-None-Match: * may be about the UID: the message says either.""" + features = {"save.no-uid-conflict": {"support": "ungraceful"}} + client, sent = self._client(base, put_status=412, features=features) + with pytest.raises(error.ConsistencyError) as exc_info: + self._run(self._event(client).save(no_overwrite=True)) + assert "UID" in str(exc_info.value) + + def test_plain_save_bare_409_and_object_at_url_is_put_error(self, base) -> None: + client, sent = self._client(base, put_status=409, features=self.BARE_409, get_status=200) + with pytest.raises(error.PutError) as exc_info: + self._run(self._event(client).save()) + assert not isinstance(exc_info.value, error.UidConflictError) + + def test_no_uid_conflict_where_a_missing_object_is_no_404(self, base) -> None: + """The GET proves nothing where the server does not answer 404 for + a missing object (non-existing-raises-not-found.object).""" + features = self.BARE_409 | { + "non-existing-raises-not-found.object": {"support": "unsupported"} + } + client, sent = self._client(base, put_status=409, features=features, get_status=404) + with pytest.raises(error.PutError) as exc_info: + self._run(self._event(client).save()) + assert not isinstance(exc_info.value, error.UidConflictError) + assert "GET" not in [m for m, _ in sent] + + def test_conditional_save_is_not_read_as_uid_conflict(self, base) -> None: + """A refused If-Match is about the object at the URL: one deleted + meanwhile also leaves nothing there for the GET to find.""" + client, sent = self._client(base, put_status=409, features=self.BARE_409, get_status=404) + with pytest.raises(error.PutError) as exc_info: + self._run(self._event(client, etag='"etag-a"').save()) + assert not isinstance(exc_info.value, error.UidConflictError) + assert "GET" not in [m for m, _ in sent] + + def test_other_status_is_not_read_as_uid_conflict(self, base) -> None: + client, sent = self._client(base, put_status=412, features=self.BARE_409, get_status=404) + with pytest.raises(error.PutError) as exc_info: + self._run(self._event(client).save()) + assert not isinstance(exc_info.value, error.UidConflictError) + assert "GET" not in [m for m, _ in sent] + + @pytest.mark.parametrize( + "node", + [ + {"support": "ungraceful"}, + {"support": "quirk", "status": 403, "precondition": False}, + ], + ) + def test_bare_403_is_not_read_as_uid_conflict(self, base, node) -> None: + """403 is also what a read-only calendar answers: it may be anything.""" + exc = error.AuthorizationError( + url=self.URL + "ev1.ics", reason="Forbidden", status=403, body=b"" + ) + client, sent = self._client( + base, features={"save.no-uid-conflict": node}, exc=exc, get_status=404 + ) + with pytest.raises(error.AuthorizationError) as exc_info: + self._run(self._event(client).save()) + assert not isinstance(exc_info.value, error.UidConflictError) + assert "GET" not in [m for m, _ in sent] + + def test_if_none_match_variant_inherits_parent(self) -> None: + from caldav.compatibility_hints import FeatureSet + + fs = FeatureSet(self.BARE_409) + node = fs.is_supported("save.no-uid-conflict.if-none-match", return_type=dict) + assert node["status"] == 409 + + @pytest.mark.parametrize("server", ["sogo", "purelymail"]) + def test_href_given_not_inherited_where_unprobed(self, server) -> None: + """A refusal without the precondition has no href to read: the + child must not take the parent's level.""" + from caldav import compatibility_hints + from caldav.compatibility_hints import FeatureSet + + fs = FeatureSet(getattr(compatibility_hints, server)) + assert fs.is_supported("save.no-uid-conflict.href-given", return_type=str) == "unknown" + + @pytest.mark.parametrize("server", ["bedework_3_10_3", "bedework_5_0_0"]) + def test_hints_of_bedework(self, server) -> None: + from caldav import compatibility_hints + from caldav.compatibility_hints import FeatureSet + + fs = FeatureSet(getattr(compatibility_hints, server)) + assert fs.is_supported("save.no-uid-conflict", return_type=str) == "full" + node = fs.is_supported("save.no-uid-conflict.if-none-match", return_type=dict) + assert (node["support"], node["status"], node["precondition"]) == ("quirk", 412, False) + + class TestNoCreateIfMatch(_SaveRequests): """save(no_create=True) sends If-Match: * (RFC 9110 section 13.1.1), so the server refuses to create a missing object in the same request, diff --git a/tests/test_uid_lookup_fallback.py b/tests/test_uid_lookup_fallback.py new file mode 100644 index 00000000..85d9fa61 --- /dev/null +++ b/tests/test_uid_lookup_fallback.py @@ -0,0 +1,392 @@ +""" +Unit tests for the fallback in get_object_by_uid() when the server fails +the UID search (issue #783: iCloud answers it with 412). +""" + +import asyncio +from unittest import mock + +import pytest + +from caldav.async_davclient import AsyncDAVClient +from caldav.calendarobjectresource import Event, Todo +from caldav.collection import Calendar +from caldav.davclient import DAVClient +from caldav.elements import cdav, dav +from caldav.lib import error +from caldav.response import DAVResponse + +CAL_URL = "https://caldav.example.com/calendars/user/cal/" +UID = "c0ffee-1234@example.com" +SLASH_UID = "imported/slash uid" + + +def ical(uid, comp="VEVENT"): + return ( + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//test//test//EN\r\n" + f"BEGIN:{comp}\r\nUID:{uid}\r\nDTSTAMP:20261019T120000Z\r\n" + "DTSTART:20261025T090000Z\r\nSUMMARY:test\r\n" + f"END:{comp}\r\nEND:VCALENDAR\r\n" + ) + + +def multistatus(*objects): + responses = "".join( + f"""{href} +{data} +HTTP/1.1 200 OK""" + for href, data in objects + ) + return ( + '' + f"{responses}" + ) + + +def response(status, body="", headers=None): + resp = mock.MagicMock() + resp.status_code = status + resp.reason = resp.reason_phrase = "test" + resp.headers = headers or {} + resp.content = body + return DAVResponse(resp) + + +PRECONDITION_FAILED = (412, "") +SERVER_ERROR = (500, "Internal Server Error") +NOT_FOUND = (404, "") +EMPTY = (207, multistatus()) + + +class ScriptedDAVClient(DAVClient): + """Answers each request with the next entry of a script: a (status, + body[, headers]) tuple, or an exception to raise the way the real client + raises on 401/403/429. Records (method, url) of every request.""" + + def __init__(self, script, features=None): + self.script = list(script) + self.requests = [] + DAVClient.__init__(self, url="https://caldav.example.com/", features=features) + + def request(self, url, method="GET", body="", headers=None): + self.requests.append((method, str(url))) + answer = self.script.pop(0) + if isinstance(answer, Exception): + raise answer + return response(*answer) + + +class AsyncScriptedDAVClient(ScriptedDAVClient, AsyncDAVClient): + async def request(self, url, method="GET", body="", headers=None): + return ScriptedDAVClient.request(self, url, method, body, headers) + + +def calendar(script, features=None, is_async=False): + client = ScriptedDAVClient(script, features=features) + if is_async: + client.__class__ = AsyncScriptedDAVClient + return Calendar(client, url=CAL_URL) + + +def methods(cal): + return [method for method, _ in cal.client.requests] + + +def forbidden(): + return error.AuthorizationError(CAL_URL, "Forbidden") + + +class TestUidLookupFallback: + def test_get_by_uid_name(self): + cal = calendar( + [PRECONDITION_FAILED, (200, ical(UID), {"Etag": '"etag-1"'})], + ) + obj = cal.get_event_by_uid(UID) + assert isinstance(obj, Event) + assert obj.id == UID + assert obj.props[dav.GetEtag.tag] == '"etag-1"' + assert obj.raw_data == ical(UID).encode() + assert methods(cal) == ["REPORT", "GET"] + assert cal.client.requests[1][1] == CAL_URL + "c0ffee-1234%40example.com.ics" + assert str(obj.url) == CAL_URL + "c0ffee-1234%40example.com.ics" + + def test_scan_when_not_found_by_name(self): + cal = calendar( + [ + PRECONDITION_FAILED, + NOT_FOUND, + ( + 207, + multistatus( + ("/calendars/user/cal/other.ics", ical("other")), + ("/calendars/user/cal/imported-1.ics", ical(SLASH_UID)), + ), + ), + ] + ) + obj = cal.get_event_by_uid(SLASH_UID) + assert obj.id == SLASH_UID + assert str(obj.url) == CAL_URL + "imported-1.ics" + assert methods(cal) == ["REPORT", "GET", "REPORT"] + + @pytest.mark.parametrize( + "get_answer", + [ + (200, ical("some-other-uid")), + SERVER_ERROR, + forbidden(), + ], + ids=["other-uid", "500", "403"], + ) + def test_scan_when_get_gives_something_else(self, get_answer): + cal = calendar( + [ + PRECONDITION_FAILED, + get_answer, + (207, multistatus(("/calendars/user/cal/ev.ics", ical(UID)))), + ] + ) + assert cal.get_event_by_uid(UID).id == UID + assert methods(cal) == ["REPORT", "GET", "REPORT"] + + def test_get_of_wrong_type_is_not_taken(self): + cal = calendar([PRECONDITION_FAILED, (200, ical(UID)), EMPTY]) + with pytest.raises(error.NotFoundError): + cal.get_todo_by_uid(UID) + assert methods(cal) == ["REPORT", "GET", "REPORT"] + + def test_no_comp_class_gives_the_right_class(self): + cal = calendar([PRECONDITION_FAILED, (200, ical(UID, "VTODO"), {"Etag": '"t"'})]) + obj = cal.get_object_by_uid(UID) + assert isinstance(obj, Todo) + assert obj.props[dav.GetEtag.tag] == '"t"' + + def test_no_get_when_get_by_url_is_unsupported(self): + cal = calendar( + [PRECONDITION_FAILED, (207, multistatus(("/calendars/user/cal/ev.ics", ical(UID))))], + features={"save-load.get-by-url": {"support": "unsupported"}}, + ) + assert cal.get_event_by_uid(UID).id == UID + assert methods(cal) == ["REPORT", "REPORT"] + + def test_server_error_falls_back(self): + cal = calendar([SERVER_ERROR, (200, ical(UID))]) + assert cal.get_event_by_uid(UID).id == UID + assert methods(cal)[-1] == "GET" + + @pytest.mark.parametrize( + "search_failure, raised", + [ + (forbidden(), error.AuthorizationError), + (error.RateLimitError(CAL_URL, "Too Many Requests"), error.RateLimitError), + (NOT_FOUND, error.NotFoundError), + ], + ids=["403", "429", "404"], + ) + def test_search_failures_that_are_raised(self, search_failure, raised): + cal = calendar([search_failure]) + with pytest.raises(raised): + cal.get_event_by_uid(UID) + assert methods(cal) == ["REPORT"] + + def test_empty_search_result_is_not_found(self): + cal = calendar([EMPTY]) + with pytest.raises(error.NotFoundError): + cal.get_event_by_uid(UID) + assert methods(cal) == ["REPORT"] + + def test_failing_scan_raises_its_own_error(self): + cal = calendar([PRECONDITION_FAILED, NOT_FOUND, forbidden()]) + with pytest.raises(error.AuthorizationError) as excinfo: + cal.get_event_by_uid(UID) + assert "412" in str(excinfo.value.__cause__) + + def test_comp_filter_is_not_retried(self): + cal = calendar([PRECONDITION_FAILED]) + with pytest.raises(error.ReportError): + cal.get_object_by_uid(UID, comp_filter=cdav.CompFilter("VEVENT")) + assert methods(cal) == ["REPORT"] + + +class TestUidLookupFallbackAsync: + def test_get_by_uid_name(self): + cal = calendar([PRECONDITION_FAILED, (200, ical(UID), {"Etag": '"etag-1"'})], is_async=True) + obj = asyncio.run(cal.get_event_by_uid(UID)) + assert obj.id == UID + assert obj.props[dav.GetEtag.tag] == '"etag-1"' + assert methods(cal) == ["REPORT", "GET"] + + def test_scan_when_not_found_by_name(self): + cal = calendar( + [ + PRECONDITION_FAILED, + NOT_FOUND, + (207, multistatus(("/calendars/user/cal/imported-1.ics", ical(SLASH_UID)))), + ], + is_async=True, + ) + assert asyncio.run(cal.get_event_by_uid(SLASH_UID)).id == SLASH_UID + assert methods(cal) == ["REPORT", "GET", "REPORT"] + + +def uid_feature(support): + return {"search.text.uid": {"support": support}} + + +SCAN_ANSWER = (207, multistatus(("/calendars/user/cal/ev.ics", ical(UID)))) + + +class TestUidLookupFeature: + """What the search.text.uid feature changes: whether the UID search is + tried, whether it may fall back to GET, and whether to scan.""" + + @pytest.mark.parametrize("support", ["unsupported", "broken", "ungraceful"]) + def test_search_is_skipped_when_known_to_fail(self, support): + cal = calendar([(200, ical(UID))], features=uid_feature(support)) + assert cal.get_event_by_uid(UID).id == UID + assert methods(cal) == ["GET"] + + @pytest.mark.parametrize("support", ["unsupported", "broken", "ungraceful"]) + def test_scan_after_get_when_known_to_fail(self, support): + cal = calendar([NOT_FOUND, SCAN_ANSWER], features=uid_feature(support)) + assert cal.get_event_by_uid(UID).id == UID + assert methods(cal) == ["GET", "REPORT"] + + def test_known_to_fail_without_get_by_url(self): + cal = calendar( + [SCAN_ANSWER], + features={ + **uid_feature("unsupported"), + "save-load.get-by-url": {"support": "unsupported"}, + }, + ) + assert cal.get_event_by_uid(UID).id == UID + assert methods(cal) == ["REPORT"] + + @pytest.mark.parametrize("support", ["full", "quirk"]) + def test_explicitly_supported_has_no_fallback(self, support): + cal = calendar([PRECONDITION_FAILED], features=uid_feature(support)) + with pytest.raises(error.ReportError): + cal.get_event_by_uid(UID) + assert methods(cal) == ["REPORT"] + + @pytest.mark.parametrize("support", [None, "fragile", "unknown"]) + def test_other_failures_try_get_but_do_not_scan(self, support): + """The search failure is raised, not turned into "not found": a + GET miss does not show the object is absent.""" + features = uid_feature(support) if support else None + cal = calendar([SERVER_ERROR, NOT_FOUND], features=features) + with pytest.raises(error.ReportError) as excinfo: + cal.get_event_by_uid(UID) + assert not isinstance(excinfo.value, error.NotFoundError) + assert methods(cal) == ["REPORT", "GET"] + + def test_other_failure_is_overridden_by_a_get_hit(self): + cal = calendar([SERVER_ERROR, (200, ical(UID))]) + assert cal.get_event_by_uid(UID).id == UID + + @pytest.mark.parametrize("support", [None, "fragile", "unknown"]) + def test_412_scans_unless_explicitly_supported(self, support): + features = uid_feature(support) if support else None + cal = calendar([PRECONDITION_FAILED, NOT_FOUND, SCAN_ANSWER], features=features) + assert cal.get_event_by_uid(UID).id == UID + assert methods(cal) == ["REPORT", "GET", "REPORT"] + + def test_async_search_is_skipped_when_known_to_fail(self): + cal = calendar([NOT_FOUND, SCAN_ANSWER], features=uid_feature("broken"), is_async=True) + assert asyncio.run(cal.get_event_by_uid(UID)).id == UID + assert methods(cal) == ["GET", "REPORT"] + + def test_async_other_failures_do_not_scan(self): + cal = calendar([SERVER_ERROR, NOT_FOUND], is_async=True) + with pytest.raises(error.ReportError): + asyncio.run(cal.get_event_by_uid(UID)) + assert methods(cal) == ["REPORT", "GET"] + + +FORCES_UID_ICS = {"save-load.stable-url": {"support": "unsupported", "forces_uid_ics": True}} + + +class TestUidIcsEnforced: + """A server that always names an object .ics has nothing for the + full scan to find when the GET misses.""" + + @pytest.mark.parametrize("miss", [NOT_FOUND, (410, "")]) + def test_no_scan_when_get_misses(self, miss): + cal = calendar([PRECONDITION_FAILED, miss], features=FORCES_UID_ICS) + with pytest.raises(error.NotFoundError): + cal.get_event_by_uid(UID) + assert methods(cal) == ["REPORT", "GET"] + + def test_no_scan_when_search_is_skipped(self): + cal = calendar([NOT_FOUND], features={**FORCES_UID_ICS, **uid_feature("ungraceful")}) + with pytest.raises(error.NotFoundError): + cal.get_event_by_uid(UID) + assert methods(cal) == ["GET"] + + @pytest.mark.parametrize("is_async", [False, True]) + def test_get_miss_is_conclusive_after_other_search_failure(self, is_async): + """The GET miss decides, not the search error.""" + cal = calendar([SERVER_ERROR, NOT_FOUND], features=FORCES_UID_ICS, is_async=is_async) + with pytest.raises(error.NotFoundError): + asyncio.run(cal.get_event_by_uid(UID)) if is_async else cal.get_event_by_uid(UID) + assert methods(cal) == ["REPORT", "GET"] + + def test_scan_when_get_fails_otherwise(self): + cal = calendar([PRECONDITION_FAILED, SERVER_ERROR, SCAN_ANSWER], features=FORCES_UID_ICS) + assert cal.get_event_by_uid(UID).id == UID + assert methods(cal) == ["REPORT", "GET", "REPORT"] + + def test_scan_when_not_enforced(self): + features = {"save-load.stable-url": {"support": "unsupported"}} + cal = calendar([PRECONDITION_FAILED, NOT_FOUND, SCAN_ANSWER], features=features) + assert cal.get_event_by_uid(UID).id == UID + + def test_async_no_scan_when_get_misses(self): + cal = calendar([PRECONDITION_FAILED, NOT_FOUND], features=FORCES_UID_ICS, is_async=True) + with pytest.raises(error.NotFoundError): + asyncio.run(cal.get_event_by_uid(UID)) + assert methods(cal) == ["REPORT", "GET"] + + +class TestUidLookupFallbackAsyncErrors: + @pytest.mark.parametrize( + "search_failure, raised", + [ + (forbidden(), error.AuthorizationError), + (error.RateLimitError(CAL_URL, "Too Many Requests"), error.RateLimitError), + ], + ids=["403", "429"], + ) + def test_search_failures_that_are_raised(self, search_failure, raised): + cal = calendar([search_failure], is_async=True) + with pytest.raises(raised): + asyncio.run(cal.get_event_by_uid(UID)) + assert methods(cal) == ["REPORT"] + + def test_failing_scan_is_chained_to_the_412(self): + cal = calendar([PRECONDITION_FAILED, NOT_FOUND, forbidden()], is_async=True) + with pytest.raises(error.AuthorizationError) as excinfo: + asyncio.run(cal.get_event_by_uid(UID)) + assert "412" in str(excinfo.value.__cause__) + + +class TestMeasuredProfiles: + """The profiles say what the tester measured: Robur ignores the UID + filter, while Zimbra and Bedework search by UID although they do not + search text.""" + + def test_robur_skips_the_search(self): + from caldav import compatibility_hints + + cal = calendar([NOT_FOUND, SCAN_ANSWER], features=compatibility_hints.robur) + assert cal.get_event_by_uid(UID).id == UID + assert methods(cal) == ["GET", "REPORT"] + + @pytest.mark.parametrize("profile", ["zimbra", "bedework_3_10_3", "bedework_5_0_0"]) + def test_profiles_with_a_working_uid_search_keep_it(self, profile): + from caldav import compatibility_hints + + cal = calendar([SCAN_ANSWER], features=getattr(compatibility_hints, profile)) + assert cal.get_event_by_uid(UID).id == UID + assert methods(cal) == ["REPORT"]