diff --git a/rest-app/db.sqlite3 b/rest-app/db.sqlite3 index 6fe1bbc..0aeefde 100644 Binary files a/rest-app/db.sqlite3 and b/rest-app/db.sqlite3 differ diff --git a/rest-app/smartpicasso/__pycache__/__init__.cpython-38.pyc b/rest-app/smartpicasso/__pycache__/__init__.cpython-38.pyc index 012896b..f266be5 100644 Binary files a/rest-app/smartpicasso/__pycache__/__init__.cpython-38.pyc and b/rest-app/smartpicasso/__pycache__/__init__.cpython-38.pyc differ diff --git a/rest-app/smartpicasso/__pycache__/settings.cpython-38.pyc b/rest-app/smartpicasso/__pycache__/settings.cpython-38.pyc index 092397f..5b58650 100644 Binary files a/rest-app/smartpicasso/__pycache__/settings.cpython-38.pyc and b/rest-app/smartpicasso/__pycache__/settings.cpython-38.pyc differ diff --git a/rest-app/smartpicasso/__pycache__/urls.cpython-38.pyc b/rest-app/smartpicasso/__pycache__/urls.cpython-38.pyc index b91070c..74b3fd5 100644 Binary files a/rest-app/smartpicasso/__pycache__/urls.cpython-38.pyc and b/rest-app/smartpicasso/__pycache__/urls.cpython-38.pyc differ diff --git a/rest-app/smartpicasso/__pycache__/wsgi.cpython-38.pyc b/rest-app/smartpicasso/__pycache__/wsgi.cpython-38.pyc index 4baa4a5..269351c 100644 Binary files a/rest-app/smartpicasso/__pycache__/wsgi.cpython-38.pyc and b/rest-app/smartpicasso/__pycache__/wsgi.cpython-38.pyc differ diff --git a/rest-app/smartpicasso/app/user/__pycache__/__init__.cpython-38.pyc b/rest-app/smartpicasso/app/user/__pycache__/__init__.cpython-38.pyc index 8142876..7a008fc 100644 Binary files a/rest-app/smartpicasso/app/user/__pycache__/__init__.cpython-38.pyc and b/rest-app/smartpicasso/app/user/__pycache__/__init__.cpython-38.pyc differ diff --git a/rest-app/smartpicasso/app/user/__pycache__/admin.cpython-38.pyc b/rest-app/smartpicasso/app/user/__pycache__/admin.cpython-38.pyc index 6218ee9..01cd263 100644 Binary files a/rest-app/smartpicasso/app/user/__pycache__/admin.cpython-38.pyc and b/rest-app/smartpicasso/app/user/__pycache__/admin.cpython-38.pyc differ diff --git a/rest-app/smartpicasso/app/user/__pycache__/models.cpython-38.pyc b/rest-app/smartpicasso/app/user/__pycache__/models.cpython-38.pyc index b3aaace..af42249 100644 Binary files a/rest-app/smartpicasso/app/user/__pycache__/models.cpython-38.pyc and b/rest-app/smartpicasso/app/user/__pycache__/models.cpython-38.pyc differ diff --git a/rest-app/smartpicasso/app/user/migrations/__pycache__/0001_initial.cpython-38.pyc b/rest-app/smartpicasso/app/user/migrations/__pycache__/0001_initial.cpython-38.pyc index 5119f86..fccce74 100644 Binary files a/rest-app/smartpicasso/app/user/migrations/__pycache__/0001_initial.cpython-38.pyc and b/rest-app/smartpicasso/app/user/migrations/__pycache__/0001_initial.cpython-38.pyc differ diff --git a/rest-app/smartpicasso/app/user/migrations/__pycache__/0002_auto_20201130_2119.cpython-38.pyc b/rest-app/smartpicasso/app/user/migrations/__pycache__/0002_auto_20201130_2119.cpython-38.pyc index 1cde8a2..34c4c8a 100644 Binary files a/rest-app/smartpicasso/app/user/migrations/__pycache__/0002_auto_20201130_2119.cpython-38.pyc and b/rest-app/smartpicasso/app/user/migrations/__pycache__/0002_auto_20201130_2119.cpython-38.pyc differ diff --git a/rest-app/smartpicasso/app/user/migrations/__pycache__/__init__.cpython-38.pyc b/rest-app/smartpicasso/app/user/migrations/__pycache__/__init__.cpython-38.pyc index e34e9eb..62d8e5b 100644 Binary files a/rest-app/smartpicasso/app/user/migrations/__pycache__/__init__.cpython-38.pyc and b/rest-app/smartpicasso/app/user/migrations/__pycache__/__init__.cpython-38.pyc differ diff --git a/rest-app/smartpicasso/app/user/serializers.py b/rest-app/smartpicasso/app/user/serializers.py index e5dee9f..cb69e9d 100644 --- a/rest-app/smartpicasso/app/user/serializers.py +++ b/rest-app/smartpicasso/app/user/serializers.py @@ -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 diff --git a/rest-app/smartpicasso/app/user/tests.py b/rest-app/smartpicasso/app/user/tests.py index a33d137..9f664c1 100644 --- a/rest-app/smartpicasso/app/user/tests.py +++ b/rest-app/smartpicasso/app/user/tests.py @@ -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) diff --git a/rest-app/smartpicasso/app/user/urls.py b/rest-app/smartpicasso/app/user/urls.py index f12a788..390985a 100644 --- a/rest-app/smartpicasso/app/user/urls.py +++ b/rest-app/smartpicasso/app/user/urls.py @@ -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') ] diff --git a/rest-app/smartpicasso/app/user/views.py b/rest-app/smartpicasso/app/user/views.py index a303436..ecb0a57 100644 --- a/rest-app/smartpicasso/app/user/views.py +++ b/rest-app/smartpicasso/app/user/views.py @@ -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) diff --git a/rest-app/smartpicasso/app/user_profile/__pycache__/__init__.cpython-38.pyc b/rest-app/smartpicasso/app/user_profile/__pycache__/__init__.cpython-38.pyc index 90b0343..6b25f62 100644 Binary files a/rest-app/smartpicasso/app/user_profile/__pycache__/__init__.cpython-38.pyc and b/rest-app/smartpicasso/app/user_profile/__pycache__/__init__.cpython-38.pyc differ diff --git a/rest-app/smartpicasso/app/user_profile/__pycache__/admin.cpython-38.pyc b/rest-app/smartpicasso/app/user_profile/__pycache__/admin.cpython-38.pyc index 1f0e340..08118cc 100644 Binary files a/rest-app/smartpicasso/app/user_profile/__pycache__/admin.cpython-38.pyc and b/rest-app/smartpicasso/app/user_profile/__pycache__/admin.cpython-38.pyc differ diff --git a/rest-app/smartpicasso/app/user_profile/__pycache__/models.cpython-38.pyc b/rest-app/smartpicasso/app/user_profile/__pycache__/models.cpython-38.pyc index 4815020..1aa4d72 100644 Binary files a/rest-app/smartpicasso/app/user_profile/__pycache__/models.cpython-38.pyc and b/rest-app/smartpicasso/app/user_profile/__pycache__/models.cpython-38.pyc differ diff --git a/rest-app/smartpicasso/app/user_profile/migrations/0002_remove_userprofile_phone_number.py b/rest-app/smartpicasso/app/user_profile/migrations/0002_remove_userprofile_phone_number.py new file mode 100644 index 0000000..7ee8a6a --- /dev/null +++ b/rest-app/smartpicasso/app/user_profile/migrations/0002_remove_userprofile_phone_number.py @@ -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', + ), + ] diff --git a/rest-app/smartpicasso/app/user_profile/migrations/__pycache__/0001_initial.cpython-38.pyc b/rest-app/smartpicasso/app/user_profile/migrations/__pycache__/0001_initial.cpython-38.pyc index e354c63..94e7772 100644 Binary files a/rest-app/smartpicasso/app/user_profile/migrations/__pycache__/0001_initial.cpython-38.pyc and b/rest-app/smartpicasso/app/user_profile/migrations/__pycache__/0001_initial.cpython-38.pyc differ diff --git a/rest-app/smartpicasso/app/user_profile/migrations/__pycache__/__init__.cpython-38.pyc b/rest-app/smartpicasso/app/user_profile/migrations/__pycache__/__init__.cpython-38.pyc index 8f1df47..48eab08 100644 Binary files a/rest-app/smartpicasso/app/user_profile/migrations/__pycache__/__init__.cpython-38.pyc and b/rest-app/smartpicasso/app/user_profile/migrations/__pycache__/__init__.cpython-38.pyc differ diff --git a/rest-app/smartpicasso/app/user_profile/models.py b/rest-app/smartpicasso/app/user_profile/models.py index 1c0c2df..4da1ccf 100644 --- a/rest-app/smartpicasso/app/user_profile/models.py +++ b/rest-app/smartpicasso/app/user_profile/models.py @@ -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: """