From dffd4f7fdc6a2bacdb7859b854958799d260f83b Mon Sep 17 00:00:00 2001 From: Gregory Haynes Date: Wed, 11 Feb 2015 17:19:29 -0800 Subject: [PATCH] Use python3 compatible urllib urllib, urlparse, urlencode have been merged together in python 3. Change-Id: Iec54be4f716096511c2702bdb3ba637a42607ee2 --- storyboard/api/auth/utils.py | 6 +++--- storyboard/common/decorators.py | 10 ++++++---- storyboard/tests/api/auth/test_oauth.py | 21 ++++++++++----------- storyboard/tests/api/test_project_groups.py | 4 ++-- storyboard/tests/api/test_projects.py | 4 ++-- storyboard/tests/api/test_stories.py | 5 +++-- storyboard/tests/api/test_task_statuses.py | 4 ++-- storyboard/tests/api/test_tasks.py | 4 ++-- 8 files changed, 30 insertions(+), 28 deletions(-) diff --git a/storyboard/api/auth/utils.py b/storyboard/api/auth/utils.py index 57f11edf..20f465b1 100644 --- a/storyboard/api/auth/utils.py +++ b/storyboard/api/auth/utils.py @@ -14,11 +14,11 @@ # limitations under the License. import six -import urllib +import six.moves.urllib.parse as urlparse def join_params(params, encode=True): return '&'.join( - ["%s=%s" % (urllib.quote(key, safe='') if encode else key, - urllib.quote(val, safe='') if encode else val) + ["%s=%s" % (urlparse.quote(key, safe='') if encode else key, + urlparse.quote(val, safe='') if encode else val) for key, val in six.iteritems(params)]) diff --git a/storyboard/common/decorators.py b/storyboard/common/decorators.py index 0948ecbe..5ad7e10d 100644 --- a/storyboard/common/decorators.py +++ b/storyboard/common/decorators.py @@ -14,14 +14,13 @@ # limitations under the License. import json -from urllib import urlencode -from urlparse import parse_qsl import functools from pecan import abort from pecan import redirect from pecan import response import rfc3987 +import six.moves.urllib.parse as urlparse from storyboard.common import exception as exc from storyboard.openstack.common.gettextutils import _ # noqa @@ -55,12 +54,15 @@ def oauth_exceptions(func): parts = rfc3987.parse(o_exc.redirect_uri, 'URI') # Add the error and error_description - params = parse_qsl(parts['query']) if parts['query'] else [] + if parts['query']: + params = urlparse.parse_qsl(parts['query']) + else: + params = [] params.append(('error', error)) params.append(('error_description', error_description)) # Overwrite the old query params and reconstruct the URL - parts['query'] = urlencode(params) + parts['query'] = urlparse.urlencode(params) location = rfc3987.compose(**parts) redirect(location) diff --git a/storyboard/tests/api/auth/test_oauth.py b/storyboard/tests/api/auth/test_oauth.py index 8621812b..7364ac8e 100644 --- a/storyboard/tests/api/auth/test_oauth.py +++ b/storyboard/tests/api/auth/test_oauth.py @@ -14,13 +14,12 @@ import datetime import requests -from urlparse import parse_qs -from urlparse import urlparse import uuid from mock import patch from oslo.config import cfg import six +import six.moves.urllib.parse as urlparse from storyboard.api.auth import ErrorMessages as e_msg from storyboard.db.api import access_tokens as token_api @@ -51,12 +50,12 @@ class BaseOAuthTest(base.FunctionalTest): self.assertEqual(expected_status_code, response.status_code) # Split the url into parts. location = response.headers.get('Location') - location_url = urlparse(location) - parameters = parse_qs(location_url[4]) + location_url = urlparse.urlparse(location) + parameters = urlparse.parse_qs(location_url[4]) # Break out the redirect uri to compare and make sure we're headed # back to the redirect URI with the appropriate error codes. - configured_url = urlparse(redirect_uri) + configured_url = urlparse.urlparse(redirect_uri) self.assertEqual(configured_url[0], location_url[0]) self.assertEqual(configured_url[1], location_url[1]) self.assertEqual(configured_url[2], location_url[2]) @@ -109,8 +108,8 @@ class TestOAuthAuthorize(BaseOAuthTest): # Assert that the redirect request goes to launchpad. location = response.headers.get('Location') - location_url = urlparse(location) - parameters = parse_qs(location_url[4]) + location_url = urlparse.urlparse(location) + parameters = urlparse.parse_qs(location_url[4]) # Check the URL conf_openid_url = CONF.oauth.openid_url @@ -123,8 +122,8 @@ class TestOAuthAuthorize(BaseOAuthTest): # Check redirect URL redirect = parameters['openid.return_to'][0] - redirect_url = urlparse(redirect) - redirect_params = parse_qs(redirect_url[4]) + redirect_url = urlparse.urlparse(redirect) + redirect_params = urlparse.parse_qs(redirect_url[4]) self.assertIn('/openid/authorize_return', redirect) self.assertEqual(random_state, @@ -385,8 +384,8 @@ class TestOAuthAuthorizeReturn(BaseOAuthTest): # Try to pull the code out of the response location = response.headers.get('Location') - location_url = urlparse(location) - parameters = parse_qs(location_url[4]) + location_url = urlparse.urlparse(location) + parameters = urlparse.parse_qs(location_url[4]) token = auth_api.authorization_code_get(parameters['code']) # Validate the redirect response diff --git a/storyboard/tests/api/test_project_groups.py b/storyboard/tests/api/test_project_groups.py index c91f6fc8..9e7f4c7c 100644 --- a/storyboard/tests/api/test_project_groups.py +++ b/storyboard/tests/api/test_project_groups.py @@ -13,8 +13,8 @@ # under the License. import json -from urllib import urlencode +import six.moves.urllib.parse as urlparse from webtest.app import AppError from storyboard.tests import base @@ -137,7 +137,7 @@ class TestProjectGroupSearch(base.FunctionalTest): self.resource = '/project_groups' def build_search_url(self, params): - return '/project_groups?%s' % (urlencode(params)) + return '/project_groups?%s' % (urlparse.urlencode(params)) def test_search_by_name(self): url = self.build_search_url({ diff --git a/storyboard/tests/api/test_projects.py b/storyboard/tests/api/test_projects.py index 4253eb70..378611ee 100644 --- a/storyboard/tests/api/test_projects.py +++ b/storyboard/tests/api/test_projects.py @@ -14,8 +14,8 @@ # under the License. import json -from urllib import urlencode +import six.moves.urllib.parse as urlparse from webtest.app import AppError from storyboard.tests import base @@ -120,7 +120,7 @@ class TestProjectSearch(base.FunctionalTest): super(TestProjectSearch, self).setUp() def build_search_url(self, params): - return '/projects?%s' % (urlencode(params)) + return '/projects?%s' % (urlparse.urlencode(params)) def test_search(self): url = self.build_search_url({ diff --git a/storyboard/tests/api/test_stories.py b/storyboard/tests/api/test_stories.py index 64460009..ae0dba98 100644 --- a/storyboard/tests/api/test_stories.py +++ b/storyboard/tests/api/test_stories.py @@ -13,7 +13,8 @@ # under the License. import json -from urllib import urlencode + +import six.moves.urllib.parse as urlparse from storyboard.db.api import tasks from storyboard.tests import base @@ -113,7 +114,7 @@ class TestStorySearch(base.FunctionalTest): def build_search_url(self, params=None, raw=''): if params: - raw = urlencode(params) + raw = urlparse.urlencode(params) return '/stories?%s' % raw def test_search(self): diff --git a/storyboard/tests/api/test_task_statuses.py b/storyboard/tests/api/test_task_statuses.py index 93693fdb..8f6e90b8 100644 --- a/storyboard/tests/api/test_task_statuses.py +++ b/storyboard/tests/api/test_task_statuses.py @@ -12,7 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. -from urllib import urlencode +import six.moves.urllib.parse as urlparse from storyboard.tests import base @@ -25,7 +25,7 @@ class TestTaskStatusesSearch(base.FunctionalTest): def build_search_url(self, params=None, raw=''): if params: - raw = urlencode(params) + raw = urlparse.urlencode(params) return '/task_statuses?%s' % raw def test_search(self): diff --git a/storyboard/tests/api/test_tasks.py b/storyboard/tests/api/test_tasks.py index 3125d686..8545dd9d 100644 --- a/storyboard/tests/api/test_tasks.py +++ b/storyboard/tests/api/test_tasks.py @@ -12,7 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. -from urllib import urlencode +import six.moves.urllib.parse as urlparse from storyboard.tests import base @@ -55,7 +55,7 @@ class TestTaskSearch(base.FunctionalTest): def build_search_url(self, params=None, raw=''): if params: - raw = urlencode(params) + raw = urlparse.urlencode(params) return '/tasks?%s' % raw def test_search(self):