add tests

This commit is contained in:
imperosol committed 2026-12-04 15:37:05 +01:00
1 parent d7fa0b5ed6
commit 2ac2bb48c2
2 files changed
+57 -1

No files matched your search

+56
View File
@@ -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 @pytest.mark.django_db
def test_logout(client: Client): def test_logout(client: Client):
user = baker.make(User) user = baker.make(User)
@@ -378,6 +421,19 @@ def test_displayed_user_self_tabs(user_factory, expected_tabs: list[str]):
"account", "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]): def test_displayed_other_user_tabs(user_factory, expected_tabs: list[str]):
+1 -1
View File
@@ -388,7 +388,7 @@ class UserGroupsForm(forms.ModelForm):
self.instance.groups.set( self.instance.groups.set(
[ [
*self.cleaned_data["groups"], *self.cleaned_data["groups"],
*self.instance.groups.filter(is_manually_manageable=False), *self.instance.groups.difference(self.fields["groups"].queryset),
] ]
) )
return self.instance return self.instance