|
1 | 1 | # largely based on DRF's test_openapi
|
2 | 2 | import json
|
3 | 3 |
|
4 |
| -import pytest |
5 | 4 | from django.conf.urls import url
|
6 | 5 | from django.test import RequestFactory, override_settings
|
7 |
| -from rest_condition import And, Not, Or |
8 |
| -from rest_framework import VERSION as DRFVERSION |
9 |
| -from rest_framework.authentication import BasicAuthentication, SessionAuthentication |
10 |
| -from rest_framework.permissions import ( |
11 |
| - AllowAny, |
12 |
| - DjangoModelPermissions, |
13 |
| - IsAdminUser, |
14 |
| - IsAuthenticated |
15 |
| -) |
16 | 6 | from rest_framework.request import Request
|
17 | 7 |
|
18 |
| -from rest_framework_json_api.optional import OAuth2Authentication, TokenMatchesOASRequirements |
| 8 | +from rest_framework_json_api.schemas.openapi import AutoSchema, SchemaGenerator |
19 | 9 | from rest_framework_json_api.views import ModelViewSet
|
20 | 10 |
|
21 | 11 | from example import models, serializers, views
|
22 | 12 |
|
23 |
| -drf_version = tuple(int(x) for x in DRFVERSION.split('.')) |
24 |
| - |
25 |
| -if drf_version >= (3, 10): |
26 |
| - from rest_framework_json_api.schemas.openapi import AutoSchema, SchemaGenerator |
27 |
| - |
28 |
| -pytestmark = pytest.mark.skipif(drf_version < (3, 10), reason="requires DRF 3.10 or higher") |
29 |
| - |
30 | 13 |
|
31 | 14 | def create_request(path):
|
32 | 15 | factory = RequestFactory()
|
@@ -131,127 +114,6 @@ def test_delete_request(snapshot):
|
131 | 114 | snapshot.assert_match(json.dumps(operation, indent=2, sort_keys=True))
|
132 | 115 |
|
133 | 116 |
|
134 |
| -@pytest.mark.skipif(TokenMatchesOASRequirements is None, reason="requires oauth") |
135 |
| -class OauthProtectedAuthorViewSet(views.AuthorViewSet): |
136 |
| - authentication_classes = (OAuth2Authentication, BasicAuthentication, |
137 |
| - SessionAuthentication) |
138 |
| - permission_classes = (TokenMatchesOASRequirements, IsAuthenticated) |
139 |
| - required_alternate_scopes = { |
140 |
| - 'GET': [['scope1', 'scope2'], ['scope3', 'scope4']], |
141 |
| - } |
142 |
| - |
143 |
| - |
144 |
| -oauth2_server = 'oauth.example.com' |
145 |
| -oauth2_config = { |
146 |
| - 'authorization_endpoint': oauth2_server + '/authorize', |
147 |
| - 'token_endpoint': oauth2_server + '/token', |
148 |
| - 'scopes_supported': ['scope1', 'scope2', 'scope3', 'scope4'], |
149 |
| - 'grant_types_supported': ['implicit', 'authorization_code', 'client_credentials', |
150 |
| - 'password'], |
151 |
| -} |
152 |
| - |
153 |
| - |
154 |
| -@pytest.mark.skipif(TokenMatchesOASRequirements is None, reason="requires oauth") |
155 |
| -def test_schema_security_list(): |
156 |
| - """ |
157 |
| - Checks for security objects |
158 |
| - """ |
159 |
| - |
160 |
| - path = '/authors/' |
161 |
| - method = 'GET' |
162 |
| - |
163 |
| - view = create_view_with_kw( |
164 |
| - OauthProtectedAuthorViewSet, |
165 |
| - method, |
166 |
| - create_request(path), |
167 |
| - {'get': 'list'} |
168 |
| - ) |
169 |
| - inspector = AutoSchema() |
170 |
| - inspector.view = view |
171 |
| - |
172 |
| - with override_settings(OAUTH2_CONFIG=oauth2_config): |
173 |
| - operation = inspector.get_operation(path, method) |
174 |
| - |
175 |
| - assert 'security' in operation |
176 |
| - assert len(operation['security']) == 4 |
177 |
| - assert operation['security'][0] == {'oauth': ['scope1', 'scope2']} |
178 |
| - assert operation['security'][1] == {'oauth': ['scope3', 'scope4']} |
179 |
| - assert operation['security'][2] == {'basicAuth': []} |
180 |
| - assert operation['security'][3] == {'cookieAuth': []} |
181 |
| - |
182 |
| - |
183 |
| -@pytest.mark.skipif(TokenMatchesOASRequirements is None, reason="requires oauth") |
184 |
| -def test_schema_security_drf_condition(): |
185 |
| - """ |
186 |
| - Checks for security objects with DRF bitwise conditional operators |
187 |
| - """ |
188 |
| - class DRF_Cond_ViewSet(OauthProtectedAuthorViewSet): |
189 |
| - # this is a crazy example just to make sure all the recursive code is covered |
190 |
| - permission_classes = [ |
191 |
| - (IsAuthenticated & DjangoModelPermissions) | |
192 |
| - ~(TokenMatchesOASRequirements & AllowAny), |
193 |
| - ~AllowAny | (IsAdminUser & IsAuthenticated), |
194 |
| - (TokenMatchesOASRequirements & AllowAny) | |
195 |
| - (IsAuthenticated & DjangoModelPermissions), |
196 |
| - ~TokenMatchesOASRequirements |
197 |
| - ] |
198 |
| - |
199 |
| - path = '/authors/' |
200 |
| - method = 'GET' |
201 |
| - |
202 |
| - view = create_view_with_kw( |
203 |
| - DRF_Cond_ViewSet, |
204 |
| - method, |
205 |
| - create_request(path), |
206 |
| - {'get': 'list'} |
207 |
| - ) |
208 |
| - inspector = AutoSchema() |
209 |
| - inspector.view = view |
210 |
| - |
211 |
| - with override_settings(OAUTH2_CONFIG=oauth2_config): |
212 |
| - operation = inspector.get_operation(path, method) |
213 |
| - |
214 |
| - assert 'security' in operation |
215 |
| - assert {'oauth': ['scope1', 'scope2']} in operation['security'] |
216 |
| - assert {'oauth': ['scope3', 'scope4']} in operation['security'] |
217 |
| - assert {'basicAuth': []} in operation['security'] |
218 |
| - assert {'cookieAuth': []} in operation['security'] |
219 |
| - |
220 |
| - |
221 |
| -@pytest.mark.skipif(TokenMatchesOASRequirements is None, reason="requires oauth") |
222 |
| -def test_schema_security_rest_condition(): |
223 |
| - """ |
224 |
| - Checks for security objects with rest_condition operator methods |
225 |
| - """ |
226 |
| - class Rest_Cond_ViewSet(OauthProtectedAuthorViewSet): |
227 |
| - permission_classes = [ |
228 |
| - Or( |
229 |
| - And(IsAuthenticated, DjangoModelPermissions), |
230 |
| - And(Not(TokenMatchesOASRequirements), AllowAny)), |
231 |
| - ] |
232 |
| - |
233 |
| - path = '/authors/' |
234 |
| - method = 'GET' |
235 |
| - |
236 |
| - view = create_view_with_kw( |
237 |
| - Rest_Cond_ViewSet, |
238 |
| - method, |
239 |
| - create_request(path), |
240 |
| - {'get': 'list'} |
241 |
| - ) |
242 |
| - inspector = AutoSchema() |
243 |
| - inspector.view = view |
244 |
| - |
245 |
| - with override_settings(OAUTH2_CONFIG=oauth2_config): |
246 |
| - operation = inspector.get_operation(path, method) |
247 |
| - |
248 |
| - assert 'security' in operation |
249 |
| - assert {'oauth': ['scope1', 'scope2']} in operation['security'] |
250 |
| - assert {'oauth': ['scope3', 'scope4']} in operation['security'] |
251 |
| - assert {'basicAuth': []} in operation['security'] |
252 |
| - assert {'cookieAuth': []} in operation['security'] |
253 |
| - |
254 |
| - |
255 | 117 | @override_settings(REST_FRAMEWORK={
|
256 | 118 | 'DEFAULT_SCHEMA_CLASS': 'rest_framework_json_api.schemas.openapi.AutoSchema'})
|
257 | 119 | def test_schema_construction():
|
|
0 commit comments