1- import secrets
2-
31import pytest
42from django .contrib .auth import get_user_model
53from django .test import Client
108User = get_user_model ()
119
1210
11+ TEST_PASSWORD = "test-password-123!Aa" # pragma: allowlist secret
12+
13+
1314@pytest .fixture
1415def status_new (db ):
1516 return Status .objects .create (name = "new" )
1617
1718
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-
2419@pytest .fixture
2520def users (db ):
26- password = _make_test_password ("user" )
2721 u1 = User .objects .create_user (
28- username = "alice" , password = password , first_name = "Alice" , last_name = "A"
22+ username = "alice" , password = TEST_PASSWORD , first_name = "Alice" , last_name = "A"
2923 )
3024 u2 = User .objects .create_user (
31- username = "bob" , password = password , first_name = "Bob" , last_name = "B"
25+ username = "bob" , password = TEST_PASSWORD , first_name = "Bob" , last_name = "B"
3226 )
33- return {"alice" : u1 , "bob" : u2 , "plain_password" : password }
27+ return {"alice" : u1 , "bob" : u2 , "plain_password" : TEST_PASSWORD }
3428
3529
3630@pytest .fixture
@@ -93,13 +87,12 @@ def test_logout_view_logs_user_out(auth_client):
9387
9488@pytest .mark .django_db
9589def test_registration_post_creates_user (client ):
96- password = _make_test_password ("register" )
9790 data = {
9891 "username" : "charlie" ,
9992 "first_name" : "Charlie" ,
10093 "last_name" : "C" ,
101- "password1" : password ,
102- "password2" : password ,
94+ "password1" : TEST_PASSWORD ,
95+ "password2" : TEST_PASSWORD ,
10396 }
10497 r = client .post (reverse ("users:create" ), data = data )
10598 assert r .status_code in (302 , 301 )
@@ -139,7 +132,7 @@ def test_user_can_update_self(auth_client, users):
139132@pytest .mark .django_db
140133def test_user_can_update_password (auth_client , users ):
141134 url = reverse ("users:update" , args = [users ["alice" ].pk ])
142- new_password = _make_test_password ( "updated" )
135+ new_password = "updated-password-123!Aa" # pragma: allowlist secret
143136 response = auth_client .post (
144137 url ,
145138 data = {
@@ -163,7 +156,7 @@ def test_user_can_update_password(auth_client, users):
163156@pytest .mark .django_db
164157def test_user_update_requires_both_password_fields (auth_client , users ):
165158 url = reverse ("users:update" , args = [users ["alice" ].pk ])
166- password_one = _make_test_password ( "only-once" )
159+ password_one = "only-once-password-123!Aa" # pragma: allowlist secret
167160 response = auth_client .post (
168161 url ,
169162 data = {
@@ -183,8 +176,8 @@ def test_user_update_requires_both_password_fields(auth_client, users):
183176@pytest .mark .django_db
184177def test_user_update_password_mismatch (auth_client , users ):
185178 url = reverse ("users:update" , args = [users ["alice" ].pk ])
186- first_password = _make_test_password ( "mismatch-1" )
187- second_password = _make_test_password ( "mismatch-2" )
179+ first_password = "mismatch-password-1-123!Aa" # pragma: allowlist secret
180+ second_password = "mismatch-password-2-123!Aa" # pragma: allowlist secret
188181 response = auth_client .post (
189182 url ,
190183 data = {
@@ -251,7 +244,7 @@ def test_only_author_can_delete(auth_client, users, status_new):
251244 assert Task .objects .filter (pk = t .pk ).exists ()
252245
253246 c = Client ()
254- c .login (username = "bob" , password = users ["password " ])
247+ c .login (username = "bob" , password = users ["plain_password " ])
255248 r2 = c .post (reverse ("tasks:delete" , args = [t .pk ]))
256249 assert r2 .status_code in (302 , 301 )
257250 assert not Task .objects .filter (pk = t .pk ).exists ()
0 commit comments