From 1e9b5c15ecc165e6d7658a6db13de98560f2b8df Mon Sep 17 00:00:00 2001 From: Ivan Studinsky Date: Sat, 15 Jun 2024 15:00:28 +0700 Subject: [PATCH] Provide tests for hashing of `OperandHolder` (#9437) --- tests/test_permissions.py | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/test_permissions.py b/tests/test_permissions.py index aefff981e..39b7ed662 100644 --- a/tests/test_permissions.py +++ b/tests/test_permissions.py @@ -716,3 +716,59 @@ class PermissionsCompositionTests(TestCase): composed_perm = (IsAuthenticatedUserOwner | permissions.IsAdminUser) hasperm = composed_perm().has_object_permission(request, None, None) assert hasperm is False + + def test_operand_holder_is_hashable(self): + assert hash((permissions.IsAuthenticated & permissions.IsAdminUser)) + + def test_operand_holder_hash_same_for_same_operands_and_operator(self): + first_operand_holder = ( + permissions.IsAuthenticated & permissions.IsAdminUser + ) + second_operand_holder = ( + permissions.IsAuthenticated & permissions.IsAdminUser + ) + + assert hash(first_operand_holder) == hash(second_operand_holder) + + def test_operand_holder_hash_differs_for_different_operands(self): + first_operand_holder = ( + permissions.IsAuthenticated & permissions.IsAdminUser + ) + second_operand_holder = ( + permissions.AllowAny & permissions.IsAdminUser + ) + third_operand_holder = ( + permissions.IsAuthenticated & permissions.AllowAny + ) + + assert hash(first_operand_holder) != hash(second_operand_holder) + assert hash(first_operand_holder) != hash(third_operand_holder) + assert hash(second_operand_holder) != hash(third_operand_holder) + + def test_operand_holder_hash_differs_for_different_operators(self): + first_operand_holder = ( + permissions.IsAuthenticated & permissions.IsAdminUser + ) + second_operand_holder = ( + permissions.IsAuthenticated | permissions.IsAdminUser + ) + + assert hash(first_operand_holder) != hash(second_operand_holder) + + def test_filtering_permissions(self): + unfiltered_permissions = [ + permissions.IsAuthenticated & permissions.IsAdminUser, + permissions.IsAuthenticated & permissions.IsAdminUser, + permissions.AllowAny, + ] + expected_permissions = [ + permissions.IsAuthenticated & permissions.IsAdminUser, + permissions.AllowAny, + ] + + filtered_permissions = [ + perm for perm + in dict.fromkeys(unfiltered_permissions) + ] + + assert filtered_permissions == expected_permissions