Code Repositories xandikos / 580c09d
Check for required components in validate_calendar. Jelmer Vernooń≥ 1 year, 6 months ago
2 changed file(s) with 25 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
3232
3333 :param cal: Calendar object
3434 """
35 for error in validate_component(cal):
36 yield error
37
38
39 def validate_component(comp):
40 """Validate a calendar component.
41
42 :param comp: Calendar component
43 """
3544 # Check text fields for invalid characters
36 for (name, value) in cal.property_items():
45 for (name, value) in comp.items():
3746 if isinstance(value, vText):
3847 if '\x0c' in value:
39 return False
40 return True
48 yield "Invalid character in field %s" % name
49 for required in comp.required:
50 try:
51 comp[required]
52 except KeyError:
53 yield "Missing required field %s" % required
54 for subcomp in comp.subcomponents:
55 for error in validate_component(subcomp):
56 yield error
4157
4258
4359 def calendar_component_delta(old_cal, new_cal):
184200 def validate(self):
185201 """Verify that file contents are valid."""
186202 cal = self.calendar
203 # TODO(jelmer): return the list of errors to the caller
187204 if cal.is_broken:
188205 raise InvalidFileContents(self.content_type, self.content)
189 if not validate_calendar(cal):
206 if list(validate_calendar(cal)):
190207 raise InvalidFileContents(self.content_type, self.content)
191208
192209 @property
7171 class ExtractCalendarUIDTests(unittest.TestCase):
7272
7373 def test_extract_str(self):
74 fi = ICalendarFile([EXAMPLE_VCALENDAR1], 'text/calendar')
7475 self.assertEqual(
7576 'bdc22720-b9e1-42c9-89c2-a85405d8fbff',
76 ICalendarFile([EXAMPLE_VCALENDAR1], 'text/calendar').get_uid())
77 fi.get_uid())
78 fi.validate()
7779
7880 def test_extract_no_uid(self):
7981 fi = ICalendarFile([EXAMPLE_VCALENDAR_NO_UID], 'text/calendar')
82 self.assertRaises(InvalidFileContents, fi.validate)
8083 self.assertRaises(KeyError, fi.get_uid)
8184
8285 def test_invalid_character(self):