Provide tests for hashing of OperandHolder (#9437)

This commit is contained in:
Ivan Studinsky 2024-06-15 15:00:28 +07:00 committed by GitHub
parent c7a7eae551
commit 1e9b5c15ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -716,3 +716,59 @@ class PermissionsCompositionTests(TestCase):
composed_perm = (IsAuthenticatedUserOwner | permissions.IsAdminUser) composed_perm = (IsAuthenticatedUserOwner | permissions.IsAdminUser)
hasperm = composed_perm().has_object_permission(request, None, None) hasperm = composed_perm().has_object_permission(request, None, None)
assert hasperm is False 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