From 1401a6168fe662216858170385829ed14ad8e7df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Tue, 11 Sep 2018 17:53:23 +0200 Subject: [PATCH] Always use urljoin to construct Blender ID URLs --- pillar/api/blender_id.py | 2 +- pillar/auth/oauth.py | 2 +- pillar/tests/__init__.py | 10 +++------- pillar/tests/config_testing.py | 2 +- tests/test_api/test_auth.py | 5 +++-- tests/test_api/test_blender_id_subclient.py | 3 ++- tests/test_api/test_organizations.py | 3 ++- 7 files changed, 13 insertions(+), 14 deletions(-) diff --git a/pillar/api/blender_id.py b/pillar/api/blender_id.py index b2f9a36c..59c0f533 100644 --- a/pillar/api/blender_id.py +++ b/pillar/api/blender_id.py @@ -220,7 +220,7 @@ def fetch_blenderid_user() -> dict: my_log = log.getChild('fetch_blenderid_user') - bid_url = '%s/api/user' % current_app.config['BLENDER_ID_ENDPOINT'] + bid_url = urljoin(current_app.config['BLENDER_ID_ENDPOINT'], 'api/user') my_log.debug('Fetching user info from %s', bid_url) credentials = current_app.config['OAUTH_CREDENTIALS']['blender-id'] diff --git a/pillar/auth/oauth.py b/pillar/auth/oauth.py index 51f2b109..9ab49180 100644 --- a/pillar/auth/oauth.py +++ b/pillar/auth/oauth.py @@ -140,7 +140,7 @@ class BlenderIdSignIn(OAuthSignIn): client_secret=self.consumer_secret, authorize_url=urljoin(base_url, 'oauth/authorize'), access_token_url=urljoin(base_url, 'oauth/token'), - base_url='%s/api/' % base_url + base_url=urljoin(base_url, 'api/'), ) def authorize(self): diff --git a/pillar/tests/__init__.py b/pillar/tests/__init__.py index 61914561..8f58e556 100644 --- a/pillar/tests/__init__.py +++ b/pillar/tests/__init__.py @@ -11,11 +11,7 @@ import pathlib import sys import typing import unittest.mock - -try: - from urllib.parse import urlencode -except ImportError: - from urllib.parse import urlencode +from urllib.parse import urlencode, urljoin from bson import ObjectId, tz_util @@ -433,7 +429,7 @@ class AbstractPillarTest(TestMinimal): """Sets up Responses to mock unhappy validation flow.""" responses.add(responses.POST, - '%s/u/validate_token' % self.app.config['BLENDER_ID_ENDPOINT'], + urljoin(self.app.config['BLENDER_ID_ENDPOINT'], 'u/validate_token'), json={'status': 'fail'}, status=403) @@ -441,7 +437,7 @@ class AbstractPillarTest(TestMinimal): """Sets up Responses to mock happy validation flow.""" responses.add(responses.POST, - '%s/u/validate_token' % self.app.config['BLENDER_ID_ENDPOINT'], + urljoin(self.app.config['BLENDER_ID_ENDPOINT'], 'u/validate_token'), json=BLENDER_ID_USER_RESPONSE, status=200) diff --git a/pillar/tests/config_testing.py b/pillar/tests/config_testing.py index ba9b1870..d0ee3434 100644 --- a/pillar/tests/config_testing.py +++ b/pillar/tests/config_testing.py @@ -1,6 +1,6 @@ """Flask configuration file for unit testing.""" -BLENDER_ID_ENDPOINT = 'http://id.local:8001' # Non existant server +BLENDER_ID_ENDPOINT = 'http://id.local:8001/' # Non existant server SERVER_NAME = 'localhost.local' PILLAR_SERVER_ENDPOINT = 'http://localhost.local/api/' diff --git a/tests/test_api/test_auth.py b/tests/test_api/test_auth.py index eff1286b..79d1cccc 100644 --- a/tests/test_api/test_auth.py +++ b/tests/test_api/test_auth.py @@ -3,6 +3,7 @@ import copy import datetime import json +from urllib.parse import urljoin import pillar.tests.common_test_data as ctd import responses @@ -761,7 +762,7 @@ class UserCreationTest(AbstractPillarTest): 'token_expires': 'Mon, 1 Jan 2218 01:02:03 GMT'} responses.add(responses.POST, - '%s/u/validate_token' % self.app.config['BLENDER_ID_ENDPOINT'], + urljoin(self.app.config['BLENDER_ID_ENDPOINT'], 'u/validate_token'), json=bid_resp, status=200) @@ -791,7 +792,7 @@ class UserCreationTest(AbstractPillarTest): 'id': ctd.BLENDER_ID_TEST_USERID}, 'token_expires': 'Mon, 1 Jan 2218 01:02:03 GMT'} responses.add(responses.POST, - '%s/u/validate_token' % self.app.config['BLENDER_ID_ENDPOINT'], + urljoin(self.app.config['BLENDER_ID_ENDPOINT'], 'u/validate_token'), json=bid_resp, status=200) diff --git a/tests/test_api/test_blender_id_subclient.py b/tests/test_api/test_blender_id_subclient.py index eb465c5c..bf3afc21 100644 --- a/tests/test_api/test_blender_id_subclient.py +++ b/tests/test_api/test_blender_id_subclient.py @@ -1,6 +1,7 @@ # -*- encoding: utf-8 -*- import json +from urllib.parse import urljoin import responses from bson import ObjectId @@ -19,7 +20,7 @@ class BlenderIdSubclientTest(AbstractPillarTest): def test_store_scst_new_user_without_full_name(self): responses.add(responses.POST, - '%s/u/validate_token' % self.app.config['BLENDER_ID_ENDPOINT'], + urljoin(self.app.config['BLENDER_ID_ENDPOINT'], 'u/validate_token'), json={'status': 'success', 'user': {'email': TEST_EMAIL_ADDRESS, 'full_name': None, diff --git a/tests/test_api/test_organizations.py b/tests/test_api/test_organizations.py index 36189d74..4a4120fe 100644 --- a/tests/test_api/test_organizations.py +++ b/tests/test_api/test_organizations.py @@ -1,5 +1,6 @@ import datetime import typing +from urllib.parse import urljoin import bson from bson import tz_util @@ -757,7 +758,7 @@ class UserCreationTest(AbstractPillarTest): 'token_expires': 'Mon, 1 Jan 2218 01:02:03 GMT'} responses.add(responses.POST, - '%s/u/validate_token' % self.app.config['BLENDER_ID_ENDPOINT'], + urljoin(self.app.config['BLENDER_ID_ENDPOINT'], 'u/validate_token'), json=blender_id_response, status=200)