11import json
2+ from typing import Any
3+ from unittest import TestCase
24
35import 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+
1843class 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 ]
0 commit comments