allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [2/4] git commit: [#7381] update GoogleCodeProjectNameValidator for hosted domains, add tests
Date Thu, 19 Jun 2014 14:52:48 GMT
[#7381] update GoogleCodeProjectNameValidator for hosted domains, add tests


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

Branch: refs/heads/master
Commit: b7d020c37f4c7d68c28ae0661fd3c7b71a0c3aae
Parents: f80727e
Author: Dave Brondsema <dbrondsema@slashdotmedia.com>
Authored: Thu May 29 19:53:02 2014 +0000
Committer: Dave Brondsema <dbrondsema@slashdotmedia.com>
Committed: Thu Jun 19 14:51:55 2014 +0000

----------------------------------------------------------------------
 .../forgeimporters/google/__init__.py           | 29 +++++--
 .../forgeimporters/google/tests/test_init.py    | 83 ++++++++++++++++++++
 2 files changed, 106 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/b7d020c3/ForgeImporters/forgeimporters/google/__init__.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/google/__init__.py b/ForgeImporters/forgeimporters/google/__init__.py
index aebc596..a7bb1d9 100644
--- a/ForgeImporters/forgeimporters/google/__init__.py
+++ b/ForgeImporters/forgeimporters/google/__init__.py
@@ -124,13 +124,30 @@ class GoogleCodeProjectNameValidator(fev.FancyValidator):
     }
 
     def _to_python(self, value, state=None):
-        url = urlparse(value.strip())
-        if url.netloc.endswith('.googlecode.com'):
-            project_name = url.netloc.split('.')[0]
+        project_name_re = re.compile(r'^[a-z0-9][a-z0-9-]{,61}$')
+        if project_name_re.match(value):
+            # just a name
+            project_name = value
         else:
-            project_name = os.path.basename(url.path.strip('/'))
-        if not re.match(r'^[a-z0-9][a-z0-9-]{,61}$', project_name):
-            raise fev.Invalid(self.message('invalid', state), value, state)
+            # try as a URL
+            project_name = None
+            project_name_simple = None
+            url = urlparse(value.strip())
+            if url.netloc.endswith('.googlecode.com'):
+                project_name = url.netloc.split('.')[0]
+            elif url.netloc == 'code.google.com':
+                path_parts = url.path.lstrip('/').split('/')
+                if len(path_parts) >= 2 and path_parts[0] == 'p':
+                    project_name = path_parts[1]
+                elif len(path_parts) >= 4 and path_parts[0] == 'a' and path_parts[2] ==
'p':
+                    project_name_simple = path_parts[3]
+                    project_name = '/'.join(path_parts[0:4])
+
+            if not project_name_simple:
+                project_name_simple = project_name
+
+            if not project_name or not project_name_re.match(project_name_simple):
+                raise fev.Invalid(self.message('invalid', state), value, state)
 
         if not GoogleCodeProjectExtractor(project_name).check_readable():
             raise fev.Invalid(self.message('unavailable', state), value, state)

http://git-wip-us.apache.org/repos/asf/allura/blob/b7d020c3/ForgeImporters/forgeimporters/google/tests/test_init.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/google/tests/test_init.py b/ForgeImporters/forgeimporters/google/tests/test_init.py
new file mode 100644
index 0000000..8acb0e3
--- /dev/null
+++ b/ForgeImporters/forgeimporters/google/tests/test_init.py
@@ -0,0 +1,83 @@
+#       Licensed to the Apache Software Foundation (ASF) under one
+#       or more contributor license agreements.  See the NOTICE file
+#       distributed with this work for additional information
+#       regarding copyright ownership.  The ASF licenses this file
+#       to you under the Apache License, Version 2.0 (the
+#       "License"); you may not use this file except in compliance
+#       with the License.  You may obtain a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#       Unless required by applicable law or agreed to in writing,
+#       software distributed under the License is distributed on an
+#       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#       KIND, either express or implied.  See the License for the
+#       specific language governing permissions and limitations
+#       under the License.
+
+from nose.tools import assert_equal
+from mock import patch
+from formencode.validators import Invalid
+
+from allura.tests import decorators as td
+from forgeimporters.google import GoogleCodeProjectNameValidator, GoogleCodeProjectExtractor
+
+
+class TestGoogleCodeProjectNameValidator(object):
+
+    def setUp(self):
+        self.readable_patcher = patch.object(GoogleCodeProjectExtractor, 'check_readable')
+        self.readable_mock = self.readable_patcher.start()
+        self.readable_mock.return_value = True
+
+    def tearDown(self):
+        self.readable_patcher.stop()
+
+    def test_simple(self):
+        assert_equal(
+            GoogleCodeProjectNameValidator()._to_python('gmapcatcher'),
+            'gmapcatcher'
+        )
+
+    def test_code_dot_google(self):
+        assert_equal(
+            GoogleCodeProjectNameValidator()._to_python('http://code.google.com/p/gmapcatcher/'),
+            'gmapcatcher'
+        )
+        assert_equal(
+            GoogleCodeProjectNameValidator()._to_python('https://code.google.com/p/gmapcatcher/'),
+            'gmapcatcher'
+        )
+
+    def test_googlecode_com(self):
+        assert_equal(
+            GoogleCodeProjectNameValidator()._to_python('http://gmapcatcher.googlecode.com/'),
+            'gmapcatcher'
+        )
+        assert_equal(
+            GoogleCodeProjectNameValidator()._to_python('https://gmapcatcher.googlecode.com/'),
+            'gmapcatcher'
+        )
+
+    def test_not_readable(self):
+        self.readable_mock.return_value = False
+        with td.raises(Invalid):
+            GoogleCodeProjectNameValidator()._to_python('gmapcatcher')
+
+    def test_invalid(self):
+        with td.raises(Invalid):
+            GoogleCodeProjectNameValidator()._to_python('http://code.google.com/')
+        with td.raises(Invalid):
+            GoogleCodeProjectNameValidator()._to_python('http://foobar.com/p/gmapcatcher')
+        with td.raises(Invalid):
+            GoogleCodeProjectNameValidator()._to_python('http://code.google.com/p/asdf_asdf')
+        with td.raises(Invalid):
+            GoogleCodeProjectNameValidator()._to_python('http://code.google.com/x/y/z')
+
+    def test_hosted_domain(self):
+        assert_equal(
+            GoogleCodeProjectNameValidator()._to_python('https://code.google.com/a/eclipselabs.org/p/restclient-tool'),
+            'a/eclipselabs.org/p/restclient-tool'
+        )
+        with td.raises(Invalid):
+            GoogleCodeProjectNameValidator()._to_python('http://code.google.com/a/eclipselabs.org/bogus')


Mime
View raw message