test_base.py 71 KB

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