Skip to content

Commit

Permalink
CreateUserView: create new user with a token
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlo-myskov committed Nov 16, 2023
1 parent 3d1f065 commit 26ed948
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
15 changes: 15 additions & 0 deletions app/user/tests/test_user_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ def test_create_user_success(self):
self.assertTrue(user.check_password(payload['password']))
self.assertNotIn('password', res.data)

def test_create_user_with_token(self):
"""Test creating a user with a token."""
payload = {
'email': 'test@example.com',
'password': 'testpass123',
'name': 'Test Name',
}
res = self.client.post(CREATE_USER_URL, payload)

self.assertEqual(res.status_code, status.HTTP_201_CREATED)
user = get_user_model().objects.get(email=payload['email'])
self.assertIn('token', res.data)
self.assertTrue(user.auth_token)
self.assertEqual(res.data['token'], user.auth_token.key)

def test_user_with_email_exists_error(self):
"""Test error returned if user with email exists."""
payload = {
Expand Down
59 changes: 58 additions & 1 deletion app/user/views.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,80 @@
"""
Views for the user API
"""
from drf_spectacular.utils import extend_schema_view, extend_schema
from drf_spectacular.utils import (
extend_schema_view,
extend_schema,
OpenApiExample
)

from rest_framework import generics, authentication, permissions
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
from rest_framework.settings import api_settings
from rest_framework import status

from user.serializers import (
UserSerializer,
AuthTokenSerializer,
)


@extend_schema_view(
post=extend_schema(
summary='Create a new user',
description='Create a new user in the system.',
examples=[
OpenApiExample(
'Create a new user',
summary='Return the created user',
description='Return the created user with the auth token',
value={
'email': 'user@example.com',
'name': 'string',
'token': 'string',
},
response_only=True,
),
],
),
)
class CreateUserView(generics.CreateAPIView):
"""Create a new user in the system."""
serializer_class = UserSerializer

def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
token, created = Token.objects.get_or_create(user=serializer.instance)
response_data = serializer.data
response_data['token'] = token.key
return Response(
response_data,
status=status.HTTP_201_CREATED,
headers=headers
)


@extend_schema_view(
post=extend_schema(
summary='Create a new auth token',
description='Get or create a new auth token for the user.',
examples=[
OpenApiExample(
'Create a new auth token',
summary='Return the created auth token',
description='Return the created auth token for the user',
value={
'token': 'string',
},
response_only=True,
),
],
),
)
class CreateTokenView(ObtainAuthToken):
"""Create a new auth token for user."""
serializer_class = AuthTokenSerializer
Expand Down

0 comments on commit 26ed948

Please sign in to comment.