|
1 | 1 | from __future__ import absolute_import, unicode_literals |
2 | 2 |
|
| 3 | +import json |
| 4 | + |
3 | 5 | from django.test import TestCase |
4 | 6 |
|
5 | 7 | from home.models import TestCaptchaEmailFormPage, TestCaptchaFormPage |
6 | 8 | from wagtailcaptcha.forms import WagtailCaptchaFormBuilder |
7 | 9 |
|
| 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'] |
8 | 28 |
|
9 | | -class TestCaptchaEmailFormPageTestCase(TestCase): |
10 | 29 | def test_captcha_form_builder_is_set(self): |
11 | 30 | page = TestCaptchaEmailFormPage() |
12 | 31 |
|
13 | 32 | self.assertIs(page.form_builder, WagtailCaptchaFormBuilder) |
14 | 33 |
|
| 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'] |
15 | 50 |
|
16 | | -class TestCaptchaFormPageTestCase(TestCase): |
17 | 51 | def test_captcha_form_builder_is_set(self): |
18 | 52 | page = TestCaptchaFormPage() |
19 | 53 |
|
20 | 54 | 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) |
0 commit comments