| 1 |
from http import HTTPStatus |
| 2 |
from unittest.mock import patch |
| 3 |
|
| 4 |
from django.urls import reverse |
| 5 |
|
| 6 |
import pytest |
| 7 |
from pytest_django.asserts import assertTemplateUsed |
| 8 |
|
| 9 |
from allauth.socialaccount.internal import flows |
| 10 |
from allauth.socialaccount.models import SocialAccount |
| 11 |
from allauth.socialaccount.providers.base.constants import AuthProcess |
| 12 |
|
| 13 |
|
| 14 |
@pytest.mark.parametrize("reauthentication_required", [False, True]) |
| 15 |
def test_disconnect(auth_client, user, settings, mailoutbox, reauthentication_required): |
| 16 |
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True |
| 17 |
settings.ACCOUNT_REAUTHENTICATION_REQUIRED = reauthentication_required |
| 18 |
account_to_del = SocialAccount.objects.create( |
| 19 |
uid="123", provider="other-server", user=user |
| 20 |
) |
| 21 |
account_to_keep = SocialAccount.objects.create( |
| 22 |
uid="456", provider="other-server", user=user |
| 23 |
) |
| 24 |
resp = auth_client.get(reverse("socialaccount_connections")) |
| 25 |
assertTemplateUsed(resp, "socialaccount/connections.html") |
| 26 |
resp = auth_client.post( |
| 27 |
reverse("socialaccount_connections"), {"account": account_to_del.pk} |
| 28 |
) |
| 29 |
if reauthentication_required: |
| 30 |
assert SocialAccount.objects.filter(pk=account_to_del.pk).exists() |
| 31 |
assert SocialAccount.objects.filter(pk=account_to_keep.pk).exists() |
| 32 |
else: |
| 33 |
assert not SocialAccount.objects.filter(pk=account_to_del.pk).exists() |
| 34 |
assert SocialAccount.objects.filter(pk=account_to_keep.pk).exists() |
| 35 |
assert len(mailoutbox) == 1 |
| 36 |
assert mailoutbox[0].subject == "[example.com] Third-Party Account Disconnected" |
| 37 |
|
| 38 |
|
| 39 |
def test_connect_with_reauthentication( |
| 40 |
auth_client, user, provider_callback_response, settings, user_password |
| 41 |
): |
| 42 |
settings.ACCOUNT_REAUTHENTICATION_REQUIRED = True |
| 43 |
resp = provider_callback_response(auth_client, process="connect") |
| 44 |
assert not SocialAccount.objects.filter(user=user).exists() |
| 45 |
assert resp.status_code == HTTPStatus.FOUND |
| 46 |
assert resp["location"] == reverse("account_reauthenticate") |
| 47 |
resp = auth_client.post(resp["location"], {"password": user_password}) |
| 48 |
assert resp.status_code == HTTPStatus.FOUND |
| 49 |
assert resp["location"] == reverse("socialaccount_connections") |
| 50 |
assert SocialAccount.objects.filter(user=user).exists() |
| 51 |
|
| 52 |
|
| 53 |
def test_connect( |
| 54 |
auth_client, user, provider_callback_response, settings, user_password, mailoutbox |
| 55 |
): |
| 56 |
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True |
| 57 |
settings.ACCOUNT_REAUTHENTICATION_REQUIRED = False |
| 58 |
resp = provider_callback_response(auth_client, process="connect") |
| 59 |
assert resp.status_code == HTTPStatus.FOUND |
| 60 |
assert SocialAccount.objects.filter(user=user).exists() |
| 61 |
assert resp["location"] == reverse("socialaccount_connections") |
| 62 |
assert len(mailoutbox) == 1 |
| 63 |
assert mailoutbox[0].subject == "[example.com] Third-Party Account Connected" |
| 64 |
|
| 65 |
|
| 66 |
@pytest.mark.parametrize( |
| 67 |
"email_authentication,account_exists, expected_action", |
| 68 |
[ |
| 69 |
(False, False, "added"), |
| 70 |
(False, True, "updated"), |
| 71 |
(True, False, "added"), |
| 72 |
(True, True, "updated"), |
| 73 |
], |
| 74 |
) |
| 75 |
def test_connect_vs_email_authentication( |
| 76 |
request_factory, |
| 77 |
sociallogin_factory, |
| 78 |
user, |
| 79 |
settings, |
| 80 |
email_authentication, |
| 81 |
account_exists, |
| 82 |
expected_action, |
| 83 |
): |
| 84 |
settings.SOCIALACCOUNT_EMAIL_AUTHENTICATION = email_authentication |
| 85 |
sociallogin = sociallogin_factory(email=user.email, provider="unittest-server") |
| 86 |
if account_exists: |
| 87 |
account = sociallogin.account |
| 88 |
account.user = user |
| 89 |
account.save() |
| 90 |
|
| 91 |
sociallogin.state["process"] = AuthProcess.CONNECT |
| 92 |
request = request_factory.get("/") |
| 93 |
request.user = user |
| 94 |
with patch( |
| 95 |
"allauth.account.adapter.DefaultAccountAdapter.add_message" |
| 96 |
) as add_message: |
| 97 |
flows.login.complete_login(request, sociallogin) |
| 98 |
assert add_message.call_args[1]["message_context"]["action"] == expected_action |
| 99 |
assert SocialAccount.objects.filter(user=user, uid=sociallogin.account.uid).exists() |