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.core.exceptions import NON_FIELD_ERRORS
|
||||||
from django.test import TestCase
|
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 . import ValidationTestCase
|
||||||
from .models import (Author, Article, ModelToValidate,
|
from .models import (Author, Article, ModelToValidate,
|
||||||
GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest)
|
GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest)
|
||||||
|
|
||||||
# Import other tests for this package.
|
# Import other tests for this package.
|
||||||
from .test_custom_messages import CustomMessagesTest
|
from .test_custom_messages import CustomMessagesTest
|
||||||
from .test_error_messages import ValidationMessagesTest
|
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')
|
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.'])
|
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):
|
def test_correct_url_value_passes(self):
|
||||||
mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://www.google.com/')
|
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
|
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):
|
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
|
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
|
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=()):
|
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):
|
def decorator(func):
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
from django.core import validators
|
from django.core import validators
|
||||||
# patch urllib2
|
# patch urllib2.OpenerDirector
|
||||||
original_urlopen = validators.urllib2.urlopen
|
original_open = validators.urllib2.OpenerDirector.open
|
||||||
def urlopen(req):
|
def custom_open(self, req, data=None, timeout=None):
|
||||||
url = req.get_full_url()
|
if req.get_full_url() in existing_urls:
|
||||||
if url in existing_urls:
|
|
||||||
return True
|
return True
|
||||||
raise Exception()
|
raise Exception()
|
||||||
try:
|
try:
|
||||||
urllib2.urlopen = urlopen
|
urllib2.OpenerDirector.open = custom_open
|
||||||
func(*args, **kwargs)
|
func(*args, **kwargs)
|
||||||
finally:
|
finally:
|
||||||
# unpatch urllib2
|
# unpatch urllib2.OpenerDirector
|
||||||
validators.urllib2.urlopen = original_urlopen
|
validators.urllib2.OpenerDirector.open = original_open
|
||||||
return wrapper
|
return wrapper
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
@ -690,6 +694,7 @@ class FieldsTests(SimpleTestCase):
|
||||||
except ValidationError, e:
|
except ValidationError, e:
|
||||||
self.assertEqual("[u'This URL appears to be a broken link.']", str(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):
|
def test_urlfield_10(self):
|
||||||
# UTF-8 in the domain.
|
# UTF-8 in the domain.
|
||||||
f = URLField(verify_exists=True)
|
f = URLField(verify_exists=True)
|
||||||
|
|
Loading…
Reference in New Issue