Skip to content

Commit

Permalink
Merge remote-tracking branch 'agjohnson/github-to-str'
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed Dec 28, 2015
2 parents 5c40259 + 065d4da commit a7b41ee
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
10 changes: 9 additions & 1 deletion allauth/socialaccount/providers/github/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ def get_avatar_url(self):

def to_str(self):
dflt = super(GitHubAccount, self).to_str()
return self.account.extra_data.get('name', dflt)
return next(
value
for value in (
self.account.extra_data.get('name', None),
self.account.extra_data.get('login', None),
dflt
)
if value is not None
)


class GitHubProvider(OAuth2Provider):
Expand Down
17 changes: 17 additions & 0 deletions allauth/socialaccount/providers/github/tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase
from allauth.socialaccount.models import SocialAccount

from .provider import GitHubProvider


class GitHubTests(OAuth2TestsMixin, TestCase):
provider_id = GitHubProvider.id

Expand Down Expand Up @@ -38,3 +40,18 @@ def get_mocked_response(self):
"events_url":"https://api.github.com/users/pennersr/events{/privacy}",
"following_url":"https://api.github.com/users/pennersr/following"
}""")

def test_account_name_null(self):
"""String conversion when GitHub responds with empty name"""
data = """{
"type": "User",
"id": 201022,
"login": "pennersr",
"name": null
}"""
self.login(MockedResponse(200, data))
socialaccount = SocialAccount.objects.get(uid='201022')
self.assertIsNone(socialaccount.extra_data.get('name'))
account = socialaccount.get_provider_account()
self.assertIsNotNone(account.to_str())
self.assertEqual(account.to_str(), 'pennersr')

0 comments on commit a7b41ee

Please sign in to comment.