-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch permissions to classes and raise rather than return
- Loading branch information
1 parent
d56f837
commit 122635c
Showing
13 changed files
with
175 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,45 @@ | ||
import pytest | ||
|
||
from django.contrib.auth.models import AnonymousUser, User | ||
from django.test import RequestFactory | ||
|
||
from worf.exceptions import HTTP401, HTTP404 | ||
from worf.permissions import Authenticated, Staff | ||
from worf.permissions import Authenticated, PublicEndpoint, Staff | ||
|
||
factory = RequestFactory() | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_authenticated(): | ||
request = factory.get("/") | ||
|
||
def test_authenticated(db, rf): | ||
permission = Authenticated() | ||
request = rf.get("/") | ||
request.user = AnonymousUser() | ||
assert isinstance(Authenticated(None, request), HTTP401) | ||
|
||
with pytest.raises(HTTP401): | ||
assert permission(request) is None | ||
|
||
request.user = User.objects.create(username="test", password="test") | ||
assert Authenticated(None, request) == 200 | ||
assert permission(request) is None | ||
|
||
|
||
def test_public_endpoint(db, rf): | ||
permission = PublicEndpoint() | ||
request = rf.get("/") | ||
request.user = AnonymousUser() | ||
assert permission(request) is None | ||
request.user = User.objects.create(username="test", password="test") | ||
assert permission(request) is None | ||
|
||
@pytest.mark.django_db | ||
def test_staff(): | ||
request = factory.get("/") | ||
|
||
def test_staff(db, rf): | ||
permission = Staff() | ||
request = rf.get("/") | ||
request.user = AnonymousUser() | ||
assert isinstance(Staff(None, request), HTTP404) | ||
|
||
with pytest.raises(HTTP404): | ||
assert permission(request) is None | ||
|
||
request.user = User.objects.create(username="test", password="test") | ||
assert isinstance(Staff(None, request), HTTP404) | ||
|
||
with pytest.raises(HTTP404): | ||
assert permission(request) is None | ||
|
||
request.user.is_staff = True | ||
request.user.save() | ||
assert Staff(None, request) == 200 | ||
permission(request) |
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,17 +1,13 @@ | ||
from django.urls import path | ||
|
||
from tests.views import ( | ||
ProfileDetail, | ||
ProfileList, | ||
ProfileListSubSet, | ||
UserDetail, | ||
UserList, | ||
) | ||
from tests import views | ||
|
||
urlpatterns = [ | ||
path("profiles/", ProfileList.as_view()), | ||
path("profiles/subset/", ProfileListSubSet.as_view()), | ||
path("profiles/<str:id>/", ProfileDetail.as_view()), | ||
path("users/", UserList.as_view()), | ||
path("users/<str:id>/", UserDetail.as_view()), | ||
path("profiles/", views.ProfileList.as_view()), | ||
path("profiles/subset/", views.ProfileListSubSet.as_view()), | ||
path("profiles/<uuid:id>/", views.ProfileDetail.as_view()), | ||
path("profiles/<uuid:id>/staff/", views.StaffDetail.as_view()), | ||
path("user/", views.UserSelf.as_view()), | ||
path("users/", views.UserList.as_view()), | ||
path("users/<int:id>/", views.UserDetail.as_view()), | ||
] |
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,17 +1,22 @@ | ||
from worf.exceptions import HTTP401, HTTP404 | ||
|
||
|
||
def Authenticated(self, request): | ||
if not request.user.is_authenticated: | ||
return HTTP401() | ||
return 200 | ||
class Authenticated: | ||
def __call__(self, request, **kwargs): | ||
if request.user.is_authenticated: | ||
return | ||
|
||
raise HTTP401() | ||
|
||
def Staff(self, request): | ||
if not request.user.is_authenticated or not request.user.is_staff: | ||
return HTTP404() | ||
return 200 | ||
|
||
class PublicEndpoint: | ||
def __call__(self, request, **kwargs): | ||
pass | ||
|
||
def PublicEndpoint(self, request): | ||
return 200 | ||
|
||
class Staff: | ||
def __call__(self, request, **kwargs): | ||
if request.user.is_authenticated and request.user.is_staff: | ||
return | ||
|
||
raise HTTP404() |
Oops, something went wrong.