test_base.py 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2012-2017 Guillaume Ayoub
  3. # Copyright © 2017-2022 Unrud <unrud@outlook.com>
  4. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Radicale tests with simple requests.
  20. """
  21. import logging
  22. import os
  23. import posixpath
  24. from typing import Any, Callable, ClassVar, Iterable, List, Optional, Tuple
  25. import defusedxml.ElementTree as DefusedET
  26. import vobject
  27. from radicale import storage, xmlutils
  28. from radicale.tests import RESPONSES, BaseTest
  29. from radicale.tests.helpers import get_file_content
  30. class TestBaseRequests(BaseTest):
  31. """Tests with simple requests."""
  32. # Allow skipping sync-token tests, when not fully supported by the backend
  33. full_sync_token_support: ClassVar[bool] = True
  34. def setup_method(self) -> None:
  35. BaseTest.setup_method(self)
  36. rights_file_path = os.path.join(self.colpath, "rights")
  37. with open(rights_file_path, "w") as f:
  38. f.write("""\
  39. [permit delete collection]
  40. user: .*
  41. collection: test-permit-delete
  42. permissions: RrWwD
  43. [forbid delete collection]
  44. user: .*
  45. collection: test-forbid-delete
  46. permissions: RrWwd
  47. [permit overwrite collection]
  48. user: .*
  49. collection: test-permit-overwrite
  50. permissions: RrWwO
  51. [forbid overwrite collection]
  52. user: .*
  53. collection: test-forbid-overwrite
  54. permissions: RrWwo
  55. [allow all]
  56. user: .*
  57. collection: .*
  58. permissions: RrWw""")
  59. self.configure({"rights": {"file": rights_file_path,
  60. "type": "from_file"}})
  61. def test_root(self) -> None:
  62. """GET request at "/"."""
  63. for path in ["", "/", "//"]:
  64. _, headers, answer = self.request("GET", path, check=302)
  65. assert headers.get("Location") == "/.web"
  66. assert answer == "Redirected to /.web"
  67. def test_root_script_name(self) -> None:
  68. """GET request at "/" with SCRIPT_NAME."""
  69. for path in ["", "/", "//"]:
  70. _, headers, _ = self.request("GET", path, check=302,
  71. SCRIPT_NAME="/radicale")
  72. assert headers.get("Location") == "/radicale/.web"
  73. def test_root_broken_script_name(self) -> None:
  74. """GET request at "/" with SCRIPT_NAME ending with "/"."""
  75. for script_name, prefix in [
  76. ("/", ""), ("//", ""), ("/radicale/", "/radicale"),
  77. ("radicale", None), ("radicale//", None)]:
  78. _, headers, _ = self.request(
  79. "GET", "/", check=500 if prefix is None else 302,
  80. SCRIPT_NAME=script_name)
  81. assert (prefix is None or
  82. headers.get("Location") == prefix + "/.web")
  83. def test_root_http_x_script_name(self) -> None:
  84. """GET request at "/" with HTTP_X_SCRIPT_NAME."""
  85. for path in ["", "/", "//"]:
  86. _, headers, _ = self.request("GET", path, check=302,
  87. HTTP_X_SCRIPT_NAME="/radicale")
  88. assert headers.get("Location") == "/radicale/.web"
  89. def test_root_broken_http_x_script_name(self) -> None:
  90. """GET request at "/" with HTTP_X_SCRIPT_NAME ending with "/"."""
  91. for script_name, prefix in [
  92. ("/", ""), ("//", ""), ("/radicale/", "/radicale"),
  93. ("radicale", None), ("radicale//", None)]:
  94. _, headers, _ = self.request(
  95. "GET", "/", check=400 if prefix is None else 302,
  96. HTTP_X_SCRIPT_NAME=script_name)
  97. assert (prefix is None or
  98. headers.get("Location") == prefix + "/.web")
  99. def test_sanitized_path(self) -> None:
  100. """GET request with unsanitized paths."""
  101. for path, sane_path in [
  102. ("//.web", "/.web"), ("//.web/", "/.web/"),
  103. ("/.web//", "/.web/"), ("/.web/a//b", "/.web/a/b")]:
  104. _, headers, _ = self.request("GET", path, check=301)
  105. assert headers.get("Location") == sane_path
  106. _, headers, _ = self.request("GET", path, check=301,
  107. SCRIPT_NAME="/radicale")
  108. assert headers.get("Location") == "/radicale%s" % sane_path
  109. _, headers, _ = self.request("GET", path, check=301,
  110. HTTP_X_SCRIPT_NAME="/radicale")
  111. assert headers.get("Location") == "/radicale%s" % sane_path
  112. def test_add_event(self) -> None:
  113. """Add an event."""
  114. self.mkcalendar("/calendar.ics/")
  115. event = get_file_content("event1.ics")
  116. path = "/calendar.ics/event1.ics"
  117. self.put(path, event)
  118. _, headers, answer = self.request("GET", path, check=200)
  119. assert "ETag" in headers
  120. assert headers["Content-Type"] == "text/calendar; charset=utf-8"
  121. assert "VEVENT" in answer
  122. assert "Event" in answer
  123. assert "UID:event" in answer
  124. def test_add_event_broken(self) -> None:
  125. """Add a broken event."""
  126. self.mkcalendar("/calendar.ics/")
  127. event = get_file_content("broken-vevent.ics")
  128. path = "/calendar.ics/broken-vevent.ics"
  129. self.put(path, event, check=400)
  130. def test_add_events_broken2(self) -> None:
  131. """Add a broken event (2nd one is broken)."""
  132. self.mkcalendar("/calendar.ics/")
  133. event = get_file_content("broken-vevents.ics")
  134. path = "/calendar.ics/"
  135. self.put(path, event, check=400)
  136. def test_add_event_without_uid(self) -> None:
  137. """Add an event without UID."""
  138. self.mkcalendar("/calendar.ics/")
  139. event = get_file_content("event1.ics").replace("UID:event1\n", "")
  140. assert "\nUID:" not in event
  141. path = "/calendar.ics/event.ics"
  142. self.put(path, event, check=400)
  143. def test_add_event_duplicate_uid(self) -> None:
  144. """Add an event with an existing UID."""
  145. self.mkcalendar("/calendar.ics/")
  146. event = get_file_content("event1.ics")
  147. self.put("/calendar.ics/event1.ics", event)
  148. status, answer = self.put(
  149. "/calendar.ics/event1-duplicate.ics", event, check=None)
  150. assert status in (403, 409)
  151. xml = DefusedET.fromstring(answer)
  152. assert xml.tag == xmlutils.make_clark("D:error")
  153. assert xml.find(xmlutils.make_clark("C:no-uid-conflict")) is not None
  154. def test_add_event_with_mixed_datetime_and_date(self) -> None:
  155. """Test event with DTSTART as DATE-TIME and EXDATE as DATE."""
  156. self.mkcalendar("/calendar.ics/")
  157. event = get_file_content("event_mixed_datetime_and_date.ics")
  158. self.put("/calendar.ics/event.ics", event)
  159. def test_add_event_with_exdate_without_rrule(self) -> None:
  160. """Test event with EXDATE but not having RRULE."""
  161. self.mkcalendar("/calendar.ics/")
  162. event = get_file_content("event_exdate_without_rrule.ics")
  163. self.put("/calendar.ics/event.ics", event)
  164. def test_add_todo(self) -> None:
  165. """Add a todo."""
  166. self.mkcalendar("/calendar.ics/")
  167. todo = get_file_content("todo1.ics")
  168. path = "/calendar.ics/todo1.ics"
  169. self.put(path, todo)
  170. _, headers, answer = self.request("GET", path, check=200)
  171. assert "ETag" in headers
  172. assert headers["Content-Type"] == "text/calendar; charset=utf-8"
  173. assert "VTODO" in answer
  174. assert "Todo" in answer
  175. assert "UID:todo" in answer
  176. def test_add_contact(self) -> None:
  177. """Add a contact."""
  178. self.create_addressbook("/contacts.vcf/")
  179. contact = get_file_content("contact1.vcf")
  180. path = "/contacts.vcf/contact.vcf"
  181. self.put(path, contact)
  182. _, headers, answer = self.request("GET", path, check=200)
  183. assert "ETag" in headers
  184. assert headers["Content-Type"] == "text/vcard; charset=utf-8"
  185. assert "VCARD" in answer
  186. assert "UID:contact1" in answer
  187. _, answer = self.get(path)
  188. assert "UID:contact1" in answer
  189. def test_add_contact_broken(self) -> None:
  190. """Add a broken contact."""
  191. self.create_addressbook("/contacts.vcf/")
  192. contact = get_file_content("broken-vcard.vcf")
  193. path = "/contacts.vcf/broken-vcards.vcf"
  194. self.put(path, contact, check=400)
  195. def test_add_contacts_broken(self) -> None:
  196. """Add broken contacts."""
  197. self.create_addressbook("/contacts.vcf/")
  198. contact = get_file_content("broken-vcards.vcf")
  199. path = "/contacts.vcf/"
  200. self.put(path, contact, check=400)
  201. def test_add_contacts_broken2(self) -> None:
  202. """Add broken contacts (only 2nd one is broken)."""
  203. self.create_addressbook("/contacts.vcf/")
  204. contact = get_file_content("broken-vcards2.vcf")
  205. path = "/contacts.vcf/"
  206. self.put(path, contact, check=400)
  207. def test_add_contacts_broken2_no_uid(self) -> None:
  208. """Add broken contacts (only 2nd one is broken and has no UID)."""
  209. self.create_addressbook("/contacts.vcf/")
  210. contact = get_file_content("broken-vcards2-no_uid.vcf")
  211. path = "/contacts.vcf/"
  212. self.put(path, contact, check=400)
  213. def test_add_contact_photo_with_data_uri(self) -> None:
  214. """Test workaround for broken PHOTO data from InfCloud"""
  215. self.create_addressbook("/contacts.vcf/")
  216. contact = get_file_content("contact_photo_with_data_uri.vcf")
  217. self.put("/contacts.vcf/contact.vcf", contact)
  218. def test_add_contact_without_uid(self) -> None:
  219. """Add a contact without UID."""
  220. self.create_addressbook("/contacts.vcf/")
  221. contact = get_file_content("contact1.vcf").replace("UID:contact1\n",
  222. "")
  223. assert "\nUID" not in contact
  224. path = "/contacts.vcf/contact.vcf"
  225. self.put(path, contact, check=400)
  226. def test_update_event(self) -> None:
  227. """Update an event."""
  228. self.mkcalendar("/calendar.ics/")
  229. event = get_file_content("event1.ics")
  230. event_modified = get_file_content("event1_modified.ics")
  231. path = "/calendar.ics/event1.ics"
  232. self.put(path, event)
  233. self.put(path, event_modified, check=204)
  234. _, answer = self.get("/calendar.ics/")
  235. assert answer.count("BEGIN:VEVENT") == 1
  236. _, answer = self.get(path)
  237. assert "DTSTAMP:20130902T150159Z" in answer
  238. def test_update_event_no_etag_strict_preconditions_true(self) -> None:
  239. """Update an event without serving etag having strict_preconditions enabled (Precondition Failed)."""
  240. self.configure({"storage": {"strict_preconditions": True}})
  241. self.mkcalendar("/calendar.ics/")
  242. event = get_file_content("event1.ics")
  243. event_modified = get_file_content("event1_modified.ics")
  244. path = "/calendar.ics/event1.ics"
  245. self.put(path, event, check=201)
  246. self.put(path, event_modified, check=412)
  247. def test_update_event_with_etag_strict_preconditions_true(self) -> None:
  248. """Update an event with serving equal etag having strict_preconditions enabled (OK)."""
  249. self.configure({"storage": {"strict_preconditions": True}})
  250. self.configure({"logging": {"response_content_on_debug": True}})
  251. self.mkcalendar("/calendar.ics/")
  252. event = get_file_content("event1.ics")
  253. event_modified = get_file_content("event1_modified.ics")
  254. path = "/calendar.ics/event1.ics"
  255. self.put(path, event, check=201)
  256. # get etag
  257. _, responses = self.report("/calendar.ics/", """\
  258. <?xml version="1.0" encoding="utf-8" ?>
  259. <C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav">
  260. <D:prop xmlns:D="DAV:">
  261. <D:getetag/>
  262. </D:prop>
  263. </C:calendar-query>""")
  264. assert len(responses) == 1
  265. response = responses["/calendar.ics/event1.ics"]
  266. assert not isinstance(response, int)
  267. status, prop = response["D:getetag"]
  268. assert status == 200 and prop.text
  269. self.put(path, event_modified, check=204, http_if_match=prop.text)
  270. def test_update_event_with_etag_mismatch(self) -> None:
  271. """Update an event with serving mismatch etag (Precondition Failed)."""
  272. self.mkcalendar("/calendar.ics/")
  273. event = get_file_content("event1.ics")
  274. event_modified = get_file_content("event1_modified.ics")
  275. path = "/calendar.ics/event1.ics"
  276. self.put(path, event, check=201)
  277. self.put(path, event_modified, check=412, http_if_match="0000")
  278. def test_add_event_with_etag(self) -> None:
  279. """Add an event with serving etag (Precondition Failed)."""
  280. self.mkcalendar("/calendar.ics/")
  281. event = get_file_content("event1.ics")
  282. path = "/calendar.ics/event1.ics"
  283. self.put(path, event, check=412, http_if_match="0000")
  284. def test_update_event_uid_event(self) -> None:
  285. """Update an event with a different UID."""
  286. self.mkcalendar("/calendar.ics/")
  287. event1 = get_file_content("event1.ics")
  288. event2 = get_file_content("event2.ics")
  289. path = "/calendar.ics/event1.ics"
  290. self.put(path, event1)
  291. status, answer = self.put(path, event2, check=None)
  292. assert status in (403, 409)
  293. xml = DefusedET.fromstring(answer)
  294. assert xml.tag == xmlutils.make_clark("D:error")
  295. assert xml.find(xmlutils.make_clark("C:no-uid-conflict")) is not None
  296. def test_put_whole_calendar(self) -> None:
  297. """Create and overwrite a whole calendar."""
  298. self.put("/calendar.ics/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
  299. event1 = get_file_content("event1.ics")
  300. self.put("/calendar.ics/test_event.ics", event1)
  301. # Overwrite
  302. events = get_file_content("event_multiple.ics")
  303. self.put("/calendar.ics/", events)
  304. self.get("/calendar.ics/test_event.ics", check=404)
  305. _, answer = self.get("/calendar.ics/")
  306. assert "\r\nUID:event\r\n" in answer and "\r\nUID:todo\r\n" in answer
  307. assert "\r\nUID:event1\r\n" not in answer
  308. def test_put_whole_calendar_without_uids(self) -> None:
  309. """Create a whole calendar without UID."""
  310. event = get_file_content("event_multiple.ics")
  311. event = event.replace("UID:event\n", "").replace("UID:todo\n", "")
  312. assert "\nUID:" not in event
  313. self.put("/calendar.ics/", event)
  314. _, answer = self.get("/calendar.ics")
  315. uids = []
  316. for line in answer.split("\r\n"):
  317. if line.startswith("UID:"):
  318. uids.append(line[len("UID:"):])
  319. assert len(uids) == 2
  320. for i, uid1 in enumerate(uids):
  321. assert uid1
  322. for uid2 in uids[i + 1:]:
  323. assert uid1 != uid2
  324. def test_put_whole_calendar_case_sensitive_uids(self) -> None:
  325. """Create a whole calendar with case-sensitive UIDs."""
  326. events = get_file_content("event_multiple_case_sensitive_uids.ics")
  327. self.put("/calendar.ics/", events)
  328. _, answer = self.get("/calendar.ics/")
  329. assert "\r\nUID:event\r\n" in answer and "\r\nUID:EVENT\r\n" in answer
  330. def test_put_whole_addressbook(self) -> None:
  331. """Create and overwrite a whole addressbook."""
  332. contacts = get_file_content("contact_multiple.vcf")
  333. self.put("/contacts.vcf/", contacts)
  334. _, answer = self.get("/contacts.vcf/")
  335. assert answer is not None
  336. assert "\r\nUID:contact1\r\n" in answer
  337. assert "\r\nUID:contact2\r\n" in answer
  338. def test_put_whole_addressbook_without_uids(self) -> None:
  339. """Create a whole addressbook without UID."""
  340. contacts = get_file_content("contact_multiple.vcf")
  341. contacts = contacts.replace("UID:contact1\n", "").replace(
  342. "UID:contact2\n", "")
  343. assert "\nUID:" not in contacts
  344. self.put("/contacts.vcf/", contacts)
  345. _, answer = self.get("/contacts.vcf")
  346. uids = []
  347. for line in answer.split("\r\n"):
  348. if line.startswith("UID:"):
  349. uids.append(line[len("UID:"):])
  350. assert len(uids) == 2
  351. for i, uid1 in enumerate(uids):
  352. assert uid1
  353. for uid2 in uids[i + 1:]:
  354. assert uid1 != uid2
  355. def test_add_event_tz_dtend_only(self) -> None:
  356. """Add an event having TZ only on DTEND."""
  357. self.mkcalendar("/calendar.ics/")
  358. event = get_file_content("event_issue1847_1.ics")
  359. path = "/calendar.ics/event_issue1847_1.ics"
  360. self.put(path, event)
  361. _, headers, answer = self.request("GET", path, check=200)
  362. def test_add_event_tz_dtstart_only(self) -> None:
  363. """Add an event having TZ only on DTSTART."""
  364. self.mkcalendar("/calendar.ics/")
  365. event = get_file_content("event_issue1847_2.ics")
  366. path = "/calendar.ics/event_issue1847_2.ics"
  367. self.put(path, event)
  368. _, headers, answer = self.request("GET", path, check=200)
  369. def test_verify(self) -> None:
  370. """Verify the storage."""
  371. contacts = get_file_content("contact_multiple.vcf")
  372. self.put("/contacts.vcf/", contacts)
  373. events = get_file_content("event_multiple.ics")
  374. self.put("/calendar.ics/", events)
  375. s = storage.load(self.configuration)
  376. assert s.verify()
  377. def test_delete(self) -> None:
  378. """Delete an event."""
  379. self.mkcalendar("/calendar.ics/")
  380. event = get_file_content("event1.ics")
  381. path = "/calendar.ics/event1.ics"
  382. self.put(path, event)
  383. _, responses = self.delete(path)
  384. assert responses[path] == 200
  385. _, answer = self.get("/calendar.ics/")
  386. assert "VEVENT" not in answer
  387. def test_mkcalendar(self) -> None:
  388. """Make a calendar."""
  389. self.mkcalendar("/calendar.ics/")
  390. _, answer = self.get("/calendar.ics/")
  391. assert "BEGIN:VCALENDAR" in answer
  392. assert "END:VCALENDAR" in answer
  393. def test_mkcalendar_overwrite(self) -> None:
  394. """Try to overwrite an existing calendar."""
  395. self.mkcalendar("/calendar.ics/")
  396. status, answer = self.mkcalendar("/calendar.ics/", check=None)
  397. assert status in (403, 409)
  398. xml = DefusedET.fromstring(answer)
  399. assert xml.tag == xmlutils.make_clark("D:error")
  400. assert xml.find(xmlutils.make_clark(
  401. "D:resource-must-be-null")) is not None
  402. def test_mkcalendar_intermediate(self) -> None:
  403. """Try make a calendar in a unmapped collection."""
  404. self.mkcalendar("/unmapped/calendar.ics/", check=409)
  405. def test_mkcol(self) -> None:
  406. """Make a collection."""
  407. self.mkcol("/user/")
  408. def test_mkcol_overwrite(self) -> None:
  409. """Try to overwrite an existing collection."""
  410. self.mkcol("/user/")
  411. self.mkcol("/user/", check=405)
  412. def test_mkcol_intermediate(self) -> None:
  413. """Try make a collection in a unmapped collection."""
  414. self.mkcol("/unmapped/user/", check=409)
  415. def test_mkcol_make_calendar(self) -> None:
  416. """Make a calendar with additional props."""
  417. mkcol_make_calendar = get_file_content("mkcol_make_calendar.xml")
  418. self.mkcol("/calendar.ics/", mkcol_make_calendar)
  419. _, answer = self.get("/calendar.ics/")
  420. assert answer is not None
  421. assert "BEGIN:VCALENDAR" in answer
  422. assert "END:VCALENDAR" in answer
  423. # Read additional properties
  424. propfind = get_file_content("propfind_calendar_color.xml")
  425. _, responses = self.propfind("/calendar.ics/", propfind)
  426. response = responses["/calendar.ics/"]
  427. assert not isinstance(response, int) and len(response) == 1
  428. status, prop = response["ICAL:calendar-color"]
  429. assert status == 200 and prop.text == "#BADA55"
  430. def test_move(self) -> None:
  431. """Move a item."""
  432. self.mkcalendar("/calendar.ics/")
  433. event = get_file_content("event1.ics")
  434. path1 = "/calendar.ics/event1.ics"
  435. path2 = "/calendar.ics/event2.ics"
  436. self.put(path1, event)
  437. self.request("MOVE", path1, check=201,
  438. HTTP_DESTINATION="http://127.0.0.1/"+path2)
  439. self.get(path1, check=404)
  440. self.get(path2)
  441. def test_move_between_collections(self) -> None:
  442. """Move a item."""
  443. self.mkcalendar("/calendar1.ics/")
  444. self.mkcalendar("/calendar2.ics/")
  445. event = get_file_content("event1.ics")
  446. path1 = "/calendar1.ics/event1.ics"
  447. path2 = "/calendar2.ics/event2.ics"
  448. self.put(path1, event)
  449. self.request("MOVE", path1, check=201,
  450. HTTP_DESTINATION="http://127.0.0.1/"+path2)
  451. self.get(path1, check=404)
  452. self.get(path2)
  453. def test_move_between_collections_duplicate_uid(self) -> None:
  454. """Move a item to a collection which already contains the UID."""
  455. self.mkcalendar("/calendar1.ics/")
  456. self.mkcalendar("/calendar2.ics/")
  457. event = get_file_content("event1.ics")
  458. path1 = "/calendar1.ics/event1.ics"
  459. path2 = "/calendar2.ics/event2.ics"
  460. self.put(path1, event)
  461. self.put("/calendar2.ics/event1.ics", event)
  462. status, _, answer = self.request(
  463. "MOVE", path1, HTTP_DESTINATION="http://127.0.0.1/"+path2)
  464. assert status in (403, 409)
  465. xml = DefusedET.fromstring(answer)
  466. assert xml.tag == xmlutils.make_clark("D:error")
  467. assert xml.find(xmlutils.make_clark("C:no-uid-conflict")) is not None
  468. def test_move_between_collections_overwrite(self) -> None:
  469. """Move a item to a collection which already contains the item."""
  470. self.mkcalendar("/calendar1.ics/")
  471. self.mkcalendar("/calendar2.ics/")
  472. event = get_file_content("event1.ics")
  473. path1 = "/calendar1.ics/event1.ics"
  474. path2 = "/calendar2.ics/event1.ics"
  475. self.put(path1, event)
  476. self.put(path2, event)
  477. self.request("MOVE", path1, check=412,
  478. HTTP_DESTINATION="http://127.0.0.1/"+path2)
  479. self.request("MOVE", path1, check=204, HTTP_OVERWRITE="T",
  480. HTTP_DESTINATION="http://127.0.0.1/"+path2)
  481. def test_move_between_collections_overwrite_uid_conflict(self) -> None:
  482. """Move an item to a collection which already contains the item with
  483. a different UID."""
  484. self.mkcalendar("/calendar1.ics/")
  485. self.mkcalendar("/calendar2.ics/")
  486. event1 = get_file_content("event1.ics")
  487. event2 = get_file_content("event2.ics")
  488. path1 = "/calendar1.ics/event1.ics"
  489. path2 = "/calendar2.ics/event2.ics"
  490. self.put(path1, event1)
  491. self.put(path2, event2)
  492. status, _, answer = self.request(
  493. "MOVE", path1, HTTP_OVERWRITE="T",
  494. HTTP_DESTINATION="http://127.0.0.1/"+path2)
  495. assert status in (403, 409)
  496. xml = DefusedET.fromstring(answer)
  497. assert xml.tag == xmlutils.make_clark("D:error")
  498. assert xml.find(xmlutils.make_clark("C:no-uid-conflict")) is not None
  499. def test_head(self) -> None:
  500. _, headers, answer = self.request("HEAD", "/", check=302)
  501. assert int(headers.get("Content-Length", "0")) > 0 and not answer
  502. def test_options(self) -> None:
  503. _, headers, _ = self.request("OPTIONS", "/", check=200)
  504. assert "DAV" in headers
  505. def test_delete_collection(self) -> None:
  506. """Delete a collection."""
  507. self.mkcalendar("/calendar.ics/")
  508. event = get_file_content("event1.ics")
  509. self.put("/calendar.ics/event1.ics", event)
  510. _, responses = self.delete("/calendar.ics/")
  511. assert responses["/calendar.ics/"] == 200
  512. self.get("/calendar.ics/", check=404)
  513. def test_delete_collection_global_forbid(self) -> None:
  514. """Delete a collection (expect forbidden)."""
  515. self.configure({"rights": {"permit_delete_collection": False}})
  516. self.mkcalendar("/calendar.ics/")
  517. event = get_file_content("event1.ics")
  518. self.put("/calendar.ics/event1.ics", event)
  519. _, responses = self.delete("/calendar.ics/", check=401)
  520. self.get("/calendar.ics/", check=200)
  521. def test_delete_collection_global_forbid_explicit_permit(self) -> None:
  522. """Delete a collection with permitted path (expect permit)."""
  523. self.configure({"rights": {"permit_delete_collection": False}})
  524. self.mkcalendar("/test-permit-delete/")
  525. event = get_file_content("event1.ics")
  526. self.put("/test-permit-delete/event1.ics", event)
  527. _, responses = self.delete("/test-permit-delete/", check=200)
  528. self.get("/test-permit-delete/", check=404)
  529. def test_delete_collection_global_permit_explicit_forbid(self) -> None:
  530. """Delete a collection with permitted path (expect forbid)."""
  531. self.configure({"rights": {"permit_delete_collection": True}})
  532. self.mkcalendar("/test-forbid-delete/")
  533. event = get_file_content("event1.ics")
  534. self.put("/test-forbid-delete/event1.ics", event)
  535. _, responses = self.delete("/test-forbid-delete/", check=401)
  536. self.get("/test-forbid-delete/", check=200)
  537. def test_delete_root_collection(self) -> None:
  538. """Delete the root collection."""
  539. self.mkcalendar("/calendar.ics/")
  540. event = get_file_content("event1.ics")
  541. self.put("/event1.ics", event)
  542. self.put("/calendar.ics/event1.ics", event)
  543. _, responses = self.delete("/")
  544. assert len(responses) == 1 and responses["/"] == 200
  545. self.get("/calendar.ics/", check=404)
  546. self.get("/event1.ics", 404)
  547. def test_overwrite_collection_global_forbid(self) -> None:
  548. """Overwrite a collection (expect forbid)."""
  549. self.configure({"rights": {"permit_overwrite_collection": False}})
  550. event = get_file_content("event1.ics")
  551. self.put("/calender.ics/", event, check=401)
  552. def test_overwrite_collection_global_forbid_explict_permit(self) -> None:
  553. """Overwrite a collection with permitted path (expect permit)."""
  554. self.configure({"rights": {"permit_overwrite_collection": False}})
  555. event = get_file_content("event1.ics")
  556. self.put("/test-permit-overwrite/", event, check=201)
  557. def test_overwrite_collection_global_permit(self) -> None:
  558. """Overwrite a collection (expect permit)."""
  559. self.configure({"rights": {"permit_overwrite_collection": True}})
  560. event = get_file_content("event1.ics")
  561. self.put("/calender.ics/", event, check=201)
  562. def test_overwrite_collection_global_permit_explict_forbid(self) -> None:
  563. """Overwrite a collection with forbidden path (expect forbid)."""
  564. self.configure({"rights": {"permit_overwrite_collection": True}})
  565. event = get_file_content("event1.ics")
  566. self.put("/test-forbid-overwrite/", event, check=401)
  567. def test_propfind(self) -> None:
  568. calendar_path = "/calendar.ics/"
  569. self.mkcalendar("/calendar.ics/")
  570. event = get_file_content("event1.ics")
  571. event_path = posixpath.join(calendar_path, "event.ics")
  572. self.put(event_path, event)
  573. _, responses = self.propfind("/", HTTP_DEPTH="1")
  574. assert len(responses) == 2
  575. assert "/" in responses and calendar_path in responses
  576. _, responses = self.propfind(calendar_path, HTTP_DEPTH="1")
  577. assert len(responses) == 2
  578. assert calendar_path in responses and event_path in responses
  579. def test_propfind_propname(self) -> None:
  580. self.mkcalendar("/calendar.ics/")
  581. event = get_file_content("event1.ics")
  582. self.put("/calendar.ics/event.ics", event)
  583. propfind = get_file_content("propname.xml")
  584. _, responses = self.propfind("/calendar.ics/", propfind)
  585. response = responses["/calendar.ics/"]
  586. assert not isinstance(response, int)
  587. status, prop = response["D:sync-token"]
  588. assert status == 200 and not prop.text
  589. _, responses = self.propfind("/calendar.ics/event.ics", propfind)
  590. response = responses["/calendar.ics/event.ics"]
  591. assert not isinstance(response, int)
  592. status, prop = response["D:getetag"]
  593. assert status == 200 and not prop.text
  594. def test_propfind_allprop(self) -> None:
  595. self.mkcalendar("/calendar.ics/")
  596. event = get_file_content("event1.ics")
  597. self.put("/calendar.ics/event.ics", event)
  598. propfind = get_file_content("allprop.xml")
  599. _, responses = self.propfind("/calendar.ics/", propfind)
  600. response = responses["/calendar.ics/"]
  601. assert not isinstance(response, int)
  602. status, prop = response["D:sync-token"]
  603. assert status == 200 and prop.text
  604. _, responses = self.propfind("/calendar.ics/event.ics", propfind)
  605. response = responses["/calendar.ics/event.ics"]
  606. assert not isinstance(response, int)
  607. status, prop = response["D:getetag"]
  608. assert status == 200 and prop.text
  609. def test_propfind_nonexistent(self) -> None:
  610. """Read a property that does not exist."""
  611. self.mkcalendar("/calendar.ics/")
  612. propfind = get_file_content("propfind_calendar_color.xml")
  613. _, responses = self.propfind("/calendar.ics/", propfind)
  614. response = responses["/calendar.ics/"]
  615. assert not isinstance(response, int) and len(response) == 1
  616. status, prop = response["ICAL:calendar-color"]
  617. assert status == 404 and not prop.text
  618. def test_propfind_max_resource_size(self) -> None:
  619. """Read property C:max-resource-size"""
  620. self.mkcalendar("/calendar.ics/")
  621. event = get_file_content("event1.ics")
  622. self.put("/calendar.ics/event.ics", event)
  623. _, responses = self.propfind("/calendar.ics/", """\
  624. <?xml version="1.0"?>
  625. <propfind xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
  626. <prop>
  627. <C:max-resource-size />
  628. </prop>
  629. </propfind>""")
  630. response = responses["/calendar.ics/"]
  631. assert not isinstance(response, int)
  632. status, prop = response["C:max-resource-size"]
  633. assert status == 200 and prop.text
  634. def test_propfind_getctag(self) -> None:
  635. """Read property CS:getctag"""
  636. self.mkcalendar("/calendar.ics/")
  637. event = get_file_content("event1.ics")
  638. self.put("/calendar.ics/event.ics", event)
  639. _, responses = self.propfind("/calendar.ics/", """\
  640. <?xml version="1.0"?>
  641. <propfind xmlns="DAV:" xmlns:CS="http://calendarserver.org/ns/">
  642. <prop>
  643. <CS:getctag />
  644. </prop>
  645. </propfind>""")
  646. response = responses["/calendar.ics/"]
  647. assert not isinstance(response, int)
  648. status, prop = response["CS:getctag"]
  649. assert status == 200 and prop.text
  650. def test_proppatch(self) -> None:
  651. """Set/Remove a property and read it back."""
  652. self.mkcalendar("/calendar.ics/")
  653. proppatch = get_file_content("proppatch_set_calendar_color.xml")
  654. _, responses = self.proppatch("/calendar.ics/", proppatch)
  655. response = responses["/calendar.ics/"]
  656. assert not isinstance(response, int) and len(response) == 1
  657. status, prop = response["ICAL:calendar-color"]
  658. assert status == 200 and not prop.text
  659. # Read property back
  660. propfind = get_file_content("propfind_calendar_color.xml")
  661. _, responses = self.propfind("/calendar.ics/", propfind)
  662. response = responses["/calendar.ics/"]
  663. assert not isinstance(response, int) and len(response) == 1
  664. status, prop = response["ICAL:calendar-color"]
  665. assert status == 200 and prop.text == "#BADA55"
  666. propfind = get_file_content("allprop.xml")
  667. _, responses = self.propfind("/calendar.ics/", propfind)
  668. response = responses["/calendar.ics/"]
  669. assert not isinstance(response, int)
  670. status, prop = response["ICAL:calendar-color"]
  671. assert status == 200 and prop.text == "#BADA55"
  672. # Remove property
  673. proppatch = get_file_content("proppatch_remove_calendar_color.xml")
  674. _, responses = self.proppatch("/calendar.ics/", proppatch)
  675. response = responses["/calendar.ics/"]
  676. assert not isinstance(response, int) and len(response) == 1
  677. status, prop = response["ICAL:calendar-color"]
  678. assert status == 200 and not prop.text
  679. # Read property back
  680. propfind = get_file_content("propfind_calendar_color.xml")
  681. _, responses = self.propfind("/calendar.ics/", propfind)
  682. response = responses["/calendar.ics/"]
  683. assert not isinstance(response, int) and len(response) == 1
  684. status, prop = response["ICAL:calendar-color"]
  685. assert status == 404
  686. def test_proppatch_multiple1(self) -> None:
  687. """Set/Remove a multiple properties and read them back."""
  688. self.mkcalendar("/calendar.ics/")
  689. propfind = get_file_content("propfind_multiple.xml")
  690. proppatch = get_file_content("proppatch_set_multiple1.xml")
  691. _, responses = self.proppatch("/calendar.ics/", proppatch)
  692. response = responses["/calendar.ics/"]
  693. assert not isinstance(response, int) and len(response) == 2
  694. status, prop = response["ICAL:calendar-color"]
  695. assert status == 200 and not prop.text
  696. status, prop = response["C:calendar-description"]
  697. assert status == 200 and not prop.text
  698. # Read properties back
  699. _, responses = self.propfind("/calendar.ics/", propfind)
  700. response = responses["/calendar.ics/"]
  701. assert not isinstance(response, int) and len(response) == 2
  702. status, prop = response["ICAL:calendar-color"]
  703. assert status == 200 and prop.text == "#BADA55"
  704. status, prop = response["C:calendar-description"]
  705. assert status == 200 and prop.text == "test"
  706. # Remove properties
  707. proppatch = get_file_content("proppatch_remove_multiple1.xml")
  708. _, responses = self.proppatch("/calendar.ics/", proppatch)
  709. response = responses["/calendar.ics/"]
  710. assert not isinstance(response, int) and len(response) == 2
  711. status, prop = response["ICAL:calendar-color"]
  712. assert status == 200 and not prop.text
  713. status, prop = response["C:calendar-description"]
  714. assert status == 200 and not prop.text
  715. # Read properties back
  716. _, responses = self.propfind("/calendar.ics/", propfind)
  717. response = responses["/calendar.ics/"]
  718. assert not isinstance(response, int) and len(response) == 2
  719. status, prop = response["ICAL:calendar-color"]
  720. assert status == 404
  721. status, prop = response["C:calendar-description"]
  722. assert status == 404
  723. def test_proppatch_multiple2(self) -> None:
  724. """Set/Remove a multiple properties and read them back."""
  725. self.mkcalendar("/calendar.ics/")
  726. propfind = get_file_content("propfind_multiple.xml")
  727. proppatch = get_file_content("proppatch_set_multiple2.xml")
  728. _, responses = self.proppatch("/calendar.ics/", proppatch)
  729. response = responses["/calendar.ics/"]
  730. assert not isinstance(response, int) and len(response) == 2
  731. status, prop = response["ICAL:calendar-color"]
  732. assert status == 200 and not prop.text
  733. status, prop = response["C:calendar-description"]
  734. assert status == 200 and not prop.text
  735. # Read properties back
  736. _, responses = self.propfind("/calendar.ics/", propfind)
  737. response = responses["/calendar.ics/"]
  738. assert not isinstance(response, int) and len(response) == 2
  739. assert len(response) == 2
  740. status, prop = response["ICAL:calendar-color"]
  741. assert status == 200 and prop.text == "#BADA55"
  742. status, prop = response["C:calendar-description"]
  743. assert status == 200 and prop.text == "test"
  744. # Remove properties
  745. proppatch = get_file_content("proppatch_remove_multiple2.xml")
  746. _, responses = self.proppatch("/calendar.ics/", proppatch)
  747. response = responses["/calendar.ics/"]
  748. assert not isinstance(response, int) and len(response) == 2
  749. status, prop = response["ICAL:calendar-color"]
  750. assert status == 200 and not prop.text
  751. status, prop = response["C:calendar-description"]
  752. assert status == 200 and not prop.text
  753. # Read properties back
  754. _, responses = self.propfind("/calendar.ics/", propfind)
  755. response = responses["/calendar.ics/"]
  756. assert not isinstance(response, int) and len(response) == 2
  757. status, prop = response["ICAL:calendar-color"]
  758. assert status == 404
  759. status, prop = response["C:calendar-description"]
  760. assert status == 404
  761. def test_proppatch_set_and_remove(self) -> None:
  762. """Set and remove multiple properties in single request."""
  763. self.mkcalendar("/calendar.ics/")
  764. propfind = get_file_content("propfind_multiple.xml")
  765. # Prepare
  766. proppatch = get_file_content("proppatch_set_multiple1.xml")
  767. self.proppatch("/calendar.ics/", proppatch)
  768. # Remove and set properties in single request
  769. proppatch = get_file_content("proppatch_set_and_remove.xml")
  770. _, responses = self.proppatch("/calendar.ics/", proppatch)
  771. response = responses["/calendar.ics/"]
  772. assert not isinstance(response, int) and len(response) == 2
  773. status, prop = response["ICAL:calendar-color"]
  774. assert status == 200 and not prop.text
  775. status, prop = response["C:calendar-description"]
  776. assert status == 200 and not prop.text
  777. # Read properties back
  778. _, responses = self.propfind("/calendar.ics/", propfind)
  779. response = responses["/calendar.ics/"]
  780. assert not isinstance(response, int) and len(response) == 2
  781. status, prop = response["ICAL:calendar-color"]
  782. assert status == 404
  783. status, prop = response["C:calendar-description"]
  784. assert status == 200 and prop.text == "test2"
  785. def test_put_whole_calendar_multiple_events_with_same_uid(self) -> None:
  786. """Add two events with the same UID."""
  787. self.put("/calendar.ics/", get_file_content("event2.ics"))
  788. _, responses = self.report("/calendar.ics/", """\
  789. <?xml version="1.0" encoding="utf-8" ?>
  790. <C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav">
  791. <D:prop xmlns:D="DAV:">
  792. <D:getetag/>
  793. </D:prop>
  794. </C:calendar-query>""")
  795. assert len(responses) == 1
  796. response = responses["/calendar.ics/event2.ics"]
  797. assert not isinstance(response, int)
  798. status, prop = response["D:getetag"]
  799. assert status == 200 and prop.text
  800. _, answer = self.get("/calendar.ics/")
  801. assert answer.count("BEGIN:VEVENT") == 2
  802. def _test_filter(self, filters: Iterable[str], kind: str = "event",
  803. test: Optional[str] = None, items: Iterable[int] = (1,)
  804. ) -> List[str]:
  805. filter_template = "<C:filter>%s</C:filter>"
  806. create_collection_fn: Callable[[str], Any]
  807. if kind in ("event", "journal", "todo", "valarm"):
  808. create_collection_fn = self.mkcalendar
  809. path = "/calendar.ics/"
  810. filename_template = "%s%d.ics"
  811. namespace = "urn:ietf:params:xml:ns:caldav"
  812. report = "calendar-query"
  813. elif kind == "contact":
  814. create_collection_fn = self.create_addressbook
  815. if test:
  816. filter_template = '<C:filter test="%s">%%s</C:filter>' % test
  817. path = "/contacts.vcf/"
  818. filename_template = "%s%d.vcf"
  819. namespace = "urn:ietf:params:xml:ns:carddav"
  820. report = "addressbook-query"
  821. else:
  822. raise ValueError("Unsupported kind: %r" % kind)
  823. status, _, = self.delete(path, check=None)
  824. assert status in (200, 404)
  825. create_collection_fn(path)
  826. logging.warning("Upload items %r", items)
  827. for i in items:
  828. logging.warning("Upload %d", i)
  829. filename = filename_template % (kind, i)
  830. event = get_file_content(filename)
  831. self.put(posixpath.join(path, filename), event)
  832. logging.warning("Upload items finished")
  833. filters_text = "".join(filter_template % f for f in filters)
  834. _, responses = self.report(path, """\
  835. <?xml version="1.0" encoding="utf-8" ?>
  836. <C:{1} xmlns:C="{0}">
  837. <D:prop xmlns:D="DAV:">
  838. <D:getetag/>
  839. </D:prop>
  840. {2}
  841. </C:{1}>""".format(namespace, report, filters_text))
  842. assert responses is not None
  843. paths = []
  844. for path, props in responses.items():
  845. assert not isinstance(props, int) and len(props) == 1
  846. status, prop = props["D:getetag"]
  847. assert status == 200 and prop.text
  848. paths.append(path)
  849. return paths
  850. def test_addressbook_empty_filter(self) -> None:
  851. self._test_filter([""], kind="contact")
  852. def test_addressbook_prop_filter(self) -> None:
  853. assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
  854. <C:prop-filter name="NICKNAME">
  855. <C:text-match collation="i;unicode-casemap" match-type="contains"
  856. >es</C:text-match>
  857. </C:prop-filter>"""], "contact")
  858. assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
  859. <C:prop-filter name="NICKNAME">
  860. <C:text-match collation="i;unicode-casemap">es</C:text-match>
  861. </C:prop-filter>"""], "contact")
  862. assert "/contacts.vcf/contact1.vcf" not in self._test_filter(["""\
  863. <C:prop-filter name="NICKNAME">
  864. <C:text-match collation="i;unicode-casemap" match-type="contains"
  865. >a</C:text-match>
  866. </C:prop-filter>"""], "contact")
  867. assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
  868. <C:prop-filter name="NICKNAME">
  869. <C:text-match collation="i;unicode-casemap" match-type="equals"
  870. >test</C:text-match>
  871. </C:prop-filter>"""], "contact")
  872. assert "/contacts.vcf/contact1.vcf" not in self._test_filter(["""\
  873. <C:prop-filter name="NICKNAME">
  874. <C:text-match collation="i;unicode-casemap" match-type="equals"
  875. >tes</C:text-match>
  876. </C:prop-filter>"""], "contact")
  877. assert "/contacts.vcf/contact1.vcf" not in self._test_filter(["""\
  878. <C:prop-filter name="NICKNAME">
  879. <C:text-match collation="i;unicode-casemap" match-type="equals"
  880. >est</C:text-match>
  881. </C:prop-filter>"""], "contact")
  882. assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
  883. <C:prop-filter name="NICKNAME">
  884. <C:text-match collation="i;unicode-casemap" match-type="starts-with"
  885. >tes</C:text-match>
  886. </C:prop-filter>"""], "contact")
  887. assert "/contacts.vcf/contact1.vcf" not in self._test_filter(["""\
  888. <C:prop-filter name="NICKNAME">
  889. <C:text-match collation="i;unicode-casemap" match-type="starts-with"
  890. >est</C:text-match>
  891. </C:prop-filter>"""], "contact")
  892. assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
  893. <C:prop-filter name="NICKNAME">
  894. <C:text-match collation="i;unicode-casemap" match-type="ends-with"
  895. >est</C:text-match>
  896. </C:prop-filter>"""], "contact")
  897. assert "/contacts.vcf/contact1.vcf" not in self._test_filter(["""\
  898. <C:prop-filter name="NICKNAME">
  899. <C:text-match collation="i;unicode-casemap" match-type="ends-with"
  900. >tes</C:text-match>
  901. </C:prop-filter>"""], "contact")
  902. def test_addressbook_prop_filter_any(self) -> None:
  903. assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
  904. <C:prop-filter name="NICKNAME">
  905. <C:text-match collation="i;unicode-casemap">test</C:text-match>
  906. </C:prop-filter>
  907. <C:prop-filter name="EMAIL">
  908. <C:text-match collation="i;unicode-casemap">test</C:text-match>
  909. </C:prop-filter>"""], "contact", test="anyof")
  910. assert "/contacts.vcf/contact1.vcf" not in self._test_filter(["""\
  911. <C:prop-filter name="NICKNAME">
  912. <C:text-match collation="i;unicode-casemap">a</C:text-match>
  913. </C:prop-filter>
  914. <C:prop-filter name="EMAIL">
  915. <C:text-match collation="i;unicode-casemap">test</C:text-match>
  916. </C:prop-filter>"""], "contact", test="anyof")
  917. assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
  918. <C:prop-filter name="NICKNAME">
  919. <C:text-match collation="i;unicode-casemap">test</C:text-match>
  920. </C:prop-filter>
  921. <C:prop-filter name="EMAIL">
  922. <C:text-match collation="i;unicode-casemap">test</C:text-match>
  923. </C:prop-filter>"""], "contact")
  924. def test_addressbook_prop_filter_all(self) -> None:
  925. assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
  926. <C:prop-filter name="NICKNAME">
  927. <C:text-match collation="i;unicode-casemap">tes</C:text-match>
  928. </C:prop-filter>
  929. <C:prop-filter name="NICKNAME">
  930. <C:text-match collation="i;unicode-casemap">est</C:text-match>
  931. </C:prop-filter>"""], "contact", test="allof")
  932. assert "/contacts.vcf/contact1.vcf" not in self._test_filter(["""\
  933. <C:prop-filter name="NICKNAME">
  934. <C:text-match collation="i;unicode-casemap">test</C:text-match>
  935. </C:prop-filter>
  936. <C:prop-filter name="EMAIL">
  937. <C:text-match collation="i;unicode-casemap">test</C:text-match>
  938. </C:prop-filter>"""], "contact", test="allof")
  939. def test_calendar_empty_filter(self) -> None:
  940. self._test_filter([""])
  941. def test_calendar_tag_filter(self) -> None:
  942. """Report request with tag-based filter on calendar."""
  943. assert "/calendar.ics/event1.ics" in self._test_filter(["""\
  944. <C:comp-filter name="VCALENDAR"></C:comp-filter>"""])
  945. def test_item_tag_filter(self) -> None:
  946. """Report request with tag-based filter on an item."""
  947. assert "/calendar.ics/event1.ics" in self._test_filter(["""\
  948. <C:comp-filter name="VCALENDAR">
  949. <C:comp-filter name="VEVENT"></C:comp-filter>
  950. </C:comp-filter>"""])
  951. assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
  952. <C:comp-filter name="VCALENDAR">
  953. <C:comp-filter name="VTODO"></C:comp-filter>
  954. </C:comp-filter>"""])
  955. def test_item_not_tag_filter(self) -> None:
  956. """Report request with tag-based is-not filter on an item."""
  957. assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
  958. <C:comp-filter name="VCALENDAR">
  959. <C:comp-filter name="VEVENT">
  960. <C:is-not-defined />
  961. </C:comp-filter>
  962. </C:comp-filter>"""])
  963. assert "/calendar.ics/event1.ics" in self._test_filter(["""\
  964. <C:comp-filter name="VCALENDAR">
  965. <C:comp-filter name="VTODO">
  966. <C:is-not-defined />
  967. </C:comp-filter>
  968. </C:comp-filter>"""])
  969. def test_item_prop_filter(self) -> None:
  970. """Report request with prop-based filter on an item."""
  971. assert "/calendar.ics/event1.ics" in self._test_filter(["""\
  972. <C:comp-filter name="VCALENDAR">
  973. <C:comp-filter name="VEVENT">
  974. <C:prop-filter name="SUMMARY"></C:prop-filter>
  975. </C:comp-filter>
  976. </C:comp-filter>"""])
  977. assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
  978. <C:comp-filter name="VCALENDAR">
  979. <C:comp-filter name="VEVENT">
  980. <C:prop-filter name="UNKNOWN"></C:prop-filter>
  981. </C:comp-filter>
  982. </C:comp-filter>"""])
  983. def test_item_not_prop_filter(self) -> None:
  984. """Report request with prop-based is-not filter on an item."""
  985. assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
  986. <C:comp-filter name="VCALENDAR">
  987. <C:comp-filter name="VEVENT">
  988. <C:prop-filter name="SUMMARY">
  989. <C:is-not-defined />
  990. </C:prop-filter>
  991. </C:comp-filter>
  992. </C:comp-filter>"""])
  993. assert "/calendar.ics/event1.ics" in self._test_filter(["""\
  994. <C:comp-filter name="VCALENDAR">
  995. <C:comp-filter name="VEVENT">
  996. <C:prop-filter name="UNKNOWN">
  997. <C:is-not-defined />
  998. </C:prop-filter>
  999. </C:comp-filter>
  1000. </C:comp-filter>"""])
  1001. def test_mutiple_filters(self) -> None:
  1002. """Report request with multiple filters on an item."""
  1003. assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
  1004. <C:comp-filter name="VCALENDAR">
  1005. <C:comp-filter name="VEVENT">
  1006. <C:prop-filter name="SUMMARY">
  1007. <C:is-not-defined />
  1008. </C:prop-filter>
  1009. </C:comp-filter>
  1010. </C:comp-filter>""", """
  1011. <C:comp-filter name="VCALENDAR">
  1012. <C:comp-filter name="VEVENT">
  1013. <C:prop-filter name="UNKNOWN">
  1014. <C:is-not-defined />
  1015. </C:prop-filter>
  1016. </C:comp-filter>
  1017. </C:comp-filter>"""])
  1018. assert "/calendar.ics/event1.ics" in self._test_filter(["""\
  1019. <C:comp-filter name="VCALENDAR">
  1020. <C:comp-filter name="VEVENT">
  1021. <C:prop-filter name="SUMMARY"></C:prop-filter>
  1022. </C:comp-filter>
  1023. </C:comp-filter>""", """
  1024. <C:comp-filter name="VCALENDAR">
  1025. <C:comp-filter name="VEVENT">
  1026. <C:prop-filter name="UNKNOWN">
  1027. <C:is-not-defined />
  1028. </C:prop-filter>
  1029. </C:comp-filter>
  1030. </C:comp-filter>"""])
  1031. assert "/calendar.ics/event1.ics" in self._test_filter(["""\
  1032. <C:comp-filter name="VCALENDAR">
  1033. <C:comp-filter name="VEVENT">
  1034. <C:prop-filter name="SUMMARY"></C:prop-filter>
  1035. <C:prop-filter name="UNKNOWN">
  1036. <C:is-not-defined />
  1037. </C:prop-filter>
  1038. </C:comp-filter>
  1039. </C:comp-filter>"""])
  1040. def test_text_match_filter(self) -> None:
  1041. """Report request with text-match filter on calendar."""
  1042. assert "/calendar.ics/event1.ics" in self._test_filter(["""\
  1043. <C:comp-filter name="VCALENDAR">
  1044. <C:comp-filter name="VEVENT">
  1045. <C:prop-filter name="SUMMARY">
  1046. <C:text-match>event</C:text-match>
  1047. </C:prop-filter>
  1048. </C:comp-filter>
  1049. </C:comp-filter>"""])
  1050. assert "/calendar.ics/event1.ics" in self._test_filter(["""\
  1051. <C:comp-filter name="VCALENDAR">
  1052. <C:comp-filter name="VEVENT">
  1053. <C:prop-filter name="CATEGORIES">
  1054. <C:text-match>some_category1</C:text-match>
  1055. </C:prop-filter>
  1056. </C:comp-filter>
  1057. </C:comp-filter>"""])
  1058. assert "/calendar.ics/event1.ics" in self._test_filter(["""\
  1059. <C:comp-filter name="VCALENDAR">
  1060. <C:comp-filter name="VEVENT">
  1061. <C:prop-filter name="CATEGORIES">
  1062. <C:text-match collation="i;octet">some_category1</C:text-match>
  1063. </C:prop-filter>
  1064. </C:comp-filter>
  1065. </C:comp-filter>"""])
  1066. assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
  1067. <C:comp-filter name="VCALENDAR">
  1068. <C:comp-filter name="VEVENT">
  1069. <C:prop-filter name="UNKNOWN">
  1070. <C:text-match>event</C:text-match>
  1071. </C:prop-filter>
  1072. </C:comp-filter>
  1073. </C:comp-filter>"""])
  1074. assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
  1075. <C:comp-filter name="VCALENDAR">
  1076. <C:comp-filter name="VEVENT">
  1077. <C:prop-filter name="SUMMARY">
  1078. <C:text-match>unknown</C:text-match>
  1079. </C:prop-filter>
  1080. </C:comp-filter>
  1081. </C:comp-filter>"""])
  1082. assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
  1083. <C:comp-filter name="VCALENDAR">
  1084. <C:comp-filter name="VEVENT">
  1085. <C:prop-filter name="SUMMARY">
  1086. <C:text-match negate-condition="yes">event</C:text-match>
  1087. </C:prop-filter>
  1088. </C:comp-filter>
  1089. </C:comp-filter>"""])
  1090. def test_param_filter(self) -> None:
  1091. """Report request with param-filter on calendar."""
  1092. assert "/calendar.ics/event1.ics" in self._test_filter(["""\
  1093. <C:comp-filter name="VCALENDAR">
  1094. <C:comp-filter name="VEVENT">
  1095. <C:prop-filter name="ATTENDEE">
  1096. <C:param-filter name="PARTSTAT">
  1097. <C:text-match collation="i;ascii-casemap"
  1098. >ACCEPTED</C:text-match>
  1099. </C:param-filter>
  1100. </C:prop-filter>
  1101. </C:comp-filter>
  1102. </C:comp-filter>"""])
  1103. assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
  1104. <C:comp-filter name="VCALENDAR">
  1105. <C:comp-filter name="VEVENT">
  1106. <C:prop-filter name="ATTENDEE">
  1107. <C:param-filter name="PARTSTAT">
  1108. <C:text-match collation="i;ascii-casemap"
  1109. >UNKNOWN</C:text-match>
  1110. </C:param-filter>
  1111. </C:prop-filter>
  1112. </C:comp-filter>
  1113. </C:comp-filter>"""])
  1114. assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
  1115. <C:comp-filter name="VCALENDAR">
  1116. <C:comp-filter name="VEVENT">
  1117. <C:prop-filter name="ATTENDEE">
  1118. <C:param-filter name="PARTSTAT">
  1119. <C:is-not-defined />
  1120. </C:param-filter>
  1121. </C:prop-filter>
  1122. </C:comp-filter>
  1123. </C:comp-filter>"""])
  1124. assert "/calendar.ics/event1.ics" in self._test_filter(["""\
  1125. <C:comp-filter name="VCALENDAR">
  1126. <C:comp-filter name="VEVENT">
  1127. <C:prop-filter name="ATTENDEE">
  1128. <C:param-filter name="UNKNOWN">
  1129. <C:is-not-defined />
  1130. </C:param-filter>
  1131. </C:prop-filter>
  1132. </C:comp-filter>
  1133. </C:comp-filter>"""])
  1134. def test_time_range_filter_events(self) -> None:
  1135. """Report request with time-range filter on events."""
  1136. answer = self._test_filter(["""\
  1137. <C:comp-filter name="VCALENDAR">
  1138. <C:comp-filter name="VEVENT">
  1139. <C:time-range start="20130801T000000Z" end="20131001T000000Z"/>
  1140. </C:comp-filter>
  1141. </C:comp-filter>"""], "event", items=range(1, 6))
  1142. assert "/calendar.ics/event1.ics" in answer
  1143. assert "/calendar.ics/event2.ics" in answer
  1144. assert "/calendar.ics/event3.ics" in answer
  1145. assert "/calendar.ics/event4.ics" in answer
  1146. assert "/calendar.ics/event5.ics" in answer
  1147. answer = self._test_filter(["""\
  1148. <C:comp-filter name="VCALENDAR">
  1149. <C:comp-filter name="VTODO">
  1150. <C:time-range start="20130801T000000Z" end="20131001T000000Z"/>
  1151. </C:comp-filter>
  1152. </C:comp-filter>"""], "event", items=range(1, 6))
  1153. assert "/calendar.ics/event1.ics" not in answer
  1154. answer = self._test_filter(["""\
  1155. <C:comp-filter name="VCALENDAR">
  1156. <C:comp-filter name="VEVENT">
  1157. <C:prop-filter name="ATTENDEE">
  1158. <C:param-filter name="PARTSTAT">
  1159. <C:is-not-defined />
  1160. </C:param-filter>
  1161. </C:prop-filter>
  1162. <C:time-range start="20130801T000000Z" end="20131001T000000Z"/>
  1163. </C:comp-filter>
  1164. </C:comp-filter>"""], items=range(1, 6))
  1165. assert "/calendar.ics/event1.ics" not in answer
  1166. assert "/calendar.ics/event2.ics" not in answer
  1167. assert "/calendar.ics/event3.ics" not in answer
  1168. assert "/calendar.ics/event4.ics" not in answer
  1169. assert "/calendar.ics/event5.ics" not in answer
  1170. answer = self._test_filter(["""\
  1171. <C:comp-filter name="VCALENDAR">
  1172. <C:comp-filter name="VEVENT">
  1173. <C:time-range start="20130902T000000Z" end="20131001T000000Z"/>
  1174. </C:comp-filter>
  1175. </C:comp-filter>"""], items=range(1, 6))
  1176. assert "/calendar.ics/event1.ics" not in answer
  1177. assert "/calendar.ics/event2.ics" in answer
  1178. assert "/calendar.ics/event3.ics" in answer
  1179. assert "/calendar.ics/event4.ics" in answer
  1180. assert "/calendar.ics/event5.ics" in answer
  1181. answer = self._test_filter(["""\
  1182. <C:comp-filter name="VCALENDAR">
  1183. <C:comp-filter name="VEVENT">
  1184. <C:time-range start="20130903T000000Z" end="20130908T000000Z"/>
  1185. </C:comp-filter>
  1186. </C:comp-filter>"""], items=range(1, 6))
  1187. assert "/calendar.ics/event1.ics" not in answer
  1188. assert "/calendar.ics/event2.ics" not in answer
  1189. assert "/calendar.ics/event3.ics" in answer
  1190. assert "/calendar.ics/event4.ics" in answer
  1191. assert "/calendar.ics/event5.ics" in answer
  1192. answer = self._test_filter(["""\
  1193. <C:comp-filter name="VCALENDAR">
  1194. <C:comp-filter name="VEVENT">
  1195. <C:time-range start="20130903T000000Z" end="20130904T000000Z"/>
  1196. </C:comp-filter>
  1197. </C:comp-filter>"""], items=range(1, 6))
  1198. assert "/calendar.ics/event1.ics" not in answer
  1199. assert "/calendar.ics/event2.ics" not in answer
  1200. assert "/calendar.ics/event3.ics" in answer
  1201. assert "/calendar.ics/event4.ics" not in answer
  1202. assert "/calendar.ics/event5.ics" not in answer
  1203. answer = self._test_filter(["""\
  1204. <C:comp-filter name="VCALENDAR">
  1205. <C:comp-filter name="VEVENT">
  1206. <C:time-range start="20130805T000000Z" end="20130810T000000Z"/>
  1207. </C:comp-filter>
  1208. </C:comp-filter>"""], items=range(1, 6))
  1209. assert "/calendar.ics/event1.ics" not in answer
  1210. assert "/calendar.ics/event2.ics" not in answer
  1211. assert "/calendar.ics/event3.ics" not in answer
  1212. assert "/calendar.ics/event4.ics" not in answer
  1213. assert "/calendar.ics/event5.ics" not in answer
  1214. # HACK: VObject doesn't match RECURRENCE-ID to recurrences, the
  1215. # overwritten recurrence is still used for filtering.
  1216. answer = self._test_filter(["""\
  1217. <C:comp-filter name="VCALENDAR">
  1218. <C:comp-filter name="VEVENT">
  1219. <C:time-range start="20170601T063000Z" end="20170601T070000Z"/>
  1220. </C:comp-filter>
  1221. </C:comp-filter>"""], items=(6, 7, 8, 9))
  1222. assert "/calendar.ics/event6.ics" in answer
  1223. assert "/calendar.ics/event7.ics" in answer
  1224. assert "/calendar.ics/event8.ics" in answer
  1225. assert "/calendar.ics/event9.ics" in answer
  1226. answer = self._test_filter(["""\
  1227. <C:comp-filter name="VCALENDAR">
  1228. <C:comp-filter name="VEVENT">
  1229. <C:time-range start="20170701T060000Z"/>
  1230. </C:comp-filter>
  1231. </C:comp-filter>"""], items=(6, 7, 8, 9))
  1232. assert "/calendar.ics/event6.ics" in answer
  1233. assert "/calendar.ics/event7.ics" in answer
  1234. assert "/calendar.ics/event8.ics" in answer
  1235. assert "/calendar.ics/event9.ics" not in answer
  1236. answer = self._test_filter(["""\
  1237. <C:comp-filter name="VCALENDAR">
  1238. <C:comp-filter name="VEVENT">
  1239. <C:time-range start="20170702T070000Z" end="20170704T060000Z"/>
  1240. </C:comp-filter>
  1241. </C:comp-filter>"""], items=(6, 7, 8, 9))
  1242. assert "/calendar.ics/event6.ics" not in answer
  1243. assert "/calendar.ics/event7.ics" not in answer
  1244. assert "/calendar.ics/event8.ics" not in answer
  1245. assert "/calendar.ics/event9.ics" not in answer
  1246. answer = self._test_filter(["""\
  1247. <C:comp-filter name="VCALENDAR">
  1248. <C:comp-filter name="VEVENT">
  1249. <C:time-range start="20170602T075959Z" end="20170602T080000Z"/>
  1250. </C:comp-filter>
  1251. </C:comp-filter>"""], items=(9,))
  1252. assert "/calendar.ics/event9.ics" in answer
  1253. answer = self._test_filter(["""\
  1254. <C:comp-filter name="VCALENDAR">
  1255. <C:comp-filter name="VEVENT">
  1256. <C:time-range start="20170602T080000Z" end="20170603T083000Z"/>
  1257. </C:comp-filter>
  1258. </C:comp-filter>"""], items=(9,))
  1259. assert "/calendar.ics/event9.ics" not in answer
  1260. def test_time_range_filter_without_comp_filter(self) -> None:
  1261. """Report request with time-range filter without comp-filter on events."""
  1262. answer = self._test_filter(["""\
  1263. <C:comp-filter name="VCALENDAR">
  1264. <C:time-range start="20130801T000000Z" end="20131001T000000Z"/>
  1265. </C:comp-filter>"""], "event", items=range(1, 6))
  1266. assert "/calendar.ics/event1.ics" in answer
  1267. assert "/calendar.ics/event2.ics" in answer
  1268. assert "/calendar.ics/event3.ics" in answer
  1269. assert "/calendar.ics/event4.ics" in answer
  1270. assert "/calendar.ics/event5.ics" in answer
  1271. answer = self._test_filter(["""\
  1272. <C:comp-filter name="VCALENDAR">
  1273. <C:time-range start="20130902T000000Z" end="20131001T000000Z"/>
  1274. </C:comp-filter>"""], items=range(1, 6))
  1275. assert "/calendar.ics/event1.ics" not in answer
  1276. assert "/calendar.ics/event2.ics" in answer
  1277. assert "/calendar.ics/event3.ics" in answer
  1278. assert "/calendar.ics/event4.ics" in answer
  1279. assert "/calendar.ics/event5.ics" in answer
  1280. answer = self._test_filter(["""\
  1281. <C:comp-filter name="VCALENDAR">
  1282. <C:time-range start="20130903T000000Z" end="20130908T000000Z"/>
  1283. </C:comp-filter>"""], items=range(1, 6))
  1284. assert "/calendar.ics/event1.ics" not in answer
  1285. assert "/calendar.ics/event2.ics" not in answer
  1286. assert "/calendar.ics/event3.ics" in answer
  1287. assert "/calendar.ics/event4.ics" in answer
  1288. assert "/calendar.ics/event5.ics" in answer
  1289. answer = self._test_filter(["""\
  1290. <C:comp-filter name="VCALENDAR">
  1291. <C:time-range start="20130903T000000Z" end="20130904T000000Z"/>
  1292. </C:comp-filter>"""], items=range(1, 6))
  1293. assert "/calendar.ics/event1.ics" not in answer
  1294. assert "/calendar.ics/event2.ics" not in answer
  1295. assert "/calendar.ics/event3.ics" in answer
  1296. assert "/calendar.ics/event4.ics" not in answer
  1297. assert "/calendar.ics/event5.ics" not in answer
  1298. answer = self._test_filter(["""\
  1299. <C:comp-filter name="VCALENDAR">
  1300. <C:time-range start="20130805T000000Z" end="20130810T000000Z"/>
  1301. </C:comp-filter>"""], items=range(1, 6))
  1302. assert "/calendar.ics/event1.ics" not in answer
  1303. assert "/calendar.ics/event2.ics" not in answer
  1304. assert "/calendar.ics/event3.ics" not in answer
  1305. assert "/calendar.ics/event4.ics" not in answer
  1306. assert "/calendar.ics/event5.ics" not in answer
  1307. # HACK: VObject doesn't match RECURRENCE-ID to recurrences, the
  1308. # overwritten recurrence is still used for filtering.
  1309. answer = self._test_filter(["""\
  1310. <C:comp-filter name="VCALENDAR">
  1311. <C:time-range start="20170601T063000Z" end="20170601T070000Z"/>
  1312. </C:comp-filter>"""], items=(6, 7, 8, 9))
  1313. assert "/calendar.ics/event6.ics" in answer
  1314. assert "/calendar.ics/event7.ics" in answer
  1315. assert "/calendar.ics/event8.ics" in answer
  1316. assert "/calendar.ics/event9.ics" in answer
  1317. answer = self._test_filter(["""\
  1318. <C:comp-filter name="VCALENDAR">
  1319. <C:time-range start="20170701T060000Z"/>
  1320. </C:comp-filter>"""], items=(6, 7, 8, 9))
  1321. assert "/calendar.ics/event6.ics" in answer
  1322. assert "/calendar.ics/event7.ics" in answer
  1323. assert "/calendar.ics/event8.ics" in answer
  1324. assert "/calendar.ics/event9.ics" not in answer
  1325. answer = self._test_filter(["""\
  1326. <C:comp-filter name="VCALENDAR">
  1327. <C:time-range start="20170702T070000Z" end="20170704T060000Z"/>
  1328. </C:comp-filter>"""], items=(6, 7, 8, 9))
  1329. assert "/calendar.ics/event6.ics" not in answer
  1330. assert "/calendar.ics/event7.ics" not in answer
  1331. assert "/calendar.ics/event8.ics" not in answer
  1332. assert "/calendar.ics/event9.ics" not in answer
  1333. answer = self._test_filter(["""\
  1334. <C:comp-filter name="VCALENDAR">
  1335. <C:time-range start="20170602T075959Z" end="20170602T080000Z"/>
  1336. </C:comp-filter>"""], items=(9,))
  1337. assert "/calendar.ics/event9.ics" in answer
  1338. answer = self._test_filter(["""\
  1339. <C:comp-filter name="VCALENDAR">
  1340. <C:time-range start="20170602T080000Z" end="20170603T083000Z"/>
  1341. </C:comp-filter>"""], items=(9,))
  1342. assert "/calendar.ics/event9.ics" not in answer
  1343. def test_time_range_filter_events_rrule(self) -> None:
  1344. """Report request with time-range filter on events with rrules."""
  1345. answer = self._test_filter(["""\
  1346. <C:comp-filter name="VCALENDAR">
  1347. <C:comp-filter name="VEVENT">
  1348. <C:time-range start="20130801T000000Z" end="20131001T000000Z"/>
  1349. </C:comp-filter>
  1350. </C:comp-filter>"""], "event", items=(1, 2))
  1351. assert "/calendar.ics/event1.ics" in answer
  1352. assert "/calendar.ics/event2.ics" in answer
  1353. answer = self._test_filter(["""\
  1354. <C:comp-filter name="VCALENDAR">
  1355. <C:comp-filter name="VEVENT">
  1356. <C:time-range start="20140801T000000Z" end="20141001T000000Z"/>
  1357. </C:comp-filter>
  1358. </C:comp-filter>"""], "event", items=(1, 2))
  1359. assert "/calendar.ics/event1.ics" not in answer
  1360. assert "/calendar.ics/event2.ics" in answer
  1361. answer = self._test_filter(["""\
  1362. <C:comp-filter name="VCALENDAR">
  1363. <C:comp-filter name="VEVENT">
  1364. <C:time-range start="20120801T000000Z" end="20121001T000000Z"/>
  1365. </C:comp-filter>
  1366. </C:comp-filter>"""], "event", items=(1, 2))
  1367. assert "/calendar.ics/event1.ics" not in answer
  1368. assert "/calendar.ics/event2.ics" not in answer
  1369. answer = self._test_filter(["""\
  1370. <C:comp-filter name="VCALENDAR">
  1371. <C:comp-filter name="VEVENT">
  1372. <C:time-range start="20130903T000000Z" end="20130907T000000Z"/>
  1373. </C:comp-filter>
  1374. </C:comp-filter>"""], "event", items=(1, 2))
  1375. assert "/calendar.ics/event1.ics" not in answer
  1376. assert "/calendar.ics/event2.ics" not in answer
  1377. def test_time_range_filter_todos(self) -> None:
  1378. """Report request with time-range filter on todos."""
  1379. answer = self._test_filter(["""\
  1380. <C:comp-filter name="VCALENDAR">
  1381. <C:comp-filter name="VTODO">
  1382. <C:time-range start="20130801T000000Z" end="20131001T000000Z"/>
  1383. </C:comp-filter>
  1384. </C:comp-filter>"""], "todo", items=range(1, 9))
  1385. assert "/calendar.ics/todo1.ics" in answer
  1386. assert "/calendar.ics/todo2.ics" in answer
  1387. assert "/calendar.ics/todo3.ics" in answer
  1388. assert "/calendar.ics/todo4.ics" in answer
  1389. assert "/calendar.ics/todo5.ics" in answer
  1390. assert "/calendar.ics/todo6.ics" in answer
  1391. assert "/calendar.ics/todo7.ics" in answer
  1392. assert "/calendar.ics/todo8.ics" in answer
  1393. answer = self._test_filter(["""\
  1394. <C:comp-filter name="VCALENDAR">
  1395. <C:comp-filter name="VTODO">
  1396. <C:time-range start="20130901T160000Z" end="20130901T183000Z"/>
  1397. </C:comp-filter>
  1398. </C:comp-filter>"""], "todo", items=range(1, 9))
  1399. assert "/calendar.ics/todo1.ics" not in answer
  1400. assert "/calendar.ics/todo2.ics" in answer
  1401. assert "/calendar.ics/todo3.ics" in answer
  1402. assert "/calendar.ics/todo4.ics" not in answer
  1403. assert "/calendar.ics/todo5.ics" not in answer
  1404. assert "/calendar.ics/todo6.ics" not in answer
  1405. assert "/calendar.ics/todo7.ics" in answer
  1406. assert "/calendar.ics/todo8.ics" in answer
  1407. answer = self._test_filter(["""\
  1408. <C:comp-filter name="VCALENDAR">
  1409. <C:comp-filter name="VTODO">
  1410. <C:time-range start="20130903T160000Z" end="20130901T183000Z"/>
  1411. </C:comp-filter>
  1412. </C:comp-filter>"""], "todo", items=range(1, 9))
  1413. assert "/calendar.ics/todo2.ics" not in answer
  1414. answer = self._test_filter(["""\
  1415. <C:comp-filter name="VCALENDAR">
  1416. <C:comp-filter name="VTODO">
  1417. <C:time-range start="20130903T160000Z" end="20130901T173000Z"/>
  1418. </C:comp-filter>
  1419. </C:comp-filter>"""], "todo", items=range(1, 9))
  1420. assert "/calendar.ics/todo2.ics" not in answer
  1421. answer = self._test_filter(["""\
  1422. <C:comp-filter name="VCALENDAR">
  1423. <C:comp-filter name="VTODO">
  1424. <C:time-range start="20130903T160000Z" end="20130903T173000Z"/>
  1425. </C:comp-filter>
  1426. </C:comp-filter>"""], "todo", items=range(1, 9))
  1427. assert "/calendar.ics/todo3.ics" not in answer
  1428. answer = self._test_filter(["""\
  1429. <C:comp-filter name="VCALENDAR">
  1430. <C:comp-filter name="VTODO">
  1431. <C:time-range start="20130903T160000Z" end="20130803T203000Z"/>
  1432. </C:comp-filter>
  1433. </C:comp-filter>"""], "todo", items=range(1, 9))
  1434. assert "/calendar.ics/todo7.ics" in answer
  1435. def test_time_range_filter_events_valarm(self) -> None:
  1436. """Report request with time-range filter on events having absolute VALARM."""
  1437. answer = self._test_filter(["""\
  1438. <C:comp-filter name="VCALENDAR">
  1439. <C:comp-filter name="VEVENT">
  1440. <C:comp-filter name="VALARM">
  1441. <C:time-range start="20151010T030000Z" end="20151010T040000Z"/>
  1442. </C:comp-filter>
  1443. </C:comp-filter>
  1444. </C:comp-filter>"""], "valarm", items=[1, 2])
  1445. assert "/calendar.ics/valarm1.ics" not in answer
  1446. assert "/calendar.ics/valarm2.ics" in answer # absolute date
  1447. answer = self._test_filter(["""\
  1448. <C:comp-filter name="VCALENDAR">
  1449. <C:comp-filter name="VEVENT">
  1450. <C:comp-filter name="VALARM">
  1451. <C:time-range start="20151010T010000Z" end="20151010T020000Z"/>
  1452. </C:comp-filter>
  1453. </C:comp-filter>
  1454. </C:comp-filter>"""], "valarm", items=[1, 2])
  1455. assert "/calendar.ics/valarm1.ics" not in answer
  1456. assert "/calendar.ics/valarm2.ics" not in answer
  1457. answer = self._test_filter(["""\
  1458. <C:comp-filter name="VCALENDAR">
  1459. <C:comp-filter name="VEVENT">
  1460. <C:comp-filter name="VALARM">
  1461. <C:time-range start="20151010T080000Z" end="20151010T090000Z"/>
  1462. </C:comp-filter>
  1463. </C:comp-filter>
  1464. </C:comp-filter>"""], "valarm", items=[1, 2])
  1465. assert "/calendar.ics/valarm1.ics" not in answer
  1466. assert "/calendar.ics/valarm2.ics" not in answer
  1467. answer = self._test_filter(["""\
  1468. <C:comp-filter name="VCALENDAR">
  1469. <C:comp-filter name="VEVENT">
  1470. <C:comp-filter name="VALARM">
  1471. <C:time-range start="20151010T053000Z" end="20151010T055000Z"/>
  1472. </C:comp-filter>
  1473. </C:comp-filter>
  1474. </C:comp-filter>"""], "valarm", items=[1, 2])
  1475. assert "/calendar.ics/valarm1.ics" in answer # -15 min offset
  1476. assert "/calendar.ics/valarm2.ics" not in answer
  1477. def test_time_range_filter_todos_completed(self) -> None:
  1478. answer = self._test_filter(["""\
  1479. <C:comp-filter name="VCALENDAR">
  1480. <C:comp-filter name="VTODO">
  1481. <C:prop-filter name="COMPLETED">
  1482. <C:time-range start="20130918T000000Z" end="20130922T000000Z"/>
  1483. </C:prop-filter>
  1484. </C:comp-filter>
  1485. </C:comp-filter>"""], "todo", items=range(1, 9))
  1486. assert "/calendar.ics/todo6.ics" in answer
  1487. def test_time_range_filter_todos_rrule(self) -> None:
  1488. """Report request with time-range filter on todos with rrules."""
  1489. answer = self._test_filter(["""\
  1490. <C:comp-filter name="VCALENDAR">
  1491. <C:comp-filter name="VTODO">
  1492. <C:time-range start="20130801T000000Z" end="20131001T000000Z"/>
  1493. </C:comp-filter>
  1494. </C:comp-filter>"""], "todo", items=(1, 2, 9))
  1495. assert "/calendar.ics/todo1.ics" in answer
  1496. assert "/calendar.ics/todo2.ics" in answer
  1497. assert "/calendar.ics/todo9.ics" in answer
  1498. answer = self._test_filter(["""\
  1499. <C:comp-filter name="VCALENDAR">
  1500. <C:comp-filter name="VTODO">
  1501. <C:time-range start="20140801T000000Z" end="20141001T000000Z"/>
  1502. </C:comp-filter>
  1503. </C:comp-filter>"""], "todo", items=(1, 2, 9))
  1504. assert "/calendar.ics/todo1.ics" not in answer
  1505. assert "/calendar.ics/todo2.ics" in answer
  1506. assert "/calendar.ics/todo9.ics" in answer
  1507. answer = self._test_filter(["""\
  1508. <C:comp-filter name="VCALENDAR">
  1509. <C:comp-filter name="VTODO">
  1510. <C:time-range start="20140902T000000Z" end="20140903T000000Z"/>
  1511. </C:comp-filter>
  1512. </C:comp-filter>"""], "todo", items=(1, 2))
  1513. assert "/calendar.ics/todo1.ics" not in answer
  1514. assert "/calendar.ics/todo2.ics" in answer
  1515. answer = self._test_filter(["""\
  1516. <C:comp-filter name="VCALENDAR">
  1517. <C:comp-filter name="VTODO">
  1518. <C:time-range start="20140904T000000Z" end="20140914T000000Z"/>
  1519. </C:comp-filter>
  1520. </C:comp-filter>"""], "todo", items=(1, 2))
  1521. assert "/calendar.ics/todo1.ics" not in answer
  1522. assert "/calendar.ics/todo2.ics" not in answer
  1523. answer = self._test_filter(["""\
  1524. <C:comp-filter name="VCALENDAR">
  1525. <C:comp-filter name="VTODO">
  1526. <C:time-range start="20130902T000000Z" end="20130906T235959Z"/>
  1527. </C:comp-filter>
  1528. </C:comp-filter>"""], "todo", items=(9,))
  1529. assert "/calendar.ics/todo9.ics" not in answer
  1530. def test_time_range_filter_journals(self) -> None:
  1531. """Report request with time-range filter on journals."""
  1532. answer = self._test_filter(["""\
  1533. <C:comp-filter name="VCALENDAR">
  1534. <C:comp-filter name="VJOURNAL">
  1535. <C:time-range start="19991229T000000Z" end="20000202T000000Z"/>
  1536. </C:comp-filter>
  1537. </C:comp-filter>"""], "journal", items=(1, 2, 3))
  1538. assert "/calendar.ics/journal1.ics" not in answer
  1539. assert "/calendar.ics/journal2.ics" in answer
  1540. assert "/calendar.ics/journal3.ics" in answer
  1541. answer = self._test_filter(["""\
  1542. <C:comp-filter name="VCALENDAR">
  1543. <C:comp-filter name="VJOURNAL">
  1544. <C:time-range start="19991229T000000Z" end="20000202T000000Z"/>
  1545. </C:comp-filter>
  1546. </C:comp-filter>"""], "journal", items=(1, 2, 3))
  1547. assert "/calendar.ics/journal1.ics" not in answer
  1548. assert "/calendar.ics/journal2.ics" in answer
  1549. assert "/calendar.ics/journal3.ics" in answer
  1550. answer = self._test_filter(["""\
  1551. <C:comp-filter name="VCALENDAR">
  1552. <C:comp-filter name="VJOURNAL">
  1553. <C:time-range start="19981229T000000Z" end="19991012T000000Z"/>
  1554. </C:comp-filter>
  1555. </C:comp-filter>"""], "journal", items=(1, 2, 3))
  1556. assert "/calendar.ics/journal1.ics" not in answer
  1557. assert "/calendar.ics/journal2.ics" not in answer
  1558. assert "/calendar.ics/journal3.ics" not in answer
  1559. answer = self._test_filter(["""\
  1560. <C:comp-filter name="VCALENDAR">
  1561. <C:comp-filter name="VJOURNAL">
  1562. <C:time-range start="20131229T000000Z" end="21520202T000000Z"/>
  1563. </C:comp-filter>
  1564. </C:comp-filter>"""], "journal", items=(1, 2, 3))
  1565. assert "/calendar.ics/journal1.ics" not in answer
  1566. assert "/calendar.ics/journal2.ics" in answer
  1567. assert "/calendar.ics/journal3.ics" not in answer
  1568. answer = self._test_filter(["""\
  1569. <C:comp-filter name="VCALENDAR">
  1570. <C:comp-filter name="VJOURNAL">
  1571. <C:time-range start="20000101T000000Z" end="20000202T000000Z"/>
  1572. </C:comp-filter>
  1573. </C:comp-filter>"""], "journal", items=(1, 2, 3))
  1574. assert "/calendar.ics/journal1.ics" not in answer
  1575. assert "/calendar.ics/journal2.ics" in answer
  1576. assert "/calendar.ics/journal3.ics" in answer
  1577. def test_time_range_filter_journals_rrule(self) -> None:
  1578. """Report request with time-range filter on journals with rrules."""
  1579. answer = self._test_filter(["""\
  1580. <C:comp-filter name="VCALENDAR">
  1581. <C:comp-filter name="VJOURNAL">
  1582. <C:time-range start="19991229T000000Z" end="20000202T000000Z"/>
  1583. </C:comp-filter>
  1584. </C:comp-filter>"""], "journal", items=(1, 2))
  1585. assert "/calendar.ics/journal1.ics" not in answer
  1586. assert "/calendar.ics/journal2.ics" in answer
  1587. answer = self._test_filter(["""\
  1588. <C:comp-filter name="VCALENDAR">
  1589. <C:comp-filter name="VJOURNAL">
  1590. <C:time-range start="20051229T000000Z" end="20060202T000000Z"/>
  1591. </C:comp-filter>
  1592. </C:comp-filter>"""], "journal", items=(1, 2))
  1593. assert "/calendar.ics/journal1.ics" not in answer
  1594. assert "/calendar.ics/journal2.ics" in answer
  1595. answer = self._test_filter(["""\
  1596. <C:comp-filter name="VCALENDAR">
  1597. <C:comp-filter name="VJOURNAL">
  1598. <C:time-range start="20060102T000000Z" end="20060202T000000Z"/>
  1599. </C:comp-filter>
  1600. </C:comp-filter>"""], "journal", items=(1, 2))
  1601. assert "/calendar.ics/journal1.ics" not in answer
  1602. assert "/calendar.ics/journal2.ics" not in answer
  1603. def test_report_item(self) -> None:
  1604. """Test report request on an item"""
  1605. calendar_path = "/calendar.ics/"
  1606. self.mkcalendar(calendar_path)
  1607. event = get_file_content("event1.ics")
  1608. event_path = posixpath.join(calendar_path, "event.ics")
  1609. self.put(event_path, event)
  1610. _, responses = self.report(event_path, """\
  1611. <?xml version="1.0" encoding="utf-8" ?>
  1612. <C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav">
  1613. <D:prop xmlns:D="DAV:">
  1614. <D:getetag />
  1615. </D:prop>
  1616. </C:calendar-query>""")
  1617. assert len(responses) == 1
  1618. response = responses[event_path]
  1619. assert isinstance(response, dict)
  1620. status, prop = response["D:getetag"]
  1621. assert status == 200 and prop.text
  1622. def test_report_free_busy(self) -> None:
  1623. """Test free busy report on a few items"""
  1624. calendar_path = "/calendar.ics/"
  1625. self.mkcalendar(calendar_path)
  1626. for i in (1, 2, 10):
  1627. filename = "event{}.ics".format(i)
  1628. event = get_file_content(filename)
  1629. self.put(posixpath.join(calendar_path, filename), event)
  1630. code, responses = self.report(calendar_path, """\
  1631. <?xml version="1.0" encoding="utf-8" ?>
  1632. <C:free-busy-query xmlns:C="urn:ietf:params:xml:ns:caldav">
  1633. <C:time-range start="20130901T140000Z" end="20130908T220000Z"/>
  1634. </C:free-busy-query>""", 200, is_xml=False)
  1635. for response in responses.values():
  1636. assert isinstance(response, vobject.base.Component)
  1637. assert len(responses) == 1
  1638. vcalendar = list(responses.values())[0]
  1639. assert isinstance(vcalendar, vobject.base.Component)
  1640. assert len(vcalendar.vfreebusy_list) == 3
  1641. types = {}
  1642. for vfb in vcalendar.vfreebusy_list:
  1643. fbtype_val = vfb.fbtype.value
  1644. if fbtype_val not in types:
  1645. types[fbtype_val] = 0
  1646. types[fbtype_val] += 1
  1647. assert types == {'BUSY': 2, 'FREE': 1}
  1648. # Test max_freebusy_occurrence limit
  1649. self.configure({"reporting": {"max_freebusy_occurrence": 1}})
  1650. code, responses = self.report(calendar_path, """\
  1651. <?xml version="1.0" encoding="utf-8" ?>
  1652. <C:free-busy-query xmlns:C="urn:ietf:params:xml:ns:caldav">
  1653. <C:time-range start="20130901T140000Z" end="20130908T220000Z"/>
  1654. </C:free-busy-query>""", 400, is_xml=False)
  1655. def _report_sync_token(
  1656. self, calendar_path: str, sync_token: Optional[str] = None, **kwargs
  1657. ) -> Tuple[str, RESPONSES]:
  1658. sync_token_xml = (
  1659. "<sync-token><![CDATA[%s]]></sync-token>" % sync_token
  1660. if sync_token else "<sync-token />")
  1661. status, _, answer = self.request("REPORT", calendar_path, """\
  1662. <?xml version="1.0" encoding="utf-8" ?>
  1663. <sync-collection xmlns="DAV:">
  1664. <prop>
  1665. <getetag />
  1666. </prop>
  1667. %s
  1668. </sync-collection>""" % sync_token_xml, **kwargs)
  1669. xml = DefusedET.fromstring(answer)
  1670. if status in (403, 409):
  1671. assert xml.tag == xmlutils.make_clark("D:error")
  1672. assert sync_token and xml.find(
  1673. xmlutils.make_clark("D:valid-sync-token")) is not None
  1674. return "", {}
  1675. assert status == 207
  1676. assert xml.tag == xmlutils.make_clark("D:multistatus")
  1677. sync_token = xml.find(xmlutils.make_clark("D:sync-token")).text.strip()
  1678. assert sync_token
  1679. responses = self.parse_responses(answer)
  1680. for href, response in responses.items():
  1681. if not isinstance(response, int):
  1682. status, prop = response["D:getetag"]
  1683. assert status == 200 and prop.text and len(response) == 1
  1684. responses[href] = response = 200
  1685. assert response in (200, 404)
  1686. return sync_token, responses
  1687. def test_report_sync_collection_no_change(self) -> None:
  1688. """Test sync-collection report without modifying the collection"""
  1689. calendar_path = "/calendar.ics/"
  1690. self.mkcalendar(calendar_path)
  1691. event = get_file_content("event1.ics")
  1692. event_path = posixpath.join(calendar_path, "event.ics")
  1693. self.put(event_path, event)
  1694. sync_token, responses = self._report_sync_token(calendar_path)
  1695. assert len(responses) == 1 and responses[event_path] == 200
  1696. new_sync_token, responses = self._report_sync_token(
  1697. calendar_path, sync_token)
  1698. if not self.full_sync_token_support and not new_sync_token:
  1699. return
  1700. assert sync_token == new_sync_token and len(responses) == 0
  1701. def test_report_sync_collection_add(self) -> None:
  1702. """Test sync-collection report with an added item"""
  1703. calendar_path = "/calendar.ics/"
  1704. self.mkcalendar(calendar_path)
  1705. sync_token, responses = self._report_sync_token(calendar_path)
  1706. assert len(responses) == 0
  1707. event = get_file_content("event1.ics")
  1708. event_path = posixpath.join(calendar_path, "event.ics")
  1709. self.put(event_path, event)
  1710. sync_token, responses = self._report_sync_token(
  1711. calendar_path, sync_token)
  1712. if not self.full_sync_token_support and not sync_token:
  1713. return
  1714. assert len(responses) == 1 and responses[event_path] == 200
  1715. def test_report_sync_collection_delete(self) -> None:
  1716. """Test sync-collection report with a deleted item"""
  1717. calendar_path = "/calendar.ics/"
  1718. self.mkcalendar(calendar_path)
  1719. event = get_file_content("event1.ics")
  1720. event_path = posixpath.join(calendar_path, "event.ics")
  1721. self.put(event_path, event)
  1722. sync_token, responses = self._report_sync_token(calendar_path)
  1723. assert len(responses) == 1 and responses[event_path] == 200
  1724. self.delete(event_path)
  1725. sync_token, responses = self._report_sync_token(
  1726. calendar_path, sync_token)
  1727. if not self.full_sync_token_support and not sync_token:
  1728. return
  1729. assert len(responses) == 1 and responses[event_path] == 404
  1730. def test_report_sync_collection_create_delete(self) -> None:
  1731. """Test sync-collection report with a created and deleted item"""
  1732. calendar_path = "/calendar.ics/"
  1733. self.mkcalendar(calendar_path)
  1734. sync_token, responses = self._report_sync_token(calendar_path)
  1735. assert len(responses) == 0
  1736. event = get_file_content("event1.ics")
  1737. event_path = posixpath.join(calendar_path, "event.ics")
  1738. self.put(event_path, event)
  1739. self.delete(event_path)
  1740. sync_token, responses = self._report_sync_token(
  1741. calendar_path, sync_token)
  1742. if not self.full_sync_token_support and not sync_token:
  1743. return
  1744. assert len(responses) == 1 and responses[event_path] == 404
  1745. def test_report_sync_collection_modify_undo(self) -> None:
  1746. """Test sync-collection report with a modified and changed back item"""
  1747. calendar_path = "/calendar.ics/"
  1748. self.mkcalendar(calendar_path)
  1749. event1 = get_file_content("event1.ics")
  1750. event2 = get_file_content("event1_modified.ics")
  1751. event_path = posixpath.join(calendar_path, "event.ics")
  1752. self.put(event_path, event1)
  1753. sync_token, responses = self._report_sync_token(calendar_path)
  1754. assert len(responses) == 1 and responses[event_path] == 200
  1755. self.put(event_path, event2, check=204)
  1756. self.put(event_path, event1, check=204)
  1757. sync_token, responses = self._report_sync_token(
  1758. calendar_path, sync_token)
  1759. if not self.full_sync_token_support and not sync_token:
  1760. return
  1761. assert len(responses) == 1 and responses[event_path] == 200
  1762. def test_report_sync_collection_move(self) -> None:
  1763. """Test sync-collection report a moved item"""
  1764. calendar_path = "/calendar.ics/"
  1765. self.mkcalendar(calendar_path)
  1766. event = get_file_content("event1.ics")
  1767. event1_path = posixpath.join(calendar_path, "event1.ics")
  1768. event2_path = posixpath.join(calendar_path, "event2.ics")
  1769. self.put(event1_path, event)
  1770. sync_token, responses = self._report_sync_token(calendar_path)
  1771. assert len(responses) == 1 and responses[event1_path] == 200
  1772. self.request("MOVE", event1_path, check=201,
  1773. HTTP_DESTINATION="http://127.0.0.1/"+event2_path)
  1774. sync_token, responses = self._report_sync_token(
  1775. calendar_path, sync_token)
  1776. if not self.full_sync_token_support and not sync_token:
  1777. return
  1778. assert len(responses) == 2 and (responses[event1_path] == 404 and
  1779. responses[event2_path] == 200)
  1780. def test_report_sync_collection_move_undo(self) -> None:
  1781. """Test sync-collection report with a moved and moved back item"""
  1782. calendar_path = "/calendar.ics/"
  1783. self.mkcalendar(calendar_path)
  1784. event = get_file_content("event1.ics")
  1785. event1_path = posixpath.join(calendar_path, "event1.ics")
  1786. event2_path = posixpath.join(calendar_path, "event2.ics")
  1787. self.put(event1_path, event)
  1788. sync_token, responses = self._report_sync_token(calendar_path)
  1789. assert len(responses) == 1 and responses[event1_path] == 200
  1790. self.request("MOVE", event1_path, check=201,
  1791. HTTP_DESTINATION="http://127.0.0.1/"+event2_path)
  1792. self.request("MOVE", event2_path, check=201,
  1793. HTTP_DESTINATION="http://127.0.0.1/"+event1_path)
  1794. sync_token, responses = self._report_sync_token(
  1795. calendar_path, sync_token)
  1796. if not self.full_sync_token_support and not sync_token:
  1797. return
  1798. assert len(responses) == 2 and (responses[event1_path] == 200 and
  1799. responses[event2_path] == 404)
  1800. def test_report_sync_collection_invalid_sync_token(self) -> None:
  1801. """Test sync-collection report with an invalid sync token"""
  1802. calendar_path = "/calendar.ics/"
  1803. self.mkcalendar(calendar_path)
  1804. sync_token, _ = self._report_sync_token(
  1805. calendar_path, "http://radicale.org/ns/sync/INVALID")
  1806. assert not sync_token
  1807. def test_report_sync_collection_invalid_sync_token_with_user(self) -> None:
  1808. """Test sync-collection report with an invalid sync token and user+host+useragent"""
  1809. self.configure({"auth": {"type": "none"}})
  1810. calendar_path = "/calendar.ics/"
  1811. self.mkcalendar(calendar_path)
  1812. sync_token, _ = self._report_sync_token(
  1813. calendar_path, "http://radicale.org/ns/sync/INVALID", login="testuser:", remote_host="192.0.2.1", remote_useragent="Testclient/1.0")
  1814. assert not sync_token
  1815. def test_propfind_sync_token(self) -> None:
  1816. """Retrieve the sync-token with a propfind request"""
  1817. calendar_path = "/calendar.ics/"
  1818. self.mkcalendar(calendar_path)
  1819. propfind = get_file_content("allprop.xml")
  1820. _, responses = self.propfind(calendar_path, propfind)
  1821. response = responses[calendar_path]
  1822. assert not isinstance(response, int)
  1823. status, sync_token = response["D:sync-token"]
  1824. assert status == 200 and sync_token.text
  1825. event = get_file_content("event1.ics")
  1826. event_path = posixpath.join(calendar_path, "event.ics")
  1827. self.put(event_path, event)
  1828. _, responses = self.propfind(calendar_path, propfind)
  1829. response = responses[calendar_path]
  1830. assert not isinstance(response, int)
  1831. status, new_sync_token = response["D:sync-token"]
  1832. assert status == 200 and new_sync_token.text
  1833. assert sync_token.text != new_sync_token.text
  1834. def test_propfind_same_as_sync_collection_sync_token(self) -> None:
  1835. """Compare sync-token property with sync-collection sync-token"""
  1836. calendar_path = "/calendar.ics/"
  1837. self.mkcalendar(calendar_path)
  1838. propfind = get_file_content("allprop.xml")
  1839. _, responses = self.propfind(calendar_path, propfind)
  1840. response = responses[calendar_path]
  1841. assert not isinstance(response, int)
  1842. status, sync_token = response["D:sync-token"]
  1843. assert status == 200 and sync_token.text
  1844. report_sync_token, _ = self._report_sync_token(calendar_path)
  1845. assert sync_token.text == report_sync_token
  1846. def test_calendar_getcontenttype(self) -> None:
  1847. """Test report request on an item"""
  1848. self.mkcalendar("/test/")
  1849. for component in ("event", "todo", "journal"):
  1850. event = get_file_content("%s1.ics" % component)
  1851. status, _ = self.delete("/test/test.ics", check=None)
  1852. assert status in (200, 404)
  1853. self.put("/test/test.ics", event)
  1854. _, responses = self.report("/test/", """\
  1855. <?xml version="1.0" encoding="utf-8" ?>
  1856. <C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav">
  1857. <D:prop xmlns:D="DAV:">
  1858. <D:getcontenttype />
  1859. </D:prop>
  1860. </C:calendar-query>""")
  1861. assert len(responses) == 1
  1862. response = responses["/test/test.ics"]
  1863. assert not isinstance(response, int) and len(response) == 1
  1864. status, prop = response["D:getcontenttype"]
  1865. assert status == 200 and prop.text == (
  1866. "text/calendar;charset=utf-8;component=V%s" %
  1867. component.upper())
  1868. def test_addressbook_getcontenttype(self) -> None:
  1869. """Test report request on an item"""
  1870. self.create_addressbook("/test/")
  1871. contact = get_file_content("contact1.vcf")
  1872. self.put("/test/test.vcf", contact)
  1873. _, responses = self.report("/test/", """\
  1874. <?xml version="1.0" encoding="utf-8" ?>
  1875. <C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav">
  1876. <D:prop xmlns:D="DAV:">
  1877. <D:getcontenttype />
  1878. </D:prop>
  1879. </C:calendar-query>""")
  1880. assert len(responses) == 1
  1881. response = responses["/test/test.vcf"]
  1882. assert not isinstance(response, int) and len(response) == 1
  1883. status, prop = response["D:getcontenttype"]
  1884. assert status == 200 and prop.text == "text/vcard;charset=utf-8"
  1885. def test_authorization(self) -> None:
  1886. self.configure({"auth": {"type": "none"}})
  1887. _, responses = self.propfind("/", """\
  1888. <?xml version="1.0" encoding="utf-8"?>
  1889. <propfind xmlns="DAV:">
  1890. <prop>
  1891. <current-user-principal />
  1892. </prop>
  1893. </propfind>""", login="user:")
  1894. response = responses["/"]
  1895. assert not isinstance(response, int) and len(response) == 1
  1896. status, prop = response["D:current-user-principal"]
  1897. assert status == 200 and len(prop) == 1
  1898. element = prop.find(xmlutils.make_clark("D:href"))
  1899. assert element is not None and element.text == "/user/"
  1900. def test_authentication(self) -> None:
  1901. """Test if server sends authentication request."""
  1902. self.configure({"auth": {"type": "htpasswd",
  1903. "htpasswd_filename": os.devnull,
  1904. "htpasswd_encryption": "plain"},
  1905. "rights": {"type": "owner_only"}})
  1906. status, headers, _ = self.request("MKCOL", "/user/")
  1907. assert status in (401, 403)
  1908. assert headers.get("WWW-Authenticate")
  1909. def test_principal_collection_creation(self) -> None:
  1910. """Verify existence of the principal collection."""
  1911. self.configure({"auth": {"type": "none"}})
  1912. self.propfind("/user/", login="user:")
  1913. def test_authentication_current_user_principal_hack(self) -> None:
  1914. """Test if server sends authentication request when accessing
  1915. current-user-principal prop (workaround for DAVx5)."""
  1916. status, headers, _ = self.request("PROPFIND", "/", """\
  1917. <?xml version="1.0" encoding="utf-8"?>
  1918. <propfind xmlns="DAV:">
  1919. <prop>
  1920. <current-user-principal />
  1921. </prop>
  1922. </propfind>""")
  1923. assert status in (401, 403)
  1924. assert headers.get("WWW-Authenticate")
  1925. def test_existence_of_root_collections(self) -> None:
  1926. """Verify that the root collection always exists."""
  1927. # Use PROPFIND because GET returns message
  1928. self.propfind("/")
  1929. # it should still exist after deletion
  1930. self.delete("/")
  1931. self.propfind("/")
  1932. def test_well_known(self) -> None:
  1933. for path in ["/.well-known/caldav", "/.well-known/carddav"]:
  1934. for path in [path, "/foo" + path]:
  1935. _, headers, _ = self.request("GET", path, check=301)
  1936. assert headers.get("Location") == "/"
  1937. def test_well_known_script_name(self) -> None:
  1938. for path in ["/.well-known/caldav", "/.well-known/carddav"]:
  1939. for path in [path, "/foo" + path]:
  1940. _, headers, _ = self.request(
  1941. "GET", path, check=301, SCRIPT_NAME="/radicale")
  1942. assert headers.get("Location") == "/radicale/"
  1943. def test_well_known_not_found(self) -> None:
  1944. for path in ["/.well-known", "/.well-known/", "/.well-known/foo"]:
  1945. for path in [path, "/foo" + path]:
  1946. self.get(path, check=404)
  1947. def test_custom_headers(self) -> None:
  1948. self.configure({"headers": {"test": "123"}})
  1949. # Test if header is set on success
  1950. _, headers, _ = self.request("OPTIONS", "/", check=200)
  1951. assert headers.get("test") == "123"
  1952. # Test if header is set on failure
  1953. _, headers, _ = self.request("GET", "/.well-known/foo", check=404)
  1954. assert headers.get("test") == "123"
  1955. def test_timezone_seconds(self) -> None:
  1956. """Verify that timezones with minutes and seconds work."""
  1957. self.mkcalendar("/calendar.ics/")
  1958. event = get_file_content("event_timezone_seconds.ics")
  1959. self.put("/calendar.ics/event.ics", event)