From 977316e0cb1a266b62112f58226f0a447f71336a Mon Sep 17 00:00:00 2001 From: Julien Phalip Date: Mon, 31 Oct 2011 11:09:10 +0000 Subject: [PATCH] Fixed #16816 -- Tweaked the test mock for `URLField.verify_exists` to allow tests to pass when there is no Internet connection available. Many thanks to Ramiro Morales, Aymeric Augustin and Florian Apolloner for the patch reviews. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17059 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/modeltests/validation/tests.py | 6 ++++++ tests/regressiontests/forms/tests/fields.py | 23 +++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/tests/modeltests/validation/tests.py b/tests/modeltests/validation/tests.py index a7e13e767f..5c94f439d1 100644 --- a/tests/modeltests/validation/tests.py +++ b/tests/modeltests/validation/tests.py @@ -6,9 +6,13 @@ from django import forms from django.core.exceptions import NON_FIELD_ERRORS from django.test import TestCase +# Import the verify_exists_urls from the 'forms' test app +from regressiontests.forms.tests.fields import verify_exists_urls + from . import ValidationTestCase from .models import (Author, Article, ModelToValidate, GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest) + # Import other tests for this package. from .test_custom_messages import CustomMessagesTest from .test_error_messages import ValidationMessagesTest @@ -71,10 +75,12 @@ class BaseModelValidationTests(ValidationTestCase): mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://qa-dev.w3.org/link-testsuite/http.php?code=404') self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url_verify', [u'This URL appears to be a broken link.']) + @verify_exists_urls(existing_urls=('http://www.google.com/',)) def test_correct_url_value_passes(self): mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://www.google.com/') self.assertEqual(None, mtv.full_clean()) # This will fail if there's no Internet connection + @verify_exists_urls(existing_urls=('http://qa-dev.w3.org/link-testsuite/http.php?code=301',)) def test_correct_url_with_redirect(self): mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://qa-dev.w3.org/link-testsuite/http.php?code=301') #example.com is a redirect to iana.org now self.assertEqual(None, mtv.full_clean()) # This will fail if there's no Internet connection diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py index d37ad5ee58..ecb6c23b45 100644 --- a/tests/regressiontests/forms/tests/fields.py +++ b/tests/regressiontests/forms/tests/fields.py @@ -49,23 +49,27 @@ def fix_os_paths(x): def verify_exists_urls(existing_urls=()): + """ + Patches urllib to simulate the availability of some urls even when there + is no Internet connection. This hack should be removed alongside with + `URLField.verify_exists` in Django 1.5. + """ def decorator(func): @wraps(func) def wrapper(*args, **kwargs): from django.core import validators - # patch urllib2 - original_urlopen = validators.urllib2.urlopen - def urlopen(req): - url = req.get_full_url() - if url in existing_urls: + # patch urllib2.OpenerDirector + original_open = validators.urllib2.OpenerDirector.open + def custom_open(self, req, data=None, timeout=None): + if req.get_full_url() in existing_urls: return True raise Exception() try: - urllib2.urlopen = urlopen + urllib2.OpenerDirector.open = custom_open func(*args, **kwargs) finally: - # unpatch urllib2 - validators.urllib2.urlopen = original_urlopen + # unpatch urllib2.OpenerDirector + validators.urllib2.OpenerDirector.open = original_open return wrapper return decorator @@ -690,8 +694,9 @@ class FieldsTests(SimpleTestCase): except ValidationError, e: self.assertEqual("[u'This URL appears to be a broken link.']", str(e)) + @verify_exists_urls((u'http://xn--hxargifdar.idn.icann.org/%CE%91%CF%81%CF%87%CE%B9%CE%BA%CE%AE_%CF%83%CE%B5%CE%BB%CE%AF%CE%B4%CE%B1',)) def test_urlfield_10(self): - # UTF-8 in the domain. + # UTF-8 in the domain. f = URLField(verify_exists=True) url = u'http://\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac.idn.icann.org/\u0391\u03c1\u03c7\u03b9\u03ba\u03ae_\u03c3\u03b5\u03bb\u03af\u03b4\u03b1' self.assertEqual(url, f.clean(url))