| 1 |
from http import HTTPStatus |
| 2 |
from unittest.mock import patch |
| 3 |
|
| 4 |
from django.urls import reverse |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from allauth.account.models import EmailAddress |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture(autouse=True) |
| 12 |
def prbc_settings(settings_impacting_urls): |
| 13 |
with settings_impacting_urls(ACCOUNT_PASSWORD_RESET_BY_CODE_ENABLED=True): |
| 14 |
yield |
| 15 |
|
| 16 |
|
| 17 |
@pytest.mark.parametrize("login_on_password_reset", [False, True]) |
| 18 |
def test_flow( |
| 19 |
user, |
| 20 |
client, |
| 21 |
mailoutbox, |
| 22 |
get_last_password_reset_code, |
| 23 |
password_factory, |
| 24 |
login_on_password_reset, |
| 25 |
settings, |
| 26 |
): |
| 27 |
settings.ACCOUNT_LOGIN_ON_PASSWORD_RESET = login_on_password_reset |
| 28 |
new_password = password_factory() |
| 29 |
assert not user.check_password(new_password) |
| 30 |
resp = client.post(reverse("account_reset_password"), {"email": user.email}) |
| 31 |
assert resp.status_code == HTTPStatus.FOUND |
| 32 |
assert resp["location"] == reverse("account_confirm_password_reset_code") |
| 33 |
|
| 34 |
# Test wrong code |
| 35 |
with patch( |
| 36 |
"allauth.account.signals.password_reset_code_rejected" |
| 37 |
) as code_rejected_signal: |
| 38 |
resp = client.post( |
| 39 |
reverse("account_confirm_password_reset_code"), |
| 40 |
{"code": "WRONG"}, |
| 41 |
) |
| 42 |
assert resp.status_code == HTTPStatus.OK |
| 43 |
assert code_rejected_signal.send.called |
| 44 |
signal_kwargs = code_rejected_signal.send.call_args[1] |
| 45 |
assert signal_kwargs["user"] == user |
| 46 |
assert signal_kwargs["last_attempt"] is False |
| 47 |
|
| 48 |
# Test right code |
| 49 |
resp = client.post( |
| 50 |
reverse("account_confirm_password_reset_code"), |
| 51 |
{"code": get_last_password_reset_code(client, mailoutbox)}, |
| 52 |
) |
| 53 |
assert resp.status_code == HTTPStatus.FOUND |
| 54 |
assert resp["location"] == reverse("account_complete_password_reset") |
| 55 |
resp = client.post( |
| 56 |
reverse("account_complete_password_reset"), |
| 57 |
{"password1": new_password, "password2": new_password}, |
| 58 |
) |
| 59 |
assert resp.status_code == HTTPStatus.FOUND |
| 60 |
assert ( |
| 61 |
resp["location"] == settings.LOGIN_REDIRECT_URL |
| 62 |
if login_on_password_reset |
| 63 |
else reverse("account_password_reset_completed") |
| 64 |
) |
| 65 |
user.refresh_from_db() |
| 66 |
assert user.check_password(new_password) |
| 67 |
|
| 68 |
|
| 69 |
def test_prevent_enumeration(db, client, mailoutbox): |
| 70 |
resp = client.post( |
| 71 |
reverse("account_reset_password"), {"email": "[email protected]"} |
| 72 |
) |
| 73 |
assert resp.status_code == HTTPStatus.FOUND |
| 74 |
assert resp["location"] == reverse("account_confirm_password_reset_code") |
| 75 |
assert mailoutbox[0].subject == "[example.com] Unknown Account" |
| 76 |
resp = client.post( |
| 77 |
reverse("account_confirm_password_reset_code"), |
| 78 |
{"code": "none?"}, |
| 79 |
) |
| 80 |
assert resp.status_code == HTTPStatus.OK |
| 81 |
|
| 82 |
|
| 83 |
def test_indirect_email_verification( |
| 84 |
db, client, mailoutbox, user_factory, get_last_password_reset_code |
| 85 |
): |
| 86 |
user = user_factory(email_verified=False) |
| 87 |
address = EmailAddress.objects.get(user=user, email=user.email, verified=False) |
| 88 |
resp = client.post(reverse("account_reset_password"), {"email": address.email}) |
| 89 |
assert resp.status_code == HTTPStatus.FOUND |
| 90 |
resp = client.post( |
| 91 |
reverse("account_confirm_password_reset_code"), |
| 92 |
{"code": get_last_password_reset_code(client, mailoutbox)}, |
| 93 |
) |
| 94 |
assert resp.status_code == HTTPStatus.FOUND |
| 95 |
address.refresh_from_db() |
| 96 |
assert address.verified |