Skip to content

Commit

Permalink
Merge pull request #832 from sigmavirus24/bug/829
Browse files Browse the repository at this point in the history
Fix reprs for auth classes with our GitHub object
  • Loading branch information
sigmavirus24 committed Apr 18, 2018
2 parents 354e9e1 + 3b04b76 commit 53c1649
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion github3/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(self, username='', password='', token=''):

def _repr(self):
if self.session.auth:
return '<GitHub [{0[0]}]>'.format(self.session.auth)
return '<GitHub [{!r}]>'.format(self.session.auth)
return '<Anonymous GitHub at 0x{0:x}>'.format(id(self))

@requires_auth
Expand Down
15 changes: 14 additions & 1 deletion github3/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,29 @@


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


class BasicAuth(requests.auth.HTTPBasicAuth):
"""Sub-class requests's class so we have a nice repr."""

def __repr__(self):
"""Use the username as the representation."""
return 'basic {}'.format(self.username)


class TokenAuth(requests.auth.AuthBase):
def __init__(self, token):
self.token = token

def __repr__(self):
"""Return a nice view of the token in use."""
return 'token {}...'.format(self.token[:4])

def __ne__(self, other):
return not self == other

Expand Down Expand Up @@ -61,7 +74,7 @@ def basic_auth(self, username, password):
if not (username and password):
return

self.auth = (username, password)
self.auth = BasicAuth(username, password)

def build_url(self, *args, **kwargs):
"""Builds a new API url from scratch."""
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/test_github_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ def test_basic_login_does_not_use_falsey_values(self):
# Make sure we have a clean session to test with
s = self.build_session()
s.basic_auth(*auth)
assert s.auth != auth
assert s.auth != session.BasicAuth(*auth)

def test_basic_login(self):
"""Test that basic auth will work with a valid combination"""
s = self.build_session()
s.basic_auth('username', 'password')
assert s.auth == ('username', 'password')
assert s.auth == session.BasicAuth('username', 'password')

def test_basic_login_disables_token_auth(self):
"""Test that basic auth will remove the Authorization header.
Expand Down Expand Up @@ -238,16 +238,16 @@ def test_can_use_temporary_basic_auth(self):
s = self.build_session()
s.basic_auth('foo', 'bar')
with s.temporary_basic_auth('temp', 'pass'):
assert s.auth != ('foo', 'bar')
assert s.auth != session.BasicAuth('foo', 'bar')

assert s.auth == ('foo', 'bar')
assert s.auth == session.BasicAuth('foo', 'bar')

def test_temporary_basic_auth_replaces_auth(self):
"""Test that temporary_basic_auth sets the proper credentials."""
s = self.build_session()
s.basic_auth('foo', 'bar')
with s.temporary_basic_auth('temp', 'pass'):
assert s.auth == ('temp', 'pass')
assert s.auth == session.BasicAuth('temp', 'pass')

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

pr = s.prepare_request(req)
assert 'Authorization' in pr.headers
assert s.auth == ('user', 'password')
assert s.auth == session.BasicAuth('user', 'password')

def test_retrieve_client_credentials_when_set(self):
"""Test that retrieve_client_credentials will return the credentials.
Expand Down

0 comments on commit 53c1649

Please sign in to comment.