summaryrefslogtreecommitdiff
path: root/tests/forms_tests
diff options
context:
space:
mode:
authorCoen van der Kamp <coen@fourdigits.nl>2023-03-08 20:12:34 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-04-28 06:58:10 +0200
commit7bbbadc69383f0a2b99253e153b974f8783e876d (patch)
treef0e4e8a38904aa9772fdae066dbda9dfd41d08b6 /tests/forms_tests
parent070cbac0dbf6a09b55aad322137ab168b75bf56b (diff)
downloaddjango-7bbbadc69383f0a2b99253e153b974f8783e876d.tar.gz
Fixed #34380 -- Allowed specifying a default URL scheme in forms.URLField.
This also deprecates "http" as the default scheme.
Diffstat (limited to 'tests/forms_tests')
-rw-r--r--tests/forms_tests/field_tests/test_urlfield.py35
-rw-r--r--tests/forms_tests/tests/test_error_messages.py6
2 files changed, 34 insertions, 7 deletions
diff --git a/tests/forms_tests/field_tests/test_urlfield.py b/tests/forms_tests/field_tests/test_urlfield.py
index 042a3bf586..058a2992ed 100644
--- a/tests/forms_tests/field_tests/test_urlfield.py
+++ b/tests/forms_tests/field_tests/test_urlfield.py
@@ -1,10 +1,12 @@
from django.core.exceptions import ValidationError
from django.forms import URLField
-from django.test import SimpleTestCase
+from django.test import SimpleTestCase, ignore_warnings
+from django.utils.deprecation import RemovedInDjango60Warning
from . import FormFieldAssertionsMixin
+@ignore_warnings(category=RemovedInDjango60Warning)
class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
def test_urlfield_widget(self):
f = URLField()
@@ -26,7 +28,9 @@ class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
f.clean("http://abcdefghijklmnopqrstuvwxyz.com")
def test_urlfield_clean(self):
- f = URLField(required=False)
+ # RemovedInDjango60Warning: When the deprecation ends, remove the
+ # assume_scheme argument.
+ f = URLField(required=False, assume_scheme="https")
tests = [
("http://localhost", "http://localhost"),
("http://example.com", "http://example.com"),
@@ -38,8 +42,8 @@ class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
"http://example.com?some_param=some_value",
"http://example.com?some_param=some_value",
),
- ("valid-with-hyphens.com", "http://valid-with-hyphens.com"),
- ("subdomain.domain.com", "http://subdomain.domain.com"),
+ ("valid-with-hyphens.com", "https://valid-with-hyphens.com"),
+ ("subdomain.domain.com", "https://subdomain.domain.com"),
("http://200.8.9.10", "http://200.8.9.10"),
("http://200.8.9.10:8000/test", "http://200.8.9.10:8000/test"),
("http://valid-----hyphens.com", "http://valid-----hyphens.com"),
@@ -49,7 +53,7 @@ class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
),
(
"www.example.com/s/http://code.djangoproject.com/ticket/13804",
- "http://www.example.com/s/http://code.djangoproject.com/ticket/13804",
+ "https://www.example.com/s/http://code.djangoproject.com/ticket/13804",
),
# Normalization.
("http://example.com/ ", "http://example.com/"),
@@ -135,3 +139,24 @@ class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
msg = "__init__() got multiple values for keyword argument 'strip'"
with self.assertRaisesMessage(TypeError, msg):
URLField(strip=False)
+
+ def test_urlfield_assume_scheme(self):
+ f = URLField()
+ # RemovedInDjango60Warning: When the deprecation ends, replace with:
+ # "https://example.com"
+ self.assertEqual(f.clean("example.com"), "http://example.com")
+ f = URLField(assume_scheme="http")
+ self.assertEqual(f.clean("example.com"), "http://example.com")
+ f = URLField(assume_scheme="https")
+ self.assertEqual(f.clean("example.com"), "https://example.com")
+
+
+class URLFieldAssumeSchemeDeprecationTest(FormFieldAssertionsMixin, SimpleTestCase):
+ 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."
+ )
+ with self.assertWarnsMessage(RemovedInDjango60Warning, msg):
+ f = URLField()
+ self.assertEqual(f.clean("example.com"), "http://example.com")
diff --git a/tests/forms_tests/tests/test_error_messages.py b/tests/forms_tests/tests/test_error_messages.py
index 2ad356858f..e44c6d6668 100644
--- a/tests/forms_tests/tests/test_error_messages.py
+++ b/tests/forms_tests/tests/test_error_messages.py
@@ -23,7 +23,8 @@ from django.forms import (
utils,
)
from django.template import Context, Template
-from django.test import SimpleTestCase, TestCase
+from django.test import SimpleTestCase, TestCase, ignore_warnings
+from django.utils.deprecation import RemovedInDjango60Warning
from django.utils.safestring import mark_safe
from ..models import ChoiceModel
@@ -167,7 +168,8 @@ class FormsErrorMessagesTestCase(SimpleTestCase, AssertFormErrorsMixin):
"invalid": "INVALID",
"max_length": '"%(value)s" has more than %(limit_value)d characters.',
}
- f = URLField(error_messages=e, max_length=17)
+ with ignore_warnings(category=RemovedInDjango60Warning):
+ f = URLField(error_messages=e, max_length=17)
self.assertFormErrors(["REQUIRED"], f.clean, "")
self.assertFormErrors(["INVALID"], f.clean, "abc.c")
self.assertFormErrors(