From 6444a8c58392d23e511d81ea84621768e81a2658 Mon Sep 17 00:00:00 2001 From: Raymond Penners Date: Fri, 3 Nov 2023 16:05:48 +0100 Subject: [PATCH] tests(socialaccount): Query email --- .../providers/authentiq/tests.py | 3 ++ .../socialaccount/providers/github/tests.py | 48 ++++++++++++++++--- .../providers/linkedin_oauth2/tests.py | 16 +++++-- allauth/tests.py | 4 ++ test_settings.py | 2 +- 5 files changed, 61 insertions(+), 12 deletions(-) diff --git a/allauth/socialaccount/providers/authentiq/tests.py b/allauth/socialaccount/providers/authentiq/tests.py index 1ed90d945b..a69859c071 100644 --- a/allauth/socialaccount/providers/authentiq/tests.py +++ b/allauth/socialaccount/providers/authentiq/tests.py @@ -27,6 +27,9 @@ def get_mocked_response(self): ), ) + @override_settings( + SOCIALACCOUNT_QUERY_EMAIL=False, + ) def test_default_scopes_no_email(self): scopes = self.provider.get_default_scope() self.assertIn("aq:name", scopes) diff --git a/allauth/socialaccount/providers/github/tests.py b/allauth/socialaccount/providers/github/tests.py index 2f27109e07..f183180019 100644 --- a/allauth/socialaccount/providers/github/tests.py +++ b/allauth/socialaccount/providers/github/tests.py @@ -9,9 +9,10 @@ class GitHubTests(OAuth2TestsMixin, TestCase): provider_id = GitHubProvider.id def get_mocked_response(self): - return MockedResponse( - 200, - """ + return [ + MockedResponse( + 200, + """ { "type":"User", "organizations_url":"https://api.github.com/users/pennersr/orgs", @@ -42,19 +43,52 @@ def get_mocked_response(self): "events_url":"https://api.github.com/users/pennersr/events{/privacy}", "following_url":"https://api.github.com/users/pennersr/following" }""", - ) + ), + MockedResponse( + 200, + """ + { + "email": "octocat@github.com", + "verified": true, + "primary": true, + "visibility": "public" + } + """, + ), + ] def test_account_name_null(self): """String conversion when GitHub responds with empty name""" - data = """{ + mocks = [ + MockedResponse( + 200, + """ + { "type": "User", "id": 201022, "login": "pennersr", "name": null - }""" - self.login(MockedResponse(200, data)) + } + """, + ), + MockedResponse( + 200, + """ + [ + { + "email": "octocat@github.com", + "verified": true, + "primary": true, + "visibility": "public" + } + ] + """, + ), + ] + self.login(mocks) 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") + self.assertEqual(socialaccount.user.email, "octocat@github.com") diff --git a/allauth/socialaccount/providers/linkedin_oauth2/tests.py b/allauth/socialaccount/providers/linkedin_oauth2/tests.py index d272c7d734..30cc69bde3 100644 --- a/allauth/socialaccount/providers/linkedin_oauth2/tests.py +++ b/allauth/socialaccount/providers/linkedin_oauth2/tests.py @@ -15,9 +15,16 @@ class LinkedInOAuth2Tests(OAuth2TestsMixin, TestCase): provider_id = LinkedInOAuth2Provider.id def get_mocked_response(self): - return MockedResponse( - 200, - """ + return [ + MockedResponse( + 200, + """ + {} + """, + ), + MockedResponse( + 200, + """ { "profilePicture": { "displayImage": "urn:li:digitalmediaAsset:12345abcdefgh-12abcd" @@ -43,7 +50,8 @@ def get_mocked_response(self): } } """, - ) + ), + ] def test_data_to_str(self): data = { diff --git a/allauth/tests.py b/allauth/tests.py index 78063187c9..ba36e3fe6a 100644 --- a/allauth/tests.py +++ b/allauth/tests.py @@ -32,6 +32,10 @@ def json(self): def raise_for_status(self): pass + @property + def ok(self): + return self.status_code // 100 == 2 + @property def text(self): return self.content.decode("utf8") diff --git a/test_settings.py b/test_settings.py index d54288d598..1073f33dc2 100644 --- a/test_settings.py +++ b/test_settings.py @@ -220,7 +220,7 @@ class MyPBKDF2PasswordHasher(PBKDF2PasswordHasher): ACCOUNT_EMAIL_CONFIRMATION_COOLDOWN = 0 ACCOUNT_RATE_LIMITS = {} - +SOCIALACCOUNT_QUERY_EMAIL = True SOCIALACCOUNT_PROVIDERS = { "openid_connect": { "APPS": [