Skip to content

Commit

Permalink
Fix server error on deletion of team assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCoding committed Feb 23, 2024
1 parent 40129e9 commit 311ab2a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ansible_base/rbac/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def perform_destroy(self, instance):
rd = instance.object_role.role_definition
obj = instance.object_role.content_object
with transaction.atomic():
rd.remove_permission(instance.user, obj)
rd.remove_permission(instance.actor, obj)


class RoleTeamAssignmentViewSet(BaseAssignmentViewSet):
Expand Down
10 changes: 10 additions & 0 deletions ansible_base/rbac/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ class Meta:
def __repr__(self):
return f'RoleUserAssignment(pk={self.id})'

@property
def actor(self):
"Really simple helper to give same behavior between user and role assignments"
return self.user


class RoleTeamAssignment(AssignmentBase):
role_definition = models.ForeignKey(
Expand All @@ -299,6 +304,11 @@ class Meta:
def __repr__(self):
return f'RoleTeamAssignment(pk={self.id})'

@property
def actor(self):
"Really simple helper to give same behavior between user and role assignments"
return self.team


class ObjectRole(ObjectRoleFields):
"""
Expand Down
20 changes: 20 additions & 0 deletions test_app/tests/rbac/api/test_rbac_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,23 @@ def test_make_user_assignment(admin_api_client, inv_rd, rando, inventory):
response = admin_api_client.post(url, data=data, format="json")
assert response.status_code == 201, response.data
assert response.data['created_by']


@pytest.mark.django_db
def test_remove_user_assignment(admin_api_client, inv_rd, rando, inventory):
assignment = inv_rd.give_permission(rando, inventory)
url = reverse('roleuserassignment-detail', kwargs={'pk': assignment.pk})
response = admin_api_client.delete(url)
assert response.status_code == 204, response.data

assert not type(assignment).objects.filter(pk=assignment.pk).exists()


@pytest.mark.django_db
def test_remove_team_assignment(admin_api_client, inv_rd, team, inventory):
assignment = inv_rd.give_permission(team, inventory)
url = reverse('roleteamassignment-detail', kwargs={'pk': assignment.pk})
response = admin_api_client.delete(url)
assert response.status_code == 204, response.data

assert not type(assignment).objects.filter(pk=assignment.pk).exists()

0 comments on commit 311ab2a

Please sign in to comment.