Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check node being available on filtered queryset to prevent index error. #9539

Merged
merged 1 commit into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions kolibri/core/content/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,8 +969,12 @@ def get_grandchild_ids(self, child_ids, depth, page_size):

def get_tree_queryset(self, request, pk):
# Get the model for the parent node here - we do this so that we trigger a 404 immediately if the node
# does not exist (or exists but is not available).
parent_id = pk if pk and self.get_queryset().filter(id=pk).exists() else None
# does not exist (or exists but is not available, or is filtered).
parent_id = (
pk
if pk and self.filter_queryset(self.get_queryset()).filter(id=pk).exists()
else None
)

if parent_id is None:
raise Http404
Expand Down
8 changes: 8 additions & 0 deletions kolibri/core/content/test/test_content_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,14 @@ def test_contentnode_tree(self):
)
self._recurse_and_assert([response.data], [root])

def test_contentnode_tree_filtered_queryset_node(self):
root = content.ContentNode.objects.get(title="root")
response = self.client.get(
reverse("kolibri:core:contentnode_tree-detail", kwargs={"pk": root.id})
+ "?parent={}".format(uuid.uuid4().hex)
)
self.assertEqual(response.status_code, 404)

@unittest.skipIf(
getattr(settings, "DATABASES")["default"]["ENGINE"]
== "django.db.backends.postgresql",
Expand Down