Skip to content

Commit

Permalink
🐛(back) exclude archived forums from the list of advanced search
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
carofun committed Nov 3, 2021
1 parent 832aa1d commit f038df7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/ashley/machina_extensions/forum_search/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
73 changes: 73 additions & 0 deletions tests/ashley/test_forum_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,76 @@ def test_forum_search_archived_forum(self):
self.assertContains(
response, "Your search has returned <b>0</b> 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,
)

0 comments on commit f038df7

Please sign in to comment.