diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 62e7aee..3d493f6 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,7 +3,8 @@ Changelog Unreleased ------------------ -#. Add Wagtail 2.0 compatibility +#. Add Wagtail 2.0 compatibility. +#. Ensure captcha field is properly removed from submission data. (issue #11) 0.2 (2016-10-16) ------------------ diff --git a/tests/test_models.py b/tests/test_models.py index 1dcb6bf..7e54896 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,20 +1,67 @@ from __future__ import absolute_import, unicode_literals +import json + from django.test import TestCase from home.models import TestCaptchaEmailFormPage, TestCaptchaFormPage from wagtailcaptcha.forms import WagtailCaptchaFormBuilder +try: + from test.test_support import EnvironmentVarGuard # Python 2 +except ImportError: + from test.support import EnvironmentVarGuard # Python 3 + + +class CaptchaTestingModeMixin(TestCase): + """Allow Captcha to pass regardless of the value provided""" + + def setUp(self): + self.captcha_testing_mode_env = EnvironmentVarGuard() + self.captcha_testing_mode_env.set('RECAPTCHA_TESTING', 'True') + + self.captcha_form_data = {'recaptcha_response_field': 'PASSED'} + + +class TestCaptchaEmailFormPageTestCase(CaptchaTestingModeMixin, TestCase): + fixtures = ['test_data.json'] -class TestCaptchaEmailFormPageTestCase(TestCase): def test_captcha_form_builder_is_set(self): page = TestCaptchaEmailFormPage() self.assertIs(page.form_builder, WagtailCaptchaFormBuilder) + def test_captcha_field_is_removed_from_submission_data(self): + page = TestCaptchaEmailFormPage.objects.get(slug='email-form') + form_data = dict(self.captcha_form_data, name='Robert') + form = page.get_form(form_data) + + with self.captcha_testing_mode_env: + self.assertTrue(form.is_valid()) + + form_submission = page.process_form_submission(form) + submission_data = json.loads(form_submission.form_data) + + self.assertNotIn(WagtailCaptchaFormBuilder.CAPTCHA_FIELD_NAME, submission_data) + + +class TestCaptchaFormPageTestCase(CaptchaTestingModeMixin, TestCase): + fixtures = ['test_data.json'] -class TestCaptchaFormPageTestCase(TestCase): def test_captcha_form_builder_is_set(self): page = TestCaptchaFormPage() self.assertIs(page.form_builder, WagtailCaptchaFormBuilder) + + def test_captcha_field_is_removed_from_submission_data(self): + page = TestCaptchaFormPage.objects.get(slug='form') + form_data = dict(self.captcha_form_data, name='Robert') + form = page.get_form(form_data) + + with self.captcha_testing_mode_env: + self.assertTrue(form.is_valid()) + + form_submission = page.process_form_submission(form) + submission_data = json.loads(form_submission.form_data) + + self.assertNotIn(WagtailCaptchaFormBuilder.CAPTCHA_FIELD_NAME, submission_data) diff --git a/tests/testapp/home/fixtures/test_data.json b/tests/testapp/home/fixtures/test_data.json index f199733..7ed1a29 100644 --- a/tests/testapp/home/fixtures/test_data.json +++ b/tests/testapp/home/fixtures/test_data.json @@ -129,14 +129,14 @@ "numchild": 0, "title": "Form", "draft_title": "Form", - "slug": "contact", + "slug": "form", "content_type": [ "home", "testcaptchaformpage" ], "live": true, "has_unpublished_changes": false, - "url_path": "/home/contact/", + "url_path": "/home/form/", "owner": [ "admin" ], diff --git a/wagtailcaptcha/forms.py b/wagtailcaptcha/forms.py index 9cae4ef..ccd0354 100644 --- a/wagtailcaptcha/forms.py +++ b/wagtailcaptcha/forms.py @@ -19,3 +19,8 @@ def formfields(self): fields[self.CAPTCHA_FIELD_NAME] = ReCaptchaField(label='') return fields + + +def remove_captcha_field(form): + form.fields.pop(WagtailCaptchaFormBuilder.CAPTCHA_FIELD_NAME, None) + form.cleaned_data.pop(WagtailCaptchaFormBuilder.CAPTCHA_FIELD_NAME, None) diff --git a/wagtailcaptcha/models.py b/wagtailcaptcha/models.py index 798897f..cc8140d 100644 --- a/wagtailcaptcha/models.py +++ b/wagtailcaptcha/models.py @@ -1,7 +1,7 @@ from __future__ import absolute_import, unicode_literals import wagtail -from .forms import WagtailCaptchaFormBuilder +from .forms import WagtailCaptchaFormBuilder, remove_captcha_field if wagtail.VERSION >= (2, 0): from wagtail.contrib.forms.models import AbstractEmailForm, AbstractForm @@ -17,8 +17,7 @@ def __init__(self, *args, **kwargs): self.form_builder = WagtailCaptchaFormBuilder def process_form_submission(self, form): - if WagtailCaptchaFormBuilder.CAPTCHA_FIELD_NAME in form.fields: - form.fields.pop(WagtailCaptchaFormBuilder.CAPTCHA_FIELD_NAME) + remove_captcha_field(form) return super(WagtailCaptchaEmailForm, self).process_form_submission(form) class Meta: @@ -32,5 +31,9 @@ def __init__(self, *args, **kwargs): super(WagtailCaptchaForm, self).__init__(*args, **kwargs) self.form_builder = WagtailCaptchaFormBuilder + def process_form_submission(self, form): + remove_captcha_field(form) + return super(WagtailCaptchaForm, self).process_form_submission(form) + class Meta: abstract = True