Skip to content

Commit 368200f

Browse files
committed
Generate test passwords dynamically
1 parent 7ecb8d6 commit 368200f

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

task_manager/users/tests.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import secrets
2+
13
import pytest
24
from django.contrib.auth import get_user_model
35
from django.test import Client
@@ -13,22 +15,28 @@ def status_new(db):
1315
return Status.objects.create(name="new")
1416

1517

18+
def _make_test_password(label: str) -> str:
19+
"""Generate a unique, complex password for tests without hardcoding secrets."""
20+
21+
return f"{label}-{secrets.token_urlsafe(8)}!Aa1"
22+
23+
1624
@pytest.fixture
1725
def users(db):
18-
password = "user-pass-for-tests"
26+
password = _make_test_password("user")
1927
u1 = User.objects.create_user(
2028
username="alice", password=password, first_name="Alice", last_name="A"
2129
)
2230
u2 = User.objects.create_user(
2331
username="bob", password=password, first_name="Bob", last_name="B"
2432
)
25-
return {"alice": u1, "bob": u2, "password": password}
33+
return {"alice": u1, "bob": u2, "plain_password": password}
2634

2735

2836
@pytest.fixture
2937
def auth_client(users):
3038
c = Client()
31-
c.login(username="alice", password=users["password"])
39+
c.login(username="alice", password=users["plain_password"])
3240
return c
3341

3442

@@ -85,12 +93,13 @@ def test_logout_view_logs_user_out(auth_client):
8593

8694
@pytest.mark.django_db
8795
def test_registration_post_creates_user(client):
96+
password = _make_test_password("register")
8897
data = {
8998
"username": "charlie",
9099
"first_name": "Charlie",
91100
"last_name": "C",
92-
"password1": "test-pass-123!",
93-
"password2": "test-pass-123!",
101+
"password1": password,
102+
"password2": password,
94103
}
95104
r = client.post(reverse("users:create"), data=data)
96105
assert r.status_code in (302, 301)
@@ -130,7 +139,7 @@ def test_user_can_update_self(auth_client, users):
130139
@pytest.mark.django_db
131140
def test_user_can_update_password(auth_client, users):
132141
url = reverse("users:update", args=[users["alice"].pk])
133-
new_password = "strong-pass-for-tests!1"
142+
new_password = _make_test_password("updated")
134143
response = auth_client.post(
135144
url,
136145
data={
@@ -154,13 +163,14 @@ def test_user_can_update_password(auth_client, users):
154163
@pytest.mark.django_db
155164
def test_user_update_requires_both_password_fields(auth_client, users):
156165
url = reverse("users:update", args=[users["alice"].pk])
166+
password_one = _make_test_password("only-once")
157167
response = auth_client.post(
158168
url,
159169
data={
160170
"username": "alice",
161171
"first_name": "Alice",
162172
"last_name": "A",
163-
"password1": "OnlyOnce!",
173+
"password1": password_one,
164174
"password2": "",
165175
},
166176
)
@@ -173,14 +183,16 @@ def test_user_update_requires_both_password_fields(auth_client, users):
173183
@pytest.mark.django_db
174184
def test_user_update_password_mismatch(auth_client, users):
175185
url = reverse("users:update", args=[users["alice"].pk])
186+
first_password = _make_test_password("mismatch-1")
187+
second_password = _make_test_password("mismatch-2")
176188
response = auth_client.post(
177189
url,
178190
data={
179191
"username": "alice",
180192
"first_name": "Alice",
181193
"last_name": "A",
182-
"password1": "MismatchPass!1",
183-
"password2": "MismatchPass!2",
194+
"password1": first_password,
195+
"password2": second_password,
184196
},
185197
)
186198

@@ -209,7 +221,7 @@ def test_delete_requires_auth_redirects(client, users):
209221
@pytest.mark.django_db
210222
def test_user_can_delete_self(users):
211223
c = Client()
212-
c.login(username="bob", password=users["password"])
224+
c.login(username="bob", password=users["plain_password"])
213225
url = reverse("users:delete", args=[users["bob"].pk])
214226
r_get = c.get(url)
215227
assert r_get.status_code == 200
@@ -255,7 +267,7 @@ def test_user_with_tasks_cannot_be_deleted(users, status_new):
255267
)
256268

257269
c = Client()
258-
c.login(username="bob", password=users["password"])
270+
c.login(username="bob", password=users["plain_password"])
259271
url = reverse("users:delete", args=[users["bob"].pk])
260272
r = c.post(url)
261273
assert r.status_code in (302, 301)

0 commit comments

Comments
 (0)