Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide tests for hashing of OperandHolder #9437

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,3 +716,59 @@ def has_object_permission(self, request, view, obj):
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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May fail if hash returns zero. The proper check would be that no exception is thrown. (E.g., via >= 0)


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)
]
Comment on lines +769 to +772
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filtered_permissions = list(dict.fromkeys(unfiltered_permissions))


assert filtered_permissions == expected_permissions
Loading