incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tvansteenbu...@apache.org
Subject [49/50] git commit: [#6526] Fix MountPointValidator to provide default value
Date Wed, 28 Aug 2013 14:35:30 GMT
[#6526] Fix MountPointValidator to provide default value

Signed-off-by: Tim Van Steenburgh <tvansteenburgh@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/a4dc01b3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/a4dc01b3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/a4dc01b3

Branch: refs/heads/tv/6526
Commit: a4dc01b395929b0ba05a14f1a37e590ebdf42a6f
Parents: 6ff3125
Author: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Authored: Wed Aug 28 14:30:28 2013 +0000
Committer: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Committed: Wed Aug 28 14:30:28 2013 +0000

----------------------------------------------------------------------
 Allura/allura/lib/validators.py        | 23 ++++++-----
 Allura/allura/tests/test_validators.py | 61 +++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/a4dc01b3/Allura/allura/lib/validators.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/validators.py b/Allura/allura/lib/validators.py
index fa82d32..3295bb0 100644
--- a/Allura/allura/lib/validators.py
+++ b/Allura/allura/lib/validators.py
@@ -19,6 +19,7 @@ import json
 from bson import ObjectId
 import formencode as fe
 from formencode import validators as fev
+from pylons import tmpl_context as c
 from . import helpers as h
 from datetime import datetime
 
@@ -74,20 +75,13 @@ class MaxBytesValidator(fev.FancyValidator):
 
 class MountPointValidator(fev.UnicodeString):
     def __init__(self, app_class,
-            reserved_mount_points=('feed', 'index', 'icon', '_nav.json')):
-        super(self.__class__, self).__init__()
+            reserved_mount_points=('feed', 'index', 'icon', '_nav.json'), **kw):
+        super(self.__class__, self).__init__(**kw)
         self.app_class = app_class
         self.reserved_mount_points = reserved_mount_points
 
     def _to_python(self, value, state):
-        from pylons import tmpl_context as c
-        project = state.project if hasattr(state, 'project') else c.project
         mount_point, App = value, self.app_class
-        if not mount_point:
-            base_mount_point = mount_point = App.default_mount_point
-            for x in range(10):
-                if project.app_instance(mount_point) is None: break
-                mount_point = base_mount_point + '-%d' % x
         if not App.relaxed_mount_points:
             mount_point = mount_point.lower()
         if not App.validate_mount_point(mount_point):
@@ -96,11 +90,20 @@ class MountPointValidator(fev.UnicodeString):
         if mount_point in self.reserved_mount_points:
             raise fe.Invalid('Mount point "%s" is reserved' % mount_point,
                     value, state)
-        if project.app_instance(mount_point) is not None:
+        if c.project and c.project.app_instance(mount_point) is not None:
             raise fe.Invalid('Mount point "%s" is already in use' % mount_point,
                     value, state)
         return mount_point
 
+    def empty_value(self, value):
+        base_mount_point = mount_point = self.app_class.default_mount_point
+        i = 0
+        while True:
+            if not c.project or c.project.app_instance(mount_point) is None:
+                return mount_point
+            mount_point = base_mount_point + '-%d' % i
+            i += 1
+
 class TaskValidator(fev.FancyValidator):
     def _to_python(self, value, state):
         try:

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/a4dc01b3/Allura/allura/tests/test_validators.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_validators.py b/Allura/allura/tests/test_validators.py
index b0d165c..26779ce 100644
--- a/Allura/allura/tests/test_validators.py
+++ b/Allura/allura/tests/test_validators.py
@@ -17,6 +17,7 @@
 
 import unittest
 import formencode as fe
+from mock import Mock, patch
 
 from allura import model as M
 from allura.lib import validators as v
@@ -92,6 +93,66 @@ class TestUserValidator(unittest.TestCase):
         self.assertEqual(str(cm.exception), "Invalid username")
 
 
+class TestMountPointValidator(unittest.TestCase):
+
+    @patch('allura.lib.validators.c')
+    def test_valid(self, c):
+        App = Mock()
+        App.relaxed_mount_points = False
+        App.validate_mount_point.return_value = True
+        c.project.app_instance.return_value = None
+        val = v.MountPointValidator(App)
+        self.assertEqual('mymount', val.to_python('Mymount'))
+
+    @patch('allura.lib.validators.c')
+    def test_invalid(self, c):
+        App = Mock()
+        App.relaxed_mount_points = False
+        App.validate_mount_point.return_value = False
+        c.project.app_instance.return_value = False
+        val = v.MountPointValidator(App)
+        with self.assertRaises(fe.Invalid):
+            val.to_python('mymount')
+
+    @patch('allura.lib.validators.c')
+    def test_relaxed_mount_points(self, c):
+        App = Mock()
+        App.relaxed_mount_points = True
+        App.validate_mount_point.return_value = True
+        c.project.app_instance.return_value = None
+        val = v.MountPointValidator(App)
+        self.assertEqual('Mymount', val.to_python('Mymount'))
+
+    @patch('allura.lib.validators.c')
+    def test_in_use(self, c):
+        App = Mock()
+        App.relaxed_mount_points = False
+        App.validate_mount_point.return_value = True
+        c.project.app_instance.return_value = True
+        val = v.MountPointValidator(App)
+        with self.assertRaises(fe.Invalid):
+            val.to_python('mymount')
+
+    @patch('allura.lib.validators.c')
+    def test_reserved(self, c):
+        App = Mock()
+        App.relaxed_mount_points = False
+        App.validate_mount_point.return_value = True
+        c.project.app_instance.return_value = False
+        val = v.MountPointValidator(App)
+        with self.assertRaises(fe.Invalid):
+            val.to_python('feed')
+
+    @patch('allura.lib.validators.c')
+    def test_empty(self, c):
+        App = Mock()
+        App.default_mount_point = 'wiki'
+        c.project.app_instance.side_effect = lambda mp: None if mp != 'wiki' else True
+        val = v.MountPointValidator(App)
+        self.assertEqual('wiki-0', val.to_python(''))
+        self.assertEqual('wiki-0', val.to_python(None))
+
+
 class TestTaskValidator(unittest.TestCase):
     val = v.TaskValidator
 


Mime
View raw message