| 1 |
from http import HTTPStatus |
| 2 |
|
| 3 |
from django.conf import settings |
| 4 |
from django.contrib.auth import get_user_model |
| 5 |
from django.urls import reverse, reverse_lazy |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from allauth.account.adapter import get_adapter as get_account_adapter |
| 10 |
from allauth.socialaccount.models import SocialAccount |
| 11 |
|
| 12 |
|
| 13 |
@pytest.mark.parametrize( |
| 14 |
"phone_verified, phone_valid, phone_taken, expected_url", |
| 15 |
[ |
| 16 |
(True, True, False, settings.LOGIN_REDIRECT_URL), |
| 17 |
(False, True, False, reverse_lazy("account_verify_phone")), |
| 18 |
(True, False, False, reverse_lazy("socialaccount_signup")), |
| 19 |
(True, True, True, reverse_lazy("socialaccount_signup")), |
| 20 |
], |
| 21 |
) |
| 22 |
def test_signup_with_phone( |
| 23 |
db, |
| 24 |
settings_impacting_urls, |
| 25 |
client, |
| 26 |
phone, |
| 27 |
phone_verified, |
| 28 |
phone_valid, |
| 29 |
phone_taken, |
| 30 |
expected_url, |
| 31 |
user_factory, |
| 32 |
): |
| 33 |
if phone_taken: |
| 34 |
user_factory(phone=phone) |
| 35 |
with settings_impacting_urls( |
| 36 |
SOCIALACCOUNT_AUTO_SIGNUP=True, |
| 37 |
ACCOUNT_LOGIN_METHODS=("phone",), |
| 38 |
ACCOUNT_SIGNUP_FIELDS=["phone*"], |
| 39 |
): |
| 40 |
resp = client.post(reverse("dummy_login")) |
| 41 |
assert resp.status_code == HTTPStatus.FOUND |
| 42 |
assert resp["location"].startswith(f"{reverse('dummy_authenticate')}?state=") |
| 43 |
resp = client.post( |
| 44 |
resp["location"], |
| 45 |
{ |
| 46 |
"id": "123", |
| 47 |
"email": "[email protected]", |
| 48 |
"email_verified": True, |
| 49 |
"phone": phone if phone_valid else "*INVALID*", |
| 50 |
"phone_verified": phone_verified, |
| 51 |
}, |
| 52 |
) |
| 53 |
assert resp.status_code == HTTPStatus.FOUND |
| 54 |
assert resp["location"] == expected_url |
| 55 |
if not phone_valid or phone_taken: |
| 56 |
return |
| 57 |
get_user_model().objects.filter(email="[email protected]").exists() |
| 58 |
socialaccount = SocialAccount.objects.get(uid="123") |
| 59 |
account = socialaccount.get_provider_account() |
| 60 |
assert account.to_str() == "[email protected]" |
| 61 |
assert get_account_adapter().get_phone(socialaccount.user) == ( |
| 62 |
phone, |
| 63 |
phone_verified, |
| 64 |
) |