-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…192 issues
- Loading branch information
1 parent
4e31ab4
commit 8ae0e27
Showing
3 changed files
with
84 additions
and
19 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 |
---|---|---|
|
@@ -2,10 +2,11 @@ | |
from unittest.mock import Mock | ||
|
||
import pytest | ||
from asgiref.sync import sync_to_async | ||
from django.contrib.auth.models import AnonymousUser, User | ||
|
||
from ninja_extra import ControllerBase, api_controller, http_get, permissions | ||
from ninja_extra.testing import TestClient | ||
from ninja_extra.testing import TestAsyncClient, TestClient | ||
|
||
anonymous_request = Mock() | ||
anonymous_request.user = AnonymousUser() | ||
|
@@ -250,6 +251,13 @@ def index(self): | |
def permission_accept_type_and_instance(self): | ||
return {"success": True} | ||
|
||
@http_get( | ||
"permission/async/", | ||
permissions=[permissions.IsAdminUser() & permissions.IsAuthenticatedOrReadOnly], | ||
) | ||
async def permission_accept_type_and_instance_async(self): | ||
return {"success": True} | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.parametrize("route", ["permission/", "index/"]) | ||
|
@@ -269,3 +277,23 @@ def test_permission_controller_instance(route): | |
res = client.get(route, user=user) | ||
assert res.status_code == 200 | ||
assert res.json() == {"success": True} | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.asyncio | ||
async def test_permission_controller_instance_async(): | ||
user = await sync_to_async(User.objects.create_user)( | ||
username="eadwin", | ||
email="[email protected]", | ||
password="password", | ||
is_staff=True, | ||
is_superuser=True, | ||
) | ||
|
||
client = TestAsyncClient(Some2Controller) | ||
res = await client.get("/permission/async/", user=AnonymousUser()) | ||
assert res.status_code == 403 | ||
|
||
res = await client.get("/permission/async/", user=user) | ||
assert res.status_code == 200 | ||
assert res.json() == {"success": True} |