From 3f3166679659ac19b0651fb92d321153568a53b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20W=C3=B3jcik?= Date: Fri, 24 Feb 2017 16:18:34 -0500 Subject: [PATCH] Fix the exception message when validating unicode URLs (#1486) --- mongoengine/fields.py | 4 ++-- tests/fields/fields.py | 26 ++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/mongoengine/fields.py b/mongoengine/fields.py index 0ea7d3b6..11425095 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -139,12 +139,12 @@ class URLField(StringField): # Check first if the scheme is valid scheme = value.split('://')[0].lower() if scheme not in self.schemes: - self.error('Invalid scheme {} in URL: {}'.format(scheme, value)) + self.error(u'Invalid scheme {} in URL: {}'.format(scheme, value)) return # Then check full URL if not self.url_regex.match(value): - self.error('Invalid URL: {}'.format(value)) + self.error(u'Invalid URL: {}'.format(value)) return diff --git a/tests/fields/fields.py b/tests/fields/fields.py index e6898e1b..318c0c59 100644 --- a/tests/fields/fields.py +++ b/tests/fields/fields.py @@ -341,11 +341,12 @@ class FieldTest(MongoDBTestCase): person.validate() def test_url_validation(self): - """Ensure that URLFields validate urls properly. - """ + """Ensure that URLFields validate urls properly.""" class Link(Document): url = URLField() + Link.drop_collection() + link = Link() link.url = 'google' self.assertRaises(ValidationError, link.validate) @@ -353,6 +354,27 @@ class FieldTest(MongoDBTestCase): link.url = 'http://www.google.com:8080' link.validate() + def test_unicode_url_validation(self): + """Ensure unicode URLs are validated properly.""" + class Link(Document): + url = URLField() + + Link.drop_collection() + + link = Link() + link.url = u'http://привет.com' + + # TODO fix URL validation - this *IS* a valid URL + # For now we just want to make sure that the error message is correct + try: + link.validate() + self.assertTrue(False) + except ValidationError as e: + self.assertEqual( + unicode(e), + u"ValidationError (Link:None) (Invalid URL: http://\u043f\u0440\u0438\u0432\u0435\u0442.com: ['url'])" + ) + def test_url_scheme_validation(self): """Ensure that URLFields validate urls with specific schemes properly. """