Ticket #16816: 16816.verify-exists-test-mock.2.diff

File 16816.verify-exists-test-mock.2.diff, 3.4 KB (added by Julien Phalip, 13 years ago)
  • tests/modeltests/validation/tests.py

    diff -r 880cbeeca67f tests/modeltests/validation/tests.py
    a b  
    99from . import ValidationTestCase
    1010from .models import (Author, Article, ModelToValidate,
    1111    GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest)
     12from ..regressiontests.forms.tests.fields import verify_exists_urls
    1213# Import other tests for this package.
    1314from .test_custom_messages import CustomMessagesTest
    1415from .test_error_messages import ValidationMessagesTest
     
    7172        mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://qa-dev.w3.org/link-testsuite/http.php?code=404')
    7273        self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url_verify', [u'This URL appears to be a broken link.'])
    7374
     75    @verify_exists_urls(existing_urls=('http://www.google.com/',))
    7476    def test_correct_url_value_passes(self):
    7577        mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://www.google.com/')
    7678        self.assertEqual(None, mtv.full_clean()) # This will fail if there's no Internet connection
    7779
     80    @verify_exists_urls(existing_urls=('http://qa-dev.w3.org/link-testsuite/http.php?code=301',))
    7881    def test_correct_url_with_redirect(self):
    7982        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
    8083        self.assertEqual(None, mtv.full_clean()) # This will fail if there's no Internet connection
  • tests/regressiontests/forms/tests/fields.py

    diff -r 880cbeeca67f tests/regressiontests/forms/tests/fields.py
    a b  
    5353        @wraps(func)
    5454        def wrapper(*args, **kwargs):
    5555            from django.core import validators
    56             # patch urllib2
    57             original_urlopen = validators.urllib2.urlopen
    58             def urlopen(req):
    59                 url = req.get_full_url()
    60                 if url in existing_urls:
     56            # patch urllib2.OpenerDirector
     57            original_open = validators.urllib2.OpenerDirector.open
     58            def custom_open(self, req, data=None):
     59                if req.get_full_url() in existing_urls:
    6160                    return True
    6261                raise Exception()
    6362            try:
    64                 urllib2.urlopen = urlopen
     63                urllib2.OpenerDirector.open = custom_open
    6564                func(*args, **kwargs)
    6665            finally:
    67                 # unpatch urllib2
    68                 validators.urllib2.urlopen = original_urlopen
     66                # unpatch urllib2.OpenerDirector
     67                validators.urllib2.OpenerDirector.open = original_open
    6968        return wrapper
    7069    return decorator
    7170
     
    690689        except ValidationError, e:
    691690            self.assertEqual("[u'This URL appears to be a broken link.']", str(e))
    692691
     692    @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',))
    693693    def test_urlfield_10(self):
    694         # UTF-8 in the domain. 
     694        # UTF-8 in the domain.
    695695        f = URLField(verify_exists=True)
    696696        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'
    697697        self.assertEqual(url, f.clean(url))
Back to Top