Fakultas Ilmu Komputer UI

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

Fix google login response and add admin role to user serializer

parent d1a17c65
......@@ -13,4 +13,4 @@ def validate_google_token(access_token):
content = {'message': 'wrong google token / this google token is already expired.'}
return False, JsonResponse(content, status=status.HTTP_400_BAD_REQUEST)
return True, data.get('email')
return True, data
......@@ -11,6 +11,8 @@ class CustomUserDetailsSerializer(serializers.ModelSerializer):
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:
......
......@@ -9,7 +9,9 @@ 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 .serializers import (
CustomLoginSerializer, CustomRegisterSerializer,CustomUserDetailsSerializer
)
from .google_utils import validate_google_token
class UserModelTests(APITestCase):
......@@ -131,6 +133,25 @@ class UserModelTests(APITestCase):
CustomUser.objects.create_superuser(
email='super@user.com', password='foo', is_staff=False)
def test_user_serializer(self):
user_data = CustomUserDetailsSerializer(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 = CustomUserDetailsSerializer(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 = CustomUserDetailsSerializer(admin_user).data
self.assertEqual('admin', admin_user_data.get('role'))
def test_post_registration_user_succeed(self):
data = {
'name': 'tes',
......@@ -326,11 +347,12 @@ class UserModelTests(APITestCase):
class TestValidateGoogleToken(TestCase):
def test_validate_access_token_succeed(self, mock_json_loads, mock_get):
mock_get.return_value.text = "ABCDEFGH"
mock_json_loads.return_value = {"email": 'mock_user@email.com'}
mock_json_loads.return_value = {"name": "mock", "email": 'mock_user@email.com'}
success, google_data = validate_google_token("ABCDEFGH")
self.assertTrue(success)
self.assertEqual('mock_user@email.com', google_data)
self.assertEqual('mock_user@email.com', google_data.get('email'))
self.assertEqual('mock', google_data.get('name'))
def test_validate_access_token_failed(self, mock_json_loads, mock_get):
mock_get.return_value.text = "123123123"
......@@ -346,7 +368,7 @@ class TestValidateGoogleToken(TestCase):
@patch('authentication.views.validate_google_token')
class TestGoogleLogin(TestCase):
def test_google_login_succeed(self, mock_validate_google_token):
mock_validate_google_token.return_value = True, 'email@email.com'
mock_validate_google_token.return_value = True, {"name": "tes", "email": 'email@email.com'}
data = {'access_token': 'QWERTY'}
response = self.client.post('/auth/google/', data, format='json')
......@@ -354,6 +376,9 @@ class TestGoogleLogin(TestCase):
self.assertIn('access_token', json_response)
self.assertIn('refresh_token', json_response)
self.assertIn('user', json_response)
self.assertIn('role', json_response.get('user'))
self.assertIn('name', json_response.get('user'))
self.assertIn('email', json_response.get('user'))
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_google_login_failed(self, mock_validate_google_token):
......
......@@ -61,15 +61,15 @@ class GoogleView(APIView):
return google_data
try:
user = CustomUser.objects.get(email=
google_data
google_data.get('email')
)
except CustomUser.DoesNotExist:
user = CustomUser()
# random default password
user.password = make_password(BaseUserManager().make_random_password())
user.email = google_data
user.save()
user = CustomUser.objects.create_user(
email=google_data.get('email'),
name=google_data.get('name'),
# random default password
password=make_password(BaseUserManager().make_random_password()),
)
token = RefreshToken.for_user(user) # generate token
response = {}
response['user'] = CustomUserDetailsSerializer(user).data
......
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