Skip to content

Commit

Permalink
feat: extension des droits des utilisateurs de l'équipe - edition, de…
Browse files Browse the repository at this point in the history
…placement et verrouillage des sujets (#917)

## Description

🎸 cf titre

## Type de changement

🚧 technique

🦺 les droits sont ajoutés à un groupe comprennant les utilisateurs
`staff`.
🦺 le numéro du groupe est defini en dur dans les settings
  • Loading branch information
vincentporte authored Feb 13, 2025
1 parent e35ed3c commit 7ef0c62
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 16 deletions.
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@
SITE_ID = 1

DSP_FORUM_RELATED_ID = 108
STAFF_GROUP_ID = 48

API_BAN_BASE_URL = "https://api-adresse.data.gouv.fr"

Expand Down
15 changes: 12 additions & 3 deletions lacommunaute/forum/tests/test_categoryforum_createview.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import pytest
from django.conf import settings
from django.urls import reverse
from machina.core.db.models import get_model
from pytest_django.asserts import assertContains

from lacommunaute.forum.models import Forum
from lacommunaute.users.factories import UserFactory
from lacommunaute.users.factories import GroupFactory, UserFactory


UserForumPermission = get_model("forum_permission", "UserForumPermission")
GroupForumPermission = get_model("forum_permission", "GroupForumPermission")


@pytest.fixture(name="staff_group")
def fixture_staff_group():
return GroupFactory(id=settings.STAFF_GROUP_ID)


def test_user_access(client, db):
Expand All @@ -33,15 +41,15 @@ def test_form_title_and_context_data(client, db):
assertContains(response, reverse("forum_extension:documentation"))


def test_success_url(client, db):
def test_success_url(client, db, staff_group):
client.force_login(UserFactory(is_staff=True))
url = reverse("forum_extension:create_category")
response = client.post(url, data={"name": "Test", "description": "Test", "short_description": "Test"})
assert response.status_code == 302
assert response.url == reverse("forum_extension:documentation")


def test_create_category_with_perms(client, db):
def test_create_category_with_perms(client, db, staff_group):
client.force_login(UserFactory(is_staff=True))
url = reverse("forum_extension:create_category")
response = client.post(url, data={"name": "Test", "description": "Test", "short_description": "Test"})
Expand All @@ -52,3 +60,4 @@ def test_create_category_with_perms(client, db):
assert forum.parent is None

assert UserForumPermission.objects.filter(forum=forum).count() == 14
assert GroupForumPermission.objects.filter(forum=forum, group=staff_group).count() == 3
15 changes: 12 additions & 3 deletions lacommunaute/forum/tests/test_subcategoryforum_createview.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import pytest
from django.conf import settings
from django.urls import reverse
from machina.core.db.models import get_model
from pytest_django.asserts import assertContains

from lacommunaute.forum.factories import CategoryForumFactory
from lacommunaute.forum.models import Forum
from lacommunaute.users.factories import UserFactory
from lacommunaute.users.factories import GroupFactory, UserFactory


UserForumPermission = get_model("forum_permission", "UserForumPermission")
GroupForumPermission = get_model("forum_permission", "GroupForumPermission")


@pytest.fixture(name="staff_group")
def fixture_staff_group():
return GroupFactory(id=settings.STAFF_GROUP_ID)


def test_user_access(client, db):
Expand Down Expand Up @@ -38,7 +46,7 @@ def test_form_title_and_context_datas(client, db):
)


def test_success_url(client, db):
def test_success_url(client, db, staff_group):
client.force_login(UserFactory(is_staff=True))
category_forum = CategoryForumFactory()
url = reverse("forum_extension:create_subcategory", kwargs={"pk": category_forum.pk})
Expand All @@ -49,7 +57,7 @@ def test_success_url(client, db):
)


def test_create_subcategory_with_perms(client, db):
def test_create_subcategory_with_perms(client, db, staff_group):
client.force_login(UserFactory(is_staff=True))
category_forum = CategoryForumFactory()
url = reverse("forum_extension:create_subcategory", kwargs={"pk": category_forum.pk})
Expand All @@ -61,3 +69,4 @@ def test_create_subcategory_with_perms(client, db):
assert forum.parent == category_forum

assert UserForumPermission.objects.filter(forum=forum).count() == 14
assert GroupForumPermission.objects.filter(forum=forum, group=staff_group).count() == 3
7 changes: 6 additions & 1 deletion lacommunaute/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
from lacommunaute.forum_conversation.forms import PostForm
from lacommunaute.forum_conversation.view_mixins import FilteredTopicsListViewMixin
from lacommunaute.forum_upvote.models import UpVote
from lacommunaute.utils.perms import add_public_perms_on_forum, forum_visibility_content_tree_from_forums
from lacommunaute.utils.perms import (
add_public_perms_on_forum,
add_staff_perms_on_forum,
forum_visibility_content_tree_from_forums,
)


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -152,6 +156,7 @@ def test_func(self):
def form_valid(self, form):
response = super().form_valid(form)
add_public_perms_on_forum(form.instance)
add_staff_perms_on_forum(form.instance)
return response


Expand Down
13 changes: 13 additions & 0 deletions lacommunaute/utils/perms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from django.conf import settings
from django.contrib.auth.models import Group
from machina.core.db.models import get_model
from machina.core.loading import get_class


ForumPermission = get_model("forum_permission", "ForumPermission")
UserForumPermission = get_model("forum_permission", "UserForumPermission")
GroupForumPermission = get_model("forum_permission", "GroupForumPermission")
ForumVisibilityContentTree = get_class("forum.visibility", "ForumVisibilityContentTree")


Expand Down Expand Up @@ -34,6 +37,16 @@ def add_public_perms_on_forum(forum):
UserForumPermission.objects.bulk_create(anonymous_authorized_perms + authentified_authorized_perms)


def add_staff_perms_on_forum(forum):
permissions = ["can_edit_posts", "can_move_topics", "can_lock_topics"]
group = Group.objects.get(id=settings.STAFF_GROUP_ID)
authorized_perms = [
GroupForumPermission(group=group, permission=permission, has_perm=True, forum=forum)
for permission in ForumPermission.objects.filter(codename__in=permissions)
]
GroupForumPermission.objects.bulk_create(authorized_perms)


def forum_visibility_content_tree_from_forums(request, forums):
return ForumVisibilityContentTree.from_forums(
request.forum_permission_handler.forum_list_filter(
Expand Down
37 changes: 28 additions & 9 deletions lacommunaute/utils/tests/tests_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest
from bs4 import BeautifulSoup
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.core.files.storage import default_storage
from django.core.files.uploadedfile import SimpleUploadedFile
Expand All @@ -24,7 +25,7 @@
from lacommunaute.forum_file.models import PublicFile
from lacommunaute.stats.models import ForumStat
from lacommunaute.users.enums import IdentityProvider
from lacommunaute.users.factories import UserFactory
from lacommunaute.users.factories import GroupFactory, UserFactory
from lacommunaute.utils.date import get_last_sunday
from lacommunaute.utils.math import percent
from lacommunaute.utils.matomo import (
Expand All @@ -34,13 +35,14 @@
get_matomo_forums_data,
get_matomo_visits_data,
)
from lacommunaute.utils.perms import add_public_perms_on_forum
from lacommunaute.utils.perms import add_public_perms_on_forum, add_staff_perms_on_forum
from lacommunaute.utils.testing import parse_response_to_soup
from lacommunaute.utils.urls import urlize


ForumPermission = get_model("forum_permission", "ForumPermission")
UserForumPermission = get_model("forum_permission", "UserForumPermission")
GroupForumPermission = get_model("forum_permission", "GroupForumPermission")

faker = Faker()

Expand All @@ -63,7 +65,7 @@ def test_is_an_image(self):
f = SimpleUploadedFile(filename, force_bytes("file_content"))
attachment = AttachmentFactory(post=self.post, file=f)

out = Template("{% load attachments_tags %}" "{{ attachment|is_image }}").render(
out = Template("{% load attachments_tags %}{{ attachment|is_image }}").render(
Context(
{
"attachment": attachment,
Expand All @@ -79,7 +81,7 @@ def test_is_not_an_image(self):
f = SimpleUploadedFile(filename, force_bytes("file_content"))
attachment = AttachmentFactory(post=self.post, file=f)

out = Template("{% load attachments_tags %}" "{{ attachment|is_image }}").render(
out = Template("{% load attachments_tags %}{{ attachment|is_image }}").render(
Context(
{
"attachment": attachment,
Expand All @@ -93,7 +95,7 @@ def test_is_available(self):
f = SimpleUploadedFile("test.png", force_bytes("file_content"))
attachment = AttachmentFactory(post=self.post, file=f)

out = Template("{% load attachments_tags %}" "{{ attachment|is_available }}").render(
out = Template("{% load attachments_tags %}{{ attachment|is_available }}").render(
Context(
{
"attachment": attachment,
Expand All @@ -108,7 +110,7 @@ def test_is_not_available(self):
attachment = AttachmentFactory(post=self.post, file=f)

with patch.object(default_storage, "size", side_effect=FileNotFoundError):
out = Template("{% load attachments_tags %}" "{{ attachment|is_available }}").render(
out = Template("{% load attachments_tags %}{{ attachment|is_available }}").render(
Context(
{
"attachment": attachment,
Expand Down Expand Up @@ -232,7 +234,7 @@ def test_relativetimesince_fr(self):

d = datetime.now() - timedelta(days=2)
out = template.render(Context({"date": d}))
self.assertEqual(out, f"{date(d,'l')}, {time(d)}")
self.assertEqual(out, f"{date(d, 'l')}, {time(d)}")

d = datetime.now() - timedelta(days=10)
out = template.render(Context({"date": d}))
Expand Down Expand Up @@ -702,8 +704,8 @@ def test_replace_in_hxget(self):
)


class TestAddPublicPermsOnForum:
def test_public_perms(self, db):
class TestAddPermsOnForum:
def test_add_public_perms_on_forum(self, db):
forum = ForumFactory()
add_public_perms_on_forum(forum)

Expand Down Expand Up @@ -738,6 +740,23 @@ def test_public_perms(self, db):
== 7
)

def test_add_staff_perms_on_forum(self, db):
forum = ForumFactory()
group = GroupFactory(id=settings.STAFF_GROUP_ID)
add_staff_perms_on_forum(forum)

permissions = ["can_edit_posts", "can_move_topics", "can_lock_topics"]

assert (
GroupForumPermission.objects.filter(
forum=forum,
group=group,
has_perm=True,
permission__in=ForumPermission.objects.filter(codename__in=permissions),
).count()
== 3
)


class TestImageSizeValidator:
def test_size_validator(self, db):
Expand Down

0 comments on commit 7ef0c62

Please sign in to comment.