Skip to content

Commit

Permalink
Add register a user view
Browse files Browse the repository at this point in the history
* add a register a user view
  • Loading branch information
kasulani committed Mar 30, 2018
1 parent 605e70d commit 7591f9e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
10 changes: 9 additions & 1 deletion music/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from rest_framework import serializers
from django.contrib.auth.models import User

from .models import Songs


Expand All @@ -18,4 +20,10 @@ class TokenSerializer(serializers.Serializer):
"""
This serializer serializes the token data
"""
token = serializers.CharField(max_length=255)
token = serializers.CharField(max_length=255)


class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ("username", "email")
36 changes: 35 additions & 1 deletion music/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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)
5 changes: 3 additions & 2 deletions music/urls.py
Original file line number Diff line number Diff line change
@@ -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/<int:pk>/', 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")
]
30 changes: 28 additions & 2 deletions music/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,4 +113,30 @@ def post(self, request, *args, **kwargs):
)})
serializer.is_valid()
return Response(serializer.data)
return Response(status=status.HTTP_401_UNAUTHORIZED)
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
)

0 comments on commit 7591f9e

Please sign in to comment.