backend #2

Merged
s460930 merged 5 commits from backend into develop 2020-12-05 19:08:57 +01:00
22 changed files with 104 additions and 9 deletions

Binary file not shown.

View File

@ -20,7 +20,7 @@ class UserSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ('first_name', 'last_name', 'phone_number')
fields = ('first_name', 'last_name')
class UserRegistrationSerializer(serializers.ModelSerializer):
@ -40,8 +40,7 @@ class UserRegistrationSerializer(serializers.ModelSerializer):
UserProfile.objects.create(
user=user,
first_name=profile_data['first_name'],
last_name=profile_data['last_name'],
phone_number=profile_data['phone_number'],
last_name=profile_data['last_name']
)
return user

View File

@ -1,9 +1,11 @@
from django.test import TestCase
from django.urls import reverse
from rest_framework import serializers
from rest_framework import status
from rest_framework.test import APITestCase, APIClient
from smartpicasso.app.user.models import User
from smartpicasso.app.user.models import User, UserManager
from smartpicasso.app.user.serializers import UserLoginSerializer, UserRegistrationSerializer
class UserApiTest(APITestCase):
@ -29,3 +31,57 @@ class UserTest(TestCase):
email = 'test@test.com'
user = User.objects.create_user(email=email, password='test')
self.assertEqual(str(user), email)
class UserLoginSerializerTest(TestCase):
serializer = UserLoginSerializer()
def test_validate_wrong_credentials(self):
data = {'email': 'test@test.com', 'password': '123'}
self.assertRaises(serializers.ValidationError, self.serializer.validate, data)
def test_validate_success(self):
User.objects.create_user(email='test@test.com', password='test')
data = {'email': 'test@test.com', 'password': 'test'}
result = self.serializer.validate(data)
self.assertEqual(result['email'], 'test@test.com')
self.assertIn('token', result)
class UserRegistrationSerializerTest(TestCase):
serializer = UserRegistrationSerializer()
def test_create(self):
profile = {"first_name": "test", "last_name": "test"}
user = self.serializer.create({"email": "test@test.com", "password": "test", "profile": profile})
self.assertNotEqual(user, None)
self.assertEqual(user.email, "test@test.com")
class UserManagerTest(TestCase):
manager = User.objects
def test_create_user_none_email(self):
email = None
self.assertRaises(ValueError, self.manager.create_user, email)
def test_create_user(self):
user = self.manager.create_user("test@test.pl", "test")
self.assertNotEqual(user, None)
self.assertEqual(user.email, "test@test.pl")
self.assertEqual(user.is_active, True)
self.assertEqual(user.is_superuser, False)
self.assertEqual(user.is_staff, False)
def test_create_superuser_none_password(self):
password = None
self.assertRaises(TypeError, self.manager.create_superuser, "super@test.pl", password)
def test_create_superuser(self):
user = self.manager.create_superuser("super@test.pl", "test")
self.assertNotEqual(user, None)
self.assertEqual(user.email, "super@test.pl")
self.assertEqual(user.is_active, True)
self.assertEqual(user.is_superuser, True)
self.assertEqual(user.is_staff, True)

View File

@ -4,8 +4,9 @@
from django.conf.urls import url
from smartpicasso.app.user.views import UserLoginView
from smartpicasso.app.user.views import UserLoginView, UserRegistrationView
urlpatterns = [
url(r'^authenticate', UserLoginView.as_view(), name='authenticate')
url(r'^authenticate', UserLoginView.as_view(), name='authenticate'),
url(r'^register', UserRegistrationView.as_view(), name='register')
]

View File

@ -1,9 +1,9 @@
from rest_framework import status
from rest_framework.generics import RetrieveAPIView
from rest_framework.generics import RetrieveAPIView, CreateAPIView
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from smartpicasso.app.user.serializers import UserLoginSerializer
from smartpicasso.app.user.serializers import UserLoginSerializer, UserRegistrationSerializer
class UserLoginView(RetrieveAPIView):
@ -25,3 +25,24 @@ class UserLoginView(RetrieveAPIView):
status_code = status.HTTP_200_OK
return Response(response, status=status_code)
class UserRegistrationView(CreateAPIView):
"""
View for user registration
"""
permission_classes = (AllowAny,)
serializer_class = UserRegistrationSerializer
def post(self, request):
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
status_code = status.HTTP_201_CREATED
response = {
'success': 'True',
'status_code': status_code,
'message': 'User registered successfully'
}
return Response(response, status=status_code)

View File

@ -0,0 +1,19 @@
# Generated by Django 3.1.3 on 2020-12-01 21:54
from django.db import migrations
class Migration(migrations.Migration):
atomic = False
dependencies = [
('user_profile', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='userprofile',
name='phone_number',
),
]

View File

@ -15,7 +15,6 @@ class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
first_name = models.CharField(max_length=50, unique=False)
last_name = models.CharField(max_length=50, unique=False)
phone_number = models.CharField(max_length=10, unique=False, null=True, blank=True)
class Meta:
"""