1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-31 06:51:10 +03:00

value_to_python should encode lookup fields as ascii

This commit is contained in:
Wayne Witzel III 2017-03-08 12:30:30 -05:00
parent e8126aab34
commit c058f76960
2 changed files with 15 additions and 0 deletions

View File

@ -149,6 +149,11 @@ class FieldLookupBackend(BaseFilterBackend):
return field.to_python(value)
def value_to_python(self, model, lookup, value):
try:
lookup = lookup.encode("ascii")
except UnicodeEncodeError:
raise ValueError("%r is not an allowed field name. Must be ascii encodable." % lookup)
field, new_lookup = self.get_field_from_lookup(model, lookup)
# Type names are stored without underscores internally, but are presented and

View File

@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-
import pytest
from rest_framework.exceptions import PermissionDenied
@ -24,6 +26,14 @@ def test_valid_in(valid_value):
assert 'foo' in value
def test_invalid_field():
invalid_field = u"ヽヾ"
field_lookup = FieldLookupBackend()
with pytest.raises(ValueError) as excinfo:
field_lookup.value_to_python(WorkflowJobTemplate, invalid_field, 'foo')
assert 'is not an allowed field name. Must be ascii encodable.' in excinfo.value.message
@pytest.mark.parametrize('lookup_suffix', ['', 'contains', 'startswith', 'in'])
@pytest.mark.parametrize('password_field', Credential.PASSWORD_FIELDS)
def test_filter_on_password_field(password_field, lookup_suffix):