diff -r 880cbeeca67f tests/modeltests/validation/tests.py
a
|
b
|
|
9 | 9 | from . import ValidationTestCase |
10 | 10 | from .models import (Author, Article, ModelToValidate, |
11 | 11 | GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest) |
| 12 | from ..regressiontests.forms.tests.fields import verify_exists_urls |
12 | 13 | # Import other tests for this package. |
13 | 14 | from .test_custom_messages import CustomMessagesTest |
14 | 15 | from .test_error_messages import ValidationMessagesTest |
… |
… |
|
71 | 72 | mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://qa-dev.w3.org/link-testsuite/http.php?code=404') |
72 | 73 | self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url_verify', [u'This URL appears to be a broken link.']) |
73 | 74 | |
| 75 | @verify_exists_urls(existing_urls=('http://www.google.com/',)) |
74 | 76 | def test_correct_url_value_passes(self): |
75 | 77 | mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://www.google.com/') |
76 | 78 | self.assertEqual(None, mtv.full_clean()) # This will fail if there's no Internet connection |
77 | 79 | |
| 80 | @verify_exists_urls(existing_urls=('http://qa-dev.w3.org/link-testsuite/http.php?code=301',)) |
78 | 81 | def test_correct_url_with_redirect(self): |
79 | 82 | 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 |
80 | 83 | self.assertEqual(None, mtv.full_clean()) # This will fail if there's no Internet connection |
diff -r 880cbeeca67f tests/regressiontests/forms/tests/fields.py
a
|
b
|
|
53 | 53 | @wraps(func) |
54 | 54 | def wrapper(*args, **kwargs): |
55 | 55 | 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: |
61 | 60 | return True |
62 | 61 | raise Exception() |
63 | 62 | try: |
64 | | urllib2.urlopen = urlopen |
| 63 | urllib2.OpenerDirector.open = custom_open |
65 | 64 | func(*args, **kwargs) |
66 | 65 | finally: |
67 | | # unpatch urllib2 |
68 | | validators.urllib2.urlopen = original_urlopen |
| 66 | # unpatch urllib2.OpenerDirector |
| 67 | validators.urllib2.OpenerDirector.open = original_open |
69 | 68 | return wrapper |
70 | 69 | return decorator |
71 | 70 | |
… |
… |
|
690 | 689 | except ValidationError, e: |
691 | 690 | self.assertEqual("[u'This URL appears to be a broken link.']", str(e)) |
692 | 691 | |
| 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',)) |
693 | 693 | def test_urlfield_10(self): |
694 | | # UTF-8 in the domain. |
| 694 | # UTF-8 in the domain. |
695 | 695 | f = URLField(verify_exists=True) |
696 | 696 | 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' |
697 | 697 | self.assertEqual(url, f.clean(url)) |