Skip to content

Commit

Permalink
Merge 411f378 into d124211
Browse files Browse the repository at this point in the history
  • Loading branch information
AltusBarry committed Jan 8, 2019
2 parents d124211 + 411f378 commit 3cda438
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 51 deletions.
2 changes: 2 additions & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Praekelt Consulting
* Shaun Sephton
* Peter Pistorius
* Hedley Roos
* Altus Barry
* Cilliers Blignaut

bTaylor Design
--------------
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ Pending
-------

#. Added testing for Django 2.1 (no code changes needed).
#. Update tests to no longer test reCAPTCHA v1.
#. Update unit tests to no longer make use of reCAPTCHA v1.
#. Added deprecation warnings for reCAPTCHA v1 support.
#. Remove need for RECAPTCHA_TESTING environment variable during unit testing.

1.4.0 (2018-02-08)
------------------
Expand Down
33 changes: 0 additions & 33 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,39 +111,6 @@ Local Development and Functional Testing
Google provides test keys which are set as the default for ``RECAPTCHA_PUBLIC_KEY`` and ``RECAPTCHA_PRIVATE_KEY``. These cannot be used in production since they always validate to true and a warning will be shown on the reCAPTCHA.


Unit Testing
~~~~~~~~~~~~

Django reCAPTCHA introduces an environment variable ``RECAPTCHA_TESTING`` which
helps facilitate tests. The environment variable should be set to ``"True"``,
and cleared, using the ``setUp()`` and ``tearDown()`` methods in your test
classes.

Setting ``RECAPTCHA_TESTING`` to ``True`` causes Django reCAPTCHA to accept
``"PASSED"`` as the ``recaptcha_response_field`` value. Note that if you are
using the new No Captcha reCaptcha (ie. with ``NOCAPTCHA = True`` in your
settings) the response field is called ``g-recaptcha-response``.

Example:

.. code-block:: python
import os
os.environ['RECAPTCHA_TESTING'] = 'True'
form_params = {'recaptcha_response_field': 'PASSED'} # use 'g-recaptcha-response' param name if using NOCAPTCHA
form = RegistrationForm(form_params) # assuming only one ReCaptchaField
form.is_valid() # True
os.environ['RECAPTCHA_TESTING'] = 'False'
form.is_valid() # False
Passing any other values will cause Django reCAPTCHA to continue normal
processing and return a form error.

Check ``tests.py`` for a full example.


AJAX
~~~~~

Expand Down
4 changes: 0 additions & 4 deletions captcha/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ def clean(self, values):
recaptcha_challenge_value = force_text(values[0])
recaptcha_response_value = force_text(values[1])

if os.environ.get('RECAPTCHA_TESTING', None) == 'True' and \
recaptcha_response_value == 'PASSED':
return values[0]

if not self.required:
return

Expand Down
Empty file added captcha/tests/__init__.py
Empty file.
1 change: 1 addition & 0 deletions captcha/tests/requirements/py27.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock
Empty file.
File renamed without changes.
27 changes: 15 additions & 12 deletions captcha/tests.py → captcha/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
import warnings
import os
import warnings

try:
from unittest.mock import patch
except ImportError:
from mock import patch

from captcha import fields
from django.forms import Form
from django.test import TestCase, override_settings

from captcha.client import RecaptchaResponse


class TestForm(Form):
captcha = fields.ReCaptchaField(attrs={'theme': 'white'})


class TestCase(TestCase):
def setUp(self):
os.environ['RECAPTCHA_TESTING'] = 'True'

# No longer supports reCAPTCHA v1, override settings during tests to always
# use v2 reCAPTCHA. Prevents HTTP 410 error.
@override_settings(NOCAPTCHA=True)
def test_envvar_enabled(self):
@patch("captcha.fields.client.submit")
def test_client_success_response(self, mocked_submit):
mocked_submit.return_value = RecaptchaResponse(is_valid=True)
form_params = {'recaptcha_response_field': 'PASSED'}
form = TestForm(form_params)
self.assertTrue(form.is_valid())

# No longer supports reCAPTCHA v1, override settings during tests to always
# use v2 reCAPTCHA. Prevents HTTP 410 error.
@override_settings(NOCAPTCHA=True)
def test_envvar_disabled(self):
os.environ['RECAPTCHA_TESTING'] = 'False'
@patch("captcha.fields.client.submit")
def test_client_failure_response(self, mocked_submit):
mocked_submit.return_value = RecaptchaResponse(is_valid=False, error_code="410")
form_params = {'recaptcha_response_field': 'PASSED'}
form = TestForm(form_params)
self.assertFalse(form.is_valid())
Expand All @@ -35,9 +42,8 @@ def test_envvar_disabled(self):
# use v2 reCAPTCHA. Prevents HTTP 410 error.
@override_settings(NOCAPTCHA=True)
def test_deprecation_warning(self):
os.environ['RECAPTCHA_TESTING'] = 'False'
warnings.simplefilter("always")
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
form_params = {'recaptcha_response_field': 'PASSED'}
form = TestForm(form_params)

Expand All @@ -46,6 +52,3 @@ def test_deprecation_warning(self):
assert len(w) == 1
assert issubclass(w[-1].category, DeprecationWarning)
assert "reCAPTCHA v1 will no longer be" in str(w[-1].message)

def tearDown(self):
del os.environ['RECAPTCHA_TESTING']
2 changes: 1 addition & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "captcha.tests.settings.coveralls_settings")

from django.core.management import execute_from_command_line

Expand Down
15 changes: 15 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,18 @@ deps =
django111: Django<2.0
django2: Django<2.1
django21: Django<2.2

[testenv:django111-py27]
deps =
{[testenv]deps}
-rcaptcha/tests/requirements/py27.txt

[testenv:django2-py27]
deps =
{[testenv]deps}
-rcaptcha/tests/requirements/py27.txt

[testenv:django21-py27]
deps =
{[testenv]deps}
-rcaptcha/tests/requirements/py27.txt

0 comments on commit 3cda438

Please sign in to comment.