Skip to content

Commit 943e746

Browse files
committed
test: cover user password update scenarios
1 parent ce04ff7 commit 943e746

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

task_manager/users/tests.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,68 @@ def test_user_can_update_self(auth_client, users):
9191
assert users["alice"].first_name == "Al"
9292

9393

94+
@pytest.mark.django_db
95+
def test_user_can_update_password(auth_client, users):
96+
url = reverse("users:update", args=[users["alice"].pk])
97+
new_password = "Secur3Pass!234"
98+
response = auth_client.post(
99+
url,
100+
data={
101+
"username": "alice",
102+
"first_name": "Alice",
103+
"last_name": "A",
104+
"password1": new_password,
105+
"password2": new_password,
106+
},
107+
)
108+
109+
assert response.status_code in (302, 301)
110+
111+
users["alice"].refresh_from_db()
112+
assert users["alice"].check_password(new_password)
113+
114+
fresh_client = Client()
115+
assert fresh_client.login(username="alice", password=new_password)
116+
117+
118+
@pytest.mark.django_db
119+
def test_user_update_requires_both_password_fields(auth_client, users):
120+
url = reverse("users:update", args=[users["alice"].pk])
121+
response = auth_client.post(
122+
url,
123+
data={
124+
"username": "alice",
125+
"first_name": "Alice",
126+
"last_name": "A",
127+
"password1": "OnlyOnce123!",
128+
"password2": "",
129+
},
130+
)
131+
132+
assert response.status_code == 200
133+
html = response.content.decode()
134+
assert "Please enter the password twice." in html
135+
136+
137+
@pytest.mark.django_db
138+
def test_user_update_password_mismatch(auth_client, users):
139+
url = reverse("users:update", args=[users["alice"].pk])
140+
response = auth_client.post(
141+
url,
142+
data={
143+
"username": "alice",
144+
"first_name": "Alice",
145+
"last_name": "A",
146+
"password1": "Mismatch123!",
147+
"password2": "Mismatch321!",
148+
},
149+
)
150+
151+
assert response.status_code == 200
152+
html = response.content.decode()
153+
assert "The entered passwords do not match." in html
154+
155+
94156
@pytest.mark.django_db
95157
def test_user_cannot_update_other(auth_client, users):
96158
url = reverse("users:update", args=[users["bob"].pk])

0 commit comments

Comments
 (0)