| 1 |
from http import HTTPStatus |
| 2 |
from unittest.mock import patch |
| 3 |
|
| 4 |
from django.urls import NoReverseMatch, reverse, reverse_lazy |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from allauth.account.forms import ChangePasswordForm |
| 9 |
|
| 10 |
|
| 11 |
def test_change_unusable_password_redirects_to_set(client, user, user_password): |
| 12 |
user.set_unusable_password() |
| 13 |
user.save() |
| 14 |
client.force_login(user) |
| 15 |
resp = client.get(reverse("account_change_password")) |
| 16 |
assert resp.status_code == HTTPStatus.FOUND |
| 17 |
assert resp["location"] == reverse("account_set_password") |
| 18 |
|
| 19 |
|
| 20 |
def test_set_usable_password_redirects_to_change(auth_client, user): |
| 21 |
resp = auth_client.get(reverse("account_set_password")) |
| 22 |
assert resp.status_code == HTTPStatus.FOUND |
| 23 |
assert resp["location"] == reverse("account_change_password") |
| 24 |
|
| 25 |
|
| 26 |
@pytest.mark.parametrize( |
| 27 |
"logout,next_url,redirect_chain", |
| 28 |
[ |
| 29 |
(False, "", [(reverse_lazy("account_change_password"), HTTPStatus.FOUND)]), |
| 30 |
(False, "/foo", [("/foo", HTTPStatus.FOUND)]), |
| 31 |
( |
| 32 |
True, |
| 33 |
"", |
| 34 |
[ |
| 35 |
(reverse_lazy("account_change_password"), HTTPStatus.FOUND), |
| 36 |
( |
| 37 |
"/accounts/login/?next=/accounts/password/change/", |
| 38 |
HTTPStatus.FOUND, |
| 39 |
), |
| 40 |
], |
| 41 |
), |
| 42 |
(True, "/foo", [("/foo", HTTPStatus.FOUND)]), |
| 43 |
], |
| 44 |
) |
| 45 |
def test_set_password( |
| 46 |
client, user, next_url, password_factory, logout, settings, redirect_chain |
| 47 |
): |
| 48 |
settings.ACCOUNT_LOGOUT_ON_PASSWORD_CHANGE = logout |
| 49 |
user.set_unusable_password() |
| 50 |
user.save() |
| 51 |
client.force_login(user) |
| 52 |
password = password_factory() |
| 53 |
data = {"password1": password, "password2": password} |
| 54 |
if next_url: |
| 55 |
data["next"] = next_url |
| 56 |
resp = client.post( |
| 57 |
reverse("account_set_password"), |
| 58 |
data, |
| 59 |
follow=True, |
| 60 |
) |
| 61 |
assert resp.redirect_chain == redirect_chain |
| 62 |
|
| 63 |
|
| 64 |
@pytest.mark.parametrize( |
| 65 |
"logout,next_url,redirect_chain", |
| 66 |
[ |
| 67 |
(False, "", [(reverse_lazy("account_change_password"), HTTPStatus.FOUND)]), |
| 68 |
(False, "/foo", [("/foo", HTTPStatus.FOUND)]), |
| 69 |
( |
| 70 |
True, |
| 71 |
"", |
| 72 |
[ |
| 73 |
(reverse_lazy("account_change_password"), HTTPStatus.FOUND), |
| 74 |
( |
| 75 |
"/accounts/login/?next=/accounts/password/change/", |
| 76 |
HTTPStatus.FOUND, |
| 77 |
), |
| 78 |
], |
| 79 |
), |
| 80 |
(True, "/foo", [("/foo", HTTPStatus.FOUND)]), |
| 81 |
], |
| 82 |
) |
| 83 |
def test_change_password( |
| 84 |
auth_client, |
| 85 |
user, |
| 86 |
user_password, |
| 87 |
next_url, |
| 88 |
password_factory, |
| 89 |
logout, |
| 90 |
settings, |
| 91 |
redirect_chain, |
| 92 |
mailoutbox, |
| 93 |
): |
| 94 |
settings.ACCOUNT_LOGOUT_ON_PASSWORD_CHANGE = logout |
| 95 |
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True |
| 96 |
password = password_factory() |
| 97 |
data = {"oldpassword": user_password, "password1": password, "password2": password} |
| 98 |
if next_url: |
| 99 |
data["next"] = next_url |
| 100 |
resp = auth_client.post( |
| 101 |
reverse("account_change_password"), |
| 102 |
data, |
| 103 |
follow=True, |
| 104 |
) |
| 105 |
assert resp.redirect_chain == redirect_chain |
| 106 |
assert len(mailoutbox) == 1 |
| 107 |
assert "Your password has been changed" in mailoutbox[0].body |
| 108 |
|
| 109 |
|
| 110 |
def test_change_password_forgotten_link_not_present(client, db): |
| 111 |
with patch("allauth.account.fields.reverse") as reverse_mock: |
| 112 |
reverse_mock.side_effect = NoReverseMatch |
| 113 |
form = ChangePasswordForm() |
| 114 |
assert form.fields["oldpassword"].help_text == "" |
| 115 |
|
| 116 |
|
| 117 |
def test_change_password_forgotten_link_present(client, db): |
| 118 |
form = ChangePasswordForm() |
| 119 |
assert ( |
| 120 |
form.fields["oldpassword"].help_text |
| 121 |
== '<a href="/accounts/password/reset/">Forgot your password?</a>' |
| 122 |
) |