|
| 1 | +from django.test import TestCase |
| 2 | +from oidc_provider.lib.errors import AuthorizeError |
| 3 | + |
| 4 | +try: |
| 5 | + from urllib.parse import ( |
| 6 | + urlparse, |
| 7 | + parse_qsl |
| 8 | + ) |
| 9 | +except ImportError: |
| 10 | + from urlparse import ( |
| 11 | + urlparse, |
| 12 | + parse_qsl |
| 13 | + ) |
| 14 | + |
| 15 | + |
| 16 | +def compare(expected, created): |
| 17 | + """Compare expected and created urls""" |
| 18 | + ex_parsed = list(urlparse(expected)) |
| 19 | + ex_qp = dict(parse_qsl(ex_parsed[4])) |
| 20 | + ex_frag = dict(parse_qsl(ex_parsed[5])) |
| 21 | + |
| 22 | + cr_parsed = list(urlparse(created)) |
| 23 | + cr_qp = dict(parse_qsl(cr_parsed[4])) |
| 24 | + cr_frag = dict(parse_qsl(cr_parsed[5])) |
| 25 | + |
| 26 | + # Validate scheme, netloc, path match |
| 27 | + assert ex_parsed[:3] == cr_parsed[:3] |
| 28 | + # Validate qp and frags match |
| 29 | + assert ex_qp == cr_qp |
| 30 | + assert ex_frag == cr_frag |
| 31 | + |
| 32 | + |
| 33 | +class TestImplicitAuthorizeErrorNonImplicit(TestCase): |
| 34 | + """Tests with grant_type code - all responses in query params""" |
| 35 | + redirect_uri = 'https://example.com/' |
| 36 | + grant_type = 'code' |
| 37 | + error = 'login_required' |
| 38 | + desc = 'The+Authorization+Server+requires+End-User+authentication' |
| 39 | + |
| 40 | + def test_no_params(self): |
| 41 | + """Test with a path only and no query/frag params""" |
| 42 | + redirect_uri = self.redirect_uri + 'path' |
| 43 | + error = AuthorizeError(redirect_uri, self.error, self.grant_type) |
| 44 | + created_uri = error.create_uri(redirect_uri, '') |
| 45 | + expected_uri = '{}?error={}&error_description={}'.format( |
| 46 | + redirect_uri, self.error, self.desc) |
| 47 | + compare(expected_uri, created_uri) |
| 48 | + |
| 49 | + def test_query_params_only(self): |
| 50 | + """Test with query param in redirect uri""" |
| 51 | + redirect_uri = self.redirect_uri + "path/?action=something" |
| 52 | + error = AuthorizeError(redirect_uri, self.error, self.grant_type) |
| 53 | + created_uri = error.create_uri(redirect_uri, '') |
| 54 | + expected_uri = '{}&error={}&error_description={}'.format( |
| 55 | + redirect_uri, self.error, self.desc) |
| 56 | + compare(expected_uri, created_uri) |
| 57 | + |
| 58 | + def test_frag_params_only(self): |
| 59 | + """Test with fragment params only""" |
| 60 | + redirect_uri = self.redirect_uri + 'path' |
| 61 | + frag = '#action=something' |
| 62 | + error = AuthorizeError(redirect_uri + frag, self.error, self.grant_type) |
| 63 | + created_uri = error.create_uri(redirect_uri + frag, '') |
| 64 | + expected_uri = '{}path?error={}&error_description={}{}'.format( |
| 65 | + self.redirect_uri, self.error, self.desc, frag) |
| 66 | + compare(expected_uri, created_uri) |
| 67 | + |
| 68 | + def test_query_and_frag_params(self): |
| 69 | + """Test with both qp's and fragment""" |
| 70 | + redirect_uri = self.redirect_uri + 'path?my_qp=test' |
| 71 | + frag = '#action=something' |
| 72 | + error = AuthorizeError(redirect_uri + frag, self.error, self.grant_type) |
| 73 | + created_uri = error.create_uri(redirect_uri + frag, '') |
| 74 | + expected_uri = '{}path?my_qp=test&error={}&error_description={}{}' \ |
| 75 | + .format(self.redirect_uri, self.error, self.desc, frag) |
| 76 | + compare(expected_uri, created_uri) |
| 77 | + |
| 78 | + def test_with_state(self): |
| 79 | + """Test with state""" |
| 80 | + redirect_uri = self.redirect_uri + 'path' |
| 81 | + state = 'my_state' |
| 82 | + error = AuthorizeError(redirect_uri, self.error, self.grant_type) |
| 83 | + created_uri = error.create_uri(redirect_uri, state) |
| 84 | + expected_uri = '{}path?error={}&error_description={}&state={}' \ |
| 85 | + .format(self.redirect_uri, self.error, self.desc, state) |
| 86 | + compare(expected_uri, created_uri) |
| 87 | + |
| 88 | + def test_with_deep_link(self): |
| 89 | + """Test with a non-http schema; deep link style (think slack://)""" |
| 90 | + redirect_uri = 'slack://example.com/path' |
| 91 | + state = 'my_state' |
| 92 | + error = AuthorizeError(redirect_uri, self.error, self.grant_type) |
| 93 | + created_uri = error.create_uri(redirect_uri, state) |
| 94 | + expected_uri = '{}?error={}&error_description={}&state={}' \ |
| 95 | + .format(redirect_uri, self.error, self.desc, state) |
| 96 | + compare(expected_uri, created_uri) |
| 97 | + |
| 98 | + |
| 99 | +class TestImplicitAuthorizeErrorImplicit(TestCase): |
| 100 | + """Tests with grant_type code - all responses in query params""" |
| 101 | + redirect_uri = 'https://example.com/' |
| 102 | + grant_type = 'implicit' |
| 103 | + error = 'login_required' |
| 104 | + desc = 'The+Authorization+Server+requires+End-User+authentication' |
| 105 | + |
| 106 | + def test_no_params(self): |
| 107 | + """Test with a path only and no query/frag params""" |
| 108 | + redirect_uri = self.redirect_uri + 'path' |
| 109 | + error = AuthorizeError(redirect_uri, self.error, self.grant_type) |
| 110 | + created_uri = error.create_uri(redirect_uri, '') |
| 111 | + expected_uri = '{}#error={}&error_description={}'.format( |
| 112 | + redirect_uri, self.error, self.desc) |
| 113 | + compare(expected_uri, created_uri) |
| 114 | + |
| 115 | + def test_query_params_only(self): |
| 116 | + """Test with query param in redirect uri""" |
| 117 | + redirect_uri = self.redirect_uri + "path/?action=something" |
| 118 | + error = AuthorizeError(redirect_uri, self.error, self.grant_type) |
| 119 | + created_uri = error.create_uri(redirect_uri, '') |
| 120 | + expected_uri = '{}#error={}&error_description={}'.format( |
| 121 | + redirect_uri, self.error, self.desc) |
| 122 | + compare(expected_uri, created_uri) |
| 123 | + |
| 124 | + def test_frag_params_only(self): |
| 125 | + """Test with fragment params only""" |
| 126 | + redirect_uri = self.redirect_uri + 'path' |
| 127 | + frag = '#action=something' |
| 128 | + error = AuthorizeError(redirect_uri + frag, self.error, self.grant_type) |
| 129 | + created_uri = error.create_uri(redirect_uri + frag, '') |
| 130 | + expected_uri = '{}path{}&error={}&error_description={}'.format( |
| 131 | + self.redirect_uri, frag, self.error, self.desc) |
| 132 | + compare(expected_uri, created_uri) |
| 133 | + |
| 134 | + def test_query_and_frag_params(self): |
| 135 | + """Test with both qp's and fragment""" |
| 136 | + redirect_uri = self.redirect_uri + 'path?my_qp=test' |
| 137 | + frag = '#action=something' |
| 138 | + error = AuthorizeError(redirect_uri + frag, self.error, self.grant_type) |
| 139 | + created_uri = error.create_uri(redirect_uri + frag, '') |
| 140 | + expected_uri = '{}path?my_qp=test{}&error={}&error_description={}' \ |
| 141 | + .format(self.redirect_uri, frag, self.error, self.desc) |
| 142 | + compare(expected_uri, created_uri) |
| 143 | + |
| 144 | + def test_with_state(self): |
| 145 | + """Test with state""" |
| 146 | + redirect_uri = self.redirect_uri + 'path' |
| 147 | + state = 'my_state' |
| 148 | + error = AuthorizeError(redirect_uri, self.error, self.grant_type) |
| 149 | + created_uri = error.create_uri(redirect_uri, state) |
| 150 | + expected_uri = '{}path#error={}&error_description={}&state={}' \ |
| 151 | + .format(self.redirect_uri, self.error, self.desc, state) |
| 152 | + compare(expected_uri, created_uri) |
| 153 | + |
| 154 | + def test_with_deep_link(self): |
| 155 | + """Test with a non-http schema; deep link style (think slack://)""" |
| 156 | + redirect_uri = 'slack://example.com/path' |
| 157 | + state = 'my_state' |
| 158 | + error = AuthorizeError(redirect_uri, self.error, self.grant_type) |
| 159 | + created_uri = error.create_uri(redirect_uri, state) |
| 160 | + expected_uri = '{}#error={}&error_description={}&state={}' \ |
| 161 | + .format(redirect_uri, self.error, self.desc, state) |
| 162 | + compare(expected_uri, created_uri) |
0 commit comments