summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2024-07-11 08:03:00 +0200
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-07-11 11:10:15 +0200
commitc5d196a65264136ee6795356871a29f3d22ec52f (patch)
tree88a3aded8e8b489d27651400a3e6370fc803bd40
parent8e59e33400ffcec262116d75f7886d96e2b57980 (diff)
[4.2.x] Fixed auth_tests and file_storage tests on Python 3.8.
-rw-r--r--tests/auth_tests/test_hashers.py43
-rw-r--r--tests/file_storage/test_base.py48
2 files changed, 44 insertions, 47 deletions
diff --git a/tests/auth_tests/test_hashers.py b/tests/auth_tests/test_hashers.py
index 3da495f2be..33bca3fbc1 100644
--- a/tests/auth_tests/test_hashers.py
+++ b/tests/auth_tests/test_hashers.py
@@ -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 = [
diff --git a/tests/file_storage/test_base.py b/tests/file_storage/test_base.py
index c5338b8e66..ee47987900 100644
--- a/tests/file_storage/test_base.py
+++ b/tests/file_storage/test_base.py
@@ -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")