test_base.py 91 KB

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