1
0

test_base.py 96 KB

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