Skip to content

Commit

Permalink
Put in tests to show recaptcha validation prevents submissions (#189)
Browse files Browse the repository at this point in the history
* Put in tests to show recaptcha validation prevents submissions

* add robot tests to coverage

* fix flake8

* try improve caching
  • Loading branch information
djay committed Sep 25, 2019
1 parent 02a1893 commit 6328fa1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -26,6 +26,7 @@ cache:
- eggs
- downloads
- buildout-cache
- $HOME/buildout-cache
before_install:
# install chrome webdriver
- mkdir webdriver;
Expand Down
6 changes: 4 additions & 2 deletions CHANGES.rst
Expand Up @@ -25,10 +25,12 @@ issues if those validators, or default values, were misconfigured in the first p

- Add collective.z3cform.norobots integration #145
[1letter/gomez]

- For CSV and XML attachments send an empty string if the value is None instead of the string "None"
[nngu6036]


- Put in tests to show recaptcha validation prevents submissions
[djay]

2.1.0 (2019-04-25)
------------------
Expand Down
2 changes: 1 addition & 1 deletion buildout.cfg
Expand Up @@ -4,7 +4,7 @@ extends = tests-5.2.x.cfg
parts +=
releaser
i18ndude
omelette
# omelette

[instance]
eggs +=
Expand Down
4 changes: 4 additions & 0 deletions src/collective/easyform/tests/base.py
Expand Up @@ -58,6 +58,10 @@ def setUpZope(self, app, configurationContext):
def setUpPloneSite(self, portal):
# Install the collective.easyform product
self.applyProfile(portal, "collective.easyform:default")
try:
self.applyProfile(portal, "plone.formwidget.recaptcha:default")
except KeyError:
pass
setRoles(portal, TEST_USER_ID, ["Manager"])
portal.manage_changeProperties(email_from_address="mdummy@address.com")
portal.MailHost = mailhost = MailHostMock()
Expand Down
9 changes: 9 additions & 0 deletions src/collective/easyform/tests/fixtures/recaptcha.xml
@@ -0,0 +1,9 @@
<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>
<field name="verification" type="collective.easyform.fields.ReCaptcha">
<description/>
<required>False</required>
<title>Verification</title>
</field>
</schema>
</model>
52 changes: 52 additions & 0 deletions src/collective/easyform/tests/testValidators.py
@@ -1,4 +1,7 @@
# -*- coding: utf-8 -*-
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 @@ -337,3 +340,52 @@ def test_allowed_type_no_ext(self):
DummyFile(filename="foo"), allowed_types=("txt",)
)
self.assertEqual(translate(validation), u'File type "" is not allowed!')


class TestSingleRecaptchaValidator(base.EasyFormTestCase):

""" 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()

# 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)
request.method = "POST"
form = EasyFormForm(self.ff1, request)()
self.assertIn('The code you entered was wrong, please enter the new one.', form)

def test_wrong(self):
data = {"verification": "123"}
request = self.LoadRequestForm(**data)
request.method = "POST"
form = EasyFormForm(self.ff1, request)()
self.assertIn('The code you entered was wrong, please enter the new one.', form)

0 comments on commit 6328fa1

Please sign in to comment.