test_base.py 83 KB

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