Code Repositories xandikos / b838f8e
Add really basic validation test for calendar. Jelmer Vernooń≥ 11 months ago
3 changed file(s) with 40 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
249249 extract_from_calendar(calendar, c, requested)
250250 serialized_cal = c.to_ical()
251251 # TODO(jelmer): Don't hardcode encoding
252 # TODO(jelmer): Strip invalid characters or raise an exception
252253 el.text = serialized_cal.decode('utf-8')
253254
254255
2323 import logging
2424
2525 from icalendar.cal import Calendar, component_factory
26 from icalendar.prop import vText
2627 from xandikos.store import File, InvalidFileContents
28
29
30 def validate_calendar(cal):
31 """Validate a calendar object.
32
33 :param cal: Calendar object
34 """
35 # Check text fields for invalid characters
36 for (name, value) in cal.property_items():
37 if isinstance(value, vText):
38 if '\x0c' in value:
39 return False
40 return True
2741
2842
2943 def calendar_component_delta(old_cal, new_cal):
169183
170184 def validate(self):
171185 """Verify that file contents are valid."""
172 self.calendar
186 cal = self.calendar
187 if cal.is_broken:
188 raise InvalidFileContents(self.content_type, self.content)
189 if not validate_calendar(cal):
190 raise InvalidFileContents(self.content_type, self.content)
173191
174192 @property
175193 def calendar(self):
2121 import unittest
2222
2323 from xandikos.icalendar import ICalendarFile
24 from xandikos.store import InvalidFileContents
2425
2526 EXAMPLE_VCALENDAR1 = b"""\
2627 BEGIN:VCALENDAR
5152 END:VCALENDAR
5253 """
5354
55 EXAMPLE_VCALENDAR_INVALID_CHAR = b"""\
56 BEGIN:VCALENDAR
57 VERSION:2.0
58 PRODID:-//bitfire web engineering//DAVdroid 0.8.0 (ical4j 1.0.x)//EN
59 BEGIN:VTODO
60 CREATED:20150314T223512Z
61 DTSTAMP:20150527T221952Z
62 LAST-MODIFIED:20150314T223512Z
63 STATUS:NEEDS-ACTION
64 SUMMARY:do somethi ng
65 ID:bdc22720-b9e1-42c9-89c2-a85405d8fbff
66 END:VTODO
67 END:VCALENDAR
68 """
69
5470
5571 class ExtractCalendarUIDTests(unittest.TestCase):
5672
6278 def test_extract_no_uid(self):
6379 fi = ICalendarFile([EXAMPLE_VCALENDAR_NO_UID], 'text/calendar')
6480 self.assertRaises(KeyError, fi.get_uid)
81
82 def test_invalid_character(self):
83 fi = ICalendarFile([EXAMPLE_VCALENDAR_INVALID_CHAR], 'text/calendar')
84 self.assertRaises(InvalidFileContents, fi.validate)