-
Notifications
You must be signed in to change notification settings - Fork 324
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
CAPTCHA_TEST_MODE doesn't work with override_settings decorator #84
Comments
agree: neither @django.test.override_settings nor django.conf.settings worked for me. I can see them altered, but since captcha.conf.settings is only imported the first time, these are ignored. Interestingly, I see the tests(django-simple-captcha/captcha/tests/tests.py) overriding these on the fly, but pretty sure those aren't passing(test_test_mode_issue15). |
I suggest to use django-appsettings or similar project to manage application settings. |
There is a solution for this. To make the CAPTCHA_TEST_MODE work, it needs to be set to True and the form will only be valid if you set captcha_0 and captcha_1. captcha_0 needs a hash (I copied one from my browser) and captcha_1 needs to contain "PASSED" (can be lower or uppercase) in string format. for example: |
Hello,
Now, if TESTING is True, CAPTCHA_TEST_MODE is also set to True and you can validate your form as usually.
|
FTR neither does this work:
This is the only way how I managed to get it working:
|
This worked for me:
I hope it helps. |
Seeing the 2 previous answers which are actually the only ones working, maybe the package should provide it's own decorator/context manager eg something like '@ignore_captcha_errors'? Starting from there, I wrote this small piece of code and tested it and it works! from captcha.conf import settings as captcha_settings
from django.test.utils import TestContextDecorator
class ignore_captcha_errors(TestContextDecorator):
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 To be used as 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 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) I'll do a PR for that |
Django provides some decorators to modify settings on a per-test or per-test-class basis:
django.test.override_settings
anddjango.test.modify_settings
(https://docs.djangoproject.com/en/dev/topics/testing/tools/#overriding-settings).However, it seems django-simple-captcha reads and stores the project's value of
settings.CAPTCHA_TEST_MODE
(and other settings) only the first timecaptcha.conf.settings
is imported. Consequently, one must either specify CAPTCHA_TEST_MODE globally for all tests in a custom settings.py, or monkey-patchcaptcha.conf.settings
...It would make testing easier if settings were always read on-the-fly, rather than cached.
Or, failing that, it would be good to at least provide notice in the config documentation (particularly for CAPTCHA_TEST_MODE) that they cannot be overridden by
override_settings
andmodify_settings
.The text was updated successfully, but these errors were encountered: