test_base.py 72 KB

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