Fakultas Ilmu Komputer UI

Commit f0842edf authored by Glenda Emanuella Sutanto's avatar Glenda Emanuella Sutanto
Browse files

Filtered get all diet recommendation request by token

parent 7e768657
......@@ -203,6 +203,8 @@ class GetProfilesTest(APITestCase):
class DietRecommendationTests(APITestCase):
client = APIClient()
@classmethod
def setUpTestData(cls):
cls.BASE_URL = "/diet-recommendation/"
......@@ -239,6 +241,16 @@ class DietRecommendationTests(APITestCase):
cls.SPECIFIC_URL = f"{cls.BASE_URL}{cls.diet_recommendation.id}/"
cls.login_data = {
'email': 'email2@email.com',
'password': 'abc',
'role': 'client',
}
cls.login_response = cls.client.post('/auth/user-login/', cls.login_data, format='json')
cls.json_login_response = json.loads(cls.login_response.content)
cls.login_credentials = "Bearer " + cls.json_login_response['access_token']
def test_string_representation(self):
self.assertEqual(str(self.diet_recommendation), 'email2@email.com - Test, S.Gz')
......@@ -275,6 +287,7 @@ class DietRecommendationTests(APITestCase):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_get_all_diet_recommendation(self):
self.client.credentials(HTTP_AUTHORIZATION=self.login_credentials)
response = self.client.get(self.BASE_URL)
json_response = json.loads(response.content)
self.assertEqual(len(json_response), 1)
......
......@@ -46,3 +46,9 @@ class GetProfilesViewSet(generics.ListAPIView):
class DietRecommendationViewSet(viewsets.ModelViewSet):
serializer_class = DietRecommendationSerializer
queryset = DietRecommendation.objects.all()
def list(self, request):
instances = self.get_queryset().filter(client=request.user)
serializer = DietRecommendationSerializer(instances, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
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