test_base.py 79 KB

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