From 0b2da889427244966994e06f127ac25ee3cb260b Mon Sep 17 00:00:00 2001 From: s460930 Date: Wed, 9 Dec 2020 19:21:30 +0100 Subject: [PATCH 1/4] SMART-42 implemented retrieve user profile view --- rest-app/db.sqlite3 | Bin 167936 -> 167936 bytes .../__pycache__/__init__.cpython-38.pyc | Bin 167 -> 167 bytes .../__pycache__/settings.cpython-38.pyc | Bin 3500 -> 3500 bytes .../__pycache__/urls.cpython-38.pyc | Bin 1015 -> 1015 bytes .../__pycache__/wsgi.cpython-38.pyc | Bin 580 -> 580 bytes .../user/__pycache__/__init__.cpython-38.pyc | Bin 176 -> 176 bytes .../app/user/__pycache__/admin.cpython-38.pyc | Bin 217 -> 217 bytes .../user/__pycache__/models.cpython-38.pyc | Bin 2416 -> 2416 bytes .../__pycache__/0001_initial.cpython-38.pyc | Bin 1028 -> 1028 bytes .../0002_auto_20201130_2119.cpython-38.pyc | Bin 545 -> 545 bytes .../__pycache__/__init__.cpython-38.pyc | Bin 187 -> 187 bytes rest-app/smartpicasso/app/user/serializers.py | 3 +- .../__pycache__/__init__.cpython-38.pyc | Bin 184 -> 184 bytes .../__pycache__/admin.cpython-38.pyc | Bin 225 -> 225 bytes .../__pycache__/models.cpython-38.pyc | Bin 1060 -> 1060 bytes .../__pycache__/0001_initial.cpython-38.pyc | Bin 1055 -> 1055 bytes .../__pycache__/__init__.cpython-38.pyc | Bin 195 -> 195 bytes .../smartpicasso/app/user_profile/models.py | 1 + .../smartpicasso/app/user_profile/views.py | 37 +++++++++++++++++- 19 files changed, 38 insertions(+), 3 deletions(-) diff --git a/rest-app/db.sqlite3 b/rest-app/db.sqlite3 index 0aeefde76f76489d795545caff8a5e92ce52c00a..3841dcd432ea01b0a42fbe197540b9d975e33331 100644 GIT binary patch delta 108 zcmV-y0F(cKpbCJX3XmHCFOeKW0WX1IwO|3E{|^qu00a!d01sCT!44S>ZLxu|30{|$|1gQW3 delta 19 ZcmZ3^xSWwYl$V!_0SG4a9Gl2J2LLKa1n&R< diff --git a/rest-app/smartpicasso/__pycache__/settings.cpython-38.pyc b/rest-app/smartpicasso/__pycache__/settings.cpython-38.pyc index 5b5865037fe9425e9bf83e0e5d76b99d6948d776..8f74e3094d892ecb8ae9f48b7dc1ce49287c9758 100644 GIT binary patch delta 20 acmZ1@y+)cll$V!_0SM~-&u!#h$O`~6cLf;$ delta 20 acmZ1@y+)cll$V!_0SG4a9NWmfkQV?nGzDJ( diff --git a/rest-app/smartpicasso/__pycache__/urls.cpython-38.pyc b/rest-app/smartpicasso/__pycache__/urls.cpython-38.pyc index 74b3fd52bd0832a495c073c74b9c2eb2b89b19cf..a7c2f9b2241829ed7dbf70406c61415ca40844da 100644 GIT binary patch delta 20 acmey){+*pWl$V!_0SM~-&u!#>!wdjBRRzcZ delta 20 acmey){+*pWl$V!_0SG4a9NWnKh8X}o5(W+c diff --git a/rest-app/smartpicasso/__pycache__/wsgi.cpython-38.pyc b/rest-app/smartpicasso/__pycache__/wsgi.cpython-38.pyc index 269351cdcfc2ff197e546f0837801c5067288685..624cd2cd49421e1ae7d3ab06f995aea75f1d8b2e 100644 GIT binary patch delta 20 acmX@Ya)gCDl$V!_0SM~-&u!$kWC8#&@C2v; delta 20 acmX@Ya)gCDl$V!_0SG4a9NWlk$pip1tpx4> 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 7a008fc13de5ed1c5ad00761da70068fc00e5ca8..72eb12b4eafa74b963799d6effcf364e0bf4a65b 100644 GIT binary patch delta 19 ZcmdnMxPg&7l$V!_0SM~-&rRfB3IHk|1jGOU delta 19 ZcmdnMxPg&7l$V!_0SG4a9Gl3!6aXug1quKF 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 01cd26348969e76516a1d59a4483c163e0a13e48..dc5c2583a881bfa98316680e6b51aa8d967169e6 100644 GIT binary patch delta 19 Zcmcb~c$1Mkl$V!_0SM~-&rRe$2LLb&1wQ}) delta 19 Zcmcb~c$1Mkl$V!_0SG4a9Gl2}4gfKN1%&_r 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 af422490e0d4d204c91c7a4ea15cb87dc094f022..66e537eb877091ef17f6feffde1897331eb846e8 100644 GIT binary patch delta 20 acmew$^g)O_l$V!_0SM~-&u!#RlpK0D^k|a~rvTG6MiAfdugY delta 20 acmZqSXyM=v<>lpK0D`@nk8R}s$qWE4V+D%< 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 34c4c8a626f4a77737187501670febe8e5d44858..00ad2b87204a0b20afd7f5b1091671b6db95a57d 100644 GIT binary patch delta 20 acmZ3;vXF&4l$V!_0SM~-&u!!uV*&sycmzKH delta 20 acmZ3;vXF&4l$V!_0SNYPKDLotj0pfRS_Ihu 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 62d8e5b70e88d3ed03a0201522c41878e50f7972..310f28ac9dd9255d172748e897fb136402d09c0e 100644 GIT binary patch delta 19 ZcmdnZxSNqXl$V!_0SM~-&rRgs001ks1myq# delta 19 ZcmdnZxSNqXl$V!_0SG4a9Gl3!0RSy51uFmm 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_profile/__pycache__/__init__.cpython-38.pyc b/rest-app/smartpicasso/app/user_profile/__pycache__/__init__.cpython-38.pyc index 6b25f622f576112f60d3465b4d8354ac09c3b4a4..f69046bef81ca4411b010344ca131db4652ee03d 100644 GIT binary patch delta 19 ZcmdnNxPy^9l$V!_0SM~-&rRfB3jixd1l#}s delta 19 ZcmdnNxPy^9l$V!_0SG4a9Gl3!762^01tI_d 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 08118cc5d5a0f51ce01cebee94ac4fc8b24ff034..975329643db64764625b762b10864b6cafc07357 100644 GIT binary patch delta 19 ZcmaFJ_>hr1l$V!_0SM~-&rRgM1^_WL1y=w7 delta 19 ZcmaFJ_>hr1l$V!_0SG4a9Gl2}4FEE#1)Tr@ 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 1aa4d7255859d909c737419499fb3717e8bdf01b..bd1d7a14781d8050cba52cfe539a8ac9f71b0c50 100644 GIT binary patch delta 20 acmZ3&v4n#=l$V!_0SM~-&u!$EWB~vzf&@|k delta 20 acmZ3&v4n#=l$V!_0SMZ6AKS<+$pQc{8U(%o 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 94e7772ebae22bca859d004989044af2256d95ce..a792bc3bae0aa9f90a8e42d68858ab984c49fadf 100644 GIT binary patch delta 20 acmbQwF`t7wl$V!_0SM~-&u!!uVF3Uvx&%W2 delta 20 acmbQwF`t7wl$V!_0SG4a9NWk(!U6y;cLa$5 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 48eab08fa8b1530035b9522ce27a0f16441013b1..0b6cc2bcbff79dfefbeeb1cbf6e4c5aede37a43c 100644 GIT binary patch delta 19 ZcmX@ic$kqpl$V!_0SM~-&rRgs0RSxB1pNR2 delta 19 ZcmX@ic$kqpl$V!_0SG4a9Gl3!0{|{m1w#M; diff --git a/rest-app/smartpicasso/app/user_profile/models.py b/rest-app/smartpicasso/app/user_profile/models.py index 4da1ccf..01908c7 100644 --- a/rest-app/smartpicasso/app/user_profile/models.py +++ b/rest-app/smartpicasso/app/user_profile/models.py @@ -13,6 +13,7 @@ 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) diff --git a/rest-app/smartpicasso/app/user_profile/views.py b/rest-app/smartpicasso/app/user_profile/views.py index 91ea44a..9835f6c 100644 --- a/rest-app/smartpicasso/app/user_profile/views.py +++ b/rest-app/smartpicasso/app/user_profile/views.py @@ -1,3 +1,36 @@ -from django.shortcuts import render +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 -# Create your views here. +from smartpicasso.app.user_profile.models import UserProfile + + +class UserProfileView(RetrieveAPIView): + permission_classes = (IsAuthenticated,) + authentication_classes = 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', + 'data': { + '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 does not exist', + 'error': str(e) + } + return Response(response, status=status_code) -- 2.20.1 From 3c6736c98cfca5e7a449731518f354ebe6aa39b1 Mon Sep 17 00:00:00 2001 From: s460930 Date: Wed, 9 Dec 2020 19:24:41 +0100 Subject: [PATCH 2/4] SMART-42 registered retreive user profile endpoint --- rest-app/smartpicasso/app/user/views.py | 4 ++++ rest-app/smartpicasso/app/user_profile/urls.py | 11 +++++++++++ rest-app/smartpicasso/app/user_profile/views.py | 4 ++++ rest-app/smartpicasso/urls.py | 1 + 4 files changed, 20 insertions(+) create mode 100644 rest-app/smartpicasso/app/user_profile/urls.py 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/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 9835f6c..8cec569 100644 --- a/rest-app/smartpicasso/app/user_profile/views.py +++ b/rest-app/smartpicasso/app/user_profile/views.py @@ -1,3 +1,7 @@ +""" +@author: p.dolata +""" + from rest_framework import status from rest_framework.generics import RetrieveAPIView from rest_framework.permissions import IsAuthenticated 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), ] -- 2.20.1 From dae09902075a796227f9d8c1b36efb55d214b18b Mon Sep 17 00:00:00 2001 From: s460930 Date: Wed, 9 Dec 2020 19:36:14 +0100 Subject: [PATCH 3/4] SMART-42 added username field to UserProfile entity --- rest-app/db.sqlite3 | Bin 167936 -> 172032 bytes .../__pycache__/urls.cpython-38.pyc | Bin 1015 -> 1065 bytes .../__pycache__/models.cpython-38.pyc | Bin 1060 -> 1086 bytes .../migrations/0003_userprofile_username.py | 20 ++++++++++++++++++ 4 files changed, 20 insertions(+) create mode 100644 rest-app/smartpicasso/app/user_profile/migrations/0003_userprofile_username.py diff --git a/rest-app/db.sqlite3 b/rest-app/db.sqlite3 index 3841dcd432ea01b0a42fbe197540b9d975e33331..212aed72f7115830d37481d6ece05f9f1af015eb 100644 GIT binary patch delta 432 zcmZozz}0YoYl5_(F#`jG77%L!F*6VcPSi1GG~Sr7gr8ZQOL#K7fG!i4@W#fwoNg}S ztPHC5j-|z^Mezki`DvLssRjlH#_=ElC?CwuOUz9*GB7gGH8j#Suv9R#urfBaGBVdQ zwlK3aG2I*~wM>*ze3Jo-0ULt=0|S5V#>Qm+$+`K{A>4191smS*Z~j)#_kdA!I%@)> zDj%;D&qJOSJo$WHAULtHY5T4OMqMrzJ}<_Ojg2jgOy=&>3$htC1&op1re*F17M-4u z&B)8FX6_2&nQ^N&>AJ9stEw`#$WGsw%_z&LGW~ou<9%Hn1tpMfkmHpU$`Xr`GZKq5 zObs*@{QN@{{6c+v6hi$x14CUw;nW6l!-YIX!-@Pr9@rP*JO}wCd4Yb}$g}+*50f)9 z4_J5-{~iAAATP%9PtTQSnhp^?#lMz+0{`>Pf&~ry(6k=oyTp; zvmOK|HnwcvmB6UW#lo{5XhWkHlezQs!W>3T0i)95)S~!;qWrYXoK#J7C$Q*ro?J#= zO^qg2M|N>lRmNuDlEkE(RAiONZ1w3*xs3N&fu3Me+}J3~wEaRJqv1q;zM~8*JO>&0 z!+8$!N%8{SyOC%6K^`V&W*!!h2PW~~;olDOMlApITzRJHK+y>d{HOTW@=xG@zFDxK zfq(i0eWrr#f&onI3Y<*9kWmF<6%hXe|D*{l98COC3_#UU8yo%jjRTojeI+G*4J^`3 zQ_WH=4a|*AOcIk4lT6am3@nmUO)OJV4Gm0^0}@M$Dzjbka}rAur$6LnQZNo=1Zy-; zPD?bjNHj`GvowQfOg2hQOfyJIF-lHKF)%~X*vrSHz{3S}4HN%%2B0VI0G+#061}@u>b%7 diff --git a/rest-app/smartpicasso/__pycache__/urls.cpython-38.pyc b/rest-app/smartpicasso/__pycache__/urls.cpython-38.pyc index a7c2f9b2241829ed7dbf70406c61415ca40844da..e76a947179e147a2c61850b757b3ddb7016c40ce 100644 GIT binary patch delta 123 zcmey)zLG;bl$V!_0SJ7=F2SyI?j znX=fj*qfQsnWH#TID#28IX4#UVPaI8EXXX%5noW0pO%@EI$4eRumBIx02Y28Mge9X IMj-qP04`J(m;e9( delta 94 zcmZ3<@ts{el$V!_0SM~-&&8WCGcY^`agYHEkmCTv#SRm-jfB}!nX=fj*qfQsnW8vS f*n=4~IX2epVVWGnyq6DT4hugIqX07xBhNno-jTPr#ia zg)@b#g&~EjnJG$eGautsMo|%R! delta 113 zcmdnTv4n#+l$V!_0SM~-&&8)qDoYk`3VRBK$M4RN!kNO= z!jQt%%oHWCS%&c{qo^=YYcUIu;9z87E)oSY{4@n8voWVkE@8Ih=7CBGF$zuI#oPh_ Dv(Xma 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, + ), + ] -- 2.20.1 From f3e9c33034125500684e155c41f0a2869b1698cf Mon Sep 17 00:00:00 2001 From: s460930 Date: Wed, 9 Dec 2020 20:57:11 +0100 Subject: [PATCH 4/4] SMART-42 added tests for retrieve user profile endpoint --- rest-app/db.sqlite3 | Bin 172032 -> 172032 bytes rest-app/smartpicasso/app/user/tests.py | 46 ++++++++++-- .../__pycache__/models.cpython-38.pyc | Bin 1086 -> 1266 bytes .../smartpicasso/app/user_profile/models.py | 3 + .../smartpicasso/app/user_profile/tests.py | 66 +++++++++++++++++- .../smartpicasso/app/user_profile/views.py | 10 +-- 6 files changed, 112 insertions(+), 13 deletions(-) diff --git a/rest-app/db.sqlite3 b/rest-app/db.sqlite3 index 212aed72f7115830d37481d6ece05f9f1af015eb..9d5678dd7a84199d9edd14718c3b760603b67e1a 100644 GIT binary patch delta 873 zcmb7?Pj3=I9ENuXV+&asdy`;ejoeJ>?(FQ$EKL*FA_!qADl2~ylkV)F(qL2CVye9q zz5&KWzJQ)M5o5U4Yd?ai=QiFw8r_0T8V^MeGpEV>d*0_aouf$SDAJ3I%WrS5ip$;g z&+FocUlNE81XzgkcSd(@KGRAu~K} zUPKTC0FMQD3{PPXo_?BZbzr}@1lqFz<^=c==7!xBcvuJ5%~;Hw=g3Ax=N`t6gI$Ml z1$&x`6iyTcc{VDYD{Q96KBob;k`wPWahx5$f;Bysb2XT(WAK%m{}Ojbihv-?BPs+XYfZb;{@K4Li6HLlfNw6?5Fie`**f%|#pGmPkBF7}y!z&`(rNlE>C9o_bGRMR*Bq!g{SidN-%qiU2 zH9Xxvu|hw{Ju1k_GR;>zBss^`qrf!V*2uufK-bVn*T7K0(A>(<%*xb4&)C?^)YQnt z$Ra7lz}UhvG0E7(!X(Yo(8$=r+`!P%G|kK~G0D6ju_Prw$Jn8`L@#mj-F(hz0!%Cr zH&5cf!@r#$=;m1d>ACVue$!2PnG{T!l$m`w2^q=-^cC3Or})?MPvC#PS+JmifBFP{ krjpGp6aL6AS|G4pAc5(-{KN?Y+u0kK{_<~TY52nr06ZgH?*IS* 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_profile/__pycache__/models.cpython-38.pyc b/rest-app/smartpicasso/app/user_profile/__pycache__/models.cpython-38.pyc index 5a858c65eeea097f5cc339b424e611601c862687..4adad8b729c403c8ee77a749781a26f759aafb2c 100644 GIT binary patch delta 327 zcmdnT@rjc!l$V!_0SKg6s{JAC_#6I6z&wB7KRj_W~L~i z6y9J4O}@!WjPpVnL7Jcd#AXI!XCN+C0}>?+H4Isd&5S9G;t)1dGmw?S1m!VK1bQOLvDrmCYVlFNznw-h3$f?PAi={XTit3xEhg5FrX8WG448D=L8mK?Vph3NiA5AQK035y(_cjv^_LGP%iT GnF|4X0zYs7 delta 173 zcmeywxsQV{l$V!_0SKmxT#R49G?8x+W6s2lu_EpaDV!->Eet7K%}h~(Dcr#fnmm)W z80SsC!gPpjB}0)g1H)u@rW9=!-_(*s5upBJ79hdF$iiGC3S{_c3Kj7I=^}m*Apjx- zCtqU@nk>tt$W$ac`2dT!RuM=C$nIj0N|06|Mj=K%5M%<&X>t?^f^>>cZe%G001K@k A*8l(j diff --git a/rest-app/smartpicasso/app/user_profile/models.py b/rest-app/smartpicasso/app/user_profile/models.py index 01908c7..8f25c64 100644 --- a/rest-app/smartpicasso/app/user_profile/models.py +++ b/rest-app/smartpicasso/app/user_profile/models.py @@ -17,6 +17,9 @@ class UserProfile(models.Model): 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/views.py b/rest-app/smartpicasso/app/user_profile/views.py index 8cec569..04758b4 100644 --- a/rest-app/smartpicasso/app/user_profile/views.py +++ b/rest-app/smartpicasso/app/user_profile/views.py @@ -13,17 +13,17 @@ from smartpicasso.app.user_profile.models import UserProfile class UserProfileView(RetrieveAPIView): permission_classes = (IsAuthenticated,) - authentication_classes = JSONWebTokenAuthentication + 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', + 'success': 'True', 'status_code': status_code, 'message': 'User profile fetched successfully', - 'data': { + 'profile': { 'username': user_profile.username, 'first_name': user_profile.first_name, 'last_name': user_profile.last_name @@ -32,9 +32,9 @@ class UserProfileView(RetrieveAPIView): except Exception as e: status_code = status.HTTP_400_BAD_REQUEST response = { - 'success': 'false', + 'success': 'False', 'status_code': status_code, - 'message': 'User does not exist', + 'message': 'User profile does not exist', 'error': str(e) } return Response(response, status=status_code) -- 2.20.1