1
0
mirror of https://github.com/django/django.git synced 2025-03-13 19:00:45 +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
]
for password, encoded, hasher_side_effect in cases:
with (
self.subTest(encoded=encoded),
mock.patch(
with self.subTest(encoded=encoded):
with mock.patch(
"django.contrib.auth.hashers.identify_hasher",
side_effect=hasher_side_effect,
) as mock_identify_hasher,
mock.patch(
"django.contrib.auth.hashers.make_password"
) as mock_make_password,
mock.patch(
"django.contrib.auth.hashers.get_random_string",
side_effect=lambda size: "x" * size,
),
mock.patch.object(hasher, "verify"),
):
# Ensure make_password is called to standardize timing.
check_password(password, encoded)
self.assertEqual(hasher.verify.call_count, 0)
self.assertEqual(mock_identify_hasher.mock_calls, [mock.call(encoded)])
self.assertEqual(
mock_make_password.mock_calls,
[mock.call("x" * UNUSABLE_PASSWORD_SUFFIX_LENGTH)],
)
) as mock_identify_hasher:
with mock.patch(
"django.contrib.auth.hashers.make_password"
) as mock_make_password:
with mock.patch(
"django.contrib.auth.hashers.get_random_string",
side_effect=lambda size: "x" * size,
):
with mock.patch.object(hasher, "verify"):
# make_password() is called to standardize timing.
check_password(password, encoded)
self.assertEqual(hasher.verify.call_count, 0)
self.assertEqual(
mock_identify_hasher.mock_calls,
[mock.call(encoded)],
)
self.assertEqual(
mock_make_password.mock_calls,
[mock.call("x" * UNUSABLE_PASSWORD_SUFFIX_LENGTH)],
)
def test_encode_invalid_salt(self):
hasher_classes = [

View File

@ -27,15 +27,15 @@ class StorageValidateFileNameTests(SimpleTestCase):
s = CustomStorage()
# The initial name passed to `save` is not valid nor safe, fail early.
for name in self.invalid_file_names:
with (
self.subTest(name=name),
mock.patch.object(s, "get_available_name") as mock_get_available_name,
mock.patch.object(s, "_save") as mock_internal_save,
):
with self.assertRaisesMessage(
SuspiciousFileOperation, self.error_msg % name
):
s.save(name, content="irrelevant")
with self.subTest(name=name):
with mock.patch.object(
s, "get_available_name"
) as mock_get_available_name:
with mock.patch.object(s, "_save") as mock_internal_save:
with self.assertRaisesMessage(
SuspiciousFileOperation, self.error_msg % name
):
s.save(name, content="irrelevant")
self.assertEqual(mock_get_available_name.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
# name from `get_available_name` is not.
for name in self.invalid_file_names:
with (
self.subTest(name=name),
mock.patch.object(s, "get_available_name", return_value=name),
mock.patch.object(s, "_save") as mock_internal_save,
):
with self.assertRaisesMessage(
SuspiciousFileOperation, self.error_msg % name
):
s.save("valid-file-name.txt", content="irrelevant")
with self.subTest(name=name):
with mock.patch.object(s, "get_available_name", return_value=name):
with mock.patch.object(s, "_save") as mock_internal_save:
with self.assertRaisesMessage(
SuspiciousFileOperation, self.error_msg % name
):
s.save("valid-file-name.txt", content="irrelevant")
self.assertEqual(mock_internal_save.mock_calls, [])
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
# from `_save` is not (this is achieved by monkeypatching _save).
for name in self.invalid_file_names:
with (
self.subTest(name=name),
mock.patch.object(s, "_save", return_value=name),
):
with self.assertRaisesMessage(
SuspiciousFileOperation, self.error_msg % name
):
s.save("valid-file-name.txt", content="irrelevant")
with self.subTest(name=name):
with mock.patch.object(s, "_save", return_value=name):
with self.assertRaisesMessage(
SuspiciousFileOperation, self.error_msg % name
):
s.save("valid-file-name.txt", content="irrelevant")