test_base.py 77 KB

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