test_base.py 80 KB

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