1
0
mirror of https://github.com/django/django.git synced 2025-03-31 19:46:42 +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,29 +621,30 @@ 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"), with mock.patch.object(hasher, "verify"):
): # make_password() is called to standardize timing.
# Ensure 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(
self.assertEqual(mock_identify_hasher.mock_calls, [mock.call(encoded)]) mock_identify_hasher.mock_calls,
self.assertEqual( [mock.call(encoded)],
mock_make_password.mock_calls, )
[mock.call("x" * UNUSABLE_PASSWORD_SUFFIX_LENGTH)], self.assertEqual(
) mock_make_password.mock_calls,
[mock.call("x" * UNUSABLE_PASSWORD_SUFFIX_LENGTH)],
)
def test_encode_invalid_salt(self): def test_encode_invalid_salt(self):
hasher_classes = [ hasher_classes = [

View File

@ -27,15 +27,15 @@ 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
): ):
s.save(name, content="irrelevant") s.save(name, content="irrelevant")
self.assertEqual(mock_get_available_name.mock_calls, []) self.assertEqual(mock_get_available_name.mock_calls, [])
self.assertEqual(mock_internal_save.mock_calls, []) self.assertEqual(mock_internal_save.mock_calls, [])
@ -44,15 +44,13 @@ 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(
): SuspiciousFileOperation, self.error_msg % name
with self.assertRaisesMessage( ):
SuspiciousFileOperation, self.error_msg % name s.save("valid-file-name.txt", content="irrelevant")
):
s.save("valid-file-name.txt", content="irrelevant")
self.assertEqual(mock_internal_save.mock_calls, []) self.assertEqual(mock_internal_save.mock_calls, [])
def test_validate_after_internal_save(self): def test_validate_after_internal_save(self):
@ -60,11 +58,9 @@ 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(
): SuspiciousFileOperation, self.error_msg % name
with self.assertRaisesMessage( ):
SuspiciousFileOperation, self.error_msg % name s.save("valid-file-name.txt", content="irrelevant")
):
s.save("valid-file-name.txt", content="irrelevant")