2020-02-12 13:48:49 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.forms import EmailField
|
2016-04-09 17:17:34 +00:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
|
|
|
from . import FormFieldAssertionsMixin
|
|
|
|
|
|
|
|
|
|
|
|
class EmailFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
|
|
|
|
def test_emailfield_1(self):
|
|
|
|
f = EmailField()
|
2023-06-14 10:23:06 +00:00
|
|
|
self.assertEqual(f.max_length, 320)
|
2018-01-21 07:09:10 +00:00
|
|
|
self.assertWidgetRendersTo(
|
2023-06-14 10:23:06 +00:00
|
|
|
f, '<input type="email" name="f" id="id_f" maxlength="320" required>'
|
2018-01-21 07:09:10 +00:00
|
|
|
)
|
2016-04-09 17:17:34 +00:00
|
|
|
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
|
|
|
f.clean("")
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
|
|
|
f.clean(None)
|
|
|
|
self.assertEqual("person@example.com", f.clean("person@example.com"))
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValidationError, "'Enter a valid email address.'"
|
|
|
|
):
|
|
|
|
f.clean("foo")
|
|
|
|
self.assertEqual(
|
|
|
|
"local@domain.with.idn.xyz\xe4\xf6\xfc\xdfabc.part.com",
|
|
|
|
f.clean("local@domain.with.idn.xyzäöüßabc.part.com"),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_email_regexp_for_performance(self):
|
|
|
|
f = EmailField()
|
|
|
|
# Check for runaway regex security problem. This will take a long time
|
|
|
|
# if the security fix isn't in place.
|
|
|
|
addr = "viewx3dtextx26qx3d@yahoo.comx26latlngx3d15854521645943074058"
|
|
|
|
self.assertEqual(addr, f.clean(addr))
|
|
|
|
|
|
|
|
def test_emailfield_not_required(self):
|
|
|
|
f = EmailField(required=False)
|
|
|
|
self.assertEqual("", f.clean(""))
|
|
|
|
self.assertEqual("", f.clean(None))
|
|
|
|
self.assertEqual("person@example.com", f.clean("person@example.com"))
|
|
|
|
self.assertEqual(
|
|
|
|
"example@example.com", f.clean(" example@example.com \t \t ")
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2016-04-09 17:17:34 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValidationError, "'Enter a valid email address.'"
|
|
|
|
):
|
|
|
|
f.clean("foo")
|
|
|
|
|
|
|
|
def test_emailfield_min_max_length(self):
|
|
|
|
f = EmailField(min_length=10, max_length=15)
|
2016-03-28 18:02:04 +00:00
|
|
|
self.assertWidgetRendersTo(
|
|
|
|
f,
|
2018-01-21 07:09:10 +00:00
|
|
|
'<input id="id_f" type="email" name="f" maxlength="15" minlength="10" '
|
|
|
|
"required>",
|
2016-03-28 18:02:04 +00:00
|
|
|
)
|
2016-04-09 17:17:34 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValidationError,
|
|
|
|
"'Ensure this value has at least 10 characters (it has 9).'",
|
|
|
|
):
|
|
|
|
f.clean("a@foo.com")
|
|
|
|
self.assertEqual("alf@foo.com", f.clean("alf@foo.com"))
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValidationError,
|
|
|
|
"'Ensure this value has at most 15 characters (it has 20).'",
|
|
|
|
):
|
|
|
|
f.clean("alf123456788@foo.com")
|
2016-07-03 14:41:59 +00:00
|
|
|
|
|
|
|
def test_emailfield_strip_on_none_value(self):
|
|
|
|
f = EmailField(required=False, empty_value=None)
|
2020-09-25 10:10:45 +00:00
|
|
|
self.assertIsNone(f.clean(""))
|
2016-07-03 14:41:59 +00:00
|
|
|
self.assertIsNone(f.clean(None))
|
|
|
|
|
|
|
|
def test_emailfield_unable_to_set_strip_kwarg(self):
|
2023-11-21 15:11:58 +00:00
|
|
|
msg = "got multiple values for keyword argument 'strip'"
|
2016-07-03 14:41:59 +00:00
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
EmailField(strip=False)
|