From 6a59e4658e1f8da7d6f0dfe2174d405081f83e6e Mon Sep 17 00:00:00 2001 From: Stefan Wojcik Date: Wed, 22 Feb 2017 12:57:00 -0500 Subject: [PATCH 1/2] fix the exception message when validating unicode URLs --- mongoengine/fields.py | 4 ++-- tests/fields/fields.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/mongoengine/fields.py b/mongoengine/fields.py index 0ea7d3b65..114250954 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -139,12 +139,12 @@ def validate(self, value): # 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 e6898e1b3..d900d7060 100644 --- a/tests/fields/fields.py +++ b/tests/fields/fields.py @@ -346,6 +346,25 @@ def test_url_validation(self): class Link(Document): url = URLField() + 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_unicode_url_validation(self): + """Ensure unicode URLs are validated properly.""" + class Link(Document): + url = URLField() + link = Link() link.url = 'google' self.assertRaises(ValidationError, link.validate) From 3534daf37d4d721740e115b8b6425834b8e7943d Mon Sep 17 00:00:00 2001 From: Stefan Wojcik Date: Fri, 24 Feb 2017 16:15:03 -0500 Subject: [PATCH 2/2] fix tests structure --- tests/fields/fields.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/tests/fields/fields.py b/tests/fields/fields.py index d900d7060..318c0c595 100644 --- a/tests/fields/fields.py +++ b/tests/fields/fields.py @@ -341,15 +341,30 @@ class Person(Document): 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) + + 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 + # 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() @@ -360,18 +375,6 @@ class Link(Document): u"ValidationError (Link:None) (Invalid URL: http://\u043f\u0440\u0438\u0432\u0435\u0442.com: ['url'])" ) - def test_unicode_url_validation(self): - """Ensure unicode URLs are validated properly.""" - class Link(Document): - url = URLField() - - link = Link() - link.url = 'google' - self.assertRaises(ValidationError, link.validate) - - link.url = 'http://www.google.com:8080' - link.validate() - def test_url_scheme_validation(self): """Ensure that URLFields validate urls with specific schemes properly. """