Skip to content

Commit

Permalink
Replace unstable EnvironmentVarGuard with mock
Browse files Browse the repository at this point in the history
  • Loading branch information
katdom13 committed Nov 24, 2023
1 parent 6b72e82 commit 3ee61dc
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from __future__ import absolute_import, unicode_literals

try:
from test.support import EnvironmentVarGuard # Python < 3.10
except ImportError:
from test.support.os_helper import EnvironmentVarGuard # Python >= 3.10
from unittest import mock

from django.test import TestCase
from home.forms import CustomCaptchaFormBuilder
Expand All @@ -21,11 +18,18 @@ class CaptchaTestingModeMixin(TestCase):
"""Allow Captcha to pass regardless of the value provided"""

def setUp(self):
self.captcha_testing_mode_env = EnvironmentVarGuard()
self.captcha_testing_mode_env.set("RECAPTCHA_TESTING", "True")
# Use unittest.mock.patch to set the environment variable
self.captcha_testing_mode_patch = mock.patch.dict(
"os.environ", {"RECAPTCHA_TESTING": "True"}
)
self.captcha_testing_mode_patch.start()

self.captcha_form_data = {"recaptcha_response_field": "PASSED"}

def tearDown(self):
# Clean up the patch after the test
self.captcha_testing_mode_patch.stop()


class TestCaptchaEmailFormPageTestCase(CaptchaTestingModeMixin, TestCase):
fixtures = ["test_data.json"]
Expand Down

0 comments on commit 3ee61dc

Please sign in to comment.