@@ -51,6 +51,38 @@ def test_registration_get(client):
5151 assert 'name="password2"' in html
5252
5353
54+ @pytest .mark .django_db
55+ def test_registration_form_english_texts (client ):
56+ response = client .get (reverse ("users:create" ))
57+ assert response .status_code == 200
58+
59+ html = response .content .decode ()
60+ assert "First name" in html
61+ assert "Last name" in html
62+ assert "Password confirmation" in html
63+
64+
65+ @pytest .mark .django_db
66+ def test_login_page_uses_custom_authentication_form (client ):
67+ response = client .get (reverse ("login" ))
68+ assert response .status_code == 200
69+
70+ html = response .content .decode ()
71+ assert "Log in" in html
72+ assert "Username" in html
73+ assert "Password" in html
74+
75+
76+ @pytest .mark .django_db
77+ def test_logout_view_logs_user_out (auth_client ):
78+ response = auth_client .post (reverse ("logout" ))
79+ assert response .status_code in (302 , 301 )
80+
81+ # The logout view should end the session, so following requests must be anonymous
82+ follow_up = auth_client .get (reverse ("home" ))
83+ assert follow_up .wsgi_request .user .is_anonymous
84+
85+
5486@pytest .mark .django_db
5587def test_registration_post_creates_user (client ):
5688 data = {
@@ -79,6 +111,10 @@ def test_user_can_update_self(auth_client, users):
79111 url = reverse ("users:update" , args = [users ["alice" ].pk ])
80112 r_get = auth_client .get (url )
81113 assert r_get .status_code == 200
114+ html = r_get .content .decode ()
115+ assert "Password" in html
116+ assert "Password confirmation" in html
117+ assert "Please enter the password again for confirmation." in html
82118
83119 r_post = auth_client .post (
84120 url ,
@@ -91,6 +127,68 @@ def test_user_can_update_self(auth_client, users):
91127 assert users ["alice" ].first_name == "Al"
92128
93129
130+ @pytest .mark .django_db
131+ def test_user_can_update_password (auth_client , users ):
132+ url = reverse ("users:update" , args = [users ["alice" ].pk ])
133+ new_password = "Secur3Pass!234"
134+ response = auth_client .post (
135+ url ,
136+ data = {
137+ "username" : "alice" ,
138+ "first_name" : "Alice" ,
139+ "last_name" : "A" ,
140+ "password1" : new_password ,
141+ "password2" : new_password ,
142+ },
143+ )
144+
145+ assert response .status_code in (302 , 301 )
146+
147+ users ["alice" ].refresh_from_db ()
148+ assert users ["alice" ].check_password (new_password )
149+
150+ fresh_client = Client ()
151+ assert fresh_client .login (username = "alice" , password = new_password )
152+
153+
154+ @pytest .mark .django_db
155+ def test_user_update_requires_both_password_fields (auth_client , users ):
156+ url = reverse ("users:update" , args = [users ["alice" ].pk ])
157+ response = auth_client .post (
158+ url ,
159+ data = {
160+ "username" : "alice" ,
161+ "first_name" : "Alice" ,
162+ "last_name" : "A" ,
163+ "password1" : "OnlyOnce123!" ,
164+ "password2" : "" ,
165+ },
166+ )
167+
168+ assert response .status_code == 200
169+ html = response .content .decode ()
170+ assert "Please enter the password twice." in html
171+
172+
173+ @pytest .mark .django_db
174+ def test_user_update_password_mismatch (auth_client , users ):
175+ url = reverse ("users:update" , args = [users ["alice" ].pk ])
176+ response = auth_client .post (
177+ url ,
178+ data = {
179+ "username" : "alice" ,
180+ "first_name" : "Alice" ,
181+ "last_name" : "A" ,
182+ "password1" : "Mismatch123!" ,
183+ "password2" : "Mismatch321!" ,
184+ },
185+ )
186+
187+ assert response .status_code == 200
188+ html = response .content .decode ()
189+ assert "The entered passwords do not match." in html
190+
191+
94192@pytest .mark .django_db
95193def test_user_cannot_update_other (auth_client , users ):
96194 url = reverse ("users:update" , args = [users ["bob" ].pk ])
0 commit comments