Skip to content

Commit b31c2eb

Browse files
committed
Check validity of email contents for password reset requests tests
1 parent 12a6c80 commit b31c2eb

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

packages/hidp/tests/unit_tests/test_api/test_passwords.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def test_password_reset_request_valid_email(self):
2424
Verify behaviour when a valid email is provided.
2525
2626
- A password reset mail is sent
27+
- The correct password reset mail is sent with the correct URL
2728
- The response status code is 204 No Content
2829
- The response is empty
2930
"""
@@ -37,9 +38,19 @@ def test_password_reset_request_valid_email(self):
3738
},
3839
)
3940

40-
self.assertEqual(response.status_code, HTTPStatus.NO_CONTENT)
4141
self.assertEqual(len(mail.outbox), 1)
42-
self.assertEqual("Reset your password", mail.outbox[0].subject)
42+
email = mail.outbox[0]
43+
self.assertEqual("Reset your password", email.subject)
44+
self.assertEqual(email.to, [user.email])
45+
uidb64 = urlsafe_base64_encode(force_bytes(user.pk))
46+
self.assertRegex(
47+
email.body,
48+
# Matches the password reset URL:
49+
# password_reset_url/MDE5MTkyY2UtODE0Yy03NjNlLTlhMGUtMmM1ODk3MGNkYTFj/cced4c-9a0766ea185039a6d293ff660c04007e/
50+
r"password_reset_url/{uidb64}/[0-9a-z]+-[0-9a-f]+/".format(uidb64=uidb64),
51+
)
52+
53+
self.assertEqual(response.status_code, HTTPStatus.NO_CONTENT)
4354
self.assertIsNone(response.data)
4455

4556
mail.outbox = []
@@ -54,9 +65,13 @@ def test_password_reset_request_valid_email(self):
5465
},
5566
)
5667

57-
self.assertEqual(response.status_code, HTTPStatus.NO_CONTENT)
5868
self.assertEqual(len(mail.outbox), 1)
59-
self.assertEqual("Set a password", mail.outbox[0].subject)
69+
email = mail.outbox[0]
70+
self.assertEqual("Set a password", email.subject)
71+
self.assertEqual(email.to, [user.email])
72+
self.assertIn("set_password_url/", email.body)
73+
74+
self.assertEqual(response.status_code, HTTPStatus.NO_CONTENT)
6075
self.assertIsNone(response.data)
6176

6277
def test_password_reset_request_invalid_email(self):
@@ -149,6 +164,7 @@ def test_password_reset_confirmation_valid(self):
149164
Verify behaviour when a valid token, password and user ID are provided.
150165
151166
- The user's password is updated
167+
- A changed password email is sent
152168
- The session hash of the initiating session has been updated
153169
- Other sessions are no longer valid and contain the old session hash
154170
- The response status code is 204 No Content
@@ -176,6 +192,12 @@ def test_password_reset_confirmation_valid(self):
176192
self.verified_user.refresh_from_db()
177193
self.assertTrue(self.verified_user.check_password(new_password))
178194

195+
self.assertEqual(len(mail.outbox), 1)
196+
email = mail.outbox[0]
197+
self.assertEqual("Your password has been changed", email.subject)
198+
self.assertEqual(email.to, [self.verified_user.email])
199+
self.assertIn("password_changed_url/", email.body)
200+
179201
# Session that initiated the password change no longer has the old session hash
180202
self.assertNotEqual(
181203
self.client.session.get("_auth_user_hash"), pre_password_change_session_hash

0 commit comments

Comments
 (0)