From f038df75046504d16657f6d65a14cda49d41bb3e Mon Sep 17 00:00:00 2001 From: carofun Date: Wed, 3 Nov 2021 10:17:02 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B(back)=20exclude=20archived=20forum?= =?UTF-8?q?s=20from=20the=20list=20of=20advanced=20search?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If we want to use the advanced search, we shouldn't be able to select a forum that has been archived. The list must ignore archived forums. --- CHANGELOG.md | 1 + .../machina_extensions/forum_search/forms.py | 2 +- .../forum_moderation/test_forum_moderation.py | 2 +- tests/ashley/test_forum_search.py | 73 +++++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40a57298..b2b14be9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Versioning](https://semver.org/spec/v2.0.0.html). ### Fixed - exclude archived forums from the list of forums to move topics to +- exclude archived forums from the list of forums of advanced search ## [1.1.0] - 2021-10-28 diff --git a/src/ashley/machina_extensions/forum_search/forms.py b/src/ashley/machina_extensions/forum_search/forms.py index 8e30bcfb..8e1bac3e 100644 --- a/src/ashley/machina_extensions/forum_search/forms.py +++ b/src/ashley/machina_extensions/forum_search/forms.py @@ -27,7 +27,7 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) user = kwargs.pop("user", None) self.allowed_forums = PermissionHandler().get_readable_forums( - Forum.objects.filter(lti_contexts=lti_contexts), user + Forum.objects.filter(archived=False, lti_contexts=lti_contexts), user ) # self.allowed_forums is used in search method of MachinaSearchForm if self.allowed_forums: diff --git a/tests/ashley/machina_extensions/forum_moderation/test_forum_moderation.py b/tests/ashley/machina_extensions/forum_moderation/test_forum_moderation.py index 311cf6bd..6525aef9 100644 --- a/tests/ashley/machina_extensions/forum_moderation/test_forum_moderation.py +++ b/tests/ashley/machina_extensions/forum_moderation/test_forum_moderation.py @@ -266,7 +266,7 @@ def test_forum_moderation_list_forums_with_deleted_form(self): ) self.assertEqual(response.status_code, 200) - + # Control that we get an error and the move is not executed self.assertContains( response, diff --git a/tests/ashley/test_forum_search.py b/tests/ashley/test_forum_search.py index b08454a6..d247329c 100644 --- a/tests/ashley/test_forum_search.py +++ b/tests/ashley/test_forum_search.py @@ -612,3 +612,76 @@ def test_forum_search_archived_forum(self): self.assertContains( response, "Your search has returned 0 results", html=True ) + + def test_forum_search_archived_form_search(self): + """ + Create different forums in the same lti_context, make sure user can't + select archived forums in the advanced search. + """ + user = UserFactory() + + lti_context = LTIContextFactory(lti_consumer=user.lti_consumer) + forum = ForumFactory() + forum2 = ForumFactory() + forum3 = ForumFactory() + forum.lti_contexts.add(lti_context) + forum2.lti_contexts.add(lti_context) + forum3.lti_contexts.add(lti_context) + + PostFactory( + topic=TopicFactory(forum=forum), + ) + + PostFactory( + topic=TopicFactory(forum=forum2), + ) + PostFactory( + topic=TopicFactory(forum=forum3), + ) + # Creates the session + self.client.force_login(user, "ashley.auth.backend.LTIBackend") + session = self.client.session + session[SESSION_LTI_CONTEXT_ID] = lti_context.id + session.save() + + assign_perm("can_read_forum", user, forum) + assign_perm("can_read_forum", user, forum2) + assign_perm("can_read_forum", user, forum3) + + # Load the advanced search form + form = SearchForm(user=user, lti_context=lti_context) + + # Check that only forums that are allowed are proposed as choice + self.assertEqual( + form.fields["search_forums"].choices, + [ + (forum.id, "{} {}".format("-" * forum.margin_level, forum.name)), + (forum2.id, "{} {}".format("-" * forum.margin_level, forum2.name)), + (forum3.id, "{} {}".format("-" * forum.margin_level, forum3.name)), + ], + ) + + # Archive the forum2 + forum2.archived = True + forum2.save() + + form = SearchForm(user=user, lti_context=lti_context) + + # Check that forum2 is not proposed as choice anymore as it has been archived + self.assertEqual( + form.fields["search_forums"].choices, + [ + (forum.id, "{} {}".format("-" * forum.margin_level, forum.name)), + (forum3.id, "{} {}".format("-" * forum.margin_level, forum3.name)), + ], + ) + + # Despite that, we force the request on the forum that is not allowed + response = self.client.get(f"/forum/search/?q=world&search_forums={forum2.id}") + self.assertEqual(response.status_code, 200) + # Control that we get an error and the search is not executed + self.assertContains( + response, + f"Select a valid choice. {forum2.id} is not one of the available choices.", + html=True, + )