This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
generated from edu-python-course/django-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Auth Token] Implement user authentication (#109)
- Loading branch information
Showing
10 changed files
with
136 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
""" | ||
Users application API resources | ||
""" | ||
|
||
from rest_framework.authtoken.models import Token | ||
from rest_framework.authtoken.views import ObtainAuthToken | ||
from rest_framework.response import Response | ||
|
||
|
||
class AuthTokenAPIView(ObtainAuthToken): | ||
def post(self, request, *args, **kwargs): | ||
ctx = {"request": request} | ||
serializer = self.serializer_class(data=request.data, context=ctx) | ||
serializer.is_valid(raise_exception=True) | ||
user = serializer.validated_data["user"] | ||
token, _ = Token.objects.get_or_create(user=user) | ||
|
||
return Response({ | ||
"user_pk": user.pk, | ||
"token": token.key, | ||
}) |
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,13 @@ | ||
""" | ||
Users application API routes | ||
""" | ||
|
||
from django.urls import path | ||
|
||
from users.resources import AuthTokenAPIView | ||
|
||
app_name = "users" | ||
urlpatterns = [ | ||
path("auth-token/", AuthTokenAPIView.as_view(), name="auth-token"), | ||
] |
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,35 @@ | ||
from http import HTTPStatus | ||
|
||
from rest_framework import test | ||
from rest_framework.reverse import reverse | ||
|
||
|
||
class TestAuthTokenAPIView(test.APITestCase): | ||
fixtures = ["users"] | ||
|
||
@classmethod | ||
def setUpTestData(cls) -> None: | ||
cls.url_path = reverse("api:users:auth-token") | ||
cls.credentials = { | ||
"username": "prombery87", | ||
"password": "ieZeiSh5k", | ||
} | ||
|
||
def setUp(self) -> None: | ||
self.client = test.APIClient() | ||
|
||
def test_valid_credentials(self): | ||
response = self.client.post(self.url_path, self.credentials) | ||
self.assertIn(b"user_pk", response.content) | ||
self.assertIn(b"token", response.content) | ||
|
||
def test_invalid_credentials(self): | ||
credentials = self.credentials.copy() | ||
credentials["username"] = "invalid" | ||
response = self.client.post(self.url_path, credentials) | ||
self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST) | ||
|
||
credentials = self.credentials.copy() | ||
credentials["password"] = "invalid" | ||
response = self.client.post(self.url_path, credentials) | ||
self.assertEqual(response.status_code, HTTPStatus.BAD_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from django.contrib.auth import get_user_model | ||
from rest_framework import test | ||
from rest_framework.authtoken.models import Token | ||
from rest_framework.reverse import reverse | ||
|
||
from users.resources import AuthTokenAPIView | ||
|
||
UserModel = get_user_model() | ||
|
||
|
||
class TestAuthTokenAPIView(test.APITestCase): | ||
fixtures = ["users"] | ||
|
||
@classmethod | ||
def setUpTestData(cls) -> None: | ||
cls.url_path = reverse("api:users:auth-token") | ||
cls.credentials = { | ||
"username": "prombery87", | ||
"password": "ieZeiSh5k", | ||
} | ||
cls.user = UserModel.objects.get(pk=2) | ||
|
||
def setUp(self) -> None: | ||
self.factory = test.APIRequestFactory() | ||
self.view = AuthTokenAPIView.as_view() | ||
|
||
def test_auth_token_created(self): | ||
request = self.factory.post(self.url_path, self.credentials) | ||
self.view(request) | ||
self.assertTrue(Token.objects.filter(user=self.user).exists()) |