Skip to content

Commit 7b8c73e

Browse files
authored
Ensure captcha field is properly removed from submission data (#16)
1 parent f0f15ab commit 7b8c73e

5 files changed

Lines changed: 64 additions & 8 deletions

File tree

CHANGELOG.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ Changelog
33

44
Unreleased
55
------------------
6-
#. Add Wagtail 2.0 compatibility
6+
#. Add Wagtail 2.0 compatibility.
7+
#. Ensure captcha field is properly removed from submission data. (issue #11)
78

89
0.2 (2016-10-16)
910
------------------

tests/test_models.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,67 @@
11
from __future__ import absolute_import, unicode_literals
22

3+
import json
4+
35
from django.test import TestCase
46

57
from home.models import TestCaptchaEmailFormPage, TestCaptchaFormPage
68
from wagtailcaptcha.forms import WagtailCaptchaFormBuilder
79

10+
try:
11+
from test.test_support import EnvironmentVarGuard # Python 2
12+
except ImportError:
13+
from test.support import EnvironmentVarGuard # Python 3
14+
15+
16+
class CaptchaTestingModeMixin(TestCase):
17+
"""Allow Captcha to pass regardless of the value provided"""
18+
19+
def setUp(self):
20+
self.captcha_testing_mode_env = EnvironmentVarGuard()
21+
self.captcha_testing_mode_env.set('RECAPTCHA_TESTING', 'True')
22+
23+
self.captcha_form_data = {'recaptcha_response_field': 'PASSED'}
24+
25+
26+
class TestCaptchaEmailFormPageTestCase(CaptchaTestingModeMixin, TestCase):
27+
fixtures = ['test_data.json']
828

9-
class TestCaptchaEmailFormPageTestCase(TestCase):
1029
def test_captcha_form_builder_is_set(self):
1130
page = TestCaptchaEmailFormPage()
1231

1332
self.assertIs(page.form_builder, WagtailCaptchaFormBuilder)
1433

34+
def test_captcha_field_is_removed_from_submission_data(self):
35+
page = TestCaptchaEmailFormPage.objects.get(slug='email-form')
36+
form_data = dict(self.captcha_form_data, name='Robert')
37+
form = page.get_form(form_data)
38+
39+
with self.captcha_testing_mode_env:
40+
self.assertTrue(form.is_valid())
41+
42+
form_submission = page.process_form_submission(form)
43+
submission_data = json.loads(form_submission.form_data)
44+
45+
self.assertNotIn(WagtailCaptchaFormBuilder.CAPTCHA_FIELD_NAME, submission_data)
46+
47+
48+
class TestCaptchaFormPageTestCase(CaptchaTestingModeMixin, TestCase):
49+
fixtures = ['test_data.json']
1550

16-
class TestCaptchaFormPageTestCase(TestCase):
1751
def test_captcha_form_builder_is_set(self):
1852
page = TestCaptchaFormPage()
1953

2054
self.assertIs(page.form_builder, WagtailCaptchaFormBuilder)
55+
56+
def test_captcha_field_is_removed_from_submission_data(self):
57+
page = TestCaptchaFormPage.objects.get(slug='form')
58+
form_data = dict(self.captcha_form_data, name='Robert')
59+
form = page.get_form(form_data)
60+
61+
with self.captcha_testing_mode_env:
62+
self.assertTrue(form.is_valid())
63+
64+
form_submission = page.process_form_submission(form)
65+
submission_data = json.loads(form_submission.form_data)
66+
67+
self.assertNotIn(WagtailCaptchaFormBuilder.CAPTCHA_FIELD_NAME, submission_data)

tests/testapp/home/fixtures/test_data.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@
129129
"numchild": 0,
130130
"title": "Form",
131131
"draft_title": "Form",
132-
"slug": "contact",
132+
"slug": "form",
133133
"content_type": [
134134
"home",
135135
"testcaptchaformpage"
136136
],
137137
"live": true,
138138
"has_unpublished_changes": false,
139-
"url_path": "/home/contact/",
139+
"url_path": "/home/form/",
140140
"owner": [
141141
"admin"
142142
],

wagtailcaptcha/forms.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ def formfields(self):
1919
fields[self.CAPTCHA_FIELD_NAME] = ReCaptchaField(label='')
2020

2121
return fields
22+
23+
24+
def remove_captcha_field(form):
25+
form.fields.pop(WagtailCaptchaFormBuilder.CAPTCHA_FIELD_NAME, None)
26+
form.cleaned_data.pop(WagtailCaptchaFormBuilder.CAPTCHA_FIELD_NAME, None)

wagtailcaptcha/models.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import absolute_import, unicode_literals
22

33
import wagtail
4-
from .forms import WagtailCaptchaFormBuilder
4+
from .forms import WagtailCaptchaFormBuilder, remove_captcha_field
55

66
if wagtail.VERSION >= (2, 0):
77
from wagtail.contrib.forms.models import AbstractEmailForm, AbstractForm
@@ -17,8 +17,7 @@ def __init__(self, *args, **kwargs):
1717
self.form_builder = WagtailCaptchaFormBuilder
1818

1919
def process_form_submission(self, form):
20-
if WagtailCaptchaFormBuilder.CAPTCHA_FIELD_NAME in form.fields:
21-
form.fields.pop(WagtailCaptchaFormBuilder.CAPTCHA_FIELD_NAME)
20+
remove_captcha_field(form)
2221
return super(WagtailCaptchaEmailForm, self).process_form_submission(form)
2322

2423
class Meta:
@@ -32,5 +31,9 @@ def __init__(self, *args, **kwargs):
3231
super(WagtailCaptchaForm, self).__init__(*args, **kwargs)
3332
self.form_builder = WagtailCaptchaFormBuilder
3433

34+
def process_form_submission(self, form):
35+
remove_captcha_field(form)
36+
return super(WagtailCaptchaForm, self).process_form_submission(form)
37+
3538
class Meta:
3639
abstract = True

0 commit comments

Comments
 (0)