-
-
Notifications
You must be signed in to change notification settings - Fork 773
/
test_rest_framework.py
383 lines (305 loc) · 17.1 KB
/
test_rest_framework.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import unittest
from datetime import timedelta
from django.conf.urls import include, url
from django.contrib.auth import get_user_model
from django.http import HttpResponse
from django.test import TestCase
from django.test.utils import override_settings
from django.utils import timezone
from oauth2_provider.models import get_access_token_model, get_application_model
from oauth2_provider.settings import oauth2_settings
try:
from unittest import mock
except ImportError:
import mock
Application = get_application_model()
AccessToken = get_access_token_model()
UserModel = get_user_model()
try:
from rest_framework import permissions
from rest_framework.views import APIView
from rest_framework.test import force_authenticate, APIRequestFactory
from oauth2_provider.contrib.rest_framework import (
IsAuthenticatedOrTokenHasScope, OAuth2Authentication, TokenHasScope,
TokenHasReadWriteScope, TokenHasMethodScope, RequiredMethodScopes,
TokenHasMethodPathScope, TokenHasResourceScope
)
class MockView(APIView):
permission_classes = (permissions.IsAuthenticated,)
def get(self, request):
return HttpResponse({"a": 1, "b": 2, "c": 3})
def post(self, request):
return HttpResponse({"a": 1, "b": 2, "c": 3})
class OAuth2View(MockView):
authentication_classes = [OAuth2Authentication]
class ScopedView(OAuth2View):
permission_classes = [permissions.IsAuthenticated, TokenHasScope]
required_scopes = ["scope1"]
class AuthenticatedOrScopedView(OAuth2View):
permission_classes = [IsAuthenticatedOrTokenHasScope]
required_scopes = ["scope1"]
class ReadWriteScopedView(OAuth2View):
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
class MethodScopeView(OAuth2View):
permission_classes = [TokenHasMethodScope]
required_scopes_map = {
"GET": ["read"],
"POST": ["create"],
"PUT": ["update", "put"],
"DELETE": ["delete"],
}
class MethodPathScopeView(OAuth2View):
permission_classes = [TokenHasMethodPathScope]
required_scopes_map_list = [
RequiredMethodScopes("GET", r"^/oauth2-method-path-scope-test/$", ["read", "get scope2"]),
RequiredMethodScopes("POST", r"^/oauth2-method-path-scope-test/$", ["create", "post scope2"]),
RequiredMethodScopes("PUT", r"^/oauth2-method-path-scope-test/$", ["update", "put scope2"]),
RequiredMethodScopes("DELETE", r"^/oauth2-method-path-scope-test/$", ["delete", "scope2 scope3"]),
]
class ResourceScopedView(OAuth2View):
permission_classes = [permissions.IsAuthenticated, TokenHasResourceScope]
required_scopes = ["resource1"]
urlpatterns = [
url(r"^oauth2/", include("oauth2_provider.urls")),
url(r"^oauth2-test/$", OAuth2View.as_view()),
url(r"^oauth2-scoped-test/$", ScopedView.as_view()),
url(r"^oauth2-read-write-test/$", ReadWriteScopedView.as_view()),
url(r"^oauth2-resource-scoped-test/$", ResourceScopedView.as_view()),
url(r"^oauth2-authenticated-or-scoped-test/$", AuthenticatedOrScopedView.as_view()),
url(r"^oauth2-method-scope-test/$", MethodScopeView.as_view()),
url(r"^oauth2-method-path-scope-test/$", MethodPathScopeView.as_view()),
]
rest_framework_installed = True
except ImportError:
rest_framework_installed = False
@override_settings(ROOT_URLCONF=__name__)
class TestOAuth2Authentication(TestCase):
def setUp(self):
oauth2_settings._SCOPES = ["read", "write", "scope1", "scope2", "resource1"]
self.test_user = UserModel.objects.create_user("test_user", "test@example.com", "123456")
self.dev_user = UserModel.objects.create_user("dev_user", "dev@example.com", "123456")
self.application = Application.objects.create(
name="Test Application",
redirect_uris="http://localhost http://example.com http://example.org",
user=self.dev_user,
client_type=Application.CLIENT_CONFIDENTIAL,
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
)
self.access_token = AccessToken.objects.create(
user=self.test_user,
scope="read write",
expires=timezone.now() + timedelta(seconds=300),
token="secret-access-token-key",
application=self.application
)
def tearDown(self):
oauth2_settings._SCOPES = ["read", "write"]
def _create_authorization_header(self, token):
return "Bearer {0}".format(token)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_authentication_allow(self):
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 200)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_authentication_denied(self):
auth = self._create_authorization_header("fake-token")
response = self.client.get("/oauth2-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 401)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_authentication_or_scope_denied(self):
# user is not authenticated
# not a correct token
auth = self._create_authorization_header("fake-token")
response = self.client.get("/oauth2-authenticated-or-scoped-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 401)
# token doesn"t have correct scope
auth = self._create_authorization_header(self.access_token.token)
factory = APIRequestFactory()
request = factory.get("/oauth2-authenticated-or-scoped-test/")
request.auth = auth
force_authenticate(request, token=self.access_token)
response = AuthenticatedOrScopedView.as_view()(request)
# authenticated but wrong scope, this is 403, not 401
self.assertEqual(response.status_code, 403)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_scoped_permission_allow(self):
self.access_token.scope = "scope1"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-scoped-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 200)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_authenticated_or_scoped_permission_allow(self):
self.access_token.scope = "scope1"
self.access_token.save()
# correct token and correct scope
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-authenticated-or-scoped-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 200)
auth = self._create_authorization_header("fake-token")
# incorrect token but authenticated
factory = APIRequestFactory()
request = factory.get("/oauth2-authenticated-or-scoped-test/")
request.auth = auth
force_authenticate(request, self.test_user)
response = AuthenticatedOrScopedView.as_view()(request)
self.assertEqual(response.status_code, 200)
# correct token but not authenticated
request = factory.get("/oauth2-authenticated-or-scoped-test/")
request.auth = auth
self.access_token.scope = "scope1"
self.access_token.save()
force_authenticate(request, token=self.access_token)
response = AuthenticatedOrScopedView.as_view()(request)
self.assertEqual(response.status_code, 200)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_scoped_permission_deny(self):
self.access_token.scope = "scope2"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-scoped-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 403)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_read_write_permission_get_allow(self):
self.access_token.scope = "read"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-read-write-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 200)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_read_write_permission_post_allow(self):
self.access_token.scope = "write"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.post("/oauth2-read-write-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 200)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_read_write_permission_get_deny(self):
self.access_token.scope = "write"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-read-write-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 403)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_read_write_permission_post_deny(self):
self.access_token.scope = "read"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.post("/oauth2-read-write-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 403)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_resource_scoped_permission_get_allow(self):
self.access_token.scope = "resource1:read"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-resource-scoped-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 200)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_resource_scoped_permission_post_allow(self):
self.access_token.scope = "resource1:write"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.post("/oauth2-resource-scoped-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 200)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_resource_scoped_permission_get_denied(self):
self.access_token.scope = "resource1:write"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-resource-scoped-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 403)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_resource_scoped_permission_post_denied(self):
self.access_token.scope = "resource1:read"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.post("/oauth2-resource-scoped-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 403)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
@mock.patch.object(oauth2_settings, "ERROR_RESPONSE_WITH_SCOPES", new=True)
def test_required_scope_in_response(self):
self.access_token.scope = "scope2"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-scoped-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 403)
self.assertEqual(response.data["required_scopes"], ["scope1"])
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_required_scope_not_in_response_by_default(self):
self.access_token.scope = "scope2"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-scoped-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 403)
self.assertNotIn("required_scopes", response.data)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_method_scope_permission_get_allow(self):
self.access_token.scope = "read"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-method-scope-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 200)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_method_scope_permission_post_allow(self):
self.access_token.scope = "create"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.post("/oauth2-method-scope-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 200)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_method_scope_permission_get_deny(self):
self.access_token.scope = "write"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-method-scope-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 403)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_method_scope_permission_post_deny(self):
self.access_token.scope = "read"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.post("/oauth2-method-scope-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 403)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_method_path_scope_permission_get_allow_1(self):
self.access_token.scope = "read"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-method-path-scope-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 200)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_method_path_scope_permission_get_allow_2(self):
self.access_token.scope = "get scope2"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-method-path-scope-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 200)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_method_path_scope_permission_post_allow_1(self):
self.access_token.scope = "create"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.post("/oauth2-method-path-scope-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 200)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_method_path_scope_permission_post_allow_2(self):
self.access_token.scope = "post scope2"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.post("/oauth2-method-path-scope-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 200)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_method_path_scope_permission_get_deny(self):
self.access_token.scope = "write"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-method-path-scope-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 403)
@unittest.skipUnless(rest_framework_installed, "djangorestframework not installed")
def test_method_path_scope_permission_post_deny(self):
self.access_token.scope = "read"
self.access_token.save()
auth = self._create_authorization_header(self.access_token.token)
response = self.client.post("/oauth2-method-path-scope-test/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 403)