|
2 | 2 | from django.test import TestCase |
3 | 3 |
|
4 | 4 | from oauth2_provider.settings import oauth2_settings |
5 | | -from oauth2_provider.validators import validate_uris |
| 5 | +from oauth2_provider.validators import RedirectURIValidator, validate_uris |
6 | 6 |
|
7 | 7 |
|
8 | 8 | class TestValidators(TestCase): |
9 | 9 | def test_validate_good_uris(self): |
10 | | - good_uris = "http://example.com/ http://example.org/?key=val http://example" |
11 | | - # Check ValidationError not thrown |
12 | | - validate_uris(good_uris) |
| 10 | + validator = RedirectURIValidator(allowed_schemes=["https"]) |
| 11 | + good_uris = [ |
| 12 | + "https://example.com/", |
| 13 | + "https://example.org/?key=val", |
| 14 | + "https://example", |
| 15 | + "https://localhost", |
| 16 | + ] |
| 17 | + for uri in good_uris: |
| 18 | + # Check ValidationError not thrown |
| 19 | + validator(uri) |
13 | 20 |
|
14 | 21 | def test_validate_custom_uri_scheme(self): |
15 | | - oauth2_settings.ALLOWED_REDIRECT_URI_SCHEMES = ["my-scheme", "http"] |
16 | | - good_uris = "my-scheme://example.com http://example.com" |
17 | | - # Check ValidationError not thrown |
18 | | - validate_uris(good_uris) |
| 22 | + validator = RedirectURIValidator(allowed_schemes=["my-scheme", "https"]) |
| 23 | + good_uris = [ |
| 24 | + "my-scheme://example.com", |
| 25 | + "my-scheme://example", |
| 26 | + "my-scheme://localhost", |
| 27 | + "https://example.com", |
| 28 | + "HTTPS://example.com", |
| 29 | + ] |
| 30 | + for uri in good_uris: |
| 31 | + # Check ValidationError not thrown |
| 32 | + validator(uri) |
19 | 33 |
|
20 | 34 | def test_validate_whitespace_separators(self): |
21 | 35 | # Check that whitespace can be used as a separator |
22 | | - good_uris = "http://example\r\nhttp://example\thttp://example" |
| 36 | + good_uris = "https://example.com\r\nhttps://example.com\thttps://example.com" |
23 | 37 | # Check ValidationError not thrown |
24 | 38 | validate_uris(good_uris) |
25 | 39 |
|
26 | 40 | def test_validate_bad_uris(self): |
27 | | - bad_uri = "http://example.com/#fragment" |
28 | | - self.assertRaises(ValidationError, validate_uris, bad_uri) |
29 | | - bad_uri = "http:/example.com" |
30 | | - self.assertRaises(ValidationError, validate_uris, bad_uri) |
31 | | - # Bad IPv6 URL, urlparse behaves differently for these |
32 | | - bad_uri = "https://[\"><script>alert()</script>" |
33 | | - self.assertRaises(ValidationError, validate_uris, bad_uri) |
34 | | - bad_uri = "my-scheme://example.com" |
35 | | - self.assertRaises(ValidationError, validate_uris, bad_uri) |
36 | | - bad_uri = "sdklfsjlfjljdflksjlkfjsdkl" |
37 | | - self.assertRaises(ValidationError, validate_uris, bad_uri) |
38 | | - bad_uri = " " |
39 | | - self.assertRaises(ValidationError, validate_uris, bad_uri) |
40 | | - bad_uri = "" |
41 | | - self.assertRaises(ValidationError, validate_uris, bad_uri) |
| 41 | + validator = RedirectURIValidator(allowed_schemes=["https"]) |
| 42 | + oauth2_settings.ALLOWED_REDIRECT_URI_SCHEMES = ["https"] |
| 43 | + bad_uris = [ |
| 44 | + "http:/example.com", |
| 45 | + "HTTP://localhost", |
| 46 | + "HTTP://example.com", |
| 47 | + "HTTP://example.com.", |
| 48 | + "http://example.com/#fragment", |
| 49 | + "my-scheme://example.com" |
| 50 | + "uri-without-a-scheme", |
| 51 | + " ", |
| 52 | + "", |
| 53 | + # Bad IPv6 URL, urlparse behaves differently for these |
| 54 | + 'https://["><script>alert()</script>', |
| 55 | + ] |
| 56 | + |
| 57 | + for uri in bad_uris: |
| 58 | + with self.assertRaises(ValidationError): |
| 59 | + validator(uri) |
0 commit comments