Fakultas Ilmu Komputer UI

Commit b5c260dc authored by Kefas Satrio Bangkit Solidedantyo's avatar Kefas Satrio Bangkit Solidedantyo
Browse files

Merge branch 'PBI-4-AddCartInLinkAPI' into 'staging'

Add cart in link user and diet profile API

See merge request !44
parents 3a737d93 40ab4975
Pipeline #72722 passed with stages
in 11 minutes and 27 seconds
......@@ -5,6 +5,9 @@ from rest_framework.test import APITestCase
from django.test import TestCase
from django.http import JsonResponse
from dietela_quiz.models import DietProfile
from payment.models import Cart
from dietela_program.models import DietelaProgram
from nutritionists.models import Nutritionist
from .models import CustomUser
from .serializers import CustomLoginSerializer, CustomRegisterSerializer
from .google_utils import validate_google_token
......@@ -68,6 +71,25 @@ class UserModelTests(APITestCase):
cls.num_of_diet_profile = DietProfile.objects.count()
cls.num_of_custom_user = CustomUser.objects.count()
cls.dietela_program = DietelaProgram.objects.create(
unique_code="PRG1",
name="Program 1",
price=350000.00,
)
cls.nutritionist = Nutritionist.objects.create(
id=1,
full_name_and_degree="Test, S.Gz",
registration_certificate_no="1234567890",
university="Universitas Indonesia",
mastered_nutritional_problems="Manajemen berat badan, hipertensi",
handled_age_group="12-17 tahun (Remaja)",
another_practice_place="RSCM",
languages="Bahasa Indonesia, Bahasa Inggris",
)
cls.cart = Cart.objects.create(program=cls.dietela_program, nutritionist=cls.nutritionist)
def test_create_user(self):
user = CustomUser.objects.create_user(name='tes', email='email2@email.com',
password='tes', diet_profile=self.diet_profile_1,)
......@@ -168,26 +190,29 @@ class UserModelTests(APITestCase):
serializer = CustomRegisterSerializer()
self.assertEqual(serializer.update(None, {}), None)
def test_link_user_and_diet_profile_succeed(self):
def test_link_user_and_diet_profile_and_cart_succeed(self):
data = {
'email': self.custom_user_2.email,
'diet_profile_id': self.diet_profile_2.id,
'cart_id': self.cart.id,
}
response = self.client.post('/auth/link-user-and-diet-profile/', data, format='json')
response = self.client.post('/auth/link-data/', data, format='json')
self.assertEqual(json.loads(response.content).get('user').get('email'), \
self.custom_user_2.email)
self.assertEqual(json.loads(response.content).get('diet_profile').get('id'), \
self.diet_profile_2.id)
self.assertEqual(json.loads(response.content).get('cart').get('id'), self.cart.id)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_link_user_and_diet_profile_failed_because_email_doesnt_exist(self):
data = {
'email': 'emaill@gmail.com',
'diet_profile_id': self.diet_profile_2.id,
'cart_id': self.cart.id,
}
response = self.client.post('/auth/link-user-and-diet-profile/', data, format='json')
response = self.client.post('/auth/link-data/', data, format='json')
self.assertEqual(json.loads(response.content).get('message'), 'User is not found.')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
......@@ -195,12 +220,24 @@ class UserModelTests(APITestCase):
data = {
'email': self.custom_user_2.email,
'diet_profile_id': -1,
'cart_id': self.cart.id,
}
response = self.client.post('/auth/link-user-and-diet-profile/', data, format='json')
response = self.client.post('/auth/link-data/', data, format='json')
self.assertEqual(json.loads(response.content).get('message'), 'Diet profile is not found.')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_link_user_and_diet_profile_failed_because_cart_doesnt_exist(self):
data = {
'email': self.custom_user_2.email,
'diet_profile_id': self.diet_profile_2.id,
'cart_id': -1,
}
response = self.client.post('/auth/link-data/', data, format='json')
self.assertEqual(json.loads(response.content).get('message'), 'Cart is not found.')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_post_login_user_succeed(self):
data = {
'email': 'email@email.com',
......
from rest_framework import routers
from django.urls import path, include
from .views import LinkUserAndDietProfileViewSet, GoogleView, CustomLoginView
from .views import LinkDataViewSet, GoogleView, CustomLoginView
router = routers.SimpleRouter()
router.register('auth/link-user-and-diet-profile', LinkUserAndDietProfileViewSet, \
basename="LinkUserAndDietProfile")
router.register('auth/link-data', LinkDataViewSet, \
basename="LinkData")
urlpatterns = [
path('auth/registration/', include('dj_rest_auth.registration.urls')),
......
......@@ -7,6 +7,8 @@ from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.hashers import make_password
from dietela_quiz.models import DietProfile
from dietela_quiz.serializers import DietProfileSerializer
from payment.models import Cart
from payment.serializers import CartSerializer
from .models import CustomUser
from .serializers import CustomUserDetailsSerializer, CustomLoginSerializer
from .google_utils import validate_google_token
......@@ -15,12 +17,13 @@ class CustomLoginView(LoginView):
serializer_class = CustomLoginSerializer
class LinkUserAndDietProfileViewSet(viewsets.ViewSet):
class LinkDataViewSet(viewsets.ViewSet):
@staticmethod
def create(request):
email = request.data['email']
diet_profile_id = request.data['diet_profile_id']
cart_id = request.data['cart_id']
try:
user = CustomUser.objects.get(email=email)
......@@ -33,12 +36,21 @@ class LinkUserAndDietProfileViewSet(viewsets.ViewSet):
return Response({'message': 'Diet profile is not found.'}, \
status=status.HTTP_400_BAD_REQUEST)
try:
cart = Cart.objects.get(id=cart_id)
except Cart.DoesNotExist:
return Response({'message': 'Cart is not found.'}, status=status.HTTP_400_BAD_REQUEST)
user.diet_profile = diet_profile
user.save()
cart.user = user
cart.save()
return Response({\
'user': CustomUserDetailsSerializer(user).data,\
'diet_profile': DietProfileSerializer(diet_profile).data\
'diet_profile': DietProfileSerializer(diet_profile).data, \
'cart': CartSerializer(cart).data \
}, status=status.HTTP_200_OK)
......
# Generated by Django 3.1 on 2021-04-25 13:05
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('payment', '0002_remove_cart_client'),
]
operations = [
migrations.AddField(
model_name='cart',
name='user',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='cart', to=settings.AUTH_USER_MODEL),
),
]
from django.db import models
from dietela_program.models import DietelaProgram
from nutritionists.models import Nutritionist
from authentication.models import CustomUser
class Cart(models.Model):
......@@ -12,6 +13,12 @@ class Cart(models.Model):
Nutritionist,
on_delete=models.CASCADE
)
user = models.ForeignKey(
CustomUser,
on_delete=models.CASCADE,
related_name="cart",
null=True
)
def __str__(self):
return f"{self.program.unique_code} - {self.nutritionist}"
return f"{self.user} - {self.program.unique_code} - {self.nutritionist}"
from rest_framework import serializers
from dietela_program.serializers import DietelaProgramSerializer
from nutritionists.serializers import NutritionistSerializer
from authentication.serializers import CustomUserDetailsSerializer
from .models import Cart
......@@ -14,4 +15,5 @@ class CartSerializer(serializers.ModelSerializer):
result = super().to_representation(instance)
result['program'] = DietelaProgramSerializer(instance.program).data
result['nutritionist'] = NutritionistSerializer(instance.nutritionist).data
result['user'] = CustomUserDetailsSerializer(instance.user).data
return result
......@@ -74,7 +74,7 @@ class CartTests(APITestCase):
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_cart_string(self):
self.assertEqual(str(self.cart_1), 'PRG1 - Test, S.Gz')
self.assertEqual(str(self.cart_1), 'None - PRG1 - Test, S.Gz')
def test_post_cart_api(self):
url = f"{self.BASE_URL}"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment