Skip to content

Commit

Permalink
Issue 12 - Created api view and serializer for registering users. (#41)
Browse files Browse the repository at this point in the history
* Created api view and serializer for registering users.

* Added additional tests and validators.
Moved bingo urls into bingo/

* Moved validation into serializer.
  • Loading branch information
someonebear authored Dec 14, 2024
1 parent 19d687f commit c98fc9a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 10 deletions.
12 changes: 4 additions & 8 deletions server/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@
from django.contrib import admin
from django.urls import path, include

from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)

urlpatterns = [
path("admin/", admin.site.urls),
path("api/healthcheck/", include(("api.healthcheck.urls"))),
path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('admin/', admin.site.urls),
# Moved authentication/bingo related urls to bingo/.
path('api/', include('bingo.urls')),
]
30 changes: 30 additions & 0 deletions server/bingo/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from rest_framework import serializers
from django.contrib.auth import get_user_model
from django.contrib.auth.password_validation import validate_password

User = get_user_model()


class UserRegisterSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'email', 'password']
extra_kwargs = {
'password': {'write_only': True},
'first_name': {'required': True},
'last_name': {'required': True}
}

def create(self, validated_data):
user = User.objects.create_user(
username=validated_data['username'],
first_name=validated_data['first_name'],
last_name=validated_data['last_name'],
email=validated_data['email'],
password=validated_data['password'],
)
return user

def validate_password(self, value):
validate_password(value)
return value
41 changes: 40 additions & 1 deletion server/bingo/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.test import TestCase, Client
from django.db.utils import IntegrityError
from django.test import TestCase
from .models import User, Challenge, Friendship

from django.core.exceptions import ValidationError


Expand Down Expand Up @@ -42,6 +43,44 @@ def test_default_total_completions(self):
self.assertEqual(challenge.total_completions, 0)


class RegisterUserTest(TestCase):
def setUp(self):
self.client = Client()

def test_register_user(self):
response = self.client.post(
'/api/register/', {'username': 'test_user', 'email': 'test@gmail.com',
'first_name': 'larry', 'last_name': 'bird', 'password': 'SuperSecure123'}
)

self.assertEqual(response.status_code, 201)

# Required fields not sent.
def test_register_user_error(self):
response = self.client.post(
'/api/register/', {'username': 'forgotten_fields', 'email': 'test@gmail.com',
'password': 'SuperSecure123'}
)

self.assertEqual(response.status_code, 400)

def test_invalid_email_error(self):
response = self.client.post(
'/api/register/', {'username': 'forgotten_fields', 'email': 'invalidemail',
'first_name': 'larry', 'last_name': 'bird', 'password': 'SuperSecure123'}
)

self.assertEqual(response.status_code, 400)

def test_insecure_password_error(self):
response = self.client.post(
'/api/register/', {'username': 'forgotten_fields', 'email': 'test@test.com',
'first_name': 'larry', 'last_name': 'bird', 'password': '1234'}
)

self.assertEqual(response.status_code, 400)


class FriendshipTest(TestCase):
def setUp(self):
# Create test users
Expand Down
13 changes: 13 additions & 0 deletions server/bingo/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.urls import path

from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)
from bingo import views

urlpatterns = [
path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('register/', views.register_user, name='register_user'),
]
14 changes: 13 additions & 1 deletion server/bingo/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
from django.shortcuts import render # noqa
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .serializers import UserRegisterSerializer

# Create your views here.


@api_view(['POST'])
def register_user(request):
serializer = UserRegisterSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response({'User created successfully.'}, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

0 comments on commit c98fc9a

Please sign in to comment.