Skip to content

Commit

Permalink
fix(search): exclude contents from index which should not be visible
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Oct 31, 2023
1 parent 3e3d195 commit 67e6152
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lacommunaute/forum_search/search_indexes.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
29 changes: 29 additions & 0 deletions lacommunaute/forum_search/tests/test_search_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'<span class="highlighted">{word}</span>' 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")

0 comments on commit 67e6152

Please sign in to comment.