Skip to content

Commit

Permalink
add in more file tests (#191)
Browse files Browse the repository at this point in the history
* add in more file tests

* fix for 5.0

* fix for py3

* flake 8
  • Loading branch information
djay committed Sep 27, 2019
1 parent b3b6f12 commit 459127c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/collective/easyform/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions src/collective/easyform/tests/fixtures/fieldset_file.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<model xmlns:easyform="http://namespaces.plone.org/supermodel/easyform" xmlns:form="http://namespaces.plone.org/supermodel/form" xmlns:i18n="http://xml.zope.org/namespaces/i18n" xmlns:indexer="http://namespaces.plone.org/supermodel/indexer" xmlns:lingua="http://namespaces.plone.org/supermodel/lingua" xmlns:marshal="http://namespaces.plone.org/supermodel/marshal" xmlns:security="http://namespaces.plone.org/supermodel/security" xmlns:users="http://namespaces.plone.org/supermodel/users" xmlns="http://namespaces.plone.org/supermodel/schema">
<schema>
<fieldset name="fs1" label="Fieldset 1">
<field name="file1" type="plone.namedfile.field.NamedFile" easyform:serverSide="False"
easyform:TValidator="python:portal.restrictedTraverse('validate_file')(value, size=300, allowed_types=('pdf', 'docx'))">
<description/>
<required>False</required>
<title>Upload</title>
</field>
</fieldset>
</schema>
</model>
88 changes: 58 additions & 30 deletions src/collective/easyform/tests/testValidators.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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()
Expand Down Expand Up @@ -172,7 +180,6 @@ class TestFieldsetValidator(TestSingleFieldValidator):


class TestCustomValidators(base.EasyFormTestCase):

""" test our validators """

def ttest_inExNumericRange(self):
Expand Down Expand Up @@ -342,40 +349,21 @@ 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)
proxy = registry.forInterface(IReCaptchaSettings)
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)
Expand All @@ -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)

0 comments on commit 459127c

Please sign in to comment.