| 1 |
from http import HTTPStatus |
| 2 |
|
| 3 |
from django.contrib.auth import get_user_model |
| 4 |
from django.test import TestCase |
| 5 |
|
| 6 |
from allauth.account.models import EmailAddress |
| 7 |
from allauth.account.utils import user_email, user_username |
| 8 |
from allauth.socialaccount.models import SocialAccount |
| 9 |
from allauth.socialaccount.providers.discord.provider import DiscordProvider |
| 10 |
from tests.apps.socialaccount.base import OAuth2TestsMixin |
| 11 |
from tests.mocking import MockedResponse |
| 12 |
|
| 13 |
|
| 14 |
class DiscordTests(OAuth2TestsMixin, TestCase): |
| 15 |
provider_id = DiscordProvider.id |
| 16 |
|
| 17 |
def get_mocked_response(self): |
| 18 |
return MockedResponse( |
| 19 |
HTTPStatus.OK, |
| 20 |
"""{ |
| 21 |
"id": "80351110224678912", |
| 22 |
"username": "nelly", |
| 23 |
"discriminator": "0", |
| 24 |
"global_name": "Nelly", |
| 25 |
"avatar": "8342729096ea3675442027381ff50dfe", |
| 26 |
"verified": true, |
| 27 |
"email": "[email protected]" |
| 28 |
}""", |
| 29 |
) |
| 30 |
|
| 31 |
def get_expected_to_str(self): |
| 32 |
return "Nelly" |
| 33 |
|
| 34 |
def test_display_name(self, multiple_login=False): |
| 35 |
email = "[email protected]" |
| 36 |
user = get_user_model()(is_active=True) |
| 37 |
user_email(user, email) |
| 38 |
user_username(user, "user") |
| 39 |
user.set_password("test") |
| 40 |
user.save() |
| 41 |
EmailAddress.objects.create(user=user, email=email, primary=True, verified=True) |
| 42 |
self.client.login(username=user.username, password="test") |
| 43 |
self.login(self.get_mocked_response(), process="connect") |
| 44 |
if multiple_login: |
| 45 |
self.login( |
| 46 |
self.get_mocked_response(), |
| 47 |
with_refresh_token=False, |
| 48 |
process="connect", |
| 49 |
) |
| 50 |
|
| 51 |
# get account |
| 52 |
sa = SocialAccount.objects.filter(user=user, provider=self.provider.id).get() |
| 53 |
# The following lines don't actually test that much, but at least |
| 54 |
# we make sure that the code is hit. |
| 55 |
provider_account = sa.get_provider_account() |
| 56 |
self.assertEqual(provider_account.to_str(), "Nelly") |
| 57 |
|
| 58 |
|
| 59 |
class OldDiscordTests(DiscordTests, TestCase): |
| 60 |
provider_id = DiscordProvider.id |
| 61 |
|
| 62 |
def get_mocked_response(self): |
| 63 |
return MockedResponse( |
| 64 |
HTTPStatus.OK, |
| 65 |
"""{ |
| 66 |
"id": "80351110224678912", |
| 67 |
"username": "Nelly", |
| 68 |
"discriminator": "1337", |
| 69 |
"avatar": "8342729096ea3675442027381ff50dfe", |
| 70 |
"verified": true, |
| 71 |
"email": "[email protected]" |
| 72 |
}""", |
| 73 |
) |
| 74 |
|
| 75 |
def get_expected_to_str(self): |
| 76 |
return "Nelly#1337" |
| 77 |
|
| 78 |
def test_display_name(self, multiple_login=False): |
| 79 |
email = "[email protected]" |
| 80 |
user = get_user_model()(is_active=True) |
| 81 |
user_email(user, email) |
| 82 |
user_username(user, "user") |
| 83 |
user.set_password("test") |
| 84 |
user.save() |
| 85 |
EmailAddress.objects.create(user=user, email=email, primary=True, verified=True) |
| 86 |
self.client.login(username=user.username, password="test") |
| 87 |
self.login(self.get_mocked_response(), process="connect") |
| 88 |
if multiple_login: |
| 89 |
self.login( |
| 90 |
self.get_mocked_response(), |
| 91 |
with_refresh_token=False, |
| 92 |
process="connect", |
| 93 |
) |
| 94 |
|
| 95 |
# get account |
| 96 |
sa = SocialAccount.objects.filter(user=user, provider=self.provider.id).get() |
| 97 |
# The following lines don't actually test that much, but at least |
| 98 |
# we make sure that the code is hit. |
| 99 |
provider_account = sa.get_provider_account() |
| 100 |
self.assertEqual(provider_account.to_str(), "Nelly#1337") |