-
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.
- Loading branch information
1 parent
d56f837
commit 399a4ce
Showing
13 changed files
with
161 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,31 @@ | ||
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 | ||
|
||
factory = RequestFactory() | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_authenticated(): | ||
request = factory.get("/") | ||
|
||
def test_authenticated(db, rf): | ||
request = rf.get("/") | ||
request.user = AnonymousUser() | ||
assert isinstance(Authenticated(None, request), HTTP401) | ||
with pytest.raises(HTTP401): | ||
Authenticated(request) | ||
|
||
request.user = User.objects.create(username="test", password="test") | ||
assert Authenticated(None, request) == 200 | ||
|
||
Authenticated(request) | ||
|
||
@pytest.mark.django_db | ||
def test_staff(): | ||
request = factory.get("/") | ||
|
||
def test_staff(db, rf): | ||
request = rf.get("/") | ||
request.user = AnonymousUser() | ||
assert isinstance(Staff(None, request), HTTP404) | ||
with pytest.raises(HTTP404): | ||
Staff(request) | ||
|
||
request.user = User.objects.create(username="test", password="test") | ||
assert isinstance(Staff(None, request), HTTP404) | ||
with pytest.raises(HTTP404): | ||
Staff(request) | ||
|
||
request.user.is_staff = True | ||
request.user.save() | ||
assert Staff(None, request) == 200 | ||
Staff(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 __init__(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 __init__(self, request, **kwargs): | ||
pass | ||
|
||
def PublicEndpoint(self, request): | ||
return 200 | ||
|
||
class Staff: | ||
def __init__(self, request, **kwargs): | ||
if request.user.is_authenticated and request.user.is_staff: | ||
return | ||
|
||
raise HTTP404() |
Oops, something went wrong.