Skip to content

Commit 905e21f

Browse files
authored
feat(github): improve GitHub mails handling (#1711)
- drop support for legacy responses - extend testsuite
1 parent a492858 commit 905e21f

3 files changed

Lines changed: 45 additions & 21 deletions

File tree

social_core/backends/github.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ def user_data(self, access_token: str, *args, **kwargs) -> dict[str, Any] | None
6262
]
6363
if primary_emails:
6464
email = primary_emails[0]
65-
if isinstance(email, dict):
66-
email = email.get("email", "")
67-
data["email"] = email
65+
data["email"] = email["email"]
6866
return data
6967

7068
def _user_data(self, access_token, path=None):

social_core/tests/backends/test_github.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import json
2+
from typing import Any
3+
from unittest import TestCase
24

35
import responses
46

@@ -15,6 +17,29 @@ def capture_github_emails(strategy, response, *args, **kwargs) -> None:
1517
strategy.session_set("github_emails", response.get("emails"))
1618

1719

20+
class TestCaptureGithubEmails(TestCase):
21+
class DummyStrategy:
22+
def __init__(self) -> None:
23+
self.data: dict[str, Any] = {}
24+
25+
def session_set(self, key, value) -> None:
26+
self.data[key] = value
27+
28+
def test_capture_github_emails_missing_emails_key(self) -> None:
29+
strategy = self.DummyStrategy()
30+
31+
capture_github_emails(strategy, {})
32+
33+
assert "github_emails" in strategy.data
34+
assert strategy.data["github_emails"] is None
35+
36+
def test_capture_github_emails_response_none_raises_attribute_error(self) -> None:
37+
strategy = self.DummyStrategy()
38+
39+
with self.assertRaises(AttributeError):
40+
capture_github_emails(strategy, None)
41+
42+
1843
class GithubOAuth2Test(OAuth2Test, BaseAuthUrlTestMixin):
1944
backend_path = "social_core.backends.github.GithubOAuth2"
2045
user_data_url = "https://api.github.com/user"
@@ -74,6 +99,7 @@ class GithubOAuth2Test(OAuth2Test, BaseAuthUrlTestMixin):
7499

75100
def do_login(self):
76101
user = super().do_login()
102+
self.assertTrue(user.social)
77103
social = user.social[0]
78104

79105
self.assertIsNotNone(social.extra_data["expires_in"])
@@ -129,11 +155,13 @@ class GithubOAuth2NoEmailTest(GithubOAuth2Test):
129155
}
130156
)
131157

132-
def add_emails_response(self, emails) -> None:
158+
def add_emails_response(
159+
self, emails: list[dict[str, str | bool]], status: int = 200
160+
) -> None:
133161
responses.add(
134162
responses.GET,
135163
self.emails_url,
136-
status=200,
164+
status=status,
137165
body=json.dumps(emails),
138166
content_type="application/json",
139167
)
@@ -150,28 +178,22 @@ def capture_emails_in_pipeline(self) -> None:
150178
}
151179
)
152180

153-
def test_login(self) -> None:
154-
self.add_emails_response(["foo@bar.com"])
181+
def test_login_email_denied(self) -> None:
182+
self.add_emails_response([], status=403)
155183
self.do_login()
156184

157-
def test_login_next_format(self) -> None:
158-
self.add_emails_response([{"email": "foo@bar.com"}])
185+
def test_login_with_empty_email_list(self) -> None:
186+
self.add_emails_response([])
159187
user = self.do_login()
160-
self.assertEqual(user.email, "foo@bar.com")
161-
162-
def test_pipeline_receives_legacy_email_response(self) -> None:
163-
emails = ["foo@bar.com"]
164-
self.add_emails_response(emails)
165-
self.capture_emails_in_pipeline()
188+
self.assertNotIn("emails", user.social[0].extra_data)
166189

190+
def test_login_next_format(self) -> None:
191+
self.add_emails_response([{"email": "foo@bar.com"}])
167192
user = self.do_login()
168-
169-
self.assertEqual(self.strategy.session_get("github_emails"), emails)
170193
self.assertEqual(user.email, "foo@bar.com")
171-
self.assertNotIn("emails", user.social[0].extra_data)
172194

173-
def test_pipeline_receives_dict_email_response(self) -> None:
174-
emails = [
195+
def test_login(self) -> None:
196+
emails: list[dict[str, str | bool]] = [
175197
{"email": "secondary@example.com", "primary": False},
176198
{"email": "foo@bar.com", "primary": True},
177199
]

social_core/tests/backends/test_github_enterprise.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ def test_login(self) -> None:
114114
responses.GET,
115115
url,
116116
status=200,
117-
body=json.dumps(["foo@bar.com"]),
117+
body=json.dumps(
118+
[
119+
{"email": "foo@bar.com", "primary": True},
120+
]
121+
),
118122
content_type="application/json",
119123
)
120124
self.do_login()

0 commit comments

Comments
 (0)