Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
seanh committed Nov 14, 2024
1 parent 32f1a93 commit 83fb543
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 34 deletions.
4 changes: 2 additions & 2 deletions h/security/predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ def get_authenticated_users_membership():
# Any member can remove themselves from a group.
return True

if "owner" in context.roles or "admin" in context.roles:
if "owner" in context.membership.roles or "admin" in context.membership.roles:
# Only owners can remove other owners.
return "owner" in membership.roles

if "moderator" in context.roles:
if "moderator" in context.membership.roles:
# Owners and admins can remove moderators.
return "owner" in membership.roles or "admin" in membership.roles

Expand Down
14 changes: 4 additions & 10 deletions h/traversal/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,10 @@ def get_user():
return GroupMembershipContext(group=group, user=user, membership=membership)


@dataclass
class GroupMembershipContext:
"""Context for group membership-related views."""

def __init__(
self, group: Group | None, user: User | None, membership: GroupMembership | None
):
self.group = group
self.user = user
self.membership = membership

@property
def roles(self) -> list[str] | None:
return self.membership.roles if self.membership else None
group: Group | None
user: User | None
membership: GroupMembership | None
10 changes: 5 additions & 5 deletions tests/unit/h/security/predicates_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest.mock import create_autospec, sentinel
from unittest.mock import sentinel

import pytest

Expand Down Expand Up @@ -470,7 +470,7 @@ def test_it(
roles=[authenticated_users_role],
)
)
context.roles = [target_users_role]
context.membership.roles = [target_users_role]

assert predicates.group_member_remove(identity, context) == expected_result

Expand All @@ -494,7 +494,7 @@ def test_any_member_can_remove_themselves_from_a_group(
roles=[role],
)
)
context.roles = [role]
context.membership.roles = [role]

assert predicates.group_member_remove(identity, context) is True

Expand Down Expand Up @@ -534,8 +534,8 @@ def user(self, factories):

@pytest.fixture
def context(self, group, user):
return create_autospec(
GroupMembershipContext, instance=True, spec_set=True, group=group, user=user
return GroupMembershipContext(
group=group, user=user, membership=GroupMembership(group=group, user=user)
)


Expand Down
14 changes: 0 additions & 14 deletions tests/unit/h/traversal/group_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pytest

from h.exceptions import InvalidUserId
from h.models import GroupMembership, GroupMembershipRoles
from h.traversal.group import (
GroupMembershipContext,
GroupRequiredRoot,
Expand Down Expand Up @@ -143,19 +142,6 @@ def pyramid_request(self, pyramid_request):
return pyramid_request


class TestGroupMembershipContext:
def test_roles(self, factories):
group = factories.Group.build()
user = factories.User.build()
membership = GroupMembership(
group=group, user=user, roles=[GroupMembershipRoles.MEMBER]
)

context = GroupMembershipContext(group, user, membership)

assert context.roles == membership.roles


@pytest.fixture
def GroupContext(patch):
return patch("h.traversal.group.GroupContext")
9 changes: 6 additions & 3 deletions tests/unit/h/views/api/groups_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import h.views.api.groups as views
from h import presenters
from h.models import User
from h.models import GroupMembership, User
from h.schemas.base import ValidationError
from h.traversal.group import GroupContext, GroupMembershipContext
from h.views.api.exceptions import PayloadError
Expand Down Expand Up @@ -435,8 +435,11 @@ def test_it(self, context, pyramid_request, group_members_service):
assert isinstance(response, HTTPNoContent)

@pytest.fixture
def context(self):
return create_autospec(GroupMembershipContext, instance=True, spec_set=True)
def context(self, factories):
group = factories.Group.build()
user = factories.User.build()
membership = GroupMembership(group=group, user=user)
return GroupMembershipContext(group=group, user=user, membership=membership)


@pytest.mark.usefixtures("user_service", "group_members_service")
Expand Down

0 comments on commit 83fb543

Please sign in to comment.