| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604 |
- # This file is part of Radicale - CalDAV and CardDAV server
- # Copyright © 2012-2017 Guillaume Ayoub
- # Copyright © 2017-2019 Unrud <unrud@outlook.com>
- #
- # This library is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This library is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
- """
- Radicale tests with simple requests.
- """
- import os
- import posixpath
- import sys
- from typing import Any, Callable, ClassVar, Iterable, List, Optional, Tuple
- import defusedxml.ElementTree as DefusedET
- import pytest
- from radicale import storage, xmlutils
- from radicale.tests import RESPONSES, BaseTest
- from radicale.tests.helpers import get_file_content
- class TestBaseRequests(BaseTest):
- """Tests with simple requests."""
- # Allow skipping sync-token tests, when not fully supported by the backend
- full_sync_token_support: ClassVar[bool] = True
- def setup(self) -> None:
- BaseTest.setup(self)
- rights_file_path = os.path.join(self.colpath, "rights")
- with open(rights_file_path, "w") as f:
- f.write("""\
- [allow all]
- user: .*
- collection: .*
- permissions: RrWw""")
- self.configure({"rights": {"file": rights_file_path,
- "type": "from_file"}})
- def test_root(self) -> None:
- """GET request at "/"."""
- _, answer = self.get("/", check=302)
- assert answer == "Redirected to .web"
- def test_script_name(self) -> None:
- """GET request at "/" with SCRIPT_NAME."""
- _, answer = self.get("/", check=302, SCRIPT_NAME="/radicale")
- assert answer == "Redirected to .web"
- _, answer = self.get("", check=302, SCRIPT_NAME="/radicale")
- assert answer == "Redirected to radicale/.web"
- def test_add_event(self) -> None:
- """Add an event."""
- self.mkcalendar("/calendar.ics/")
- event = get_file_content("event1.ics")
- path = "/calendar.ics/event1.ics"
- self.put(path, event)
- status, headers, answer = self.request("GET", path)
- assert status == 200
- assert "ETag" in headers
- assert headers["Content-Type"] == "text/calendar; charset=utf-8"
- assert "VEVENT" in answer
- assert "Event" in answer
- assert "UID:event" in answer
- def test_add_event_without_uid(self) -> None:
- """Add an event without UID."""
- self.mkcalendar("/calendar.ics/")
- event = get_file_content("event1.ics").replace("UID:event1\n", "")
- assert "\nUID:" not in event
- path = "/calendar.ics/event.ics"
- self.put(path, event, check=400)
- def test_add_event_duplicate_uid(self) -> None:
- """Add an event with an existing UID."""
- self.mkcalendar("/calendar.ics/")
- event = get_file_content("event1.ics")
- self.put("/calendar.ics/event1.ics", event)
- status, answer = self.put(
- "/calendar.ics/event1-duplicate.ics", event, check=False)
- assert status in (403, 409)
- xml = DefusedET.fromstring(answer)
- assert xml.tag == xmlutils.make_clark("D:error")
- assert xml.find(xmlutils.make_clark("C:no-uid-conflict")) is not None
- def test_add_todo(self) -> None:
- """Add a todo."""
- self.mkcalendar("/calendar.ics/")
- todo = get_file_content("todo1.ics")
- path = "/calendar.ics/todo1.ics"
- self.put(path, todo)
- status, headers, answer = self.request("GET", path)
- assert status == 200
- assert "ETag" in headers
- assert headers["Content-Type"] == "text/calendar; charset=utf-8"
- assert "VTODO" in answer
- assert "Todo" in answer
- assert "UID:todo" in answer
- def test_add_contact(self) -> None:
- """Add a contact."""
- self.create_addressbook("/contacts.vcf/")
- contact = get_file_content("contact1.vcf")
- path = "/contacts.vcf/contact.vcf"
- self.put(path, contact)
- status, headers, answer = self.request("GET", path)
- assert status == 200
- assert "ETag" in headers
- assert headers["Content-Type"] == "text/vcard; charset=utf-8"
- assert "VCARD" in answer
- assert "UID:contact1" in answer
- _, answer = self.get(path)
- assert "UID:contact1" in answer
- def test_add_contact_without_uid(self) -> None:
- """Add a contact without UID."""
- self.create_addressbook("/contacts.vcf/")
- contact = get_file_content("contact1.vcf").replace("UID:contact1\n",
- "")
- assert "\nUID" not in contact
- path = "/contacts.vcf/contact.vcf"
- self.put(path, contact, check=400)
- def test_update_event(self) -> None:
- """Update an event."""
- self.mkcalendar("/calendar.ics/")
- event = get_file_content("event1.ics")
- event_modified = get_file_content("event1_modified.ics")
- path = "/calendar.ics/event1.ics"
- self.put(path, event)
- self.put(path, event_modified)
- _, answer = self.get("/calendar.ics/")
- assert answer.count("BEGIN:VEVENT") == 1
- _, answer = self.get(path)
- assert "DTSTAMP:20130902T150159Z" in answer
- def test_update_event_uid_event(self) -> None:
- """Update an event with a different UID."""
- self.mkcalendar("/calendar.ics/")
- event1 = get_file_content("event1.ics")
- event2 = get_file_content("event2.ics")
- path = "/calendar.ics/event1.ics"
- self.put(path, event1)
- status, answer = self.put(path, event2, check=False)
- assert status in (403, 409)
- xml = DefusedET.fromstring(answer)
- assert xml.tag == xmlutils.make_clark("D:error")
- assert xml.find(xmlutils.make_clark("C:no-uid-conflict")) is not None
- def test_put_whole_calendar(self) -> None:
- """Create and overwrite a whole calendar."""
- self.put("/calendar.ics/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
- event1 = get_file_content("event1.ics")
- self.put("/calendar.ics/test_event.ics", event1)
- # Overwrite
- events = get_file_content("event_multiple.ics")
- self.put("/calendar.ics/", events)
- self.get("/calendar.ics/test_event.ics", check=404)
- _, answer = self.get("/calendar.ics/")
- assert "\r\nUID:event\r\n" in answer and "\r\nUID:todo\r\n" in answer
- assert "\r\nUID:event1\r\n" not in answer
- def test_put_whole_calendar_without_uids(self) -> None:
- """Create a whole calendar without UID."""
- event = get_file_content("event_multiple.ics")
- event = event.replace("UID:event\n", "").replace("UID:todo\n", "")
- assert "\nUID:" not in event
- self.put("/calendar.ics/", event)
- _, answer = self.get("/calendar.ics")
- uids = []
- for line in answer.split("\r\n"):
- if line.startswith("UID:"):
- uids.append(line[len("UID:"):])
- assert len(uids) == 2
- for i, uid1 in enumerate(uids):
- assert uid1
- for uid2 in uids[i + 1:]:
- assert uid1 != uid2
- def test_put_whole_addressbook(self) -> None:
- """Create and overwrite a whole addressbook."""
- contacts = get_file_content("contact_multiple.vcf")
- self.put("/contacts.vcf/", contacts)
- _, answer = self.get("/contacts.vcf/")
- assert answer is not None
- assert "\r\nUID:contact1\r\n" in answer
- assert "\r\nUID:contact2\r\n" in answer
- def test_put_whole_addressbook_without_uids(self) -> None:
- """Create a whole addressbook without UID."""
- contacts = get_file_content("contact_multiple.vcf")
- contacts = contacts.replace("UID:contact1\n", "").replace(
- "UID:contact2\n", "")
- assert "\nUID:" not in contacts
- self.put("/contacts.vcf/", contacts)
- _, answer = self.get("/contacts.vcf")
- uids = []
- for line in answer.split("\r\n"):
- if line.startswith("UID:"):
- uids.append(line[len("UID:"):])
- assert len(uids) == 2
- for i, uid1 in enumerate(uids):
- assert uid1
- for uid2 in uids[i + 1:]:
- assert uid1 != uid2
- def test_verify(self) -> None:
- """Verify the storage."""
- contacts = get_file_content("contact_multiple.vcf")
- self.put("/contacts.vcf/", contacts)
- events = get_file_content("event_multiple.ics")
- self.put("/calendar.ics/", events)
- s = storage.load(self.configuration)
- assert s.verify()
- def test_delete(self) -> None:
- """Delete an event."""
- self.mkcalendar("/calendar.ics/")
- event = get_file_content("event1.ics")
- path = "/calendar.ics/event1.ics"
- self.put(path, event)
- _, responses = self.delete(path)
- assert responses[path] == 200
- _, answer = self.get("/calendar.ics/")
- assert "VEVENT" not in answer
- def test_mkcalendar(self) -> None:
- """Make a calendar."""
- self.mkcalendar("/calendar.ics/")
- _, answer = self.get("/calendar.ics/")
- assert "BEGIN:VCALENDAR" in answer
- assert "END:VCALENDAR" in answer
- def test_mkcalendar_overwrite(self) -> None:
- """Try to overwrite an existing calendar."""
- self.mkcalendar("/calendar.ics/")
- status, answer = self.mkcalendar("/calendar.ics/", check=False)
- assert status in (403, 409)
- xml = DefusedET.fromstring(answer)
- assert xml.tag == xmlutils.make_clark("D:error")
- assert xml.find(xmlutils.make_clark(
- "D:resource-must-be-null")) is not None
- def test_mkcalendar_intermediate(self) -> None:
- """Try make a calendar in a unmapped collection."""
- status, _ = self.mkcalendar("/unmapped/calendar.ics/", check=False)
- assert status == 409
- def test_mkcol(self) -> None:
- """Make a collection."""
- self.mkcol("/user/")
- def test_mkcol_overwrite(self) -> None:
- """Try to overwrite an existing collection."""
- self.mkcol("/user/")
- status = self.mkcol("/user/", check=False)
- assert status == 405
- def test_mkcol_intermediate(self) -> None:
- """Try make a collection in a unmapped collection."""
- status = self.mkcol("/unmapped/user/", check=False)
- assert status == 409
- def test_mkcol_make_calendar(self) -> None:
- """Make a calendar with additional props."""
- mkcol_make_calendar = get_file_content("mkcol_make_calendar.xml")
- self.mkcol("/calendar.ics/", mkcol_make_calendar)
- _, answer = self.get("/calendar.ics/")
- assert answer is not None
- assert "BEGIN:VCALENDAR" in answer
- assert "END:VCALENDAR" in answer
- # Read additional properties
- propfind = get_file_content("propfind_calendar_color.xml")
- _, responses = self.propfind("/calendar.ics/", propfind)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 1
- status, prop = response["ICAL:calendar-color"]
- assert status == 200 and prop.text == "#BADA55"
- def test_move(self) -> None:
- """Move a item."""
- self.mkcalendar("/calendar.ics/")
- event = get_file_content("event1.ics")
- path1 = "/calendar.ics/event1.ics"
- path2 = "/calendar.ics/event2.ics"
- self.put(path1, event)
- status, _, _ = self.request(
- "MOVE", path1, HTTP_DESTINATION=path2, HTTP_HOST="")
- assert status == 201
- self.get(path1, check=404)
- self.get(path2)
- def test_move_between_colections(self) -> None:
- """Move a item."""
- self.mkcalendar("/calendar1.ics/")
- self.mkcalendar("/calendar2.ics/")
- event = get_file_content("event1.ics")
- path1 = "/calendar1.ics/event1.ics"
- path2 = "/calendar2.ics/event2.ics"
- self.put(path1, event)
- status, _, _ = self.request(
- "MOVE", path1, HTTP_DESTINATION=path2, HTTP_HOST="")
- assert status == 201
- self.get(path1, check=404)
- self.get(path2)
- def test_move_between_colections_duplicate_uid(self) -> None:
- """Move a item to a collection which already contains the UID."""
- self.mkcalendar("/calendar1.ics/")
- self.mkcalendar("/calendar2.ics/")
- event = get_file_content("event1.ics")
- path1 = "/calendar1.ics/event1.ics"
- path2 = "/calendar2.ics/event2.ics"
- self.put(path1, event)
- self.put("/calendar2.ics/event1.ics", event)
- status, _, answer = self.request(
- "MOVE", path1, HTTP_DESTINATION=path2, HTTP_HOST="")
- assert status in (403, 409)
- xml = DefusedET.fromstring(answer)
- assert xml.tag == xmlutils.make_clark("D:error")
- assert xml.find(xmlutils.make_clark("C:no-uid-conflict")) is not None
- def test_move_between_colections_overwrite(self) -> None:
- """Move a item to a collection which already contains the item."""
- self.mkcalendar("/calendar1.ics/")
- self.mkcalendar("/calendar2.ics/")
- event = get_file_content("event1.ics")
- path1 = "/calendar1.ics/event1.ics"
- path2 = "/calendar2.ics/event1.ics"
- self.put(path1, event)
- self.put(path2, event)
- status, _, _ = self.request(
- "MOVE", path1, HTTP_DESTINATION=path2, HTTP_HOST="")
- assert status == 412
- status, _, _ = self.request("MOVE", path1, HTTP_DESTINATION=path2,
- HTTP_HOST="", HTTP_OVERWRITE="T")
- assert status == 204
- def test_move_between_colections_overwrite_uid_conflict(self) -> None:
- """Move a item to a collection which already contains the item with
- a different UID."""
- self.mkcalendar("/calendar1.ics/")
- self.mkcalendar("/calendar2.ics/")
- event1 = get_file_content("event1.ics")
- event2 = get_file_content("event2.ics")
- path1 = "/calendar1.ics/event1.ics"
- path2 = "/calendar2.ics/event2.ics"
- self.put(path1, event1)
- self.put(path2, event2)
- status, _, answer = self.request("MOVE", path1, HTTP_DESTINATION=path2,
- HTTP_HOST="", HTTP_OVERWRITE="T")
- assert status in (403, 409)
- xml = DefusedET.fromstring(answer)
- assert xml.tag == xmlutils.make_clark("D:error")
- assert xml.find(xmlutils.make_clark("C:no-uid-conflict")) is not None
- def test_head(self) -> None:
- status, _, _ = self.request("HEAD", "/")
- assert status == 302
- def test_options(self) -> None:
- status, headers, _ = self.request("OPTIONS", "/")
- assert status == 200
- assert "DAV" in headers
- def test_delete_collection(self) -> None:
- """Delete a collection."""
- self.mkcalendar("/calendar.ics/")
- event = get_file_content("event1.ics")
- self.put("/calendar.ics/event1.ics", event)
- _, responses = self.delete("/calendar.ics/")
- assert responses["/calendar.ics/"] == 200
- self.get("/calendar.ics/", check=404)
- def test_delete_root_collection(self) -> None:
- """Delete the root collection."""
- self.mkcalendar("/calendar.ics/")
- event = get_file_content("event1.ics")
- self.put("/event1.ics", event)
- self.put("/calendar.ics/event1.ics", event)
- _, responses = self.delete("/")
- assert len(responses) == 1 and responses["/"] == 200
- self.get("/calendar.ics/", check=404)
- self.get("/event1.ics", 404)
- def test_propfind(self) -> None:
- calendar_path = "/calendar.ics/"
- self.mkcalendar("/calendar.ics/")
- event = get_file_content("event1.ics")
- event_path = posixpath.join(calendar_path, "event.ics")
- self.put(event_path, event)
- _, responses = self.propfind("/", HTTP_DEPTH="1")
- assert len(responses) == 2
- assert "/" in responses and calendar_path in responses
- _, responses = self.propfind(calendar_path, HTTP_DEPTH="1")
- assert len(responses) == 2
- assert calendar_path in responses and event_path in responses
- def test_propfind_propname(self) -> None:
- self.mkcalendar("/calendar.ics/")
- event = get_file_content("event1.ics")
- self.put("/calendar.ics/event.ics", event)
- propfind = get_file_content("propname.xml")
- _, responses = self.propfind("/calendar.ics/", propfind)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int)
- status, prop = response["D:sync-token"]
- assert status == 200 and not prop.text
- _, responses = self.propfind("/calendar.ics/event.ics", propfind)
- response = responses["/calendar.ics/event.ics"]
- assert not isinstance(response, int)
- status, prop = response["D:getetag"]
- assert status == 200 and not prop.text
- def test_propfind_allprop(self) -> None:
- self.mkcalendar("/calendar.ics/")
- event = get_file_content("event1.ics")
- self.put("/calendar.ics/event.ics", event)
- propfind = get_file_content("allprop.xml")
- _, responses = self.propfind("/calendar.ics/", propfind)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int)
- status, prop = response["D:sync-token"]
- assert status == 200 and prop.text
- _, responses = self.propfind("/calendar.ics/event.ics", propfind)
- response = responses["/calendar.ics/event.ics"]
- assert not isinstance(response, int)
- status, prop = response["D:getetag"]
- assert status == 200 and prop.text
- def test_propfind_nonexistent(self) -> None:
- """Read a property that does not exist."""
- self.mkcalendar("/calendar.ics/")
- propfind = get_file_content("propfind_calendar_color.xml")
- _, responses = self.propfind("/calendar.ics/", propfind)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 1
- status, prop = response["ICAL:calendar-color"]
- assert status == 404 and not prop.text
- def test_proppatch(self) -> None:
- """Set/Remove a property and read it back."""
- self.mkcalendar("/calendar.ics/")
- proppatch = get_file_content("proppatch_set_calendar_color.xml")
- _, responses = self.proppatch("/calendar.ics/", proppatch)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 1
- status, prop = response["ICAL:calendar-color"]
- assert status == 200 and not prop.text
- # Read property back
- propfind = get_file_content("propfind_calendar_color.xml")
- _, responses = self.propfind("/calendar.ics/", propfind)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 1
- status, prop = response["ICAL:calendar-color"]
- assert status == 200 and prop.text == "#BADA55"
- propfind = get_file_content("allprop.xml")
- _, responses = self.propfind("/calendar.ics/", propfind)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int)
- status, prop = response["ICAL:calendar-color"]
- assert status == 200 and prop.text == "#BADA55"
- # Remove property
- proppatch = get_file_content("proppatch_remove_calendar_color.xml")
- _, responses = self.proppatch("/calendar.ics/", proppatch)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 1
- status, prop = response["ICAL:calendar-color"]
- assert status == 200 and not prop.text
- # Read property back
- propfind = get_file_content("propfind_calendar_color.xml")
- _, responses = self.propfind("/calendar.ics/", propfind)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 1
- status, prop = response["ICAL:calendar-color"]
- assert status == 404
- def test_proppatch_multiple1(self) -> None:
- """Set/Remove a multiple properties and read them back."""
- self.mkcalendar("/calendar.ics/")
- propfind = get_file_content("propfind_multiple.xml")
- proppatch = get_file_content("proppatch_set_multiple1.xml")
- _, responses = self.proppatch("/calendar.ics/", proppatch)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 2
- status, prop = response["ICAL:calendar-color"]
- assert status == 200 and not prop.text
- status, prop = response["C:calendar-description"]
- assert status == 200 and not prop.text
- # Read properties back
- _, responses = self.propfind("/calendar.ics/", propfind)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 2
- status, prop = response["ICAL:calendar-color"]
- assert status == 200 and prop.text == "#BADA55"
- status, prop = response["C:calendar-description"]
- assert status == 200 and prop.text == "test"
- # Remove properties
- proppatch = get_file_content("proppatch_remove_multiple1.xml")
- _, responses = self.proppatch("/calendar.ics/", proppatch)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 2
- status, prop = response["ICAL:calendar-color"]
- assert status == 200 and not prop.text
- status, prop = response["C:calendar-description"]
- assert status == 200 and not prop.text
- # Read properties back
- _, responses = self.propfind("/calendar.ics/", propfind)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 2
- status, prop = response["ICAL:calendar-color"]
- assert status == 404
- status, prop = response["C:calendar-description"]
- assert status == 404
- def test_proppatch_multiple2(self) -> None:
- """Set/Remove a multiple properties and read them back."""
- self.mkcalendar("/calendar.ics/")
- propfind = get_file_content("propfind_multiple.xml")
- proppatch = get_file_content("proppatch_set_multiple2.xml")
- _, responses = self.proppatch("/calendar.ics/", proppatch)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 2
- status, prop = response["ICAL:calendar-color"]
- assert status == 200 and not prop.text
- status, prop = response["C:calendar-description"]
- assert status == 200 and not prop.text
- # Read properties back
- _, responses = self.propfind("/calendar.ics/", propfind)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 2
- assert len(response) == 2
- status, prop = response["ICAL:calendar-color"]
- assert status == 200 and prop.text == "#BADA55"
- status, prop = response["C:calendar-description"]
- assert status == 200 and prop.text == "test"
- # Remove properties
- proppatch = get_file_content("proppatch_remove_multiple2.xml")
- _, responses = self.proppatch("/calendar.ics/", proppatch)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 2
- status, prop = response["ICAL:calendar-color"]
- assert status == 200 and not prop.text
- status, prop = response["C:calendar-description"]
- assert status == 200 and not prop.text
- # Read properties back
- _, responses = self.propfind("/calendar.ics/", propfind)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 2
- status, prop = response["ICAL:calendar-color"]
- assert status == 404
- status, prop = response["C:calendar-description"]
- assert status == 404
- def test_proppatch_set_and_remove(self) -> None:
- """Set and remove multiple properties in single request."""
- self.mkcalendar("/calendar.ics/")
- propfind = get_file_content("propfind_multiple.xml")
- # Prepare
- proppatch = get_file_content("proppatch_set_multiple1.xml")
- self.proppatch("/calendar.ics/", proppatch)
- # Remove and set properties in single request
- proppatch = get_file_content("proppatch_set_and_remove.xml")
- _, responses = self.proppatch("/calendar.ics/", proppatch)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 2
- status, prop = response["ICAL:calendar-color"]
- assert status == 200 and not prop.text
- status, prop = response["C:calendar-description"]
- assert status == 200 and not prop.text
- # Read properties back
- _, responses = self.propfind("/calendar.ics/", propfind)
- response = responses["/calendar.ics/"]
- assert not isinstance(response, int) and len(response) == 2
- status, prop = response["ICAL:calendar-color"]
- assert status == 404
- status, prop = response["C:calendar-description"]
- assert status == 200 and prop.text == "test2"
- def test_put_whole_calendar_multiple_events_with_same_uid(self) -> None:
- """Add two events with the same UID."""
- self.put("/calendar.ics/", get_file_content("event2.ics"))
- _, responses = self.report("/calendar.ics/", """\
- <?xml version="1.0" encoding="utf-8" ?>
- <C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav">
- <D:prop xmlns:D="DAV:">
- <D:getetag/>
- </D:prop>
- </C:calendar-query>""")
- assert len(responses) == 1
- response = responses["/calendar.ics/event2.ics"]
- assert not isinstance(response, int)
- status, prop = response["D:getetag"]
- assert status == 200 and prop.text
- _, answer = self.get("/calendar.ics/")
- assert answer.count("BEGIN:VEVENT") == 2
- def _test_filter(self, filters: Iterable[str], kind: str = "event",
- test: Optional[str] = None, items: Iterable[int] = (1,)
- ) -> List[str]:
- filter_template = "<C:filter>%s</C:filter>"
- create_collection_fn: Callable[[str], Any]
- if kind in ("event", "journal", "todo"):
- create_collection_fn = self.mkcalendar
- path = "/calendar.ics/"
- filename_template = "%s%d.ics"
- namespace = "urn:ietf:params:xml:ns:caldav"
- report = "calendar-query"
- elif kind == "contact":
- create_collection_fn = self.create_addressbook
- if test:
- filter_template = '<C:filter test="%s">%%s</C:filter>' % test
- path = "/contacts.vcf/"
- filename_template = "%s%d.vcf"
- namespace = "urn:ietf:params:xml:ns:carddav"
- report = "addressbook-query"
- else:
- raise ValueError("Unsupported kind: %r" % kind)
- status, _, = self.delete(path, check=False)
- assert status in (200, 404)
- create_collection_fn(path)
- for i in items:
- filename = filename_template % (kind, i)
- event = get_file_content(filename)
- self.put(posixpath.join(path, filename), event)
- filters_text = "".join(filter_template % f for f in filters)
- _, responses = self.report(path, """\
- <?xml version="1.0" encoding="utf-8" ?>
- <C:{1} xmlns:C="{0}">
- <D:prop xmlns:D="DAV:">
- <D:getetag/>
- </D:prop>
- {2}
- </C:{1}>""".format(namespace, report, filters_text))
- assert responses is not None
- paths = []
- for path, props in responses.items():
- assert not isinstance(props, int) and len(props) == 1
- status, prop = props["D:getetag"]
- assert status == 200 and prop.text
- paths.append(path)
- return paths
- def test_addressbook_empty_filter(self) -> None:
- self._test_filter([""], kind="contact")
- def test_addressbook_prop_filter(self) -> None:
- assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap" match-type="contains"
- >es</C:text-match>
- </C:prop-filter>"""], "contact")
- assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap">es</C:text-match>
- </C:prop-filter>"""], "contact")
- assert "/contacts.vcf/contact1.vcf" not in self._test_filter(["""\
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap" match-type="contains"
- >a</C:text-match>
- </C:prop-filter>"""], "contact")
- assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap" match-type="equals"
- >test</C:text-match>
- </C:prop-filter>"""], "contact")
- assert "/contacts.vcf/contact1.vcf" not in self._test_filter(["""\
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap" match-type="equals"
- >tes</C:text-match>
- </C:prop-filter>"""], "contact")
- assert "/contacts.vcf/contact1.vcf" not in self._test_filter(["""\
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap" match-type="equals"
- >est</C:text-match>
- </C:prop-filter>"""], "contact")
- assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap" match-type="starts-with"
- >tes</C:text-match>
- </C:prop-filter>"""], "contact")
- assert "/contacts.vcf/contact1.vcf" not in self._test_filter(["""\
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap" match-type="starts-with"
- >est</C:text-match>
- </C:prop-filter>"""], "contact")
- assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap" match-type="ends-with"
- >est</C:text-match>
- </C:prop-filter>"""], "contact")
- assert "/contacts.vcf/contact1.vcf" not in self._test_filter(["""\
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap" match-type="ends-with"
- >tes</C:text-match>
- </C:prop-filter>"""], "contact")
- def test_addressbook_prop_filter_any(self) -> None:
- assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap">test</C:text-match>
- </C:prop-filter>
- <C:prop-filter name="EMAIL">
- <C:text-match collation="i;unicode-casemap">test</C:text-match>
- </C:prop-filter>"""], "contact", test="anyof")
- assert "/contacts.vcf/contact1.vcf" not in self._test_filter(["""\
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap">a</C:text-match>
- </C:prop-filter>
- <C:prop-filter name="EMAIL">
- <C:text-match collation="i;unicode-casemap">test</C:text-match>
- </C:prop-filter>"""], "contact", test="anyof")
- assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap">test</C:text-match>
- </C:prop-filter>
- <C:prop-filter name="EMAIL">
- <C:text-match collation="i;unicode-casemap">test</C:text-match>
- </C:prop-filter>"""], "contact")
- def test_addressbook_prop_filter_all(self) -> None:
- assert "/contacts.vcf/contact1.vcf" in self._test_filter(["""\
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap">tes</C:text-match>
- </C:prop-filter>
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap">est</C:text-match>
- </C:prop-filter>"""], "contact", test="allof")
- assert "/contacts.vcf/contact1.vcf" not in self._test_filter(["""\
- <C:prop-filter name="NICKNAME">
- <C:text-match collation="i;unicode-casemap">test</C:text-match>
- </C:prop-filter>
- <C:prop-filter name="EMAIL">
- <C:text-match collation="i;unicode-casemap">test</C:text-match>
- </C:prop-filter>"""], "contact", test="allof")
- def test_calendar_empty_filter(self) -> None:
- self._test_filter([""])
- def test_calendar_tag_filter(self) -> None:
- """Report request with tag-based filter on calendar."""
- assert "/calendar.ics/event1.ics" in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR"></C:comp-filter>"""])
- def test_item_tag_filter(self) -> None:
- """Report request with tag-based filter on an item."""
- assert "/calendar.ics/event1.ics" in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT"></C:comp-filter>
- </C:comp-filter>"""])
- assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VTODO"></C:comp-filter>
- </C:comp-filter>"""])
- def test_item_not_tag_filter(self) -> None:
- """Report request with tag-based is-not filter on an item."""
- assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:is-not-defined />
- </C:comp-filter>
- </C:comp-filter>"""])
- assert "/calendar.ics/event1.ics" in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VTODO">
- <C:is-not-defined />
- </C:comp-filter>
- </C:comp-filter>"""])
- def test_item_prop_filter(self) -> None:
- """Report request with prop-based filter on an item."""
- assert "/calendar.ics/event1.ics" in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="SUMMARY"></C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>"""])
- assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="UNKNOWN"></C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>"""])
- def test_item_not_prop_filter(self) -> None:
- """Report request with prop-based is-not filter on an item."""
- assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="SUMMARY">
- <C:is-not-defined />
- </C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>"""])
- assert "/calendar.ics/event1.ics" in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="UNKNOWN">
- <C:is-not-defined />
- </C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>"""])
- def test_mutiple_filters(self) -> None:
- """Report request with multiple filters on an item."""
- assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="SUMMARY">
- <C:is-not-defined />
- </C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>""", """
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="UNKNOWN">
- <C:is-not-defined />
- </C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>"""])
- assert "/calendar.ics/event1.ics" in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="SUMMARY"></C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>""", """
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="UNKNOWN">
- <C:is-not-defined />
- </C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>"""])
- assert "/calendar.ics/event1.ics" in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="SUMMARY"></C:prop-filter>
- <C:prop-filter name="UNKNOWN">
- <C:is-not-defined />
- </C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>"""])
- def test_text_match_filter(self) -> None:
- """Report request with text-match filter on calendar."""
- assert "/calendar.ics/event1.ics" in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="SUMMARY">
- <C:text-match>event</C:text-match>
- </C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>"""])
- assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="UNKNOWN">
- <C:text-match>event</C:text-match>
- </C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>"""])
- assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="SUMMARY">
- <C:text-match>unknown</C:text-match>
- </C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>"""])
- assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="SUMMARY">
- <C:text-match negate-condition="yes">event</C:text-match>
- </C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>"""])
- def test_param_filter(self) -> None:
- """Report request with param-filter on calendar."""
- assert "/calendar.ics/event1.ics" in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="ATTENDEE">
- <C:param-filter name="PARTSTAT">
- <C:text-match collation="i;ascii-casemap"
- >ACCEPTED</C:text-match>
- </C:param-filter>
- </C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>"""])
- assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="ATTENDEE">
- <C:param-filter name="PARTSTAT">
- <C:text-match collation="i;ascii-casemap"
- >UNKNOWN</C:text-match>
- </C:param-filter>
- </C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>"""])
- assert "/calendar.ics/event1.ics" not in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="ATTENDEE">
- <C:param-filter name="PARTSTAT">
- <C:is-not-defined />
- </C:param-filter>
- </C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>"""])
- assert "/calendar.ics/event1.ics" in self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="ATTENDEE">
- <C:param-filter name="UNKNOWN">
- <C:is-not-defined />
- </C:param-filter>
- </C:prop-filter>
- </C:comp-filter>
- </C:comp-filter>"""])
- def test_time_range_filter_events(self) -> None:
- """Report request with time-range filter on events."""
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:time-range start="20130801T000000Z" end="20131001T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "event", items=range(1, 6))
- assert "/calendar.ics/event1.ics" in answer
- assert "/calendar.ics/event2.ics" in answer
- assert "/calendar.ics/event3.ics" in answer
- assert "/calendar.ics/event4.ics" in answer
- assert "/calendar.ics/event5.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VTODO">
- <C:time-range start="20130801T000000Z" end="20131001T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "event", items=range(1, 6))
- assert "/calendar.ics/event1.ics" not in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:prop-filter name="ATTENDEE">
- <C:param-filter name="PARTSTAT">
- <C:is-not-defined />
- </C:param-filter>
- </C:prop-filter>
- <C:time-range start="20130801T000000Z" end="20131001T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], items=range(1, 6))
- assert "/calendar.ics/event1.ics" not in answer
- assert "/calendar.ics/event2.ics" not in answer
- assert "/calendar.ics/event3.ics" not in answer
- assert "/calendar.ics/event4.ics" not in answer
- assert "/calendar.ics/event5.ics" not in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:time-range start="20130902T000000Z" end="20131001T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], items=range(1, 6))
- assert "/calendar.ics/event1.ics" not in answer
- assert "/calendar.ics/event2.ics" in answer
- assert "/calendar.ics/event3.ics" in answer
- assert "/calendar.ics/event4.ics" in answer
- assert "/calendar.ics/event5.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:time-range start="20130903T000000Z" end="20130908T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], items=range(1, 6))
- assert "/calendar.ics/event1.ics" not in answer
- assert "/calendar.ics/event2.ics" not in answer
- assert "/calendar.ics/event3.ics" in answer
- assert "/calendar.ics/event4.ics" in answer
- assert "/calendar.ics/event5.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:time-range start="20130903T000000Z" end="20130904T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], items=range(1, 6))
- assert "/calendar.ics/event1.ics" not in answer
- assert "/calendar.ics/event2.ics" not in answer
- assert "/calendar.ics/event3.ics" in answer
- assert "/calendar.ics/event4.ics" not in answer
- assert "/calendar.ics/event5.ics" not in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:time-range start="20130805T000000Z" end="20130810T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], items=range(1, 6))
- assert "/calendar.ics/event1.ics" not in answer
- assert "/calendar.ics/event2.ics" not in answer
- assert "/calendar.ics/event3.ics" not in answer
- assert "/calendar.ics/event4.ics" not in answer
- assert "/calendar.ics/event5.ics" not in answer
- # HACK: VObject doesn't match RECURRENCE-ID to recurrences, the
- # overwritten recurrence is still used for filtering.
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:time-range start="20170601T063000Z" end="20170601T070000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], items=(6, 7, 8, 9))
- assert "/calendar.ics/event6.ics" in answer
- assert "/calendar.ics/event7.ics" in answer
- assert "/calendar.ics/event8.ics" in answer
- assert "/calendar.ics/event9.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:time-range start="20170701T060000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], items=(6, 7, 8, 9))
- assert "/calendar.ics/event6.ics" in answer
- assert "/calendar.ics/event7.ics" in answer
- assert "/calendar.ics/event8.ics" in answer
- assert "/calendar.ics/event9.ics" not in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:time-range start="20170702T070000Z" end="20170704T060000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], items=(6, 7, 8, 9))
- assert "/calendar.ics/event6.ics" not in answer
- assert "/calendar.ics/event7.ics" not in answer
- assert "/calendar.ics/event8.ics" not in answer
- assert "/calendar.ics/event9.ics" not in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:time-range start="20170602T075959Z" end="20170602T080000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], items=(9,))
- assert "/calendar.ics/event9.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:time-range start="20170602T080000Z" end="20170603T083000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], items=(9,))
- assert "/calendar.ics/event9.ics" not in answer
- def test_time_range_filter_events_rrule(self) -> None:
- """Report request with time-range filter on events with rrules."""
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:time-range start="20130801T000000Z" end="20131001T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "event", items=(1, 2))
- assert "/calendar.ics/event1.ics" in answer
- assert "/calendar.ics/event2.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:time-range start="20140801T000000Z" end="20141001T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "event", items=(1, 2))
- assert "/calendar.ics/event1.ics" not in answer
- assert "/calendar.ics/event2.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:time-range start="20120801T000000Z" end="20121001T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "event", items=(1, 2))
- assert "/calendar.ics/event1.ics" not in answer
- assert "/calendar.ics/event2.ics" not in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VEVENT">
- <C:time-range start="20130903T000000Z" end="20130907T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "event", items=(1, 2))
- assert "/calendar.ics/event1.ics" not in answer
- assert "/calendar.ics/event2.ics" not in answer
- def test_time_range_filter_todos(self) -> None:
- """Report request with time-range filter on todos."""
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VTODO">
- <C:time-range start="20130801T000000Z" end="20131001T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "todo", items=range(1, 9))
- assert "/calendar.ics/todo1.ics" in answer
- assert "/calendar.ics/todo2.ics" in answer
- assert "/calendar.ics/todo3.ics" in answer
- assert "/calendar.ics/todo4.ics" in answer
- assert "/calendar.ics/todo5.ics" in answer
- assert "/calendar.ics/todo6.ics" in answer
- assert "/calendar.ics/todo7.ics" in answer
- assert "/calendar.ics/todo8.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VTODO">
- <C:time-range start="20130901T160000Z" end="20130901T183000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "todo", items=range(1, 9))
- assert "/calendar.ics/todo1.ics" not in answer
- assert "/calendar.ics/todo2.ics" in answer
- assert "/calendar.ics/todo3.ics" in answer
- assert "/calendar.ics/todo4.ics" not in answer
- assert "/calendar.ics/todo5.ics" not in answer
- assert "/calendar.ics/todo6.ics" not in answer
- assert "/calendar.ics/todo7.ics" in answer
- assert "/calendar.ics/todo8.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VTODO">
- <C:time-range start="20130903T160000Z" end="20130901T183000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "todo", items=range(1, 9))
- assert "/calendar.ics/todo2.ics" not in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VTODO">
- <C:time-range start="20130903T160000Z" end="20130901T173000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "todo", items=range(1, 9))
- assert "/calendar.ics/todo2.ics" not in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VTODO">
- <C:time-range start="20130903T160000Z" end="20130903T173000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "todo", items=range(1, 9))
- assert "/calendar.ics/todo3.ics" not in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VTODO">
- <C:time-range start="20130903T160000Z" end="20130803T203000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "todo", items=range(1, 9))
- assert "/calendar.ics/todo7.ics" in answer
- def test_time_range_filter_todos_rrule(self) -> None:
- """Report request with time-range filter on todos with rrules."""
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VTODO">
- <C:time-range start="20130801T000000Z" end="20131001T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "todo", items=(1, 2, 9))
- assert "/calendar.ics/todo1.ics" in answer
- assert "/calendar.ics/todo2.ics" in answer
- assert "/calendar.ics/todo9.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VTODO">
- <C:time-range start="20140801T000000Z" end="20141001T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "todo", items=(1, 2, 9))
- assert "/calendar.ics/todo1.ics" not in answer
- assert "/calendar.ics/todo2.ics" in answer
- assert "/calendar.ics/todo9.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VTODO">
- <C:time-range start="20140902T000000Z" end="20140903T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "todo", items=(1, 2))
- assert "/calendar.ics/todo1.ics" not in answer
- assert "/calendar.ics/todo2.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VTODO">
- <C:time-range start="20140904T000000Z" end="20140914T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "todo", items=(1, 2))
- assert "/calendar.ics/todo1.ics" not in answer
- assert "/calendar.ics/todo2.ics" not in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VTODO">
- <C:time-range start="20130902T000000Z" end="20130906T235959Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "todo", items=(9,))
- assert "/calendar.ics/todo9.ics" not in answer
- def test_time_range_filter_journals(self) -> None:
- """Report request with time-range filter on journals."""
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VJOURNAL">
- <C:time-range start="19991229T000000Z" end="20000202T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "journal", items=(1, 2, 3))
- assert "/calendar.ics/journal1.ics" not in answer
- assert "/calendar.ics/journal2.ics" in answer
- assert "/calendar.ics/journal3.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VJOURNAL">
- <C:time-range start="19991229T000000Z" end="20000202T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "journal", items=(1, 2, 3))
- assert "/calendar.ics/journal1.ics" not in answer
- assert "/calendar.ics/journal2.ics" in answer
- assert "/calendar.ics/journal3.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VJOURNAL">
- <C:time-range start="19981229T000000Z" end="19991012T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "journal", items=(1, 2, 3))
- assert "/calendar.ics/journal1.ics" not in answer
- assert "/calendar.ics/journal2.ics" not in answer
- assert "/calendar.ics/journal3.ics" not in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VJOURNAL">
- <C:time-range start="20131229T000000Z" end="21520202T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "journal", items=(1, 2, 3))
- assert "/calendar.ics/journal1.ics" not in answer
- assert "/calendar.ics/journal2.ics" in answer
- assert "/calendar.ics/journal3.ics" not in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VJOURNAL">
- <C:time-range start="20000101T000000Z" end="20000202T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "journal", items=(1, 2, 3))
- assert "/calendar.ics/journal1.ics" not in answer
- assert "/calendar.ics/journal2.ics" in answer
- assert "/calendar.ics/journal3.ics" in answer
- def test_time_range_filter_journals_rrule(self) -> None:
- """Report request with time-range filter on journals with rrules."""
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VJOURNAL">
- <C:time-range start="19991229T000000Z" end="20000202T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "journal", items=(1, 2))
- assert "/calendar.ics/journal1.ics" not in answer
- assert "/calendar.ics/journal2.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VJOURNAL">
- <C:time-range start="20051229T000000Z" end="20060202T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "journal", items=(1, 2))
- assert "/calendar.ics/journal1.ics" not in answer
- assert "/calendar.ics/journal2.ics" in answer
- answer = self._test_filter(["""\
- <C:comp-filter name="VCALENDAR">
- <C:comp-filter name="VJOURNAL">
- <C:time-range start="20060102T000000Z" end="20060202T000000Z"/>
- </C:comp-filter>
- </C:comp-filter>"""], "journal", items=(1, 2))
- assert "/calendar.ics/journal1.ics" not in answer
- assert "/calendar.ics/journal2.ics" not in answer
- def test_report_item(self) -> None:
- """Test report request on an item"""
- calendar_path = "/calendar.ics/"
- self.mkcalendar(calendar_path)
- event = get_file_content("event1.ics")
- event_path = posixpath.join(calendar_path, "event.ics")
- self.put(event_path, event)
- _, responses = self.report(event_path, """\
- <?xml version="1.0" encoding="utf-8" ?>
- <C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav">
- <D:prop xmlns:D="DAV:">
- <D:getetag />
- </D:prop>
- </C:calendar-query>""")
- assert len(responses) == 1
- response = responses[event_path]
- assert not isinstance(response, int)
- status, prop = response["D:getetag"]
- assert status == 200 and prop.text
- def _report_sync_token(
- self, calendar_path: str, sync_token: Optional[str] = None
- ) -> Tuple[str, RESPONSES]:
- sync_token_xml = (
- "<sync-token><![CDATA[%s]]></sync-token>" % sync_token
- if sync_token else "<sync-token />")
- status, _, answer = self.request("REPORT", calendar_path, """\
- <?xml version="1.0" encoding="utf-8" ?>
- <sync-collection xmlns="DAV:">
- <prop>
- <getetag />
- </prop>
- %s
- </sync-collection>""" % sync_token_xml)
- xml = DefusedET.fromstring(answer)
- if status in (403, 409):
- assert xml.tag == xmlutils.make_clark("D:error")
- assert sync_token and xml.find(
- xmlutils.make_clark("D:valid-sync-token")) is not None
- return "", {}
- assert status == 207
- assert xml.tag == xmlutils.make_clark("D:multistatus")
- sync_token = xml.find(xmlutils.make_clark("D:sync-token")).text.strip()
- assert sync_token
- responses = self.parse_responses(answer)
- for href, response in responses.items():
- if not isinstance(response, int):
- status, prop = response["D:getetag"]
- assert status == 200 and prop.text and len(response) == 1
- responses[href] = response = 200
- assert response in (200, 404)
- return sync_token, responses
- def test_report_sync_collection_no_change(self) -> None:
- """Test sync-collection report without modifying the collection"""
- calendar_path = "/calendar.ics/"
- self.mkcalendar(calendar_path)
- event = get_file_content("event1.ics")
- event_path = posixpath.join(calendar_path, "event.ics")
- self.put(event_path, event)
- sync_token, responses = self._report_sync_token(calendar_path)
- assert len(responses) == 1 and responses[event_path] == 200
- new_sync_token, responses = self._report_sync_token(
- calendar_path, sync_token)
- if not self.full_sync_token_support and not new_sync_token:
- return
- assert sync_token == new_sync_token and len(responses) == 0
- def test_report_sync_collection_add(self) -> None:
- """Test sync-collection report with an added item"""
- calendar_path = "/calendar.ics/"
- self.mkcalendar(calendar_path)
- sync_token, responses = self._report_sync_token(calendar_path)
- assert len(responses) == 0
- event = get_file_content("event1.ics")
- event_path = posixpath.join(calendar_path, "event.ics")
- self.put(event_path, event)
- sync_token, responses = self._report_sync_token(
- calendar_path, sync_token)
- if not self.full_sync_token_support and not sync_token:
- return
- assert len(responses) == 1 and responses[event_path] == 200
- def test_report_sync_collection_delete(self) -> None:
- """Test sync-collection report with a deleted item"""
- calendar_path = "/calendar.ics/"
- self.mkcalendar(calendar_path)
- event = get_file_content("event1.ics")
- event_path = posixpath.join(calendar_path, "event.ics")
- self.put(event_path, event)
- sync_token, responses = self._report_sync_token(calendar_path)
- assert len(responses) == 1 and responses[event_path] == 200
- self.delete(event_path)
- sync_token, responses = self._report_sync_token(
- calendar_path, sync_token)
- if not self.full_sync_token_support and not sync_token:
- return
- assert len(responses) == 1 and responses[event_path] == 404
- def test_report_sync_collection_create_delete(self) -> None:
- """Test sync-collection report with a created and deleted item"""
- calendar_path = "/calendar.ics/"
- self.mkcalendar(calendar_path)
- sync_token, responses = self._report_sync_token(calendar_path)
- assert len(responses) == 0
- event = get_file_content("event1.ics")
- event_path = posixpath.join(calendar_path, "event.ics")
- self.put(event_path, event)
- self.delete(event_path)
- sync_token, responses = self._report_sync_token(
- calendar_path, sync_token)
- if not self.full_sync_token_support and not sync_token:
- return
- assert len(responses) == 1 and responses[event_path] == 404
- def test_report_sync_collection_modify_undo(self) -> None:
- """Test sync-collection report with a modified and changed back item"""
- calendar_path = "/calendar.ics/"
- self.mkcalendar(calendar_path)
- event1 = get_file_content("event1.ics")
- event2 = get_file_content("event1_modified.ics")
- event_path = posixpath.join(calendar_path, "event.ics")
- self.put(event_path, event1)
- sync_token, responses = self._report_sync_token(calendar_path)
- assert len(responses) == 1 and responses[event_path] == 200
- self.put(event_path, event2)
- self.put(event_path, event1)
- sync_token, responses = self._report_sync_token(
- calendar_path, sync_token)
- if not self.full_sync_token_support and not sync_token:
- return
- assert len(responses) == 1 and responses[event_path] == 200
- def test_report_sync_collection_move(self) -> None:
- """Test sync-collection report a moved item"""
- calendar_path = "/calendar.ics/"
- self.mkcalendar(calendar_path)
- event = get_file_content("event1.ics")
- event1_path = posixpath.join(calendar_path, "event1.ics")
- event2_path = posixpath.join(calendar_path, "event2.ics")
- self.put(event1_path, event)
- sync_token, responses = self._report_sync_token(calendar_path)
- assert len(responses) == 1 and responses[event1_path] == 200
- status, _, _ = self.request(
- "MOVE", event1_path, HTTP_DESTINATION=event2_path, HTTP_HOST="")
- assert status == 201
- sync_token, responses = self._report_sync_token(
- calendar_path, sync_token)
- if not self.full_sync_token_support and not sync_token:
- return
- assert len(responses) == 2 and (responses[event1_path] == 404 and
- responses[event2_path] == 200)
- def test_report_sync_collection_move_undo(self) -> None:
- """Test sync-collection report with a moved and moved back item"""
- calendar_path = "/calendar.ics/"
- self.mkcalendar(calendar_path)
- event = get_file_content("event1.ics")
- event1_path = posixpath.join(calendar_path, "event1.ics")
- event2_path = posixpath.join(calendar_path, "event2.ics")
- self.put(event1_path, event)
- sync_token, responses = self._report_sync_token(calendar_path)
- assert len(responses) == 1 and responses[event1_path] == 200
- status, _, _ = self.request(
- "MOVE", event1_path, HTTP_DESTINATION=event2_path, HTTP_HOST="")
- assert status == 201
- status, _, _ = self.request(
- "MOVE", event2_path, HTTP_DESTINATION=event1_path, HTTP_HOST="")
- assert status == 201
- sync_token, responses = self._report_sync_token(
- calendar_path, sync_token)
- if not self.full_sync_token_support and not sync_token:
- return
- assert len(responses) == 2 and (responses[event1_path] == 200 and
- responses[event2_path] == 404)
- def test_report_sync_collection_invalid_sync_token(self) -> None:
- """Test sync-collection report with an invalid sync token"""
- calendar_path = "/calendar.ics/"
- self.mkcalendar(calendar_path)
- sync_token, _ = self._report_sync_token(
- calendar_path, "http://radicale.org/ns/sync/INVALID")
- assert not sync_token
- def test_propfind_sync_token(self) -> None:
- """Retrieve the sync-token with a propfind request"""
- calendar_path = "/calendar.ics/"
- self.mkcalendar(calendar_path)
- propfind = get_file_content("allprop.xml")
- _, responses = self.propfind(calendar_path, propfind)
- response = responses[calendar_path]
- assert not isinstance(response, int)
- status, sync_token = response["D:sync-token"]
- assert status == 200 and sync_token.text
- event = get_file_content("event1.ics")
- event_path = posixpath.join(calendar_path, "event.ics")
- self.put(event_path, event)
- _, responses = self.propfind(calendar_path, propfind)
- response = responses[calendar_path]
- assert not isinstance(response, int)
- status, new_sync_token = response["D:sync-token"]
- assert status == 200 and new_sync_token.text
- assert sync_token.text != new_sync_token.text
- def test_propfind_same_as_sync_collection_sync_token(self) -> None:
- """Compare sync-token property with sync-collection sync-token"""
- calendar_path = "/calendar.ics/"
- self.mkcalendar(calendar_path)
- propfind = get_file_content("allprop.xml")
- _, responses = self.propfind(calendar_path, propfind)
- response = responses[calendar_path]
- assert not isinstance(response, int)
- status, sync_token = response["D:sync-token"]
- assert status == 200 and sync_token.text
- report_sync_token, _ = self._report_sync_token(calendar_path)
- assert sync_token.text == report_sync_token
- def test_calendar_getcontenttype(self) -> None:
- """Test report request on an item"""
- self.mkcalendar("/test/")
- for component in ("event", "todo", "journal"):
- event = get_file_content("%s1.ics" % component)
- status, _ = self.delete("/test/test.ics", check=False)
- assert status in (200, 404)
- self.put("/test/test.ics", event)
- _, responses = self.report("/test/", """\
- <?xml version="1.0" encoding="utf-8" ?>
- <C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav">
- <D:prop xmlns:D="DAV:">
- <D:getcontenttype />
- </D:prop>
- </C:calendar-query>""")
- assert len(responses) == 1
- response = responses["/test/test.ics"]
- assert not isinstance(response, int) and len(response) == 1
- status, prop = response["D:getcontenttype"]
- assert status == 200 and prop.text == (
- "text/calendar;charset=utf-8;component=V%s" %
- component.upper())
- def test_addressbook_getcontenttype(self) -> None:
- """Test report request on an item"""
- self.create_addressbook("/test/")
- contact = get_file_content("contact1.vcf")
- self.put("/test/test.vcf", contact)
- _, responses = self.report("/test/", """\
- <?xml version="1.0" encoding="utf-8" ?>
- <C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav">
- <D:prop xmlns:D="DAV:">
- <D:getcontenttype />
- </D:prop>
- </C:calendar-query>""")
- assert len(responses) == 1
- response = responses["/test/test.vcf"]
- assert not isinstance(response, int) and len(response) == 1
- status, prop = response["D:getcontenttype"]
- assert status == 200 and prop.text == "text/vcard;charset=utf-8"
- def test_authorization(self) -> None:
- _, responses = self.propfind("/", """\
- <?xml version="1.0" encoding="utf-8"?>
- <propfind xmlns="DAV:">
- <prop>
- <current-user-principal />
- </prop>
- </propfind>""", login="user:")
- response = responses["/"]
- assert not isinstance(response, int) and len(response) == 1
- status, prop = response["D:current-user-principal"]
- assert status == 200 and len(prop) == 1
- element = prop.find(xmlutils.make_clark("D:href"))
- assert element is not None and element.text == "/user/"
- def test_authentication(self) -> None:
- """Test if server sends authentication request."""
- self.configure({"auth": {"type": "htpasswd",
- "htpasswd_filename": os.devnull,
- "htpasswd_encryption": "plain"},
- "rights": {"type": "owner_only"}})
- status, headers, _ = self.request("MKCOL", "/user/")
- assert status in (401, 403)
- assert headers.get("WWW-Authenticate")
- def test_principal_collection_creation(self) -> None:
- """Verify existence of the principal collection."""
- self.propfind("/user/", login="user:")
- def test_authentication_current_user_principal_hack(self) -> None:
- """Test if server sends authentication request when accessing
- current-user-principal prop (workaround for DAVx5)."""
- status, headers, _ = self.request("PROPFIND", "/", """\
- <?xml version="1.0" encoding="utf-8"?>
- <propfind xmlns="DAV:">
- <prop>
- <current-user-principal />
- </prop>
- </propfind>""")
- assert status in (401, 403)
- assert headers.get("WWW-Authenticate")
- def test_existence_of_root_collections(self) -> None:
- """Verify that the root collection always exists."""
- # Use PROPFIND because GET returns message
- self.propfind("/")
- # it should still exist after deletion
- self.delete("/")
- self.propfind("/")
- def test_custom_headers(self) -> None:
- self.configure({"headers": {"test": "123"}})
- # Test if header is set on success
- status, headers, _ = self.request("OPTIONS", "/")
- assert status == 200
- assert headers.get("test") == "123"
- # Test if header is set on failure
- status, headers, _ = self.request("GET", "/.well-known/does not exist")
- assert status == 404
- assert headers.get("test") == "123"
- @pytest.mark.skipif(sys.version_info < (3, 6),
- reason="Unsupported in Python < 3.6")
- def test_timezone_seconds(self) -> None:
- """Verify that timezones with minutes and seconds work."""
- self.mkcalendar("/calendar.ics/")
- event = get_file_content("event_timezone_seconds.ics")
- self.put("/calendar.ics/event.ics", event)
|