Skip to content

Commit

Permalink
Add a test for the CONFIG_OPTIONS "validate" key
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Nov 30, 2019
1 parent f025d18 commit 09e7138
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ Custom actions can be added by appending them to
def do_thing(model_instance, form_instance, request, config, **kwargs):
pass

def do_validate(form_instance, data):
pass

Form.CONFIG_OPTIONS.append(
('do_thing', {
'title': _('Do a thing'),
Expand All @@ -100,6 +103,7 @@ Custom actions can be added by appending them to
)),
],
'process': do_thing,
'validate': do_validate,
})
)

Expand Down
43 changes: 43 additions & 0 deletions tests/testapp/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django import forms
from django.contrib.auth.models import User
from django.core import mail
from django.test import TestCase
Expand All @@ -7,6 +8,16 @@
from form_designer.models import Form, FormSubmission, FIELD_TYPES


def validate_honeypot(form, data):
if data.get("honeypot"):
raise forms.ValidationError("Hello honeypot")


Form.CONFIG_OPTIONS.append(
("honeypot", {"title": "Honeypot", "validate": validate_honeypot})
)


class FormsTest(TestCase):
def test_forms(self):
form = Form.objects.create(
Expand Down Expand Up @@ -195,3 +206,35 @@ def test_admin(self):
)
self.assertContains(response, 'id="id_email_email"')
self.assertContains(response, "blabbb@example.com")

def test_honeypot(self):
form = Form.objects.create(
title="Test honeypot form", config_json=('{"honeypot": {}}'),
)
form.fields.create(ordering=0, title="Subject", name="subject", type="text")
form.fields.create(
ordering=0,
title="honeypot",
name="honeypot",
type="hidden",
is_required=False,
)

page = Page.objects.create(override_url="/", title="")
page.formcontent_set.create(
region="main",
ordering=0,
form=form,
success_message="Thanks, we will get back to you",
)

response = self.client.post(
"/",
{
"_formcontent".format(form.id): form.id,
"fc{0}-subject".format(form.id): "Test",
"fc{0}-honeypot".format(form.id): "honey",
},
)

self.assertContains(response, "Hello honeypot")

0 comments on commit 09e7138

Please sign in to comment.