changeset 2153:d4f0bb40c38d

subscription validator
author Ana Balica <ana.balica@gmail.com>
date Wed, 17 Jul 2013 07:23:21 +0300
parents e6e4b8bcab11
children c2fe14359d78
files MoinMoin/storage/middleware/_tests/test_validation.py MoinMoin/storage/middleware/validation.py
diffstat 2 files changed, 64 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/storage/middleware/_tests/test_validation.py	Wed Jul 17 07:22:07 2013 +0300
+++ b/MoinMoin/storage/middleware/_tests/test_validation.py	Wed Jul 17 07:23:21 2013 +0300
@@ -67,6 +67,19 @@
             keys.NAME: [u"user name", ],
             keys.NAMESPACE: u"userprofiles",
             keys.EMAIL: u"foo@example.org",
+            keys.SUBSCRIPTIONS: [u"{0}:{1}".format(keys.ITEMID, make_uuid()),
+                                 u"{0}::foo".format(keys.NAME),
+                                 u"{0}::bar".format(keys.TAGS),
+                                 u"{0}::".format(keys.NAMERE),
+                                 u"{0}:userprofiles:a".format(keys.NAMEPREFIX),
+                                 ]
+        }
+
+        invalid_meta = {
+            keys.SUBSCRIPTIONS: [u"", u"unknown_tag:123",
+                                 u"{0}:123".format(keys.ITEMID),
+                                 u"{0}:foo".format(keys.NAME),
+                                 ]
         }
 
         state = {'trusted': False,  # True for loading a serialized representation or other trusted sources
@@ -86,3 +99,11 @@
                 print e.valid, e
             print m.valid, m
         assert valid
+
+        m = UserMetaSchema(invalid_meta)
+        valid = m.validate(state)
+        assert not valid
+        for e in m.children:
+            if e.name in (keys.SUBSCRIPTIONS,):
+                for value in e:
+                    assert not value.valid
--- a/MoinMoin/storage/middleware/validation.py	Wed Jul 17 07:22:07 2013 +0300
+++ b/MoinMoin/storage/middleware/validation.py	Wed Jul 17 07:23:21 2013 +0300
@@ -318,6 +318,48 @@
     except ValueError:
         return False
 
+
+def subscription_validator(element, state):
+    """
+    a subscription
+    """
+    try:
+        keyword, value = element.value.split(":", 1)
+    except ValueError:
+        element.add_error("Subscription must contain colon delimiters.")
+        return False
+
+    if keyword in (keys.ITEMID, ):
+        value_element = String(value)
+        valid = uuid_validator(value_element, state)
+    elif keyword in (keys.NAME, keys.TAGS, keys.NAMERE, keys.NAMEPREFIX, ):
+        try:
+            namespace, value = value.split(":", 1)
+        except ValueError:
+            element.add_error("Subscription must contain 2 colon delimiters.")
+            return False
+        namespace_element = String(namespace)
+        if not namespace_validator(namespace_element, state):
+            element.add_error("Not a valid namespace value.")
+            return False
+    else:
+        element.add_error(
+            "Subscription must start with one of the keywords: "
+            "'itemid', 'name', 'tags', 'namere' or 'nameprefix'.")
+        return False
+
+    value_element = String(value)
+    if keyword == keys.TAGS:
+        valid = tag_validator(value_element, state)
+    elif keyword == keys.NAME:
+        valid = name_validator(value_element, state)
+    elif keyword in (keys.NAMERE, keys.NAMEPREFIX):
+        valid = True
+    if not valid:
+        element.add_error("The value is not valid.")
+    return valid
+
+
 common_meta = (
     String.named(keys.ITEMID).validated_by(itemid_validator),
     String.named(keys.REVID).validated_by(revid_validator),
@@ -368,8 +410,7 @@
     Boolean.named(keys.SCROLL_PAGE_AFTER_EDIT).using(optional=True),
     Boolean.named(keys.MAILTO_AUTHOR).using(optional=True),
     List.named(keys.QUICKLINKS).of(String.named('quicklinks')).using(optional=True),
-    List.named(keys.SUBSCRIBED_ITEMS).of(String.named('subscribed_item')).using(optional=True),
-    List.named(keys.SUBSCRIPTION_IDS).of(String.named('subscription_id')).using(optional=True),
+    List.named(keys.SUBSCRIPTIONS).of(String.named('subscription').validated_by(subscription_validator)).using(optional=True),
     List.named(keys.EMAIL_SUBSCRIBED_EVENTS).of(String.named('email_subscribed_event')).using(optional=True),
     #TODO: DuckDict.named('bookmarks').using(optional=True),
     *common_meta