test_base.py 77 KB

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