Code Repositories xandikos / b169bb8
Don't be strict on required fields. Jelmer Vernooń≥ 1 year, 6 months ago
2 changed file(s) with 19 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
3131 _INVALID_CONTROL_CHARACTERS = ['\x0c', '\x01']
3232
3333
34 def validate_calendar(cal):
34 def validate_calendar(cal, strict=False):
3535 """Validate a calendar object.
3636
3737 :param cal: Calendar object
3838 """
39 for error in validate_component(cal):
39 for error in validate_component(cal, strict=strict):
4040 yield error
4141
4242
43 def validate_component(comp):
43 def validate_component(comp, strict=False):
4444 """Validate a calendar component.
4545
4646 :param comp: Calendar component
5252 if c in value:
5353 yield "Invalid character %s in field %s" % (
5454 c.encode('unicode_escape'), name)
55 for required in comp.required:
56 try:
57 comp[required]
58 except KeyError:
59 yield "Missing required field %s" % required
55 if strict:
56 for required in comp.required:
57 try:
58 comp[required]
59 except KeyError:
60 yield "Missing required field %s" % required
6061 for subcomp in comp.subcomponents:
61 for error in validate_component(subcomp):
62 for error in validate_component(subcomp, strict=strict):
6263 yield error
6364
6465
209210 # TODO(jelmer): return the list of errors to the caller
210211 if cal.is_broken:
211212 raise InvalidFileContents(self.content_type, self.content)
212 if list(validate_calendar(cal)):
213 if list(validate_calendar(cal, strict=False)):
213214 raise InvalidFileContents(self.content_type, self.content)
214215
215216 @property
2020
2121 import unittest
2222
23 from xandikos.icalendar import ICalendarFile
23 from xandikos.icalendar import ICalendarFile, validate_calendar
2424 from xandikos.store import InvalidFileContents
2525
2626 EXAMPLE_VCALENDAR1 = b"""\
7979
8080 def test_extract_no_uid(self):
8181 fi = ICalendarFile([EXAMPLE_VCALENDAR_NO_UID], 'text/calendar')
82 self.assertRaises(InvalidFileContents, fi.validate)
82 fi.validate()
83 self.assertEqual(["Missing required field UID"],
84 list(validate_calendar(fi.calendar, strict=True)))
85 self.assertEqual([],
86 list(validate_calendar(fi.calendar, strict=False)))
8387 self.assertRaises(KeyError, fi.get_uid)
8488
8589 def test_invalid_character(self):
8690 fi = ICalendarFile([EXAMPLE_VCALENDAR_INVALID_CHAR], 'text/calendar')
8791 self.assertRaises(InvalidFileContents, fi.validate)
92 self.assertEqual(["Invalid character b'\\\\x0c' in field SUMMARY"],
93 list(validate_calendar(fi.calendar, strict=False)))