|
1 | 1 | import json |
| 2 | +from unittest.mock import patch |
2 | 3 | from urllib.parse import quote_plus |
3 | 4 |
|
4 | 5 | import pytest |
5 | 6 | from django.contrib.auth import get_user_model |
| 7 | +from django.core.exceptions import SuspiciousOperation |
6 | 8 | from django.test import RequestFactory, TestCase |
7 | 9 | from django.urls import reverse |
8 | 10 | from django.views.generic import View |
@@ -101,21 +103,22 @@ def test_client_credential_user_is_none_on_access_token(self): |
101 | 103 | self.assertIsNone(access_token.user) |
102 | 104 |
|
103 | 105 |
|
| 106 | +class TestView(OAuthLibMixin, View): |
| 107 | + server_class = BackendApplicationServer |
| 108 | + validator_class = OAuth2Validator |
| 109 | + oauthlib_backend_class = OAuthLibCore |
| 110 | + |
| 111 | + def get_scopes(self): |
| 112 | + return ["read", "write"] |
| 113 | + |
| 114 | + |
104 | 115 | class TestExtendedRequest(BaseTest): |
105 | 116 | @classmethod |
106 | 117 | def setUpClass(cls): |
107 | 118 | cls.request_factory = RequestFactory() |
108 | 119 | super().setUpClass() |
109 | 120 |
|
110 | 121 | def test_extended_request(self): |
111 | | - class TestView(OAuthLibMixin, View): |
112 | | - server_class = BackendApplicationServer |
113 | | - validator_class = OAuth2Validator |
114 | | - oauthlib_backend_class = OAuthLibCore |
115 | | - |
116 | | - def get_scopes(self): |
117 | | - return ["read", "write"] |
118 | | - |
119 | 122 | token_request_data = { |
120 | 123 | "grant_type": "client_credentials", |
121 | 124 | } |
@@ -143,6 +146,21 @@ def get_scopes(self): |
143 | 146 | self.assertEqual(r.client, self.application) |
144 | 147 | self.assertEqual(r.scopes, ["read", "write"]) |
145 | 148 |
|
| 149 | + def test_raises_error_with_invalid_hex_in_query_params(self): |
| 150 | + request = self.request_factory.get("/fake-req?auth_token=%%7A") |
| 151 | + |
| 152 | + with pytest.raises(SuspiciousOperation): |
| 153 | + TestView().verify_request(request) |
| 154 | + |
| 155 | + @patch("oauth2_provider.views.mixins.OAuthLibMixin.get_oauthlib_core") |
| 156 | + def test_reraises_value_errors_as_is(self, patched_core): |
| 157 | + patched_core.return_value.verify_request.side_effect = ValueError("Generic error") |
| 158 | + |
| 159 | + request = self.request_factory.get("/fake-req") |
| 160 | + |
| 161 | + with pytest.raises(ValueError): |
| 162 | + TestView().verify_request(request) |
| 163 | + |
146 | 164 |
|
147 | 165 | class TestClientResourcePasswordBased(BaseTest): |
148 | 166 | def test_client_resource_password_based(self): |
|
0 commit comments