From 2ac2bb48c28754c87888f90239e085c77d522aa5 Mon Sep 17 00:00:00 2001 From: imperosol Date: Fri, 4 Dec 2026 15:37:05 +0100 Subject: [PATCH] add tests --- core/tests/test_user.py | 56 +++++++++++++++++++++++++++++++++++++++++ core/views/forms.py | 2 +- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/core/tests/test_user.py b/core/tests/test_user.py index 578e06b8..2c317b4f 100644 --- a/core/tests/test_user.py +++ b/core/tests/test_user.py @@ -295,6 +295,49 @@ def test_user_update_groups(client: Client): } +@pytest.mark.django_db +def test_user_update_groups_non_admin(client: Client): + """Test that a non-admin user can update the groups it is allowed to.""" + managed_group = baker.make(Group, is_manually_manageable=True) + other_group = baker.make(Group, is_manually_manageable=True) + operator = subscriber_user.make( + groups=[baker.make(Group, managed_groups=[managed_group])] + ) + user = baker.make(User, groups=[other_group]) + client.force_login(operator) + response = client.post( + reverse("core:user_groups", kwargs={"user_id": user.id}), + data={"groups": [managed_group.id]}, + ) + assertRedirects(response, user.get_absolute_url()) + # the manageable group should have been attributed + # and the non-manageable groups should be kept + assert set(user.groups.all()) == { + Group.objects.get(pk=settings.SITH_GROUP_PUBLIC_ID), + managed_group, + other_group, + } + + +@pytest.mark.django_db +def test_user_update_groups_denied(client: Client): + """Test that a user that can manage no group cannot access the page at all.""" + operator = subscriber_user.make(groups=[baker.make(Group)]) + user = baker.make(User) + client.force_login(operator) + response = client.get(reverse("core:user_groups", kwargs={"user_id": user.id})) + assert response.status_code == 403 + response = client.post( + reverse("core:user_groups", kwargs={"user_id": user.id}), + data={"groups": [baker.make(Group).id]}, + ) + assert response.status_code == 403 + # no group should have been attributed at all + assert set(user.groups.all()) == { + Group.objects.get(pk=settings.SITH_GROUP_PUBLIC_ID) + } + + @pytest.mark.django_db def test_logout(client: Client): user = baker.make(User) @@ -378,6 +421,19 @@ def test_displayed_user_self_tabs(user_factory, expected_tabs: list[str]): "account", ], ), + ( + # this user is in a group that is allowed to manage another group + # so the groups tab should be displayed + lambda: subscriber_user.make( + groups=[ + baker.make( + Group, + managed_groups=[baker.make(Group, is_manually_manageable=True)], + ) + ] + ), + ["infos", "godfathers", "pictures", "clubs", "groups"], + ), ], ) def test_displayed_other_user_tabs(user_factory, expected_tabs: list[str]): diff --git a/core/views/forms.py b/core/views/forms.py index 6e0b3fab..cc5898b9 100644 --- a/core/views/forms.py +++ b/core/views/forms.py @@ -388,7 +388,7 @@ class UserGroupsForm(forms.ModelForm): self.instance.groups.set( [ *self.cleaned_data["groups"], - *self.instance.groups.filter(is_manually_manageable=False), + *self.instance.groups.difference(self.fields["groups"].queryset), ] ) return self.instance