diff --git a/src/collective/easyform/fields.py b/src/collective/easyform/fields.py index 71f6063e..3e6805bc 100644 --- a/src/collective/easyform/fields.py +++ b/src/collective/easyform/fields.py @@ -35,6 +35,7 @@ class FieldExtenderValidator(z3c_validator.SimpleFieldValidator): def validate(self, value): """ Validate field by TValidator """ super(FieldExtenderValidator, self).validate(value) + efield = IFieldExtender(self.field) validators = getattr(efield, "validators", []) if validators: diff --git a/src/collective/easyform/tests/fixtures/fieldset_file.xml b/src/collective/easyform/tests/fixtures/fieldset_file.xml new file mode 100644 index 00000000..680f9977 --- /dev/null +++ b/src/collective/easyform/tests/fixtures/fieldset_file.xml @@ -0,0 +1,12 @@ + + +
+ + + False + Upload + +
+
+
diff --git a/src/collective/easyform/tests/testValidators.py b/src/collective/easyform/tests/testValidators.py index 6ef04c4a..e92a1de0 100644 --- a/src/collective/easyform/tests/testValidators.py +++ b/src/collective/easyform/tests/testValidators.py @@ -1,7 +1,11 @@ # -*- coding: utf-8 -*- +try: + from StringIO import StringIO # for Python 2 +except ImportError: + from io import StringIO # for Python 3 +from ZPublisher.HTTPRequest import FileUpload from plone.formwidget.recaptcha.interfaces import IReCaptchaSettings from plone.registry.interfaces import IRegistry - from collective.easyform import validators from collective.easyform.api import get_schema from collective.easyform.api import set_fields @@ -104,13 +108,7 @@ def test_talvalidator2(self): self.assertEqual(len(errors), 1) -class TestSingleFieldValidator(base.EasyFormTestCase): - - """ test validator in form outside of fieldset - - The test methods are reused in TestFieldsetValidator. - They use the same field, except that one has it in a fieldset. - """ +class LoadFixtureBase(base.EasyFormTestCase): schema_fixture = "single_field.xml" def afterSetUp(self): @@ -135,6 +133,16 @@ def LoadRequestForm(self, **kwargs): request.form[prefix + key] = kwargs[key] return request + +class TestSingleFieldValidator(LoadFixtureBase): + + """ test validator in form outside of fieldset + + The test methods are reused in TestFieldsetValidator. + They use the same field, except that one has it in a fieldset. + """ + schema_fixture = "single_field.xml" + def test_get_default(self): # With a GET, we should see the default value in the form. request = self.LoadRequestForm() @@ -172,7 +180,6 @@ class TestFieldsetValidator(TestSingleFieldValidator): class TestCustomValidators(base.EasyFormTestCase): - """ test our validators """ def ttest_inExNumericRange(self): @@ -342,25 +349,14 @@ def test_allowed_type_no_ext(self): self.assertEqual(translate(validation), u'File type "" is not allowed!') -class TestSingleRecaptchaValidator(base.EasyFormTestCase): +class TestSingleRecaptchaValidator(LoadFixtureBase): """ Can't test captcha passes but we can test it fails """ schema_fixture = "recaptcha.xml" def afterSetUp(self): - self.folder.invokeFactory("EasyForm", "ff1") - self.ff1 = getattr(self.folder, "ff1") - self.ff1.CSRFProtection = False # no csrf protection - self.ff1.showAll = True - field_template = api.content.create( - self.layer["portal"], "File", id="easyform_default_fields.xml" - ) - with open(join(dirname(__file__), "fixtures", self.schema_fixture)) as f: - filecontent = NamedFile(f.read(), contentType="application/xml") - field_template.file = filecontent - classImplements(BaseRequest, IFormLayer) - validators.update_validators() + super(TestSingleRecaptchaValidator, self).afterSetUp() # Put some dummy values for recaptcha registry = getUtility(IRegistry) @@ -368,14 +364,6 @@ def afterSetUp(self): proxy.public_key = u"foo" proxy.private_key = u"bar" - def LoadRequestForm(self, **kwargs): - request = self.layer["request"] - request.form.clear() - prefix = "form.widgets." - for key in kwargs.keys(): - request.form[prefix + key] = kwargs[key] - return request - def test_no_answer(self): data = {"verification": ""} request = self.LoadRequestForm(**data) @@ -389,3 +377,43 @@ def test_wrong(self): request.method = "POST" form = EasyFormForm(self.ff1, request)() self.assertIn('The code you entered was wrong, please enter the new one.', form) + + +class DummyUpload(FileUpload): + def __init__(self, size, filename): + self.file = StringIO("x" * size) + self.file.filename = filename + self.file.headers = [] + self.file.name = 'file1' + self.file.file = self.file + FileUpload.__init__(self, self.file) + + +class TestFieldsetFileValidator(LoadFixtureBase): + """ ensure file validators works + """ + + schema_fixture = "fieldset_file.xml" + + def test_wrong_type(self): + data = {"file1": DummyUpload(20, "blah.txt")} + request = self.LoadRequestForm(**data) + request.method = "POST" + form = EasyFormForm(self.ff1, request)() + self.assertNotIn('Thanks for your input.', form) + self.assertIn('File type "TXT" is not allowed!', form) + + def test_right_type(self): + data = {"file1": DummyUpload(20, "blah.pdf")} + request = self.LoadRequestForm(**data) + request.method = "POST" + form = EasyFormForm(self.ff1, request)() + self.assertIn('Thanks for your input.', form) + + def test_too_big(self): + data = {"file1": DummyUpload(2000, "blah.pdf")} + request = self.LoadRequestForm(**data) + request.method = "POST" + form = EasyFormForm(self.ff1, request)() + self.assertNotIn('Thanks for your input.', form) + self.assertIn('File is bigger than allowed size of 300 bytes!', form)