test_base.py 89 KB

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