-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure captcha field is properly removed from submission data (#16)
- Loading branch information
1 parent
f0f15ab
commit 7b8c73e
Showing
5 changed files
with
64 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters