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
This commit is contained in:
parent
422f6e8e17
commit
977316e0cb
|
@ -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
|
||||
|
|
|
@ -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,6 +694,7 @@ 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.
|
||||
f = URLField(verify_exists=True)
|
||||
|
|
Loading…
Reference in New Issue