From 67e6152142c4939f9417cb9b725c685956f2c91b Mon Sep 17 00:00:00 2001 From: vincent porte Date: Tue, 24 Oct 2023 16:14:24 +0200 Subject: [PATCH] =?UTF-8?q?fix(search):=C2=A0exclude=20contents=20from=20i?= =?UTF-8?q?ndex=20which=20should=20not=20be=20visible?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lacommunaute/forum_search/search_indexes.py | 9 +++--- .../forum_search/tests/test_search_indexes.py | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lacommunaute/forum_search/search_indexes.py b/lacommunaute/forum_search/search_indexes.py index 48f116fa7..6efb46c78 100644 --- a/lacommunaute/forum_search/search_indexes.py +++ b/lacommunaute/forum_search/search_indexes.py @@ -1,5 +1,6 @@ from haystack import indexes +from lacommunaute.forum.enums import Kind as Forum_Kind from lacommunaute.forum.models import Forum from lacommunaute.forum_conversation.models import Post @@ -47,10 +48,10 @@ def prepare_topic_subject(self, obj): return obj.topic.subject def index_queryset(self, using=None): - return Post.objects.all().exclude(approved=False) + return Post.objects.exclude(approved=False).exclude(topic__forum__kind=Forum_Kind.PRIVATE_FORUM) def read_queryset(self, using=None): - return Post.objects.all().exclude(approved=False).select_related("topic", "poster") + return self.index_queryset(using=using).select_related("topic", "poster") class ForumIndex(indexes.SearchIndex, indexes.Indexable): @@ -85,7 +86,7 @@ def prepare_forum_updated(self, obj): return obj.updated def index_queryset(self, using=None): - return Forum.objects.all() + return Forum.objects.filter(kind=Forum_Kind.PUBLIC_FORUM) def read_queryset(self, using=None): - return Forum.objects.all() + return self.index_queryset(using=using) diff --git a/lacommunaute/forum_search/tests/test_search_indexes.py b/lacommunaute/forum_search/tests/test_search_indexes.py index cec4f06a6..c8dc3c313 100644 --- a/lacommunaute/forum_search/tests/test_search_indexes.py +++ b/lacommunaute/forum_search/tests/test_search_indexes.py @@ -5,6 +5,7 @@ from django.core.management import call_command from django.urls import reverse +from lacommunaute.forum.enums import Kind as Forum_Kind from lacommunaute.forum.factories import ForumFactory from lacommunaute.forum_conversation.factories import TopicFactory @@ -138,3 +139,31 @@ def test_search_on_both_models(client, db, search_url, public_topics, public_for assert response.status_code == 200 for word in query: assert f'{word}' in response.content.decode("utf-8") + + +def test_non_public_forums_are_excluded(client, db, search_url): + for i, kind in enumerate([kind for kind in Forum_Kind if kind != Forum_Kind.PUBLIC_FORUM]): + ForumFactory(kind=kind, name=f"invisible {i}") + call_command("rebuild_index", noinput=True, interactive=False) + response = client.get(search_url, {"q": "invisible"}) + assert response.status_code == 200 + assert "Aucun résultat" in response.content.decode("utf-8") + + +def test_posts_from_non_public_forums_are_excluded(client, db, search_url): + for i, kind in enumerate([kind for kind in Forum_Kind if kind != Forum_Kind.PUBLIC_FORUM]): + TopicFactory(forum=ForumFactory(kind=kind), subject=f"invisible {i}", with_post=True) + call_command("rebuild_index", noinput=True, interactive=False) + response = client.get(search_url, {"q": "invisible"}) + assert response.status_code == 200 + assert "Aucun résultat" in response.content.decode("utf-8") + + +def test_unapproved_post_is_exclude(client, db, search_url, public_forums): + topic = TopicFactory(forum=public_forums[0], with_post=True) + topic.first_post.approved = False + topic.first_post.save() + call_command("rebuild_index", noinput=True, interactive=False) + response = client.get(search_url, {"q": topic.first_post.content.raw.split()[0]}) + assert response.status_code == 200 + assert "Aucun résultat" in response.content.decode("utf-8")