Ticket #16189: ticket16189.1.diff

File ticket16189.1.diff, 4.8 KB (added by Gregor Müllegger, 8 years ago)

Don't require access to the internet while running tests.

  • tests/regressiontests/forms/tests/error_messages.py

    diff --git a/tests/regressiontests/forms/tests/error_messages.py b/tests/regressiontests/forms/tests/error_messages.py
    index a9f4f37..3290d85 100644
    a b from django.forms import * 
    44from django.test import TestCase
    55from django.utils.safestring import mark_safe
    66from django.utils import unittest
     7from regressiontests.forms.tests.fields import verify_exists_urls
    78
    89class AssertFormErrorsMixin(object):
    910    def assertFormErrors(self, expected, the_callable, *args, **kwargs):
    class FormsErrorMessagesTestCase(unittest.TestCase, AssertFormErrorsMixin): 
    139140        self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', None))
    140141        self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', ''))
    141142
     143    @verify_exists_urls()
    142144    def test_urlfield(self):
    143145        e = {
    144146            'required': 'REQUIRED',
  • tests/regressiontests/forms/tests/fields.py

    diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py
    index 2303b4e..58c2b70 100644
    a b import re 
    3030import os
    3131import urllib2
    3232from decimal import Decimal
     33from functools import wraps
    3334
    3435from django.core.files.uploadedfile import SimpleUploadedFile
    3536from django.forms import *
    def fix_os_paths(x): 
    4849        return x
    4950
    5051
     52def verify_exists_urls(existing_urls=()):
     53    def decorator(func):
     54        @wraps(func)
     55        def wrapper(*args, **kwargs):
     56            from django.core import validators
     57            # patch urllib2
     58            original_urlopen = validators.urllib2.urlopen
     59            def urlopen(req):
     60                url = req.get_full_url()
     61                if url in existing_urls:
     62                    return True
     63                raise Exception()
     64            try:
     65                urllib2.urlopen = urlopen
     66                func(*args, **kwargs)
     67            finally:
     68                # unpatch urllib2
     69                validators.urllib2.urlopen = original_urlopen
     70        return wrapper
     71    return decorator
     72
     73
    5174class FieldsTests(TestCase):
    5275
    5376    def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs):
    class FieldsTests(TestCase): 
    592615        self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.')
    593616        self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com')
    594617
     618    @verify_exists_urls(('http://www.google.com/',))
    595619    def test_urlfield_3(self):
    596620        f = URLField(verify_exists=True)
    597         self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection
     621        self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com'))
    598622        self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')
    599623        self.assertRaises(ValidationError, f.clean, 'http://www.broken.djangoproject.com') # bad domain
    600624        self.assertRaises(ValidationError, f.clean, 'http://qa-dev.w3.org/link-testsuite/http.php?code=405') # Method not allowed
    class FieldsTests(TestCase): 
    608632        except ValidationError, e:
    609633            self.assertEqual("[u'This URL appears to be a broken link.']", str(e))
    610634
     635    @verify_exists_urls(('http://www.google.com/',))
    611636    def test_urlfield_4(self):
    612637        f = URLField(verify_exists=True, required=False)
    613638        self.assertEqual(u'', f.clean(''))
    614         self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection
     639        self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com'))
    615640
    616641    def test_urlfield_5(self):
    617642        f = URLField(min_length=15, max_length=20)
    class FieldsTests(TestCase): 
    660685        except ValidationError, e:
    661686            self.assertEqual("[u'This URL appears to be a broken link.']", str(e))
    662687
     688    @verify_exists_urls(('http://xn--tr-xka.djangoproject.com/',))
    663689    def test_urlfield_10(self):
    664         # UTF-8 char in path, enclosed by a monkey-patch to make sure
    665         # the encoding is passed to urllib2.urlopen
    666690        f = URLField(verify_exists=True)
    667         try:
    668             _orig_urlopen = urllib2.urlopen
    669             urllib2.urlopen = lambda req: True
    670             url = u'http://t\xfcr.djangoproject.com/'
    671             self.assertEqual(url, f.clean(url))
    672         finally:
    673             urllib2.urlopen = _orig_urlopen
     691        url = u'http://t\xfcr.djangoproject.com/'
     692        self.assertEqual(url, f.clean(url))
    674693
    675694    # BooleanField ################################################################
    676695
Back to Top