test_base.py 84 KB

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