Debian Codegraph @

tests/forms_tests/field_tests/test_urlfield.py

raw
1 import sys
2 from types import ModuleType
3
4 from django.conf import FORMS_URLFIELD_ASSUME_HTTPS_DEPRECATED_MSG, Settings, settings
5 from django.core.exceptions import ValidationError
6 from django.core.validators import URLValidator
7 from django.forms import URLField
8 from django.test import SimpleTestCase, ignore_warnings, override_settings
9 from django.utils.deprecation import RemovedInDjango60Warning
10
11 from . import FormFieldAssertionsMixin
12
13
14 @ignore_warnings(category=RemovedInDjango60Warning)
15 class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
16 def test_urlfield_widget(self):
17 f = URLField()
18 self.assertWidgetRendersTo(f, '<input type="url" name="f" id="id_f" required>')
19
20 def test_urlfield_widget_max_min_length(self):
21 f = URLField(min_length=15, max_length=20)
22 self.assertEqual("http://example.com", f.clean("http://example.com"))
23 self.assertWidgetRendersTo(
24 f,
25 '<input id="id_f" type="url" name="f" maxlength="20" '
26 'minlength="15" required>',
27 )
28 msg = "'Ensure this value has at least 15 characters (it has 12).'"
29 with self.assertRaisesMessage(ValidationError, msg):
30 f.clean("http://f.com")
31 msg = "'Ensure this value has at most 20 characters (it has 37).'"
32 with self.assertRaisesMessage(ValidationError, msg):
33 f.clean("http://abcdefghijklmnopqrstuvwxyz.com")
34
35 def test_urlfield_clean(self):
36 # RemovedInDjango60Warning: When the deprecation ends, remove the
37 # assume_scheme argument.
38 f = URLField(required=False, assume_scheme="https")
39 tests = [
40 ("http://localhost", "http://localhost"),
41 ("http://example.com", "http://example.com"),
42 ("http://example.com/test", "http://example.com/test"),
43 ("http://example.com.", "http://example.com."),
44 ("http://www.example.com", "http://www.example.com"),
45 ("http://www.example.com:8000/test", "http://www.example.com:8000/test"),
46 (
47 "http://example.com?some_param=some_value",
48 "http://example.com?some_param=some_value",
49 ),
50 ("valid-with-hyphens.com", "https://valid-with-hyphens.com"),
51 ("subdomain.domain.com", "https://subdomain.domain.com"),
52 ("http://200.8.9.10", "http://200.8.9.10"),
53 ("http://200.8.9.10:8000/test", "http://200.8.9.10:8000/test"),
54 ("http://valid-----hyphens.com", "http://valid-----hyphens.com"),
55 (
56 "http://some.idn.xyzäöüßabc.domain.com:123/blah",
57 "http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah",
58 ),
59 (
60 "www.example.com/s/http://code.djangoproject.com/ticket/13804",
61 "https://www.example.com/s/http://code.djangoproject.com/ticket/13804",
62 ),
63 # Normalization.
64 ("http://example.com/ ", "http://example.com/"),
65 # Valid IDN.
66 ("http://עברית.idn.icann.org/", "http://עברית.idn.icann.org/"),
67 ("http://sãopaulo.com/", "http://sãopaulo.com/"),
68 ("http://sãopaulo.com.br/", "http://sãopaulo.com.br/"),
69 ("http://пример.испытание/", "http://пример.испытание/"),
70 ("http://مثال.إختبار/", "http://مثال.إختبار/"),
71 ("http://例子.测试/", "http://例子.测试/"),
72 ("http://例子.測試/", "http://例子.測試/"),
73 (
74 "http://उदाहरण.परीक्षा/",
75 "http://उदाहरण.परीक्षा/",
76 ),
77 ("http://例え.テスト/", "http://例え.テスト/"),
78 ("http://مثال.آزمایشی/", "http://مثال.آزمایشی/"),
79 ("http://실례.테스트/", "http://실례.테스트/"),
80 ("http://العربية.idn.icann.org/", "http://العربية.idn.icann.org/"),
81 # IPv6.
82 ("http://[12:34::3a53]/", "http://[12:34::3a53]/"),
83 ("http://[a34:9238::]:8080/", "http://[a34:9238::]:8080/"),
84 # IPv6 without scheme.
85 ("[12:34::3a53]/", "https://[12:34::3a53]/"),
86 # IDN domain without scheme but with port.
87 ("ñandú.es:8080/", "https://ñandú.es:8080/"),
88 # Scheme-relative.
89 ("//example.com", "https://example.com"),
90 ("//example.com/path", "https://example.com/path"),
91 # Whitespace stripped.
92 ("\t\n//example.com \n\t\n", "https://example.com"),
93 ("\t\nhttp://example.com \n\t\n", "http://example.com"),
94 ]
95 for url, expected in tests:
96 with self.subTest(url=url):
97 self.assertEqual(f.clean(url), expected)
98
99 def test_urlfield_clean_invalid(self):
100 f = URLField()
101 tests = [
102 "foo",
103 "com.",
104 ".",
105 "http://",
106 "http://example",
107 "http://example.",
108 "http://.com",
109 "http://invalid-.com",
110 "http://-invalid.com",
111 "http://inv-.alid-.com",
112 "http://inv-.-alid.com",
113 "[a",
114 "http://[a",
115 # Non-string.
116 23,
117 # Hangs "forever" before fixing a catastrophic backtracking,
118 # see #11198.
119 "http://%s" % ("X" * 60,),
120 # A second example, to make sure the problem is really addressed,
121 # even on domains that don't fail the domain label length check in
122 # the regex.
123 "http://%s" % ("X" * 200,),
124 # Scheme prepend yields a structurally invalid URL.
125 "////]@N.AN",
126 # Scheme prepend yields an empty hostname.
127 "#@A.bO",
128 # Known problematic unicode chars.
129 "http://" + "¾" * 200,
130 # Non-ASCII character before the first colon.
131 "¾:example.com",
132 # ASCII digit before the first colon.
133 "1http://example.com",
134 # Empty scheme.
135 "://example.com",
136 ":example.com",
137 ]
138 msg = "'Enter a valid URL.'"
139 for value in tests:
140 with self.subTest(value=value):
141 with self.assertRaisesMessage(ValidationError, msg):
142 f.clean(value)
143
144 def test_urlfield_clean_required(self):
145 f = URLField()
146 msg = "'This field is required.'"
147 with self.assertRaisesMessage(ValidationError, msg):
148 f.clean(None)
149 with self.assertRaisesMessage(ValidationError, msg):
150 f.clean("")
151
152 def test_urlfield_clean_not_required(self):
153 f = URLField(required=False)
154 self.assertEqual(f.clean(None), "")
155 self.assertEqual(f.clean(""), "")
156
157 def test_urlfield_strip_on_none_value(self):
158 f = URLField(required=False, empty_value=None)
159 self.assertIsNone(f.clean(""))
160 self.assertIsNone(f.clean(None))
161
162 def test_urlfield_unable_to_set_strip_kwarg(self):
163 msg = "got multiple values for keyword argument 'strip'"
164 with self.assertRaisesMessage(TypeError, msg):
165 URLField(strip=False)
166
167 def test_urlfield_assume_scheme(self):
168 f = URLField()
169 # RemovedInDjango60Warning: When the deprecation ends, replace with:
170 # "https://example.com"
171 self.assertEqual(f.clean("example.com"), "http://example.com")
172 f = URLField(assume_scheme="http")
173 self.assertEqual(f.clean("example.com"), "http://example.com")
174 f = URLField(assume_scheme="https")
175 self.assertEqual(f.clean("example.com"), "https://example.com")
176
177 @override_settings(FORMS_URLFIELD_ASSUME_HTTPS=True)
178 def test_urlfield_assume_scheme_when_colons(self):
179 f = URLField()
180 tests = [
181 # Port number.
182 ("http://example.com:8080/", "http://example.com:8080/"),
183 ("https://example.com:443/path", "https://example.com:443/path"),
184 # Userinfo with password.
185 ("http://user:[email protected]", "http://user:[email protected]"),
186 (
187 "http://user:[email protected]:8080/",
188 "http://user:[email protected]:8080/",
189 ),
190 # Colon in path segment.
191 ("http://example.com/path:segment", "http://example.com/path:segment"),
192 ("http://example.com/a:b/c:d", "http://example.com/a:b/c:d"),
193 # Colon in query string.
194 ("http://example.com/?key=val:ue", "http://example.com/?key=val:ue"),
195 # Colon in fragment.
196 ("http://example.com/#section:1", "http://example.com/#section:1"),
197 # IPv6 -- multiple colons in host.
198 ("http://[::1]/", "http://[::1]/"),
199 ("http://[2001:db8::1]/", "http://[2001:db8::1]/"),
200 ("http://[2001:db8::1]:8080/", "http://[2001:db8::1]:8080/"),
201 # Colons across multiple components.
202 (
203 "http://user:[email protected]:8080/path:x?q=a:b#id:1",
204 "http://user:[email protected]:8080/path:x?q=a:b#id:1",
205 ),
206 # FTP with port and userinfo.
207 (
208 "ftp://user:[email protected]:21/file",
209 "ftp://user:[email protected]:21/file",
210 ),
211 (
212 "ftps://user:[email protected]:990/",
213 "ftps://user:[email protected]:990/",
214 ),
215 # Scheme-relative URLs, starts with "//".
216 ("//example.com:8080/path", "https://example.com:8080/path"),
217 ("//user:[email protected]/", "https://user:[email protected]/"),
218 ]
219 for value, expected in tests:
220 with self.subTest(value=value):
221 self.assertEqual(f.clean(value), expected)
222
223 def test_custom_validator_longer_max_length(self):
224
225 class CustomLongURLValidator(URLValidator):
226 max_length = 4096
227
228 class CustomURLField(URLField):
229 default_validators = [CustomLongURLValidator()]
230
231 field = CustomURLField()
232 # A URL with 4096 chars is valid given the custom validator.
233 prefix = "https://example.com/"
234 url = prefix + "a" * (4096 - len(prefix))
235 self.assertEqual(len(url), 4096)
236 # No ValidationError is raised.
237 field.clean(url)
238
239
240 class URLFieldAssumeSchemeDeprecationTest(FormFieldAssertionsMixin, SimpleTestCase):
241 def test_urlfield_raises_warning(self):
242 msg = (
243 "The default scheme will be changed from 'http' to 'https' in Django 6.0. "
244 "Pass the forms.URLField.assume_scheme argument to silence this warning, "
245 "or set the FORMS_URLFIELD_ASSUME_HTTPS transitional setting to True to "
246 "opt into using 'https' as the new default scheme."
247 )
248 with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx:
249 f = URLField()
250 self.assertEqual(f.clean("example.com"), "http://example.com")
251 self.assertEqual(ctx.filename, __file__)
252
253 @ignore_warnings(category=RemovedInDjango60Warning)
254 def test_urlfield_forms_urlfield_assume_https(self):
255 with self.settings(FORMS_URLFIELD_ASSUME_HTTPS=True):
256 f = URLField()
257 self.assertEqual(f.clean("example.com"), "https://example.com")
258 f = URLField(assume_scheme="http")
259 self.assertEqual(f.clean("example.com"), "http://example.com")
260
261 def test_override_forms_urlfield_assume_https_setting_warning(self):
262 msg = FORMS_URLFIELD_ASSUME_HTTPS_DEPRECATED_MSG
263 with self.assertRaisesMessage(RemovedInDjango60Warning, msg):
264 # Changing FORMS_URLFIELD_ASSUME_HTTPS via self.settings() raises a
265 # deprecation warning.
266 with self.settings(FORMS_URLFIELD_ASSUME_HTTPS=True):
267 pass
268
269 def test_settings_init_forms_urlfield_assume_https_warning(self):
270 settings_module = ModuleType("fake_settings_module")
271 settings_module.FORMS_URLFIELD_ASSUME_HTTPS = True
272 sys.modules["fake_settings_module"] = settings_module
273 msg = FORMS_URLFIELD_ASSUME_HTTPS_DEPRECATED_MSG
274 try:
275 with self.assertRaisesMessage(RemovedInDjango60Warning, msg):
276 Settings("fake_settings_module")
277 finally:
278 del sys.modules["fake_settings_module"]
279
280 def test_access_forms_urlfield_assume_https(self):
281 # Warning is not raised on access.
282 self.assertEqual(settings.FORMS_URLFIELD_ASSUME_HTTPS, False)