Fakultas Ilmu Komputer UI

Commit 249d5cf2 authored by Glenda Emanuella Sutanto's avatar Glenda Emanuella Sutanto Committed by Muzaki Azami
Browse files

User registration

parent 2730b621
from rest_framework import serializers
from .models import CustomUser
class CustomUserDetailsSerializer(serializers.ModelSerializer):
class Meta:
model = CustomUser
fields = ('email',)
read_only_fields = ('email',)
import json
from rest_framework.test import APITestCase
from rest_framework import status
from dietela_quiz.models import DietProfile
from .models import CustomUser
......@@ -28,6 +30,35 @@ class UserModelTests(APITestCase):
problem_to_solve=2,
health_problem=[1])
cls.diet_profile_2 = DietProfile.objects.create(
name="test",
email="email2@gmail.com",
age=21,
weight=70,
height=173,
gender=1,
special_condition=2,
body_activity=2,
vegetables_in_one_day=2,
fruits_in_one_day=2,
fried_food_in_one_day=2,
sweet_snacks_in_one_day=2,
sweet_drinks_in_one_day=2,
packaged_food_in_one_day=2,
large_meal_in_one_day=2,
snacks_in_one_day=2,
breakfast_type=2,
current_condition=2,
problem_to_solve=2,
health_problem=[1])
cls.custom_user_1 = CustomUser.objects.create_user(email='email@email.com', password='abc')
cls.custom_user_2 = CustomUser.objects.create_user(email='email2@gmail.com', password='abc')
cls.num_of_diet_profile = DietProfile.objects.count()
cls.num_of_custom_user = CustomUser.objects.count()
def test_create_user(self):
user = CustomUser.objects.create_user(email='email2@email.com', password='tes',
diet_profile=self.diet_profile_1,)
......@@ -67,3 +98,81 @@ class UserModelTests(APITestCase):
with self.assertRaises(ValueError):
CustomUser.objects.create_superuser(
email='super@user.com', password='foo', is_staff=False)
def test_post_registration_user_succeed(self):
data = {
'email': 'abc123@gmail.com',
'password1': '2828abab',
'password2': '2828abab',
}
response = self.client.post('/auth/registration/', data, format='json')
self.assertEqual(json.loads(response.content).get('user').get('email'), 'abc123@gmail.com')
self.assertEqual(CustomUser.objects.count(), self.num_of_custom_user + 1)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_post_registration_user_failed_because_invalid_email(self):
data = {
'email': 'abc123gmail.com',
'password1': '2828abab',
'password2': '2828abab',
}
response = self.client.post('/auth/registration/', data, format='json')
self.assertEqual(CustomUser.objects.count(), self.num_of_custom_user)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_post_registration_user_failed_because_different_password(self):
data = {
'email': 'abc123@gmail.com',
'password1': '2828abaab',
'password2': '2828abab',
}
response = self.client.post('/auth/registration/', data, format='json')
self.assertEqual(CustomUser.objects.count(), self.num_of_custom_user)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_post_registration_user_failed_because_email_already_registered(self):
data = {
'email': self.custom_user_1.email,
'password1': '2828abaab',
'password2': '2828abaab',
}
response = self.client.post('/auth/registration/', data, format='json')
self.assertEqual(CustomUser.objects.count(), self.num_of_custom_user)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_link_user_and_diet_profile_succeed(self):
data = {
'email': self.custom_user_2.email,
'diet_profile_id': self.diet_profile_2.id,
}
response = self.client.post('/auth/link-user-and-diet-profile/', 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(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,
}
response = self.client.post('/auth/link-user-and-diet-profile/', 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)
def test_link_user_and_diet_profile_failed_because_diet_profile_doesnt_exist(self):
data = {
'email': self.custom_user_2.email,
'diet_profile_id': -1,
}
response = self.client.post('/auth/link-user-and-diet-profile/', 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)
from rest_framework import routers
from django.urls import path, include
from .views import LinkUserAndDietProfileViewSet
router = routers.SimpleRouter()
router.register('auth/link-user-and-diet-profile', LinkUserAndDietProfileViewSet, \
basename="LinkUserAndDietProfile")
urlpatterns = [
path('auth/registration/', include('dj_rest_auth.registration.urls')),
]
urlpatterns += router.urls
from rest_framework import viewsets, status
from rest_framework.response import Response
from dietela_quiz.models import DietProfile
from dietela_quiz.serializers import DietProfileSerializer
from .models import CustomUser
from .serializers import CustomUserDetailsSerializer
class LinkUserAndDietProfileViewSet(viewsets.ViewSet):
@staticmethod
def create(request):
email = request.data['email']
diet_profile_id = request.data['diet_profile_id']
try:
user = CustomUser.objects.get(email=email)
except CustomUser.DoesNotExist:
return Response({'message': 'User is not found.'}, status=status.HTTP_400_BAD_REQUEST)
try:
diet_profile = DietProfile.objects.get(id=diet_profile_id)
except DietProfile.DoesNotExist:
return Response({'message': 'Diet profile is not found.'}, \
status=status.HTTP_400_BAD_REQUEST)
user.diet_profile = diet_profile
user.save()
return Response({\
'user': CustomUserDetailsSerializer(user).data,\
'diet_profile': DietProfileSerializer(diet_profile).data\
}, status=status.HTTP_200_OK)
......@@ -47,7 +47,6 @@ INSTALLED_APPS = [
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'dj_rest_auth',
'corsheaders',
'multiselectfield',
......@@ -56,9 +55,34 @@ INSTALLED_APPS = [
'nutritionists',
'dietela_quiz',
'dietela_program',
'authentication'
'authentication',
# auth
'rest_framework.authtoken',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'dj_rest_auth.registration',
'allauth.socialaccount.providers.google',
]
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
]
}
SITE_ID = 1
REST_USE_JWT = True
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_USERNAME_VALIDATORS = None
ACCOUNT_EMAIL_VERIFICATION = 'none'
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
......@@ -133,7 +157,7 @@ AUTH_PASSWORD_VALIDATORS = [
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Jakarta'
USE_I18N = True
......@@ -162,3 +186,7 @@ CORS_ORIGIN_WHITELIST = [
]
AUTH_USER_MODEL = 'authentication.CustomUser'
REST_AUTH_SERIALIZERS = {
'USER_DETAILS_SERIALIZER':'authentication.serializers.CustomUserDetailsSerializer'
}
......@@ -6,4 +6,5 @@ urlpatterns = [
path('', include('nutritionists.urls')),
path('', include('dietela_quiz.urls')),
path('', include('dietela_program.urls')),
path('', include('authentication.urls')),
]
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