Skip to content

Commit

Permalink
Merge pull request #2 from maxipavlovic/bugfix/LITE-14144
Browse files Browse the repository at this point in the history
LITE-14144 Compatibility Backends are now less error-prone
  • Loading branch information
vgrebenschikov authored Jun 26, 2020
2 parents f224e83 + e1378cc commit c1ca3c3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
14 changes: 10 additions & 4 deletions dj_rql/drf/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ class CompatibilityRQLFilterBackend(RQLFilterBackend):
"""
@classmethod
def get_query(cls, filter_instance, request, view):
query_string = cls.modify_initial_query(filter_instance, request, get_query(request))
try:
query_string = cls.modify_initial_query(filter_instance, request, get_query(request))

if not cls.is_old_syntax(filter_instance, request, query_string):
return query_string
if not cls.is_old_syntax(filter_instance, request, query_string):
return query_string

return cls.get_rql_query(filter_instance, request, query_string)
else:
return cls.get_rql_query(filter_instance, request, query_string)
except Exception:
raise RQLFilterParsingError(details={
'error': 'Bad filter query.',
})

@classmethod
def modify_initial_query(cls, filter_instance, request, query_string):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_drf/test_django_filters_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ def test_old_syntax_filters(mocker):
assert filter_instance.old_syntax_filters == {'t__in'}


def test_bad_syntax_query(mocker):
query = 'limit=10&offset=0&in=(prop,(val1,val2))&in(prop.prop,(val))' \
'&ge=(created,2020-06-01T04:00:00Z)&le=(created,2020-06-24T03:59:59Z)'
request = mocker.MagicMock(
query_params=QueryDict(query),
_request=mocker.MagicMock(META={'QUERY_STRING': query}),
)
filter_instance = BooksFilterClass(Book.objects.none())
mocker.patch('dj_rql.drf.compat.DjangoFiltersRQLFilterBackend.is_old_syntax', return_value=True)

with pytest.raises(RQLFilterParsingError):
assert DjangoFiltersRQLFilterBackend.get_query(filter_instance, request, None)


@pytest.mark.parametrize('query', (
'select(books)&k__in=v,v', 'k__in=v,v&select(books)',
))
Expand Down

0 comments on commit c1ca3c3

Please sign in to comment.