radicale/tests/test_base.py

402 lines
17 KiB
Python
Raw Normal View History

2012-09-15 10:00:13 +02:00
# This file is part of Radicale Server - Calendar Server
2016-03-31 19:57:40 +02:00
# Copyright © 2012-2016 Guillaume Ayoub
2012-09-15 10:00:13 +02:00
#
# 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 logging
2013-12-28 11:40:29 +01:00
import shutil
import tempfile
2015-07-24 16:01:03 +02:00
from radicale import Application, config
2015-07-24 16:01:03 +02:00
from . import BaseTest
from .helpers import get_file_content
2012-09-15 10:00:13 +02:00
class BaseRequests:
2012-09-15 10:00:13 +02:00
"""Tests with simple requests."""
2015-07-24 16:01:03 +02:00
storage_type = None
def setup(self):
self.configuration = config.load()
self.configuration.set("storage", "type", self.storage_type)
self.logger = logging.getLogger("radicale_test")
2012-09-15 10:00:13 +02:00
def test_root(self):
2015-07-24 14:23:11 +02:00
"""GET request at "/"."""
2012-09-15 10:00:13 +02:00
status, headers, answer = self.request("GET", "/")
assert status == 200
assert "Radicale works!" in answer
# Test the creation of the collection
2016-04-07 19:25:10 +02:00
self.request(
"PUT", "/calendar.ics/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
2013-09-05 15:13:31 +02:00
status, headers, answer = self.request("GET", "/calendar.ics/")
assert "BEGIN:VCALENDAR" in answer
assert "END:VCALENDAR" in answer
2013-09-05 15:13:31 +02:00
2015-07-24 14:23:11 +02:00
def test_add_event(self):
"""Add an event."""
2016-04-07 19:25:10 +02:00
self.request(
"PUT", "/calendar.ics/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
event = get_file_content("event1.ics")
path = "/calendar.ics/event1.ics"
2013-09-05 15:13:31 +02:00
status, headers, answer = self.request("PUT", path, event)
assert status == 201
status, headers, answer = self.request("GET", path)
2015-07-24 14:23:11 +02:00
assert "ETag" in headers.keys()
2013-09-05 15:13:31 +02:00
assert status == 200
assert "VEVENT" in answer
2015-07-24 14:23:11 +02:00
assert "Event" in answer
assert "UID:event" in answer
def test_add_todo(self):
"""Add a todo."""
2016-04-07 19:25:10 +02:00
self.request(
"PUT", "/calendar.ics/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
2015-07-24 14:23:11 +02:00
todo = get_file_content("todo.ics")
path = "/calendar.ics/todo.ics"
2013-09-05 15:13:31 +02:00
status, headers, answer = self.request("PUT", path, todo)
assert status == 201
status, headers, answer = self.request("GET", path)
2015-07-24 14:23:11 +02:00
assert "ETag" in headers.keys()
assert "VTODO" in answer
2015-07-24 14:23:11 +02:00
assert "Todo" in answer
assert "UID:todo" in answer
2013-09-05 15:13:31 +02:00
def test_delete(self):
2015-07-24 14:23:11 +02:00
"""Delete an event."""
2016-04-07 19:25:10 +02:00
self.request(
"PUT", "/calendar.ics/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
event = get_file_content("event1.ics")
path = "/calendar.ics/event1.ics"
2013-09-05 15:13:31 +02:00
status, headers, answer = self.request("PUT", path, event)
# Then we send a DELETE request
status, headers, answer = self.request("DELETE", path)
assert status == 200
2013-09-25 14:39:46 +02:00
assert "href>%s</" % path in answer
2013-09-05 15:13:31 +02:00
status, headers, answer = self.request("GET", "/calendar.ics/")
assert "VEVENT" not in answer
2013-09-05 15:13:31 +02:00
def _test_filter(self, filters, events=1):
filters_text = "".join(
"<C:filter>%s</C:filter>" % filter_ for filter_ in filters)
self.request(
"PUT", "/calendar.ics/", "BEGIN:VCALENDAR\r\nEND:VCALENDAR")
for i in range(events):
event = get_file_content("event%i.ics" % (i + 1))
self.request("PUT", "/calendar.ics/event%i.ics" % (i + 1), event)
status, headers, answer = self.request(
"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>
%s
</C:calendar-query>""" % filters_text)
return answer
def test_calendar_tag_filter(self):
"""Report request with tag-based filter on calendar."""
assert "href>/calendar.ics/event1.ics</" in self._test_filter(["""
<C:comp-filter name="VCALENDAR"></C:comp-filter>"""])
def test_item_tag_filter(self):
"""Report request with tag-based filter on an item."""
assert "href>/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 "href>/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):
"""Report request with tag-based is-not filter on an item."""
assert "href>/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 "href>/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):
"""Report request with prop-based filter on an item."""
assert "href>/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 "href>/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):
"""Report request with prop-based is-not filter on an item."""
assert "href>/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 "href>/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):
"""Report request with multiple filters on an item."""
assert "href>/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 "href>/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 "href>/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):
2016-05-27 14:44:59 +02:00
"""Report request with text-match filter on calendar."""
assert "href>/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 "href>/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 "href>/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 "href>/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>
2016-05-27 14:44:59 +02:00
</C:comp-filter>
</C:comp-filter>"""])
def test_param_filter(self):
"""Report request with param-filter on calendar."""
assert "href>/calendar.ics/event1.ics</" in self._test_filter(["""
2016-05-27 14:44:59 +02:00
<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 "href>/calendar.ics/event1.ics</" not in self._test_filter(["""
2016-05-27 14:44:59 +02:00
<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 "href>/calendar.ics/event1.ics</" not in self._test_filter(["""
2016-05-27 14:44:59 +02:00
<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 "href>/calendar.ics/event1.ics</" in self._test_filter(["""
2016-05-27 14:44:59 +02:00
<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(self):
"""Report request with time-range filter on calendar."""
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>"""], events=5)
assert "href>/calendar.ics/event1.ics</" in answer
assert "href>/calendar.ics/event2.ics</" in answer
assert "href>/calendar.ics/event3.ics</" in answer
assert "href>/calendar.ics/event4.ics</" in answer
assert "href>/calendar.ics/event5.ics</" 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>"""], events=5)
assert "href>/calendar.ics/event1.ics</" not in answer
assert "href>/calendar.ics/event2.ics</" not in answer
assert "href>/calendar.ics/event3.ics</" not in answer
assert "href>/calendar.ics/event4.ics</" not in answer
assert "href>/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>"""], events=5)
assert "href>/calendar.ics/event1.ics</" not in answer
assert "href>/calendar.ics/event2.ics</" in answer
assert "href>/calendar.ics/event3.ics</" in answer
assert "href>/calendar.ics/event4.ics</" in answer
assert "href>/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="20131001T000000Z"/>
</C:comp-filter>
</C:comp-filter>"""], events=5)
assert "href>/calendar.ics/event1.ics</" not in answer
assert "href>/calendar.ics/event2.ics</" not in answer
assert "href>/calendar.ics/event3.ics</" in answer
assert "href>/calendar.ics/event4.ics</" in answer
assert "href>/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>"""], events=5)
assert "href>/calendar.ics/event1.ics</" not in answer
assert "href>/calendar.ics/event2.ics</" not in answer
assert "href>/calendar.ics/event3.ics</" in answer
assert "href>/calendar.ics/event4.ics</" not in answer
assert "href>/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>"""], events=5)
assert "href>/calendar.ics/event1.ics</" not in answer
assert "href>/calendar.ics/event2.ics</" not in answer
assert "href>/calendar.ics/event3.ics</" not in answer
assert "href>/calendar.ics/event4.ics</" not in answer
assert "href>/calendar.ics/event5.ics</" not in answer
2013-12-28 11:40:29 +01:00
2016-04-07 19:25:10 +02:00
class TestMultiFileSystem(BaseRequests, BaseTest):
2013-12-28 11:40:29 +01:00
"""Base class for filesystem tests."""
2016-04-07 19:25:10 +02:00
storage_type = "multifilesystem"
2013-12-28 11:40:29 +01:00
def setup(self):
super().setup()
2013-12-28 11:40:29 +01:00
self.colpath = tempfile.mkdtemp()
self.configuration.set("storage", "filesystem_folder", self.colpath)
self.application = Application(self.configuration, self.logger)
2013-12-28 11:40:29 +01:00
def teardown(self):
shutil.rmtree(self.colpath)
class TestCustomStorageSystem(BaseRequests, BaseTest):
"""Base class for custom backend tests."""
storage_type = "tests.custom.storage"
2013-12-28 11:40:29 +01:00
def setup(self):
2016-03-31 19:57:40 +02:00
super().setup()
2013-12-28 11:40:29 +01:00
self.colpath = tempfile.mkdtemp()
self.configuration.set("storage", "filesystem_folder", self.colpath)
self.configuration.set("storage", "test_folder", self.colpath)
self.application = Application(self.configuration, self.logger)
2013-12-28 11:40:29 +01:00
def teardown(self):
2015-07-24 14:23:11 +02:00
shutil.rmtree(self.colpath)