From f3e9c33034125500684e155c41f0a2869b1698cf Mon Sep 17 00:00:00 2001 From: s460930 Date: Wed, 9 Dec 2020 20:57:11 +0100 Subject: [PATCH] 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)