diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-11-28 20:04:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-28 20:04:21 +0100 |
| commit | a4931cd75a1780923b02e43475ba5447df3adb31 (patch) | |
| tree | df3412eb589599430990d1efa010dd999884ff68 /tests | |
| parent | 5f9e5c1b0d5680f793ba7646d52ffab9e2c3623f (diff) | |
Refs #34380 -- Added FORMS_URLFIELD_ASSUME_HTTPS transitional setting.
This allows early adoption of the new default "https".
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/forms_tests/field_tests/test_urlfield.py | 39 | ||||
| -rw-r--r-- | tests/model_forms/tests.py | 15 |
2 files changed, 52 insertions, 2 deletions
diff --git a/tests/forms_tests/field_tests/test_urlfield.py b/tests/forms_tests/field_tests/test_urlfield.py index 2cd1a82694..8ba7842064 100644 --- a/tests/forms_tests/field_tests/test_urlfield.py +++ b/tests/forms_tests/field_tests/test_urlfield.py @@ -1,3 +1,7 @@ +import sys +from types import ModuleType + +from django.conf import FORMS_URLFIELD_ASSUME_HTTPS_DEPRECATED_MSG, Settings, settings from django.core.exceptions import ValidationError from django.forms import URLField from django.test import SimpleTestCase, ignore_warnings @@ -155,8 +159,41 @@ class URLFieldAssumeSchemeDeprecationTest(FormFieldAssertionsMixin, SimpleTestCa def test_urlfield_raises_warning(self): msg = ( "The default scheme will be changed from 'http' to 'https' in Django 6.0. " - "Pass the forms.URLField.assume_scheme argument to silence this warning." + "Pass the forms.URLField.assume_scheme argument to silence this warning, " + "or set the FORMS_URLFIELD_ASSUME_HTTPS transitional setting to True to " + "opt into using 'https' as the new default scheme." ) with self.assertWarnsMessage(RemovedInDjango60Warning, msg): f = URLField() self.assertEqual(f.clean("example.com"), "http://example.com") + + @ignore_warnings(category=RemovedInDjango60Warning) + def test_urlfield_forms_urlfield_assume_https(self): + with self.settings(FORMS_URLFIELD_ASSUME_HTTPS=True): + f = URLField() + self.assertEqual(f.clean("example.com"), "https://example.com") + f = URLField(assume_scheme="http") + self.assertEqual(f.clean("example.com"), "http://example.com") + + def test_override_forms_urlfield_assume_https_setting_warning(self): + msg = FORMS_URLFIELD_ASSUME_HTTPS_DEPRECATED_MSG + with self.assertRaisesMessage(RemovedInDjango60Warning, msg): + # Changing FORMS_URLFIELD_ASSUME_HTTPS via self.settings() raises a + # deprecation warning. + with self.settings(FORMS_URLFIELD_ASSUME_HTTPS=True): + pass + + def test_settings_init_forms_urlfield_assume_https_warning(self): + settings_module = ModuleType("fake_settings_module") + settings_module.FORMS_URLFIELD_ASSUME_HTTPS = True + sys.modules["fake_settings_module"] = settings_module + msg = FORMS_URLFIELD_ASSUME_HTTPS_DEPRECATED_MSG + try: + with self.assertRaisesMessage(RemovedInDjango60Warning, msg): + Settings("fake_settings_module") + finally: + del sys.modules["fake_settings_module"] + + def test_access_forms_urlfield_assume_https(self): + # Warning is not raised on access. + self.assertEqual(settings.FORMS_URLFIELD_ASSUME_HTTPS, False) diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index f706271106..3f927cb053 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -2930,7 +2930,8 @@ class ModelOtherFieldTests(SimpleTestCase): msg = ( "The default scheme will be changed from 'http' to 'https' in Django " "6.0. Pass the forms.URLField.assume_scheme argument to silence this " - "warning." + "warning, or set the FORMS_URLFIELD_ASSUME_HTTPS transitional setting to " + "True to opt into using 'https' as the new default scheme." ) with self.assertWarnsMessage(RemovedInDjango60Warning, msg): @@ -2939,6 +2940,18 @@ class ModelOtherFieldTests(SimpleTestCase): model = Homepage fields = "__all__" + def test_url_modelform_assume_scheme_early_adopt_https(self): + msg = "The FORMS_URLFIELD_ASSUME_HTTPS transitional setting is deprecated." + with ( + self.assertWarnsMessage(RemovedInDjango60Warning, msg), + self.settings(FORMS_URLFIELD_ASSUME_HTTPS=True), + ): + + class HomepageForm(forms.ModelForm): + class Meta: + model = Homepage + fields = "__all__" + def test_modelform_non_editable_field(self): """ When explicitly including a non-editable field in a ModelForm, the |
