test_base.py 95 KB

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