| 1 |
from http import HTTPStatus |
| 2 |
|
| 3 |
from django.contrib.auth.models import User |
| 4 |
from django.test import TestCase |
| 5 |
from django.test.utils import override_settings |
| 6 |
|
| 7 |
from allauth.account import app_settings as account_settings |
| 8 |
from allauth.account.models import EmailAddress |
| 9 |
from allauth.socialaccount.models import SocialAccount |
| 10 |
from allauth.socialaccount.providers.disqus.provider import DisqusProvider |
| 11 |
from tests.apps.socialaccount.base import OAuth2TestsMixin |
| 12 |
from tests.mocking import MockedResponse |
| 13 |
|
| 14 |
|
| 15 |
@override_settings( |
| 16 |
SOCIALACCOUNT_AUTO_SIGNUP=True, |
| 17 |
ACCOUNT_SIGNUP_FORM_CLASS=None, |
| 18 |
ACCOUNT_EMAIL_VERIFICATION=account_settings.EmailVerificationMethod.MANDATORY, |
| 19 |
) |
| 20 |
class DisqusTests(OAuth2TestsMixin, TestCase): |
| 21 |
provider_id = DisqusProvider.id |
| 22 |
|
| 23 |
def get_mocked_response( |
| 24 |
self, name="Raymond Penners", email="[email protected]" |
| 25 |
): |
| 26 |
return MockedResponse( |
| 27 |
HTTPStatus.OK, |
| 28 |
""" |
| 29 |
{"response": {"name": "%s", |
| 30 |
"avatar": { |
| 31 |
"permalink": "https://lh5.googleusercontent.com/photo.jpg" |
| 32 |
}, |
| 33 |
"email": "%s", |
| 34 |
"profileUrl": "https://plus.google.com/108204268033311374519", |
| 35 |
"id": "108204268033311374519" }} |
| 36 |
""" |
| 37 |
% (name, email), |
| 38 |
) |
| 39 |
|
| 40 |
def get_expected_to_str(self): |
| 41 |
return "[email protected]" |
| 42 |
|
| 43 |
def test_account_connect(self): |
| 44 |
email = "[email protected]" |
| 45 |
user = User.objects.create(username="user", is_active=True, email=email) |
| 46 |
user.set_password("test") |
| 47 |
user.save() |
| 48 |
EmailAddress.objects.create(user=user, email=email, primary=True, verified=True) |
| 49 |
self.client.login(username=user.username, password="test") |
| 50 |
self.login(self.get_mocked_response(), process="connect") |
| 51 |
# Check if we connected... |
| 52 |
self.assertTrue( |
| 53 |
SocialAccount.objects.filter(user=user, provider=DisqusProvider.id).exists() |
| 54 |
) |
| 55 |
# For now, we do not pick up any new email addresses on connect |
| 56 |
self.assertEqual(EmailAddress.objects.filter(user=user).count(), 1) |
| 57 |
self.assertEqual(EmailAddress.objects.filter(user=user, email=email).count(), 1) |