Fakultas Ilmu Komputer UI

Commit 0068bedd authored by Glenda Emanuella Sutanto's avatar Glenda Emanuella Sutanto
Browse files

Merge branch 'link-data-if-token-provided' into 'staging'

Link data if token provided

See merge request !82
parents 77e1346f d55b8ca6
Pipeline #83006 passed with stages
in 23 minutes and 30 seconds
......@@ -15,6 +15,8 @@ from constants.quiz_constants import (
SnacksDietRecommendation,
)
from .models import DietProfile, QuizResult
from authentication.models import CustomUser
from nutritionists.models import Nutritionist
from .formulas import (
get_body_mass_index,
get_nutrition_status,
......@@ -68,6 +70,24 @@ class DietelaQuizTests(APITestCase):
problem_to_solve=1,
health_problem=[2, 3])
self.nutritionist = Nutritionist.objects.create(
id=1,
full_name_and_degree="Test, S.Gz",
registration_certificate_no="1234567890",
university="Universitas Indonesia",
mastered_nutritional_problems="Manajemen berat badan, hipertensi",
handled_age_group="12-17 tahun (Remaja)",
another_practice_place="RSCM",
languages="Bahasa Indonesia, Bahasa Inggris",
)
self.custom_user = CustomUser.objects.create_user(
name='tes',
email='email@email.com',
password='abc',
nutritionist=self.nutritionist,
)
def test_diet_profile_saved(self):
self.assertEqual(DietProfile.objects.count(), 1)
......@@ -109,6 +129,48 @@ class DietelaQuizTests(APITestCase):
self.assertEqual(DietProfile.objects.count(), 2)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_post_diet_profile_api_with_user(self):
login_data = {
'email': 'email@email.com',
'password': 'abc',
'role': 'client'
}
login_response = self.client.post('/auth/user-login/', login_data, format='json')
json_login_response = json.loads(login_response.content)
login_credentials = "Bearer " + json_login_response['access_token']
self.client.credentials(HTTP_AUTHORIZATION=login_credentials)
data = {
'name': 'test2',
'email': 'test2@test.com',
'age': 20,
'weight': 60,
'height': 172,
'gender': 2,
'special_condition': 1,
'body_activity': 1,
'vegetables_in_one_day': 1,
'fruits_in_one_day': 1,
'fried_food_in_one_day': 1,
'sweet_snacks_in_one_day': 1,
'sweet_drinks_in_one_day': 1,
'packaged_food_in_one_day': 1,
'large_meal_in_one_day': 1,
'snacks_in_one_day': 1,
'breakfast_type': 1,
'current_condition': 1,
'problem_to_solve': 1,
'health_problem': [2, 3]
}
response = self.client.post(
'/dietela-quiz/diet-profile/', data, format='json')
self.custom_user.diet_profile = DietProfile.objects.last()
self.assertEqual(json.loads(response.content).get('name'), 'test2')
self.assertEqual(json.loads(response.content).get('quiz_result').get('age'), 20)
self.assertEqual(DietProfile.objects.count(), 2)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(self.custom_user.diet_profile.id, DietProfile.objects.last().id)
def test_auto_quiz_results_saved(self):
data = {
'name': 'test3',
......
......@@ -12,9 +12,15 @@ class DietProfileViewSet(viewsets.ModelViewSet):
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
instance = self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
if not request.user.is_anonymous:
user = request.user
user.diet_profile = instance
user.save()
quiz_result = QuizResult.objects.get(diet_profile=instance)
response = serializer.data
response['quiz_result'] = QuizResultSerializer(quiz_result).data
......
......@@ -117,6 +117,30 @@ class CartTests(APITestCase):
self.assertEqual(Cart.objects.count(), 4)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_post_cart_api_with_user(self):
login_data = {
'email': 'email@email.com',
'password': 'abc',
'role': 'client'
}
login_response = self.client.post('/auth/user-login/', login_data, format='json')
json_login_response = json.loads(login_response.content)
login_credentials = "Bearer " + json_login_response['access_token']
self.client.credentials(HTTP_AUTHORIZATION=login_credentials)
url = f"{self.BASE_URL}"
data = {
'program': 'PRG1',
'nutritionist': 1,
}
response = self.client.post(
url, data, format='json')
self.assertEqual(json.loads(response.content).get('program').get('unique_code'), 'PRG1')
self.assertEqual(json.loads(response.content).get('transaction_status'), 'unpaid')
self.assertEqual(json.loads(response.content).get('user'), self.custom_user.id)
self.assertEqual(Cart.objects.count(), 4)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_set_transaction_status_succeed_with_status_code_200(self):
url = self.SET_TRANSACTION_STATUS_URL
data = {
......
......@@ -26,6 +26,10 @@ class CartViewSet(viewsets.ModelViewSet):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
if not request.user.is_anonymous:
serializer.save(user=request.user)
headers = self.get_success_headers(serializer.data)
program = DietelaProgram.objects.get(unique_code=program_unique_id)
......
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