question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CAPTCHA_TEST_MODE doesn't work with override_settings decorator

See original GitHub issue

Django provides some decorators to modify settings on a per-test or per-test-class basis: django.test.override_settings and django.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 time captcha.conf.settings is imported. Consequently, one must either specify CAPTCHA_TEST_MODE globally for all tests in a custom settings.py, or monkey-patch captcha.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 and modify_settings.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:10
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
atodorovcommented, Jan 26, 2022

FTR neither does this work:

            try:
                settings.CAPTCHA_TEST_MODE = True

                response = self.client.post(
                    self.register_url,
                    {
                        "username": username,
                        "password1": "password",
                        "password2": "password",
                        "email": "new-tester@example.com",
                        "captcha_0": "PASSED",
                        "captcha_1": "PASSED",
                    },
                    follow=follow,
                )
            finally:
                settings.CAPTCHA_TEST_MODE = False

This is the only way how I managed to get it working:

            try:
                # https://github.com/mbi/django-simple-captcha/issues/84
                from captcha.conf import settings as captcha_settings
                captcha_settings.CAPTCHA_TEST_MODE = True

                response = self.client.post(
                    self.register_url,
                    {
                        "username": username,
                        "password1": "password",
                        "password2": "password",
                        "email": "new-tester@example.com",
                        "captcha_0": "PASSED",
                        "captcha_1": "PASSED",
                    },
                    follow=follow,
                )
            finally:
                captcha_settings.CAPTCHA_TEST_MODE = False
1reaction
jna99commented, Dec 28, 2020

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: “captcha_0”: “8e10ebf60c5f23fd6e6a9959853730cd69062a15”, “captcha_1”: “PASSED”,

Read more comments on GitHub >

github_iconTop Results From Across the Web

How Django's "override_settings" decorator works when ...
I'm checking how Django's settings module is built and how the override_settings decorator deals with the settings when testing and I just ...
Read more >
How To Handle Captcha In Selenium - LambdaTest
This article will help you handle Captcha in Selenium WebDriver while performing Selenium automation testing.
Read more >
Nested @override_settings on class and method do not work ...
It seems to me that the overridden settings at method level are not cleared up after the test method runs, leaving the settings...
Read more >
SafetyNet reCAPTCHA API - Android Developers
When using the verifyWithRecaptcha() method in your app, you must do the following: Pass in your API site key as a parameter. Override...
Read more >
Customizing the Look and Feel of reCAPTCHA
To make the reCAPTCHA widget display a different theme, ... before the <form> element where reCAPTCHA appears (this will not work if placed ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found