Skip to content

Commit 0d9ce7f

Browse files
author
Ryan P Kilby
committed
Add test to explain behavior
1 parent 73c0010 commit 0d9ce7f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

tests/test_filtering.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
)
1414

1515
from .testapp.filters import (
16+
UserFilter,
1617
PersonFilter,
1718
PostFilter,
1819
BlogPostFilter,
@@ -326,6 +327,28 @@ def test_related_filters_caching(self):
326327

327328
self.assertEqual(len(filters), 1)
328329

330+
def test_relatedfilter_queryset_required(self):
331+
# Use a secure default queryset. Previous behavior was to use the default model
332+
# manager's `all()`, however this has the side effect of exposing related data.
333+
# The default behavior should not expose information, which requires users to
334+
# explicitly set the `queryset` argument.
335+
class NoteFilter(FilterSet):
336+
title = filters.CharFilter(name='title')
337+
author = filters.RelatedFilter(UserFilter, name='author')
338+
339+
class Meta:
340+
model = Note
341+
fields = []
342+
343+
GET = {'author': User.objects.get(username='user2').pk}
344+
f = NoteFilter(GET, queryset=Note.objects.all())
345+
346+
with self.assertRaises(AssertionError) as excinfo:
347+
f.qs
348+
349+
msg = str(excinfo.exception)
350+
self.assertEqual("Expected `.get_queryset()` to return a `QuerySet`, but got `None`.", msg)
351+
329352

330353
class MiscTests(TestCase):
331354
def test_multiwidget_incompatibility(self):

0 commit comments

Comments
 (0)