Debian Codegraph @

tests/apps/socialaccount/providers/github/tests.py

raw
1 from http import HTTPStatus
2 from unittest.mock import patch
3
4 from django.test import TestCase
5
6 from allauth.account.models import EmailAddress
7 from allauth.socialaccount.models import SocialAccount
8 from allauth.socialaccount.providers.github.provider import GitHubProvider
9 from tests.apps.socialaccount.base import OAuth2TestsMixin
10 from tests.mocking import MockedResponse
11
12
13 class GitHubTests(OAuth2TestsMixin, TestCase):
14 provider_id = GitHubProvider.id
15
16 def get_mocked_response(self):
17 return [
18 MockedResponse(
19 HTTPStatus.OK,
20 """
21 {
22 "type":"User",
23 "organizations_url":"https://api.github.com/users/pennersr/orgs",
24 "gists_url":"https://api.github.com/users/pennersr/gists{/gist_id}",
25 "received_events_url":"https://api.github.com/users/pennersr/received_events",
26 "gravatar_id":"8639768262b8484f6a3380f8db2efa5b",
27 "followers":16,
28 "blog":"http://www.intenct.info",
29 "avatar_url":"https://secure.gravatar.com/avatar/8639768262b8484f6a3380f8db2efa5b?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png",
30 "login":"pennersr",
31 "created_at":"2010-02-10T12:50:51Z",
32 "company":"IntenCT",
33 "subscriptions_url":"https://api.github.com/users/pennersr/subscriptions",
34 "public_repos":14,
35 "hireable":false,
36 "url":"https://api.github.com/users/pennersr",
37 "public_gists":0,
38 "starred_url":"https://api.github.com/users/pennersr/starred{/owner}{/repo}",
39 "html_url":"https://github.com/pennersr",
40 "location":"The Netherlands",
41 "bio":null,
42 "name":"Raymond Penners",
43 "repos_url":"https://api.github.com/users/pennersr/repos",
44 "followers_url":"https://api.github.com/users/pennersr/followers",
45 "id":201022,
46 "following":0,
47 "email":"[email protected]",
48 "events_url":"https://api.github.com/users/pennersr/events{/privacy}",
49 "following_url":"https://api.github.com/users/pennersr/following"
50 }""",
51 ),
52 MockedResponse(
53 HTTPStatus.OK,
54 """
55 [{
56 "email": "[email protected]",
57 "verified": true,
58 "primary": true,
59 "visibility": "public"
60 }]
61 """,
62 ),
63 ]
64
65 def get_expected_to_str(self):
66 return "pennersr"
67
68 def test_account_name_null(self):
69 """String conversion when GitHub responds with empty name"""
70 mocks = [
71 MockedResponse(
72 HTTPStatus.OK,
73 """
74 {
75 "type": "User",
76 "id": 201022,
77 "login": "pennersr",
78 "name": null
79 }
80 """,
81 ),
82 MockedResponse(
83 HTTPStatus.OK,
84 """
85 [
86 {
87 "email": "[email protected]",
88 "verified": true,
89 "primary": true,
90 "visibility": "public"
91 },
92 {
93 "email": "[email protected]",
94 "verified": true,
95 "primary": false,
96 "visibility": "public"
97 }
98 ]
99 """,
100 ),
101 ]
102 with patch(
103 "allauth.socialaccount.adapter.DefaultSocialAccountAdapter.populate_user"
104 ) as populate_mock:
105 self.login(mocks)
106 populate_data = populate_mock.call_args[0][2]
107 assert populate_data["email"] == "[email protected]"
108 socialaccount = SocialAccount.objects.get(uid="201022")
109 self.assertIsNone(socialaccount.extra_data.get("name"))
110 account = socialaccount.get_provider_account()
111 self.assertIsNotNone(account.to_str())
112 self.assertEqual(account.to_str(), "pennersr")
113 self.assertEqual(socialaccount.user.email, "[email protected]")
114 self.assertTrue(
115 EmailAddress.objects.filter(
116 primary=False,
117 verified=True,
118 email="[email protected]",
119 user=socialaccount.user,
120 ).exists()
121 )
122 self.assertTrue("emails" not in socialaccount.extra_data)