-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add actions to group membership APIs
- Loading branch information
Showing
4 changed files
with
111 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,41 @@ | ||
from h.models import GroupMembershipRoles | ||
from h.security import Permission | ||
from h.traversal.group_membership import ( | ||
EditGroupMembershipContext, | ||
GroupMembershipContext, | ||
) | ||
|
||
|
||
class GroupMembershipJSONPresenter: | ||
def __init__(self, membership): | ||
def __init__(self, request, membership): | ||
self.request = request | ||
self.membership = membership | ||
|
||
def asdict(self): | ||
return { | ||
membership_dict = { | ||
"authority": self.membership.group.authority, | ||
"userid": self.membership.user.userid, | ||
"username": self.membership.user.username, | ||
"display_name": self.membership.user.display_name, | ||
"roles": self.membership.roles, | ||
"actions": [], | ||
} | ||
|
||
if self.request.has_permission( | ||
Permission.Group.MEMBER_REMOVE, | ||
GroupMembershipContext( | ||
self.membership.group, self.membership.user, self.membership | ||
), | ||
): | ||
membership_dict["actions"].append("delete") | ||
|
||
for role in GroupMembershipRoles: | ||
if self.request.has_permission( | ||
Permission.Group.MEMBER_EDIT, | ||
EditGroupMembershipContext( | ||
self.membership.group, self.membership.user, self.membership, role | ||
), | ||
): | ||
membership_dict["actions"].append(f"updates.roles.{role}") | ||
|
||
return membership_dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,54 @@ | ||
import pytest | ||
|
||
from h.models import GroupMembership | ||
from h.presenters.group_membership_json import GroupMembershipJSONPresenter | ||
|
||
|
||
class TestGroupMembershipJSONPresenter: | ||
def test_it(self, factories): | ||
user = factories.User.build() | ||
group = factories.Group.build() | ||
membership = GroupMembership(user=user, group=group) | ||
def test_it(self, user, group, membership, pyramid_request, pyramid_config): | ||
pyramid_config.testing_securitypolicy(permissive=False) | ||
|
||
json = GroupMembershipJSONPresenter(pyramid_request, membership).asdict() | ||
|
||
assert json == { | ||
"authority": group.authority, | ||
"userid": user.userid, | ||
"username": user.username, | ||
"display_name": user.display_name, | ||
"roles": membership.roles, | ||
"actions": [], | ||
} | ||
|
||
def test_it_with_permissive_securitypolicy( | ||
self, user, group, membership, pyramid_request, pyramid_config | ||
): | ||
pyramid_config.testing_securitypolicy(permissive=True) | ||
|
||
json = GroupMembershipJSONPresenter(membership).asdict() | ||
json = GroupMembershipJSONPresenter(pyramid_request, membership).asdict() | ||
|
||
assert json == { | ||
"authority": group.authority, | ||
"userid": user.userid, | ||
"username": user.username, | ||
"display_name": user.display_name, | ||
"roles": membership.roles, | ||
"actions": [ | ||
"delete", | ||
"updates.roles.member", | ||
"updates.roles.moderator", | ||
"updates.roles.admin", | ||
"updates.roles.owner", | ||
], | ||
} | ||
|
||
@pytest.fixture | ||
def user(self, factories): | ||
return factories.User.build() | ||
|
||
@pytest.fixture | ||
def group(self, factories): | ||
return factories.Group.build() | ||
|
||
@pytest.fixture | ||
def membership(self, user, group): | ||
return GroupMembership(user=user, group=group) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters