test_base.py 71 KB

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