Skip to content

Commit

Permalink
services: update permission name + validations
Browse files Browse the repository at this point in the history
  • Loading branch information
yashlamba authored and slint committed Feb 19, 2024
1 parent 96c2bd3 commit 31b2110
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
1 change: 0 additions & 1 deletion invenio_communities/communities/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

"""Community schema."""
import re
from copy import deepcopy
from functools import partial
from uuid import UUID

Expand Down
12 changes: 9 additions & 3 deletions invenio_communities/communities/services/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,13 @@ def _validate_and_get_parent(self, parent_data, child):
return None
try:
parent = self.service.record_cls.pid.resolve(parent_data["id"])
if parent.parent:
if not parent.children.allow:
raise ValidationError("Assigned parent is not allowed to be a parent.")
elif child.children.allow:
raise ValidationError(
"Community allowed to be a parent can't be a child."
)
elif parent.parent:
raise ValidationError(
"Assigned parent community cannot also have a parent."
)
Expand All @@ -324,14 +330,14 @@ def _validate_and_get_parent(self, parent_data, child):
def create(self, identity, data=None, record=None, **kwargs):
"""Inject parsed theme to the record."""
if "parent" in data:
self.service.require_permission(identity, "update_parent", record=record)
self.service.require_permission(identity, "manage_parent", record=record)
parent = self._validate_and_get_parent(data["parent"], record)
record.parent = parent

def update(self, identity, data=None, record=None, **kwargs):
"""Update parent community of a community."""
if "parent" in data:
self.service.require_permission(identity, "update_parent", record=record)
self.service.require_permission(identity, "manage_parent", record=record)
parent = self._validate_and_get_parent(data["parent"], record)
record.parent = parent

Expand Down
2 changes: 1 addition & 1 deletion invenio_communities/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class CommunityPermissionPolicy(BasePermissionPolicy):
can_manage_children = [SystemProcess()]

# Permission for assinging a parent community
can_update_parent = [Administration(), SystemProcess()]
can_manage_parent = [Administration(), SystemProcess()]


def can_perform_action(community, context):
Expand Down
45 changes: 39 additions & 6 deletions tests/communities/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,11 @@ def test_children_updates(

def test_parent_create(community_service, comm):
parent = comm
community_service.update(
identity=system_identity,
id_=str(parent.id),
data={**parent.data, "children": {"allow": True}},
)
child = community_service.create(
identity=system_identity,
data={**comm.data, "slug": "child", "parent": {"id": str(parent.id)}},
Expand All @@ -641,6 +646,11 @@ def test_parent_create(community_service, comm):

def test_parent_update(community_service, comm):
parent = comm
community_service.update(
identity=system_identity,
id_=str(parent.id),
data={**parent.data, "children": {"allow": True}},
)
child = community_service.create(
identity=system_identity, data={**comm.data, "slug": "child1"}
)
Expand All @@ -655,6 +665,11 @@ def test_parent_update(community_service, comm):

def test_parent_remove(community_service, comm):
parent = comm
community_service.update(
identity=system_identity,
id_=str(parent.id),
data={**parent.data, "children": {"allow": True}},
)
child = community_service.create(
identity=system_identity,
data={**comm.data, "slug": "child2", "parent": {"id": str(parent.id)}},
Expand All @@ -677,23 +692,41 @@ def test_update_parent_community_not_exists(community_service, comm):
)


def test_update_parent_community_parent_is_child(community_service, comm):
def test_parent_update_parent_children_not_allowed(community_service, comm):
parent = comm

child = community_service.create(
identity=system_identity, data={**comm.data, "slug": "child3"}
identity=system_identity, data={**comm.data, "slug": "child4"}
)

# Make the child community parent of the test parent first
with pytest.raises(ValidationError) as e:
community_service.update(
identity=system_identity,
id_=str(child.id),
data={**child.data, "parent": {"id": str(parent.id)}},
)


def test_parent_update_child_children_are_allowed(community_service, comm):
parent = comm

child = community_service.create(
identity=system_identity, data={**comm.data, "slug": "child5"}
)
community_service.update(
identity=system_identity,
id_=str(parent.id),
data={**comm.data, "parent": {"id": str(child.id)}},
data={**parent.data, "children": {"allow": True}},
)
child = community_service.update(
identity=system_identity,
id_=str(child.id),
data={**child.data, "children": {"allow": True}},
)

# ValidationError as the parent has parent (which is not allowed)
with pytest.raises(ValidationError) as e:
community_service.update(
identity=system_identity,
id_=str(child.id),
data={**comm.data, "parent": {"id": str(parent.id)}},
data={**child.data, "parent": {"id": str(parent.id)}},
)

0 comments on commit 31b2110

Please sign in to comment.