Skip to content

Commit

Permalink
Ensure captcha field is properly removed from submission data (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
loicteixeira authored Feb 26, 2018
1 parent f0f15ab commit 7b8c73e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------
Expand Down
51 changes: 49 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions tests/testapp/home/fixtures/test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand Down
5 changes: 5 additions & 0 deletions wagtailcaptcha/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 6 additions & 3 deletions wagtailcaptcha/models.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand All @@ -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

0 comments on commit 7b8c73e

Please sign in to comment.