test_base.py 80 KB

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