test_base.py 72 KB

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