diff --git a/rest-app/db.sqlite3 b/rest-app/db.sqlite3 index 0aeefde..9d5678d 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 f266be5..5271ef2 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 5b58650..8f74e30 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 74b3fd5..e76a947 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 269351c..624cd2c 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 7a008fc..72eb12b 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 01cd263..dc5c258 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 af42249..66e537e 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 fccce74..f707b2c 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 34c4c8a..00ad2b8 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 62d8e5b..310f28a 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 cb69e9d..8ea3aaf 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') + fields = ('username', 'first_name', 'last_name') class UserRegistrationSerializer(serializers.ModelSerializer): @@ -39,6 +39,7 @@ class UserRegistrationSerializer(serializers.ModelSerializer): user = User.objects.create_user(**validated_data) UserProfile.objects.create( user=user, + username=profile_data['username'], first_name=profile_data['first_name'], last_name=profile_data['last_name'] ) diff --git a/rest-app/smartpicasso/app/user/tests.py b/rest-app/smartpicasso/app/user/tests.py index 9f664c1..be066f4 100644 --- a/rest-app/smartpicasso/app/user/tests.py +++ b/rest-app/smartpicasso/app/user/tests.py @@ -1,29 +1,63 @@ +""" +@author: p.dolata +""" + 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, UserManager +from smartpicasso.app.user.models import User from smartpicasso.app.user.serializers import UserLoginSerializer, UserRegistrationSerializer +from smartpicasso.app.user_profile.models import UserProfile class UserApiTest(APITestCase): client = APIClient() + authenticate_url = reverse('authenticate') + register_url = reverse('register') + profile = {"username": 'test', "first_name": "test", "last_name": "test"} def test_login_when_user_non_exist(self): - url = reverse('authenticate') - response = self.client.post(url, {'email': 'non-exist', 'password': '123'}, format='json') + response = self.client.post(self.authenticate_url, {'email': 'non-exist', 'password': '123'}, format='json') assert response.status_code == 400 def test_login_when_user_exist(self): User.objects.create_user(email='test@test.com', password='test') - url = reverse('authenticate') - response = self.client.post(url, {'email': 'test@test.com', 'password': 'test'}, format='json') + response = self.client.post(self.authenticate_url, {'email': 'test@test.com', 'password': 'test'}, + format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['success'], 'True') self.assertIn('token', response.data) + def test_register_when_user_non_exist(self): + response = self.client.post(self.register_url, {'email': 'test@test.com', 'password': 'test', + 'profile': self.profile}, format='json') + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual(response.data['success'], 'True') + + def test_register_when_email_exist(self): + User.objects.create_user(email='test@test.com', password='test') + response = self.client.post(self.register_url, {'email': 'test@test.com', 'password': 'test', + 'profile': self.profile}, format='json') + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertIn('email', response.data) + + def test_register_when_username_exist(self): + user = User.objects.create_user(email='test2@test.com', password='test') + UserProfile.objects.create( + user=user, + username=self.profile['username'], + first_name=self.profile['first_name'], + last_name=self.profile['last_name'] + ) + response = self.client.post(self.register_url, {'email': 'test@test.com', 'password': 'test', + 'profile': self.profile}, format='json') + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertIn('profile', response.data) + self.assertIn('username', response.data['profile']) + class UserTest(TestCase): @@ -52,7 +86,7 @@ class UserRegistrationSerializerTest(TestCase): serializer = UserRegistrationSerializer() def test_create(self): - profile = {"first_name": "test", "last_name": "test"} + profile = {"username": 'test', "first_name": "test", "last_name": "test"} user = self.serializer.create({"email": "test@test.com", "password": "test", "profile": profile}) self.assertNotEqual(user, None) diff --git a/rest-app/smartpicasso/app/user/views.py b/rest-app/smartpicasso/app/user/views.py index ecb0a57..1a5bfcf 100644 --- a/rest-app/smartpicasso/app/user/views.py +++ b/rest-app/smartpicasso/app/user/views.py @@ -1,3 +1,7 @@ +""" +@author: p.dolata +""" + from rest_framework import status from rest_framework.generics import RetrieveAPIView, CreateAPIView from rest_framework.permissions import AllowAny 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 6b25f62..f69046b 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 08118cc..9753296 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 1aa4d72..4adad8b 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/0003_userprofile_username.py b/rest-app/smartpicasso/app/user_profile/migrations/0003_userprofile_username.py new file mode 100644 index 0000000..c0af2d1 --- /dev/null +++ b/rest-app/smartpicasso/app/user_profile/migrations/0003_userprofile_username.py @@ -0,0 +1,20 @@ +# Generated by Django 3.1.3 on 2020-12-09 18:34 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + atomic = False + + dependencies = [ + ('user_profile', '0002_remove_userprofile_phone_number'), + ] + + operations = [ + migrations.AddField( + model_name='userprofile', + name='username', + field=models.CharField(default='', max_length=50, unique=True), + preserve_default=False, + ), + ] 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 94e7772..a792bc3 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 48eab08..0b6cc2b 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 4da1ccf..8f25c64 100644 --- a/rest-app/smartpicasso/app/user_profile/models.py +++ b/rest-app/smartpicasso/app/user_profile/models.py @@ -13,9 +13,13 @@ class UserProfile(models.Model): """ id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile') + username = models.CharField(max_length=50, unique=True) first_name = models.CharField(max_length=50, unique=False) last_name = models.CharField(max_length=50, unique=False) + def __str__(self): + return str(self.username) + ' - ' + str(self.first_name) + ' ' + str(self.last_name) + class Meta: """ Meta to se table name in database diff --git a/rest-app/smartpicasso/app/user_profile/tests.py b/rest-app/smartpicasso/app/user_profile/tests.py index 7ce503c..bc84121 100644 --- a/rest-app/smartpicasso/app/user_profile/tests.py +++ b/rest-app/smartpicasso/app/user_profile/tests.py @@ -1,3 +1,65 @@ -from django.test import TestCase +""" +@author: p.dolata +""" -# Create your tests here. +from django.test import TestCase +from django.urls import reverse +from rest_framework import status +from rest_framework.test import APITestCase, APIClient + +from smartpicasso.app.user.models import User +from smartpicasso.app.user_profile.models import UserProfile + + +class UserProfileApiTest(APITestCase): + client = APIClient() + profile_url = reverse('profile') + authenticate_url = reverse('authenticate') + profile = {"username": 'test_user', "first_name": "first", "last_name": "last"} + + def test_get_profile_without_auth(self): + response = self.client.get(self.profile_url, format='json') + self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) + + def test_get_profile_with_invalid_token(self): + self.client.force_authenticate(user=None) + response = self.client.get(self.profile_url, format='json') + self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) + + def test_get_profile_when_user_without_profile(self): + user = User.objects.create_user(email='test@test.com', password='test') + self.client.force_authenticate(user=user) + response = self.client.get(self.profile_url, format='json') + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual(response.data['success'], 'False') + + def test_get_profile(self): + user = User.objects.create_user(email='test@test.com', password='test') + UserProfile.objects.create( + user=user, + username=self.profile['username'], + first_name=self.profile['first_name'], + last_name=self.profile['last_name'] + ) + self.client.force_authenticate(user=user) + response = self.client.get(self.profile_url, format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data['success'], 'True') + profile = response.data['profile'] + self.assertEqual(profile['username'], self.profile['username']) + self.assertEqual(profile['first_name'], self.profile['first_name']) + self.assertEqual(profile['last_name'], self.profile['last_name']) + + +class UserProfileTest(TestCase): + profile = {"username": 'test_user', "first_name": "first", "last_name": "last"} + + def test_user_profile_str(self): + user = User.objects.create_user(email='test@test.com', password='test') + user_profile = UserProfile.objects.create( + user=user, + username=self.profile['username'], + first_name=self.profile['first_name'], + last_name=self.profile['last_name'] + ) + self.assertEqual(str(user_profile), 'test_user - first last') diff --git a/rest-app/smartpicasso/app/user_profile/urls.py b/rest-app/smartpicasso/app/user_profile/urls.py new file mode 100644 index 0000000..61d89f6 --- /dev/null +++ b/rest-app/smartpicasso/app/user_profile/urls.py @@ -0,0 +1,11 @@ +""" +@author: p.dolata +""" + +from django.conf.urls import url + +from smartpicasso.app.user_profile.views import UserProfileView + +urlpatterns = [ + url(r'^profile', UserProfileView.as_view(), name='profile') +] diff --git a/rest-app/smartpicasso/app/user_profile/views.py b/rest-app/smartpicasso/app/user_profile/views.py index 91ea44a..04758b4 100644 --- a/rest-app/smartpicasso/app/user_profile/views.py +++ b/rest-app/smartpicasso/app/user_profile/views.py @@ -1,3 +1,40 @@ -from django.shortcuts import render +""" +@author: p.dolata +""" -# Create your views here. +from rest_framework import status +from rest_framework.generics import RetrieveAPIView +from rest_framework.permissions import IsAuthenticated +from rest_framework.response import Response +from rest_framework_jwt.authentication import JSONWebTokenAuthentication + +from smartpicasso.app.user_profile.models import UserProfile + + +class UserProfileView(RetrieveAPIView): + permission_classes = (IsAuthenticated,) + authentication_class = JSONWebTokenAuthentication + + def get(self, request): + try: + user_profile = UserProfile.objects.get(user=request.user) + status_code = status.HTTP_200_OK + response = { + 'success': 'True', + 'status_code': status_code, + 'message': 'User profile fetched successfully', + 'profile': { + 'username': user_profile.username, + 'first_name': user_profile.first_name, + 'last_name': user_profile.last_name + } + } + except Exception as e: + status_code = status.HTTP_400_BAD_REQUEST + response = { + 'success': 'False', + 'status_code': status_code, + 'message': 'User profile does not exist', + 'error': str(e) + } + return Response(response, status=status_code) diff --git a/rest-app/smartpicasso/urls.py b/rest-app/smartpicasso/urls.py index de1970c..bd27833 100644 --- a/rest-app/smartpicasso/urls.py +++ b/rest-app/smartpicasso/urls.py @@ -18,5 +18,6 @@ from django.urls import path, include urlpatterns = [ path('api/', include('smartpicasso.app.user.urls')), + path('api/', include('smartpicasso.app.user_profile.urls')), path('admin/', admin.site.urls), ]