test_base.py 69 KB

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