diff --git a/music/serializers.py b/music/serializers.py index 19f63d1..1032f8a 100644 --- a/music/serializers.py +++ b/music/serializers.py @@ -1,4 +1,6 @@ from rest_framework import serializers +from django.contrib.auth.models import User + from .models import Songs @@ -18,4 +20,10 @@ class TokenSerializer(serializers.Serializer): """ This serializer serializes the token data """ - token = serializers.CharField(max_length=255) \ No newline at end of file + token = serializers.CharField(max_length=255) + + +class UserSerializer(serializers.ModelSerializer): + class Meta: + model = User + fields = ("username", "email") \ No newline at end of file diff --git a/music/tests.py b/music/tests.py index d1f0bef..31d8860 100644 --- a/music/tests.py +++ b/music/tests.py @@ -116,7 +116,7 @@ def login_a_user(self, username="", password=""): def login_client(self, username="", password=""): # get a token from DRF response = self.client.post( - reverse('create-token'), + reverse("create-token"), data=json.dumps( { 'username': username, @@ -133,6 +133,24 @@ def login_client(self, username="", password=""): self.client.login(username=username, password=password) return self.token + def register_a_user(self, username="", password="", email=""): + return self.client.post( + reverse( + "auth-register", + kwargs={ + "version": "v1" + } + ), + data=json.dumps( + { + "username": username, + "password": password, + "email": email + } + ), + content_type='application/json' + ) + def setUp(self): # create a admin user self.user = User.objects.create_superuser( @@ -293,3 +311,19 @@ def test_login_user_with_valid_credentials(self): response = self.login_a_user("anonymous", "pass") # assert status code is 401 UNAUTHORIZED self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) + + +class AuthRegisterUserTest(BaseViewTest): + """ + Tests for auth/register/ endpoint + """ + def test_register_a_user(self): + response = self.register_a_user("new_user", "new_pass", "new_user@mail.com") + # assert status code is 201 CREATED + self.assertEqual(response.data["username"], "new_user") + self.assertEqual(response.data["email"], "new_user@mail.com") + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + # test with invalid data + response = self.register_a_user() + # assert status code + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) \ No newline at end of file diff --git a/music/urls.py b/music/urls.py index 2779343..97d032d 100644 --- a/music/urls.py +++ b/music/urls.py @@ -1,9 +1,10 @@ from django.urls import path -from .views import ListCreateSongsView, SongsDetailView, LoginView +from .views import ListCreateSongsView, SongsDetailView, LoginView, RegisterUsers urlpatterns = [ path('songs/', ListCreateSongsView.as_view(), name="songs-list-create"), path('songs//', SongsDetailView.as_view(), name="songs-detail"), - path('auth/login/', LoginView.as_view(), name="auth-login") + path('auth/login/', LoginView.as_view(), name="auth-login"), + path('auth/register/', RegisterUsers.as_view(), name="auth-register") ] diff --git a/music/views.py b/music/views.py index 33a32b4..8f21778 100644 --- a/music/views.py +++ b/music/views.py @@ -9,7 +9,7 @@ from .decorators import validate_request_data from .models import Songs -from .serializers import SongsSerializer, TokenSerializer +from .serializers import SongsSerializer, TokenSerializer, UserSerializer # Get the JWT settings jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER @@ -113,4 +113,30 @@ def post(self, request, *args, **kwargs): )}) serializer.is_valid() return Response(serializer.data) - return Response(status=status.HTTP_401_UNAUTHORIZED) \ No newline at end of file + return Response(status=status.HTTP_401_UNAUTHORIZED) + + +class RegisterUsers(generics.CreateAPIView): + """ + POST auth/register/ + """ + permission_classes = (permissions.AllowAny,) + + def post(self, request, *args, **kwargs): + username = request.data.get("username", "") + password = request.data.get("password", "") + email = request.data.get("email", "") + if not username and not password and not email: + return Response( + data={ + "message": "username, password and email is required to register a user" + }, + status=status.HTTP_400_BAD_REQUEST + ) + new_user = User.objects.create_user( + username=username, password=password, email=email + ) + return Response( + data=UserSerializer(new_user).data, + status=status.HTTP_201_CREATED + )