Skip to content

Commit d9b2052

Browse files
committed
Refactor Oauth2TestCase and test logger
1 parent 8dc3bbc commit d9b2052

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

tests/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
import sys
2+
import logging
23
if sys.version_info[:2] < (2, 7):
34
# The unittest module got a significant overhaul in Python 2.7,
45
# so if we're in 2.6 we can use the backported version unittest2.
56
import unittest2 as unittest
67
else:
78
import unittest
89

10+
11+
class Oauth2TestCase(unittest.TestCase):
12+
13+
logger = logging.getLogger(__file__)
14+
15+
def assertLoosely(self, response, assertion=None,
16+
skippable_errors=("invalid_grant", "interaction_required")):
17+
if response.get("error") in skippable_errors:
18+
self.logger.debug("Response = %s", response)
19+
# Some of these errors are configuration issues, not library issues
20+
raise unittest.SkipTest(response.get("error_description"))
21+
else:
22+
if assertion is None:
23+
assertion = lambda: self.assertIn("access_token", response)
24+
assertion()
25+

tests/test_client.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
from oauth2cli.oauth2 import Client
1313
from oauth2cli.authcode import obtain_auth_code
1414
from oauth2cli.assertion import JwtSigner
15-
from tests import unittest
15+
from tests import unittest, Oauth2TestCase
1616

1717

18+
logging.basicConfig(level=logging.DEBUG)
19+
logger = logging.getLogger(__file__)
20+
1821
CONFIG_FILENAME = "config.json"
1922

2023
def load_conf(filename):
@@ -46,7 +49,7 @@ def load_conf(filename):
4649
with open(filename) as f:
4750
conf = json.load(f)
4851
except:
49-
logging.warn("Unable to open/read JSON configuration %s" % filename)
52+
logger.warn("Unable to open/read JSON configuration %s" % filename)
5053
raise
5154
openid_configuration = {}
5255
try:
@@ -56,7 +59,7 @@ def load_conf(filename):
5659
discovery_uri = conf["oidp"] + '/.well-known/openid-configuration'
5760
openid_configuration.update(requests.get(discovery_uri).json())
5861
except:
59-
logging.warn("openid-configuration uri not accesible: %s", discovery_uri)
62+
logger.warn("openid-configuration uri not accesible: %s", discovery_uri)
6063
openid_configuration.update(conf.get("openid_configuration", {}))
6164
if openid_configuration.get("device_authorization_endpoint"):
6265
# The following urljoin(..., ...) trick allows a "path_name" shorthand
@@ -69,21 +72,6 @@ def load_conf(filename):
6972
THIS_FOLDER = os.path.dirname(__file__)
7073
CONFIG = load_conf(os.path.join(THIS_FOLDER, CONFIG_FILENAME)) or {}
7174

72-
logging.basicConfig(level=logging.DEBUG)
73-
74-
75-
class Oauth2TestCase(unittest.TestCase):
76-
77-
def assertLoosely(self, response, assertion=None,
78-
skippable_errors=("invalid_grant", "interaction_required")):
79-
if response.get("error") in skippable_errors:
80-
# Some of these errors are configuration issues, not library issues
81-
raise unittest.SkipTest(response.get("error_description"))
82-
else:
83-
if assertion is None:
84-
assertion = lambda: self.assertIn("access_token", response)
85-
assertion()
86-
8775

8876
# Since the OAuth2 specs uses snake_case, this test config also uses snake_case
8977
@unittest.skipUnless("client_id" in CONFIG, "client_id missing")
@@ -156,10 +144,10 @@ def test_device_flow(self):
156144
"enter the code {user_code} to authenticate.".format(**flow))
157145
except KeyError: # Some IdP might not be standard compliant
158146
msg = flow["message"] # Not a standard parameter though
159-
logging.warn(msg) # We avoid print(...) b/c its output would be buffered
147+
logger.warn(msg) # We avoid print(...) b/c its output would be buffered
160148

161149
duration = 30
162-
logging.warn("We will wait up to %d seconds for you to sign in" % duration)
150+
logger.warn("We will wait up to %d seconds for you to sign in" % duration)
163151
result = self.client.obtain_token_by_device_flow(
164152
flow,
165153
exit_condition=lambda end=time.time() + duration: time.time() > end)

0 commit comments

Comments
 (0)