changeset 2242:d274858671d8

Added validation for namespaces.
author Ashutosh Singla <ashu1461@gmail.com>
date Sun, 18 Aug 2013 14:51:05 +0530
parents b013a1c4bddd
children 880f995b9d0b
files MoinMoin/_tests/test_forms.py MoinMoin/forms.py MoinMoin/storage/middleware/indexing.py
diffstat 3 files changed, 14 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_forms.py	Sun Aug 18 14:36:36 2013 +0530
+++ b/MoinMoin/_tests/test_forms.py	Sun Aug 18 14:51:05 2013 +0530
@@ -71,6 +71,6 @@
              ([u'existingname'], 'ns1/ns2', '', 'existingname', False),
              ]
     for name, namespace, field, value, result in tests:
-        x = JSON(json.dumps({NAME: name}))
+        x = JSON(json.dumps({NAME: name, NAMESPACE: namespace}))
         state = {FQNAME: CompositeName(namespace, field, value), ITEMID: None}
         assert x.validate(state) == result
--- a/MoinMoin/forms.py	Sun Aug 18 14:36:36 2013 +0530
+++ b/MoinMoin/forms.py	Sun Aug 18 14:51:05 2013 +0530
@@ -27,6 +27,7 @@
 
 from MoinMoin.constants.forms import *
 from MoinMoin.constants.keys import ITEMID, NAME, LATEST_REVS, NAMESPACE, FQNAME
+from MoinMoin.constants.namespaces import NAMESPACES_IDENTIFIER
 from MoinMoin.i18n import _, L_, N_
 from MoinMoin.util.forms import FileStorage
 from MoinMoin.storage.middleware.validation import uuid_validator
@@ -83,17 +84,24 @@
     Will just return, if they are valid, will raise a NameNotValidError if not.
     """
     names = meta.get(NAME)
-    current_namespace = meta.get(NAMESPACE, fqname.namespace)
-    if not names:
-        return
+    current_namespace = meta.get(NAMESPACE)
+
+    if current_namespace is None:
+        raise NameNotValidError(L_("No namespace field in the meta."))
+
+    namespaces = [namespace.rstrip('/') for namespace, _ in app.cfg.namespace_mapping]
+    if current_namespace not in namespaces:  # current_namespace must be an existing namespace.
+        raise NameNotValidError(L_("%(_namespace)s is not a valid namespace.", _namespace=current_namespace))
+
     if len(names) != len(set(names)):
         raise NameNotValidError(L_("The names in the name list must be unique."))
     # Item names must not start with '@' or '+', '@something' denotes a field where as '+something' denotes a view.
     invalid_names = [name for name in names if name.startswith(('@', '+'))]
     if invalid_names:
         raise NameNotValidError(L_("Item names (%(invalid_names)s) must not start with '@' or '+'", invalid_names=", ".join(invalid_names)))
+
+    namespaces = namespaces + NAMESPACES_IDENTIFIER  # Also dont allow item names to match with identifier namespaces.
     # Item names must not match with existing namespaces.
-    namespaces = [namespace.rstrip('/') for namespace, _ in app.cfg.namespace_mapping]
     invalid_names = [name for name in names if name.split('/', 1)[0] in namespaces]
     if invalid_names:
         raise NameNotValidError(L_("Item names (%(invalid_names)s) must not match with existing namespaces.", invalid_names=", ".join(invalid_names)))
--- a/MoinMoin/storage/middleware/indexing.py	Sun Aug 18 14:36:36 2013 +0530
+++ b/MoinMoin/storage/middleware/indexing.py	Sun Aug 18 14:51:05 2013 +0530
@@ -831,7 +831,7 @@
 
     @property
     def namespace(self):
-        return self.meta.get(NAMESPACE) or u''
+        return self.meta[NAMESPACE]
 
     def _fqname(self, name=None):
         """