Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore captcha errors during a test #231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions captcha/test_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from captcha.conf import settings as captcha_settings
from django.test.utils import TestContextDecorator

class ignore_captcha_errors(TestContextDecorator):
"""provides a class decorator/context manager to ignore captcha errors during a test.
To be used as:
from captcha.test_helper import ignore_captcha_errors
class TestSomething(SimpleTestCase):
@ignore_captcha_errors()
def test_something(self):
response = self.client.post(my_url, {... 'captcha_0': 'whatever', 'captcha_1': 'passed'}, follow=True)

or

from captcha.test_helper import ignore_captcha_errors
class TestSomething(SimpleTestCase):

def test_something(self):
with ignore_captcha_errors():
response = self.client.post(my_url, {... 'captcha_0': 'whatever', 'captcha_1': 'passed'}, follow=True)
"""
def __init__(self):
super().__init__()
self.captcha_test_mode = captcha_settings.CAPTCHA_TEST_MODE

def enable(self):
captcha_settings.CAPTCHA_TEST_MODE = True

def disable(self):
captcha_settings.CAPTCHA_TEST_MODE = self.captcha_test_mode

def decorate_class(self, cls):
from django.test import SimpleTestCase
if not issubclass(cls, SimpleTestCase):
raise ValueError(
"Only subclasses of Django SimpleTestCase can be decorated "
"with ignore_captcha_errors"
)
self.captcha_test_mode = captcha_settings.CAPTCHA_TEST_MODE
return cls