8000 Merge pull request #832 from sigmavirus24/bug/829 · jayhawk87/github3.py@53c1649 · GitHub
[go: up one dir, main page]

Skip to content

Commit 53c1649

Browse files
authored
Merge pull request sigmavirus24#832 from sigmavirus24/bug/829
Fix reprs for auth classes with our GitHub object
2 parents 354e9e1 + 3b04b76 commit 53c1649

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

github3/github.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(self, username='', password='', token=''):
6666

6767
def _repr(self):
6868
if self.session.auth:
69-
return '<GitHub [{0[0]}]>'.format(self.session.auth)
69+
return '<GitHub [{!r}]>'.format(self.session.auth)
7070
return '<Anonymous GitHub at 0x{0:x}>'.format(id(self))
7171

7272
@requires_auth

github3/session.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,29 @@
1111

1212

1313
def requires_2fa(response):
14+
"""Determine whether a response requires us to prompt the user for 2FA."""
1415
if (response.status_code == 401 and 'X-GitHub-OTP' in response.headers and
1516
'required' in response.headers['X-GitHub-OTP']):
1617
return True
1718
return False
1819

1920

21+
class BasicAuth(requests.auth.HTTPBasicAuth):
22+
"""Sub-class requests's class so we have a nice repr."""
23+
24+
def __repr__(self):
25+
"""Use the username as the representation."""
26+
return 'basic {}'.format(self.username)
27+
28+
2029
class TokenAuth(requests.auth.AuthBase):
2130
def __init__(self, token):
2231
self.token = token
2332

33+
def __repr__(self):
34+
"""Return a nice view of the token in use."""
35+
return 'token {}...'.format(self.token[:4])
36+
2437
def __ne__(self, other):
2538
return not self == other
2639

@@ -61,7 +74,7 @@ def basic_auth(self, username, password):
6174
if not (username and password):
6275
return
6376

64-
self.auth = (username, password)
77+
self.auth = BasicAuth(username, password)
6578

6679
def build_url(self, *args, **kwargs):
6780
"""Builds a new API url from scratch."""

tests/unit/test_github_session.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ def test_basic_login_does_not_use_falsey_values(self):
6969
# Make sure we have a clean session to test with
7070
s = self.build_session()
7171
s.basic_auth(*auth)
72-
assert s.auth != auth
72+
assert s.auth != session.BasicAuth(*auth)
7373

7474
def test_basic_login(self):
7575
"""Test that basic auth will work with a valid combination"""
7676
s = self.build_session()
7777
s.basic_auth('username', 'password')
78-
assert s.auth == ('username', 'password')
78+
assert s.auth == session.BasicAuth('username', 'password')
7979

8080
def test_basic_login_disables_token_auth(self):
8181
"""Test that basic auth will remove the Authorization header.
@@ -238,16 +238,16 @@ def test_can_use_temporary_basic_auth(self):
238238
s = self.build_session()
239239
s.basic_auth('foo', 'bar')
240240
with s.temporary_basic_auth('temp', 'pass'):
241-
assert s.auth != ('foo', 'bar')
241+
assert s.auth != session.BasicAuth('foo', 'bar')
242242

243-
assert s.auth == ('foo', 'bar')
243+
assert s.auth == session.BasicAuth('foo', 'bar')
244244

245245
def test_temporary_basic_auth_replaces_auth(self):
246246
"""Test that temporary_basic_auth sets the proper credentials."""
247247
s = self.build_session()
248248
s.basic_auth('foo', 'bar')
249249
with s.temporary_basic_auth('temp', 'pass'):
250-
assert s.auth == ('temp', 'pass')
250+
assert s.auth == session.BasicAuth('temp', 'pass')
251251

252252
def test_no_auth(self):
253253
"""Verify that no_auth removes existing authentication."""
@@ -262,7 +262,7 @@ def test_no_auth(self):
262262

263263
pr = s.prepare_request(req)
264264
assert 'Authorization' in pr.headers
265-
assert s.auth == ('user', 'password')
265+
assert s.auth == session.BasicAuth('user', 'password')
266266

267267
def test_retrieve_client_credentials_when_set(self):
268268
"""Test that retrieve_client_credentials will return the credentials.

0 commit comments

Comments
 (0)
0