Skip to content

Commit

Permalink
add tests
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 4e2eb6e commit 61c8523
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions authentik/core/tests/test_groups_api.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
"""Test Groups API"""

from django.urls.base import reverse
from guardian.shortcuts import assign_perm

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

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_groups_api.py#L4

Added line #L4 was not covered by tests
from rest_framework.test import APITestCase

from authentik.core.models import Group, User
from authentik.core.tests.utils import create_test_admin_user
from authentik.core.tests.utils import create_test_admin_user, 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


class TestGroupsAPI(APITestCase):
"""Test Groups API"""

def setUp(self) -> None:
self.admin = create_test_admin_user()
self.login_user = create_test_user()

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

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_groups_api.py#L16

Added line #L16 was not covered by tests
self.user = User.objects.create(username="test-user")

def test_add_user(self):
"""Test add_user"""
group = Group.objects.create(name=generate_id())
self.client.force_login(self.admin)
assign_perm("authentik_core.add_user_to_group", self.login_user, group)
assign_perm("authentik_core.view_user", self.login_user)
self.client.force_login(self.login_user)

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

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_groups_api.py#L22-L24

Added lines #L22 - L24 were not covered by tests
res = self.client.post(
reverse("authentik_api:group-add-user", kwargs={"pk": group.pk}),
data={
Expand All @@ -32,7 +35,9 @@ def test_add_user(self):
def test_add_user_404(self):
"""Test add_user"""
group = Group.objects.create(name=generate_id())
self.client.force_login(self.admin)
assign_perm("authentik_core.add_user_to_group", self.login_user, group)
assign_perm("authentik_core.view_user", self.login_user)
self.client.force_login(self.login_user)

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

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_groups_api.py#L38-L40

Added lines #L38 - L40 were not covered by tests
res = self.client.post(
reverse("authentik_api:group-add-user", kwargs={"pk": group.pk}),
data={
Expand All @@ -44,8 +49,10 @@ def test_add_user_404(self):
def test_remove_user(self):
"""Test remove_user"""
group = Group.objects.create(name=generate_id())
assign_perm("authentik_core.remove_user_from_group", self.login_user, group)
assign_perm("authentik_core.view_user", self.login_user)

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

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_groups_api.py#L52-L53

Added lines #L52 - L53 were not covered by tests
group.users.add(self.user)
self.client.force_login(self.admin)
self.client.force_login(self.login_user)

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

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_groups_api.py#L55

Added line #L55 was not covered by tests
res = self.client.post(
reverse("authentik_api:group-remove-user", kwargs={"pk": group.pk}),
data={
Expand All @@ -59,8 +66,10 @@ def test_remove_user(self):
def test_remove_user_404(self):
"""Test remove_user"""
group = Group.objects.create(name=generate_id())
assign_perm("authentik_core.remove_user_from_group", self.login_user, group)
assign_perm("authentik_core.view_user", self.login_user)

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

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_groups_api.py#L69-L70

Added lines #L69 - L70 were not covered by tests
group.users.add(self.user)
self.client.force_login(self.admin)
self.client.force_login(self.login_user)

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

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_groups_api.py#L72

Added line #L72 was not covered by tests
res = self.client.post(
reverse("authentik_api:group-remove-user", kwargs={"pk": group.pk}),
data={
Expand All @@ -72,11 +81,11 @@ def test_remove_user_404(self):
def test_parent_self(self):
"""Test parent"""
group = Group.objects.create(name=generate_id())
self.client.force_login(self.admin)
self.login_user = create_test_admin_user()
self.client.force_login(self.login_user)

Check warning on line 85 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-L85

Added lines #L84 - L85 were not covered by tests
res = self.client.patch(
reverse("authentik_api:group-detail", kwargs={"pk": group.pk}),
data={
"pk": self.user.pk + 3,
"parent": group.pk,
},
)
Expand Down

0 comments on commit 61c8523

Please sign in to comment.