Fakultas Ilmu Komputer UI

Commit 52cda0b9 authored by Glenda Emanuella Sutanto's avatar Glenda Emanuella Sutanto
Browse files

[GREEN] Implemented UserAuthSerializer and made it as serializer for user...

[GREEN] Implemented UserAuthSerializer and made it as serializer for user details, removed cart from CustomUserDetailsSerializer
parent 3d50e470
......@@ -2,22 +2,48 @@ from django.contrib.auth.models import Group
from rest_framework import serializers, exceptions
from dj_rest_auth.serializers import LoginSerializer
from dj_rest_auth.registration.serializers import RegisterSerializer
from payment.serializers import CartSerializer
from .models import CustomUser
class CustomUserDetailsSerializer(serializers.ModelSerializer):
class UserAuthSerializer(serializers.ModelSerializer):
def to_representation(self, instance):
result = super().to_representation(instance)
result['is_finished_onboarding'] = True
if instance.groups.exists():
result['role'] = instance.groups.first().name
if result['role'] == 'client':
if hasattr(instance, 'diet_questionnaire'):
result['is_finished_onboarding'] = \
(len(instance.diet_questionnaire.finished_steps) == 6)
else:
result['is_finished_onboarding'] = False
if instance.is_staff:
result['role'] = 'admin'
if instance.cart.exists():
result['cart'] = CartSerializer(instance.cart.last()).data
result['transaction_status'] = instance.cart.last().transaction_status
else:
result['cart'] = {}
result['transaction_status'] = None
return result
class Meta:
model = CustomUser
fields = ('id', 'name', 'email',)
read_only_fields = ('id', 'email',)
class CustomUserDetailsSerializer(serializers.ModelSerializer):
def to_representation(self, instance):
result = super().to_representation(instance)
if instance.groups.exists():
result['role'] = instance.groups.first().name
if instance.is_staff:
result['role'] = 'admin'
return result
class Meta:
......
......@@ -10,7 +10,10 @@ from dietela_program.models import DietelaProgram
from nutritionists.models import Nutritionist
from .models import CustomUser
from .serializers import (
CustomLoginSerializer, CustomRegisterSerializer,CustomUserDetailsSerializer
CustomLoginSerializer,
CustomRegisterSerializer,
CustomUserDetailsSerializer,
UserAuthSerializer,
)
from .google_utils import validate_google_token
......@@ -93,7 +96,11 @@ class UserModelTests(APITestCase):
languages="Bahasa Indonesia, Bahasa Inggris",
)
cls.cart = Cart.objects.create(program=cls.dietela_program, nutritionist=cls.nutritionist)
cls.cart = Cart.objects.create(
program=cls.dietela_program,
nutritionist=cls.nutritionist,
user=cls.custom_user_1
)
def test_create_user(self):
user = CustomUser.objects.create_user(name='tes', email='email2@email.com',
......@@ -353,6 +360,25 @@ class UserModelTests(APITestCase):
serializer = CustomLoginSerializer()
self.assertEqual(serializer.update(None, {}), None)
def test_user_auth_serializer(self):
user_data = UserAuthSerializer(self.custom_user_1).data
self.assertIn('email', user_data)
self.assertIn('name', user_data)
self.assertEqual('client', user_data.get('role'))
self.assertIn('id', user_data)
user_no_role = CustomUser()
user_no_role.name = 'no role'
user_no_role.email = 'norole@norole.com'
user_no_role.password = 'norole2404'
user_no_role.save()
user_no_role_data = UserAuthSerializer(user_no_role).data
self.assertTrue('role' not in user_no_role_data)
admin_user = CustomUser.objects.create_superuser('super2@user.com', 'foo')
admin_user_data = UserAuthSerializer(admin_user).data
self.assertEqual('admin', admin_user_data.get('role'))
@patch('authentication.google_utils.requests.get')
@patch('authentication.google_utils.json.loads')
class TestValidateGoogleToken(TestCase):
......
......@@ -3,7 +3,7 @@ from django.utils.translation import gettext_lazy as _
class TransactionStatus(models.TextChoices):
SUCCESS = "success", _("Sucess")
SUCCESS = "success", _("Success")
PENDING = "pending", _("Pending")
UNPAID = "unpaid", _("Unpaid")
ERROR = "error", _("Error")
......@@ -117,3 +117,6 @@ class DietQuestionnaire(models.Model):
dietela_nutritionist_expectation = models.TextField(null=True, blank=True)
dietela_program_expectation = models.TextField(null=True, blank=True)
finished_steps = ArrayField(models.PositiveIntegerField(), null=True, blank=True, default=list)
def __str__(self):
return str(self.user)
......@@ -62,6 +62,9 @@ class DietQuestionnaireTests(APITestCase):
)
def test_string_representation(self):
self.assertEqual(str(self.diet_questionnaire), 'email@email.com')
def test_create_diet_questionnaire(self):
login_data = {
'email': 'email2@email.com',
......
......@@ -204,7 +204,7 @@ AUTHENTICATION_BACKENDS = (
AUTH_USER_MODEL = 'authentication.CustomUser'
REST_AUTH_SERIALIZERS = {
'USER_DETAILS_SERIALIZER':'authentication.serializers.CustomUserDetailsSerializer'
'USER_DETAILS_SERIALIZER':'authentication.serializers.UserAuthSerializer'
}
REST_AUTH_REGISTER_SERIALIZERS = {
......
# Generated by Django 3.1 on 2021-05-10 16:24
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('payment', '0004_cart_transaction_status'),
]
operations = [
migrations.AlterField(
model_name='cart',
name='transaction_status',
field=models.CharField(choices=[('success', 'Success'), ('pending', 'Pending'), ('unpaid', 'Unpaid'), ('error', 'Error')], default='unpaid', max_length=128),
),
]
Markdown is supported
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