test_base.py 85 KB

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