|
| 1 | +import sys |
| 2 | +from types import ModuleType |
| 3 | + |
| 4 | +from django.conf import FORMS_URLFIELD_ASSUME_HTTPS_DEPRECATED_MSG, Settings, settings |
1 | 5 | from django.core.exceptions import ValidationError |
2 | 6 | from django.forms import URLField |
3 | 7 | from django.test import SimpleTestCase, ignore_warnings |
@@ -155,8 +159,41 @@ class URLFieldAssumeSchemeDeprecationTest(FormFieldAssertionsMixin, SimpleTestCa |
155 | 159 | def test_urlfield_raises_warning(self): |
156 | 160 | msg = ( |
157 | 161 | "The default scheme will be changed from 'http' to 'https' in Django 6.0. " |
158 | | - "Pass the forms.URLField.assume_scheme argument to silence this warning." |
| 162 | + "Pass the forms.URLField.assume_scheme argument to silence this warning, " |
| 163 | + "or set the FORMS_URLFIELD_ASSUME_HTTPS transitional setting to True to " |
| 164 | + "opt into using 'https' as the new default scheme." |
159 | 165 | ) |
160 | 166 | with self.assertWarnsMessage(RemovedInDjango60Warning, msg): |
161 | 167 | f = URLField() |
162 | 168 | self.assertEqual(f.clean("example.com"), "http://example.com") |
| 169 | + |
| 170 | + @ignore_warnings(category=RemovedInDjango60Warning) |
| 171 | + def test_urlfield_forms_urlfield_assume_https(self): |
| 172 | + with self.settings(FORMS_URLFIELD_ASSUME_HTTPS=True): |
| 173 | + f = URLField() |
| 174 | + self.assertEqual(f.clean("example.com"), "https://example.com") |
| 175 | + f = URLField(assume_scheme="http") |
| 176 | + self.assertEqual(f.clean("example.com"), "http://example.com") |
| 177 | + |
| 178 | + def test_override_forms_urlfield_assume_https_setting_warning(self): |
| 179 | + msg = FORMS_URLFIELD_ASSUME_HTTPS_DEPRECATED_MSG |
| 180 | + with self.assertRaisesMessage(RemovedInDjango60Warning, msg): |
| 181 | + # Changing FORMS_URLFIELD_ASSUME_HTTPS via self.settings() raises a |
| 182 | + # deprecation warning. |
| 183 | + with self.settings(FORMS_URLFIELD_ASSUME_HTTPS=True): |
| 184 | + pass |
| 185 | + |
| 186 | + def test_settings_init_forms_urlfield_assume_https_warning(self): |
| 187 | + settings_module = ModuleType("fake_settings_module") |
| 188 | + settings_module.FORMS_URLFIELD_ASSUME_HTTPS = True |
| 189 | + sys.modules["fake_settings_module"] = settings_module |
| 190 | + msg = FORMS_URLFIELD_ASSUME_HTTPS_DEPRECATED_MSG |
| 191 | + try: |
| 192 | + with self.assertRaisesMessage(RemovedInDjango60Warning, msg): |
| 193 | + Settings("fake_settings_module") |
| 194 | + finally: |
| 195 | + del sys.modules["fake_settings_module"] |
| 196 | + |
| 197 | + def test_access_forms_urlfield_assume_https(self): |
| 198 | + # Warning is not raised on access. |
| 199 | + self.assertEqual(settings.FORMS_URLFIELD_ASSUME_HTTPS, False) |
0 commit comments