-
-
Notifications
You must be signed in to change notification settings - Fork 35
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 #54 from eadwinCode/allow_permission_instance
Allow permission instance in permissions parameter
- Loading branch information
Showing
9 changed files
with
131 additions
and
30 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
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
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
from typing import List, Type, Union | ||
from typing import Any, List, Type, Union | ||
|
||
from ninja_extra.permissions.base import ( | ||
BasePermission, | ||
OperandHolder, | ||
SingleOperandHolder, | ||
) | ||
|
||
PermissionType = Union[ | ||
List[Type[BasePermission]], List[OperandHolder], List[SingleOperandHolder], List | ||
PermissionType = List[ | ||
Union[Type[BasePermission], OperandHolder, SingleOperandHolder, BasePermission, Any] | ||
] |
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 |
---|---|---|
|
@@ -4,7 +4,8 @@ | |
import pytest | ||
from django.contrib.auth.models import AnonymousUser, User | ||
|
||
from ninja_extra import permissions | ||
from ninja_extra import ControllerBase, api_controller, http_get, permissions | ||
from ninja_extra.testing import TestClient | ||
|
||
anonymous_request = Mock() | ||
anonymous_request.user = AnonymousUser() | ||
|
@@ -214,3 +215,39 @@ def test_object_and_lazyness(self): | |
assert hasperm is False | ||
assert mock_deny.call_count == 1 | ||
mock_allow.assert_not_called() | ||
|
||
|
||
@api_controller( | ||
"permission/", permissions=[permissions.AllowAny, permissions.IsAdminUser()] | ||
) | ||
class Some2Controller(ControllerBase): | ||
@http_get("index/") | ||
def index(self): | ||
return {"success": True} | ||
|
||
@http_get( | ||
"permission/", | ||
permissions=[permissions.IsAdminUser() & permissions.IsAuthenticatedOrReadOnly], | ||
) | ||
def permission_accept_type_and_instance(self): | ||
return {"success": True} | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.parametrize("route", ["permission/", "index/"]) | ||
def test_permission_controller_instance(route): | ||
user = User.objects.create_user( | ||
username="eadwin", | ||
email="[email protected]", | ||
password="password", | ||
is_staff=True, | ||
is_superuser=True, | ||
) | ||
|
||
client = TestClient(Some2Controller) | ||
res = client.get(route, user=AnonymousUser()) | ||
assert res.status_code == 403 | ||
|
||
res = client.get(route, user=user) | ||
assert res.status_code == 200 | ||
assert res.json() == {"success": True} |