1
0
mirror of https://github.com/django/django.git synced 2025-05-21 22:36:28 +00:00

[4.2.x] Fixed auth_tests and file_storage tests on Python 3.8.

This commit is contained in:
Mariusz Felisiak 2024-07-11 08:03:00 +02:00 committed by Sarah Boyce
parent 8e59e33400
commit c5d196a652
2 changed files with 44 additions and 47 deletions

View File

@ -621,25 +621,26 @@ class TestUtilsHashPass(SimpleTestCase):
("letmein", make_password(password="letmein"), ValueError), # valid encoded ("letmein", make_password(password="letmein"), ValueError), # valid encoded
] ]
for password, encoded, hasher_side_effect in cases: for password, encoded, hasher_side_effect in cases:
with ( with self.subTest(encoded=encoded):
self.subTest(encoded=encoded), with mock.patch(
mock.patch(
"django.contrib.auth.hashers.identify_hasher", "django.contrib.auth.hashers.identify_hasher",
side_effect=hasher_side_effect, side_effect=hasher_side_effect,
) as mock_identify_hasher, ) as mock_identify_hasher:
mock.patch( with mock.patch(
"django.contrib.auth.hashers.make_password" "django.contrib.auth.hashers.make_password"
) as mock_make_password, ) as mock_make_password:
mock.patch( with mock.patch(
"django.contrib.auth.hashers.get_random_string", "django.contrib.auth.hashers.get_random_string",
side_effect=lambda size: "x" * size, side_effect=lambda size: "x" * size,
),
mock.patch.object(hasher, "verify"),
): ):
# Ensure make_password is called to standardize timing. with mock.patch.object(hasher, "verify"):
# make_password() is called to standardize timing.
check_password(password, encoded) check_password(password, encoded)
self.assertEqual(hasher.verify.call_count, 0) self.assertEqual(hasher.verify.call_count, 0)
self.assertEqual(mock_identify_hasher.mock_calls, [mock.call(encoded)]) self.assertEqual(
mock_identify_hasher.mock_calls,
[mock.call(encoded)],
)
self.assertEqual( self.assertEqual(
mock_make_password.mock_calls, mock_make_password.mock_calls,
[mock.call("x" * UNUSABLE_PASSWORD_SUFFIX_LENGTH)], [mock.call("x" * UNUSABLE_PASSWORD_SUFFIX_LENGTH)],

View File

@ -27,11 +27,11 @@ class StorageValidateFileNameTests(SimpleTestCase):
s = CustomStorage() s = CustomStorage()
# The initial name passed to `save` is not valid nor safe, fail early. # The initial name passed to `save` is not valid nor safe, fail early.
for name in self.invalid_file_names: for name in self.invalid_file_names:
with ( with self.subTest(name=name):
self.subTest(name=name), with mock.patch.object(
mock.patch.object(s, "get_available_name") as mock_get_available_name, s, "get_available_name"
mock.patch.object(s, "_save") as mock_internal_save, ) as mock_get_available_name:
): with mock.patch.object(s, "_save") as mock_internal_save:
with self.assertRaisesMessage( with self.assertRaisesMessage(
SuspiciousFileOperation, self.error_msg % name SuspiciousFileOperation, self.error_msg % name
): ):
@ -44,11 +44,9 @@ class StorageValidateFileNameTests(SimpleTestCase):
# The initial name passed to `save` is valid and safe, but the returned # The initial name passed to `save` is valid and safe, but the returned
# name from `get_available_name` is not. # name from `get_available_name` is not.
for name in self.invalid_file_names: for name in self.invalid_file_names:
with ( with self.subTest(name=name):
self.subTest(name=name), with mock.patch.object(s, "get_available_name", return_value=name):
mock.patch.object(s, "get_available_name", return_value=name), with mock.patch.object(s, "_save") as mock_internal_save:
mock.patch.object(s, "_save") as mock_internal_save,
):
with self.assertRaisesMessage( with self.assertRaisesMessage(
SuspiciousFileOperation, self.error_msg % name SuspiciousFileOperation, self.error_msg % name
): ):
@ -60,10 +58,8 @@ class StorageValidateFileNameTests(SimpleTestCase):
# The initial name passed to `save` is valid and safe, but the result # The initial name passed to `save` is valid and safe, but the result
# from `_save` is not (this is achieved by monkeypatching _save). # from `_save` is not (this is achieved by monkeypatching _save).
for name in self.invalid_file_names: for name in self.invalid_file_names:
with ( with self.subTest(name=name):
self.subTest(name=name), with mock.patch.object(s, "_save", return_value=name):
mock.patch.object(s, "_save", return_value=name),
):
with self.assertRaisesMessage( with self.assertRaisesMessage(
SuspiciousFileOperation, self.error_msg % name SuspiciousFileOperation, self.error_msg % name
): ):