From 25c5251acd79293c8cbaef393ceb1db388262190 Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Sat, 25 Jun 2011 13:14:31 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20#6189=20--=20Modified=20test=20that=20n?= =?UTF-8?q?eed=20Internet=20access=20so=20they=20use=20a=20mock=20instead.?= =?UTF-8?q?=20Thanks=20Gregor=20M=C3=BCellegger=20for=20the=20patch.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@16451 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../forms/tests/error_messages.py | 2 + tests/regressiontests/forms/tests/fields.py | 43 +++++++++++++------ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/tests/regressiontests/forms/tests/error_messages.py b/tests/regressiontests/forms/tests/error_messages.py index 981016f184e..93f35704ed3 100644 --- a/tests/regressiontests/forms/tests/error_messages.py +++ b/tests/regressiontests/forms/tests/error_messages.py @@ -4,6 +4,7 @@ from django.forms import * from django.test import TestCase from django.utils.safestring import mark_safe from django.utils import unittest +from regressiontests.forms.tests.fields import verify_exists_urls class AssertFormErrorsMixin(object): def assertFormErrors(self, expected, the_callable, *args, **kwargs): @@ -139,6 +140,7 @@ class FormsErrorMessagesTestCase(unittest.TestCase, AssertFormErrorsMixin): self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', None)) self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', '')) + @verify_exists_urls() def test_urlfield(self): e = { 'required': 'REQUIRED', diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py index ad8d1d92d9d..67804134ac0 100644 --- a/tests/regressiontests/forms/tests/fields.py +++ b/tests/regressiontests/forms/tests/fields.py @@ -25,11 +25,11 @@ Other than that, the Field subclasses have class-specific options for __init__(). For example, CharField has a max_length option. """ import datetime -import time import re import os import urllib2 from decimal import Decimal +from functools import wraps from django.core.files.uploadedfile import SimpleUploadedFile from django.forms import * @@ -48,6 +48,28 @@ def fix_os_paths(x): return x +def verify_exists_urls(existing_urls=()): + 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: + return True + raise Exception() + try: + urllib2.urlopen = urlopen + func(*args, **kwargs) + finally: + # unpatch urllib2 + validators.urllib2.urlopen = original_urlopen + return wrapper + return decorator + + class FieldsTests(TestCase): def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs): @@ -595,9 +617,10 @@ class FieldsTests(TestCase): self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.') self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com') + @verify_exists_urls(('http://www.google.com/',)) def test_urlfield_3(self): f = URLField(verify_exists=True) - self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection + self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example') self.assertRaises(ValidationError, f.clean, 'http://www.broken.djangoproject.com') # bad domain self.assertRaises(ValidationError, f.clean, 'http://qa-dev.w3.org/link-testsuite/http.php?code=405') # Method not allowed @@ -611,10 +634,11 @@ class FieldsTests(TestCase): except ValidationError, e: self.assertEqual("[u'This URL appears to be a broken link.']", str(e)) + @verify_exists_urls(('http://www.google.com/',)) def test_urlfield_4(self): f = URLField(verify_exists=True, required=False) self.assertEqual(u'', f.clean('')) - self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection + self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) def test_urlfield_5(self): f = URLField(min_length=15, max_length=20) @@ -663,17 +687,12 @@ class FieldsTests(TestCase): except ValidationError, e: self.assertEqual("[u'This URL appears to be a broken link.']", str(e)) + @verify_exists_urls(('http://xn--tr-xka.djangoproject.com/',)) def test_urlfield_10(self): - # UTF-8 char in path, enclosed by a monkey-patch to make sure - # the encoding is passed to urllib2.urlopen + # UTF-8 char in path f = URLField(verify_exists=True) - try: - _orig_urlopen = urllib2.urlopen - urllib2.urlopen = lambda req: True - url = u'http://t\xfcr.djangoproject.com/' - self.assertEqual(url, f.clean(url)) - finally: - urllib2.urlopen = _orig_urlopen + url = u'http://t\xfcr.djangoproject.com/' + self.assertEqual(url, f.clean(url)) # BooleanField ################################################################