| 1 |
import copy |
| 2 |
from http import HTTPStatus |
| 3 |
from unittest.mock import ANY, patch |
| 4 |
|
| 5 |
from django.contrib.auth import get_user_model |
| 6 |
from django.contrib.auth.models import AnonymousUser |
| 7 |
from django.urls import reverse |
| 8 |
|
| 9 |
import pytest |
| 10 |
from pytest_django.asserts import assertTemplateUsed |
| 11 |
|
| 12 |
from allauth.account.authentication import AUTHENTICATION_METHODS_SESSION_KEY |
| 13 |
from allauth.core import context |
| 14 |
from allauth.socialaccount.helpers import complete_social_login |
| 15 |
from allauth.socialaccount.models import SocialAccount, SocialToken |
| 16 |
from allauth.socialaccount.providers.base import AuthProcess |
| 17 |
|
| 18 |
|
| 19 |
@pytest.mark.parametrize("with_emailaddress", [False, True]) |
| 20 |
@pytest.mark.parametrize("auto_connect", [False, True]) |
| 21 |
@pytest.mark.parametrize("setting", ["off", "on-global", "on-provider"]) |
| 22 |
def test_email_authentication( |
| 23 |
db, |
| 24 |
setting, |
| 25 |
settings, |
| 26 |
user_factory, |
| 27 |
sociallogin_factory, |
| 28 |
client, |
| 29 |
request_factory, |
| 30 |
mailoutbox, |
| 31 |
auto_connect, |
| 32 |
with_emailaddress, |
| 33 |
): |
| 34 |
"""Tests that when an already existing email is given at the social signup |
| 35 |
form, enumeration preventation kicks in. |
| 36 |
""" |
| 37 |
settings.ACCOUNT_EMAIL_REQUIRED = True |
| 38 |
settings.ACCOUNT_UNIQUE_EMAIL = True |
| 39 |
settings.ACCOUNT_USERNAME_REQUIRED = False |
| 40 |
settings.ACCOUNT_LOGIN_METHODS = {"email"} |
| 41 |
settings.ACCOUNT_EMAIL_VERIFICATION = "mandatory" |
| 42 |
settings.SOCIALACCOUNT_AUTO_SIGNUP = True |
| 43 |
settings.SOCIALACCOUNT_STORE_TOKENS = True |
| 44 |
if setting == "on-global": |
| 45 |
settings.SOCIALACCOUNT_EMAIL_AUTHENTICATION = True |
| 46 |
elif setting == "on-provider": |
| 47 |
settings.SOCIALACCOUNT_PROVIDERS = copy.deepcopy( |
| 48 |
settings.SOCIALACCOUNT_PROVIDERS |
| 49 |
) |
| 50 |
settings.SOCIALACCOUNT_PROVIDERS["openid_connect"][ |
| 51 |
"EMAIL_AUTHENTICATION" |
| 52 |
] = True |
| 53 |
else: |
| 54 |
settings.SOCIALACCOUNT_EMAIL_AUTHENTICATION = False |
| 55 |
settings.SOCIALACCOUNT_EMAIL_AUTHENTICATION_AUTO_CONNECT = auto_connect |
| 56 |
|
| 57 |
user = user_factory(with_emailaddress=with_emailaddress) |
| 58 |
assert user.has_usable_password() |
| 59 |
|
| 60 |
sociallogin = sociallogin_factory( |
| 61 |
email=user.email, provider="unittest-server", with_token=True |
| 62 |
) |
| 63 |
|
| 64 |
request = request_factory.get("/") |
| 65 |
request.user = AnonymousUser() |
| 66 |
with context.request_context(request): |
| 67 |
with patch( |
| 68 |
"allauth.socialaccount.signals.social_account_updated.send" |
| 69 |
) as updated_signal: |
| 70 |
with patch( |
| 71 |
"allauth.socialaccount.signals.social_account_added.send" |
| 72 |
) as added_signal: |
| 73 |
resp = complete_social_login(request, sociallogin) |
| 74 |
user.refresh_from_db() |
| 75 |
if setting == "off": |
| 76 |
assert resp["location"] == reverse("account_email_verification_sent") |
| 77 |
assert not added_signal.called |
| 78 |
assert not updated_signal.called |
| 79 |
else: |
| 80 |
if with_emailaddress: |
| 81 |
assert resp["location"] == "/accounts/profile/" |
| 82 |
assert user.has_usable_password() |
| 83 |
else: |
| 84 |
assert not user.has_usable_password() |
| 85 |
# This should be improved. The provider vouches for the fact that |
| 86 |
# the user verified the email, so we can mark it as such locally as |
| 87 |
# well. |
| 88 |
|
| 89 |
# user.email is set, but not verified. |
| 90 |
assert resp["location"] == reverse("account_email_verification_sent") |
| 91 |
assert get_user_model().objects.count() == 1 |
| 92 |
assert SocialAccount.objects.filter(user=user.pk).exists() == auto_connect |
| 93 |
assert ( |
| 94 |
SocialToken.objects.filter(account__user=user.pk).exists() == auto_connect |
| 95 |
) |
| 96 |
assert added_signal.called == auto_connect |
| 97 |
assert not updated_signal.called |
| 98 |
|
| 99 |
|
| 100 |
def test_login_cancelled(client, db): |
| 101 |
resp = client.get(reverse("socialaccount_login_cancelled")) |
| 102 |
assert resp.status_code == HTTPStatus.OK |
| 103 |
assertTemplateUsed(resp, "socialaccount/login_cancelled.html") |
| 104 |
|
| 105 |
|
| 106 |
@pytest.mark.parametrize("store_tokens", [False, True]) |
| 107 |
@pytest.mark.parametrize( |
| 108 |
"process,did_record", |
| 109 |
[ |
| 110 |
(AuthProcess.LOGIN, True), |
| 111 |
(AuthProcess.CONNECT, False), |
| 112 |
], |
| 113 |
) |
| 114 |
def test_record_authentication( |
| 115 |
db, |
| 116 |
sociallogin_factory, |
| 117 |
client, |
| 118 |
request_factory, |
| 119 |
user, |
| 120 |
process, |
| 121 |
did_record, |
| 122 |
store_tokens, |
| 123 |
settings, |
| 124 |
): |
| 125 |
settings.SOCIALACCOUNT_STORE_TOKENS = store_tokens |
| 126 |
sociallogin = sociallogin_factory(provider="unittest-server", uid="123") |
| 127 |
sociallogin.state["process"] = process |
| 128 |
sociallogin.token = SocialToken( |
| 129 |
app=sociallogin.provider.app, token="123", token_secret="456" |
| 130 |
) |
| 131 |
SocialAccount.objects.create(user=user, uid="123", provider="unittest-server") |
| 132 |
request = request_factory.get("/") |
| 133 |
request.user = AnonymousUser() |
| 134 |
with context.request_context(request): |
| 135 |
complete_social_login(request, sociallogin) |
| 136 |
if did_record: |
| 137 |
assert request.session[AUTHENTICATION_METHODS_SESSION_KEY] == [ |
| 138 |
{ |
| 139 |
"at": ANY, |
| 140 |
"provider": sociallogin.account.provider, |
| 141 |
"method": "socialaccount", |
| 142 |
"uid": "123", |
| 143 |
} |
| 144 |
] |
| 145 |
else: |
| 146 |
assert AUTHENTICATION_METHODS_SESSION_KEY not in request.session |
| 147 |
assert ( |
| 148 |
SocialToken.objects.filter( |
| 149 |
account__uid="123", token="123", token_secret="456" |
| 150 |
).exists() |
| 151 |
== store_tokens |
| 152 |
) |