Skip to content

Commit

Permalink
fix tests and fix permission bug
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
  • Loading branch information
BeryJu committed Apr 14, 2024
1 parent 61c8523 commit 101e8df
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
5 changes: 3 additions & 2 deletions authentik/core/tests/test_groups_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rest_framework.test import APITestCase

from authentik.core.models import Group, User
from authentik.core.tests.utils import create_test_admin_user, create_test_user
from authentik.core.tests.utils import create_test_user

Check warning on line 8 in authentik/core/tests/test_groups_api.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_groups_api.py#L8

Added line #L8 was not covered by tests
from authentik.lib.generators import generate_id


Expand Down Expand Up @@ -81,7 +81,8 @@ def test_remove_user_404(self):
def test_parent_self(self):
"""Test parent"""
group = Group.objects.create(name=generate_id())
self.login_user = create_test_admin_user()
assign_perm("view_group", self.login_user, group)
assign_perm("change_group", self.login_user, group)
self.client.force_login(self.login_user)

Check warning on line 86 in authentik/core/tests/test_groups_api.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_groups_api.py#L84-L86

Added lines #L84 - L86 were not covered by tests
res = self.client.patch(
reverse("authentik_api:group-detail", kwargs={"pk": group.pk}),
Expand Down
11 changes: 10 additions & 1 deletion authentik/rbac/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@
class ObjectPermissions(DjangoObjectPermissions):
"""RBAC Permissions"""

def has_object_permission(self, request: Request, view, obj: Model):
def has_permission(self, request: Request, view) -> bool:
"""Always grant permission for object-specific requests
as view permission checking is done by `ObjectFilter`,
and write permission checking is done by `has_object_permission`"""
lookup = getattr(view, "lookup_url_kwarg", None) or getattr(view, "lookup_field", None)
if lookup and lookup in view.kwargs:
return True
return super().has_permission(request, view)

def has_object_permission(self, request: Request, view, obj: Model) -> bool:
queryset = self._queryset(view)
model_cls = queryset.model
perms = self.get_required_object_permissions(request.method, model_cls)
Expand Down
26 changes: 26 additions & 0 deletions authentik/rbac/tests/test_api_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,29 @@ def test_create_simple_denied(self):
},
)
self.assertEqual(res.status_code, 403)

def test_update_simple(self):

Check warning on line 125 in authentik/rbac/tests/test_api_filters.py

View check run for this annotation

Codecov / codecov/patch

authentik/rbac/tests/test_api_filters.py#L125

Added line #L125 was not covered by tests
"""Test update with permission"""
self.client.force_login(self.user)
inv = Invitation.objects.create(name=generate_id(), created_by=self.superuser)
self.role.assign_permission("authentik_stages_invitation.view_invitation", obj=inv)
self.role.assign_permission("authentik_stages_invitation.change_invitation", obj=inv)
res = self.client.patch(

Check warning on line 131 in authentik/rbac/tests/test_api_filters.py

View check run for this annotation

Codecov / codecov/patch

authentik/rbac/tests/test_api_filters.py#L127-L131

Added lines #L127 - L131 were not covered by tests
reverse("authentik_api:invitation-detail", kwargs={"pk": inv.pk}),
data={
"name": generate_id(),
},
)
self.assertEqual(res.status_code, 200)

Check warning on line 137 in authentik/rbac/tests/test_api_filters.py

View check run for this annotation

Codecov / codecov/patch

authentik/rbac/tests/test_api_filters.py#L137

Added line #L137 was not covered by tests

def test_update_simple_denied(self):

Check warning on line 139 in authentik/rbac/tests/test_api_filters.py

View check run for this annotation

Codecov / codecov/patch

authentik/rbac/tests/test_api_filters.py#L139

Added line #L139 was not covered by tests
"""Test update without assigning permission"""
self.client.force_login(self.user)
inv = Invitation.objects.create(name=generate_id(), created_by=self.superuser)
res = self.client.patch(

Check warning on line 143 in authentik/rbac/tests/test_api_filters.py

View check run for this annotation

Codecov / codecov/patch

authentik/rbac/tests/test_api_filters.py#L141-L143

Added lines #L141 - L143 were not covered by tests
reverse("authentik_api:invitation-detail", kwargs={"pk": inv.pk}),
data={
"name": generate_id(),
},
)
self.assertEqual(res.status_code, 403)

Check warning on line 149 in authentik/rbac/tests/test_api_filters.py

View check run for this annotation

Codecov / codecov/patch

authentik/rbac/tests/test_api_filters.py#L149

Added line #L149 was not covered by tests

0 comments on commit 101e8df

Please sign in to comment.