Update validators and filter to remove strings that contain only

whitespace

The validator ListItemRequired() was only checking for None and an empty
string, not for strings that were multiple whitespace characters. This
fixes this issue by checking each item with regex to make sure it
contains non whitespace characters

The filter remove_empty_string() also was not checking for strings that
were multiple whitespace characters. This was also fixed by using regex
tomake sure that the string contains non whitespace characters, and also
clips any trailing whitespace.
This commit is contained in:
leigh-mil
2019-11-07 11:25:18 -05:00
parent 045e06abee
commit ab9b62f54b
7 changed files with 60 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
import pytest
from wtforms.fields import RadioField
from wtforms.fields import RadioField, FieldList, StringField
from werkzeug.datastructures import ImmutableMultiDict
from atst.forms.forms import BaseForm
@@ -16,6 +16,12 @@ class FormWithChoices(BaseForm):
)
class FormWithList(BaseForm):
list = FieldList(
StringField("a very fancy list", filters=[BaseForm.remove_empty_string])
)
class TestBaseForm:
class Foo:
person = {"force_side": None}
@@ -34,3 +40,17 @@ class TestBaseForm:
form_data_3 = ImmutableMultiDict({"force_side": "dark"})
form_3 = FormWithChoices(form_data_3, obj=self.obj)
assert form_3.data["force_side"] is "dark"
@pytest.mark.parametrize(
"form_data",
[["testing", "", "QA"], ["testing", " ", "QA"], ["testing", None, "QA"]],
)
def test_blank_list_items_removed(self, form_data):
form = FormWithList(list=form_data)
assert form.validate(flash_invalid=False)
assert not form.data == ["testing", "QA"]
def test_remove_empty_string_clips_whitespace(self):
form = FormWithList(list=[" QA", " testing "])
assert form.validate(flash_invalid=False)
assert form.list.data == ["QA", "testing"]