-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from cyx2000/add_object_permission
Add object permission and update README.md
- Loading branch information
Showing
3 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from asgiref.sync import sync_to_async | ||
from django.http import HttpResponse | ||
from django.test import TestCase, override_settings | ||
|
||
from adrf.views import APIView | ||
from rest_framework.permissions import BasePermission | ||
from rest_framework.test import APIRequestFactory | ||
|
||
factory = APIRequestFactory() | ||
|
||
|
||
class AsyncObjectPermission(BasePermission): | ||
async def has_permission(self, request, view): | ||
return True | ||
|
||
async def has_object_permission(self, request, view, obj): | ||
if obj != "/async/allow": | ||
return False | ||
return True | ||
|
||
|
||
class SyncObjectPermission(BasePermission): | ||
def has_permission(self, request, view): | ||
return True | ||
|
||
def has_object_permission(self, request, view, obj): | ||
if obj != "/sync/allow": | ||
return False | ||
return True | ||
|
||
|
||
class ObjectPermissionTestView(APIView): | ||
permission_classes = (AsyncObjectPermission,) | ||
|
||
async def get(self, request): | ||
await sync_to_async(self.check_object_permissions)(request, request.path) | ||
return HttpResponse("ok") | ||
|
||
|
||
@override_settings(ROOT_URLCONF=__name__) | ||
class TestAsyncObjectPermission(TestCase): | ||
async def test_async_object_permission(self): | ||
request = factory.get("/async/allow") | ||
|
||
response = await ObjectPermissionTestView.as_view()(request) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
|
||
async def test_async_object_permission_reject(self): | ||
request = factory.get("/async/reject") | ||
|
||
response = await ObjectPermissionTestView.as_view()(request) | ||
|
||
self.assertEqual(response.status_code, 403) | ||
|
||
|
||
@override_settings(ROOT_URLCONF=__name__) | ||
class TestSyncObjectPermission(TestCase): | ||
async def test_sync_object_permission(self): | ||
request = factory.get("/sync/allow") | ||
|
||
response = await ObjectPermissionTestView.as_view( | ||
permission_classes=(SyncObjectPermission,) | ||
)(request) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
|
||
async def test_sync_object_permission_reject(self): | ||
request = factory.get("/sync/reject") | ||
|
||
response = await ObjectPermissionTestView.as_view( | ||
permission_classes=(SyncObjectPermission,) | ||
)(request) | ||
|
||
self.assertEqual(response.status_code, 403) |