Fakultas Ilmu Komputer UI

Commit 81f79bab authored by Kefas Satrio Bangkit Solideantyo's avatar Kefas Satrio Bangkit Solideantyo
Browse files

[RED] add tests for user roles

parent 4b8c921b
Pipeline #72160 failed with stage
in 4 minutes and 43 seconds
......@@ -6,6 +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 .google_utils import validate_google_token
class UserModelTests(APITestCase):
......@@ -60,6 +61,10 @@ class UserModelTests(APITestCase):
cls.custom_user_2 = CustomUser.objects.create_user(name='tes', email='email2@gmail.com', password='abc')
cls.inactive_user = CustomUser.objects.create_user(name='inactive', email='inactive@gmail.com', password='abc')
cls.inactive_user.is_active = False
cls.inactive_user.save()
cls.num_of_diet_profile = DietProfile.objects.count()
cls.num_of_custom_user = CustomUser.objects.count()
......@@ -68,6 +73,7 @@ class UserModelTests(APITestCase):
password='tes', diet_profile=self.diet_profile_1,)
self.assertEqual(str(user), 'email2@email.com')
self.assertEqual(user.email, 'email2@email.com')
self.assertTrue(user.groups.filter(name='client').exists())
self.assertTrue(user.is_active)
self.assertFalse(user.is_staff)
self.assertFalse(user.is_superuser)
......@@ -188,14 +194,16 @@ class UserModelTests(APITestCase):
data = {
'email': 'email@email.com',
'password': 'abc',
'role': 'client',
}
response = self.client.post('/auth/login/', data, format='json')
response = self.client.post('/auth/user-login/', data, format='json')
json_response = json.loads(response.content)
self.assertIn('access_token', json_response)
self.assertIn('refresh_token', json_response)
self.assertIn('user', json_response)
self.assertTrue('id' in json_response.get('user'))
self.assertIn('id', json_response.get('user'))
self.assertIn('role', json_response.get('user'))
self.assertEquals(json_response.get('user').get('name'), 'tes')
self.assertEqual(response.status_code, status.HTTP_200_OK)
......@@ -203,18 +211,30 @@ class UserModelTests(APITestCase):
data = {
'email': 'email123123123@email.com',
'password': 'abc',
'role': 'client'
}
response = self.client.post('/auth/login/', data, format='json')
response = self.client.post('/auth/user-login/', data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_post_login_user_failed_because_invalid_role(self):
data = {
'email': 'email@email.com',
'password': 'abc',
'role': 'artist'
}
response = self.client.post('/auth/user-login/', data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_post_login_user_failed_because_invalid_email_format(self):
data = {
'email': 'email123123123@email',
'password': 'abc',
'role': 'client'
}
response = self.client.post('/auth/login/', data, format='json')
response = self.client.post('/auth/user-login/', data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
......@@ -222,11 +242,37 @@ class UserModelTests(APITestCase):
data = {
'email': 'email@email.com',
'password': 'abc1234',
'role': 'client'
}
response = self.client.post('/auth/user-login/', data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_post_login_user_failed_because_inactive_account(self):
data = {
'email': 'inactive@gmail.com',
'password': 'abc',
'role': 'client'
}
response = self.client.post('/auth/user-login/', data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
response = self.client.post('/auth/login/', data, format='json')
def test_post_login_user_failed_because_no_role_in_payload(self):
data = {
'email': 'email@email.com',
'password': 'abc',
}
response = self.client.post('/auth/user-login/', data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_create_login_serializer_to_do_nothing(self):
serializer = CustomLoginSerializer()
self.assertEqual(serializer.create({}), None)
def test_update_login_serializer_to_do_nothing(self):
serializer = CustomLoginSerializer()
self.assertEqual(serializer.update(None, {}), None)
@patch('authentication.google_utils.requests.get')
@patch('authentication.google_utils.json.loads')
class TestValidateGoogleToken(TestCase):
......
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