Fakultas Ilmu Komputer UI

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

Merge branch 'fix-registration' into 'staging'

fix user role and name bug for registration

See merge request !43
parents 9fee000a cf11d20f
Pipeline #72616 passed with stages
in 8 minutes and 51 seconds
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 .models import CustomUser
class CustomUserDetailsSerializer(serializers.ModelSerializer):
def to_representation(self, instance):
......@@ -15,6 +18,7 @@ class CustomUserDetailsSerializer(serializers.ModelSerializer):
fields = ('id', 'name', 'email',)
read_only_fields = ('id', 'email',)
class CustomLoginSerializer(LoginSerializer):
def validate_auth_user_status(self, user):
......@@ -31,3 +35,29 @@ class CustomLoginSerializer(LoginSerializer):
def update(self, instance, validated_data):
pass
class CustomRegisterSerializer(RegisterSerializer):
name = serializers.CharField()
def get_cleaned_data(self):
super().get_cleaned_data()
return {
'email': self.validated_data.get('email', ''),
'password1': self.validated_data.get('password1', ''),
'name': self.validated_data.get('name', '')
}
def create(self, validated_data):
pass
def update(self, instance, validated_data):
pass
def save(self, request):
user = super().save(request)
user.name = self.get_cleaned_data().get('name')
user.save()
client_role, _created = Group.objects.get_or_create(name='client')
user.groups.add(client_role)
return user
......@@ -6,7 +6,7 @@ from django.test import TestCase
from django.http import JsonResponse
from dietela_quiz.models import DietProfile
from .models import CustomUser
from .serializers import CustomLoginSerializer
from .serializers import CustomLoginSerializer, CustomRegisterSerializer
from .google_utils import validate_google_token
class UserModelTests(APITestCase):
......@@ -118,7 +118,10 @@ class UserModelTests(APITestCase):
}
response = self.client.post('/auth/registration/', data, format='json')
self.assertEqual(json.loads(response.content).get('user').get('email'), 'abc123@gmail.com')
json_response = json.loads(response.content)
self.assertEqual(json_response.get('user').get('email'), 'abc123@gmail.com')
self.assertEqual(json_response.get('user').get('role'), 'client')
self.assertEqual(json_response.get('user').get('name'), 'tes')
self.assertEqual(CustomUser.objects.count(), self.num_of_custom_user + 1)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
......@@ -157,6 +160,14 @@ class UserModelTests(APITestCase):
self.assertEqual(CustomUser.objects.count(), self.num_of_custom_user)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_create_registration_serializer_to_do_nothing(self):
serializer = CustomRegisterSerializer()
self.assertEqual(serializer.create({}), None)
def test_update_registration_serializer_to_do_nothing(self):
serializer = CustomRegisterSerializer()
self.assertEqual(serializer.update(None, {}), None)
def test_link_user_and_diet_profile_succeed(self):
data = {
'email': self.custom_user_2.email,
......
......@@ -194,8 +194,17 @@ CORS_ORIGIN_WHITELIST = [
"http://localhost:3000",
]
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
)
AUTH_USER_MODEL = 'authentication.CustomUser'
REST_AUTH_SERIALIZERS = {
'USER_DETAILS_SERIALIZER':'authentication.serializers.CustomUserDetailsSerializer'
}
REST_AUTH_REGISTER_SERIALIZERS = {
'REGISTER_SERIALIZER': 'authentication.serializers.CustomRegisterSerializer'
}
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