test_base.py 88 KB

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