Raise value error in case of an invalid time range filters
authorGuido Günther <agx@sigxcpu.org>
Sun, 31 Jan 2016 11:51:15 +0000 (12:51 +0100)
committerGuido Günther <agx@sigxcpu.org>
Sun, 31 Jan 2016 11:51:15 +0000 (12:51 +0100)
instead of returning False. Preferably we'd only fail the single bad
time range instad of the whole request using MultiStatus later on.

calypso/xmlutils.py
tests/test_matchfilterelement.py

index f05c76b118d7981eaf3de64eae1f09dcd98e465c..7de888da11a755f0f584ce92650a2b47ee48648a 100644 (file)
@@ -281,7 +281,7 @@ def match_filter_element(vobject, fe):
         if start is None and end is None:
             msg = "time-range missing both start and stop attribute (required by RFC 4791)"
             log.error(msg)
-            return False
+            raise ValueError(msg)
         # RFC 4791 state if start is missing, assume it is -infinity
         if start is None:
             start = "00010101T000000Z"  # start of year one
index 094a14edf6a9e6c21a35fd338f6c1b5ef3393dae..582d74f0b576aa0939c75f87bc2ca71e7006e322 100644 (file)
@@ -30,7 +30,7 @@ class TestMatchFilterElement(unittest.TestCase):
 Check that the time-range parser accept ranges where start or stop is
 missing.
 """
-        xml_request1 ="""
+        valid_xml_request1 ="""
 <calendar-query xmlns="urn:ietf:params:xml:ns:caldav">
  <prop xmlns="DAV:">
   <getetag xmlns="DAV:"/>
@@ -43,7 +43,7 @@ missing.
  </filter>
 </calendar-query>
 """
-        xml_request2 ="""
+        valid_xml_request2 ="""
 <calendar-query xmlns="urn:ietf:params:xml:ns:caldav">
  <prop xmlns="DAV:">
   <getetag xmlns="DAV:"/>
@@ -56,7 +56,7 @@ missing.
  </filter>
 </calendar-query>
 """
-        xml_request3 ="""
+        invalid_xml_request1 ="""
 <calendar-query xmlns="urn:ietf:params:xml:ns:caldav">
  <prop xmlns="DAV:">
   <getetag xmlns="DAV:"/>
@@ -77,12 +77,21 @@ missing.
         # needed to get the CollectionHTTPHandler class working.  Use
         # match_filter() directly instead.
         truecount = 0
-        for xml_request in [xml_request1, xml_request2, xml_request3]:
+        for xml_request in [valid_xml_request1, valid_xml_request2]:
             root = ET.fromstring(xml_request)
             filter_element = root.find(xmlutils._tag("C", "filter"))
             for item in collection.items:
                 answer = xmlutils.match_filter(item, filter_element)
                 if answer:
-                    truecount = truecount + 1
+                    truecount += 1
         # The text vcalendar entry is either before or after the cutoff point.
         self.assertEqual(truecount, 1)
+
+        for xml_request in [invalid_xml_request1]:
+            root = ET.fromstring(xml_request)
+            filter_element = root.find(xmlutils._tag("C", "filter"))
+            for item in collection.items:
+                with self.assertRaisesRegexp(ValueError, "time-range missing both start and stop attribute"):
+                    xmlutils.match_filter(item, filter_element)
+        # The text vcalendar entry is either before or after the cutoff point.
+