diff --git a/backend/tests/test_TC005.py b/backend/tests/test_TC005.py index 84c4344..444f02f 100644 --- a/backend/tests/test_TC005.py +++ b/backend/tests/test_TC005.py @@ -1,15 +1,23 @@ -from rest_framework.test import APITestCase, APIClient # Import APIClient +''' +# This is for coverage testing independent of manage.py +import os +import django +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'secfit.settings') +django.setup() +''' + +from django.test import TestCase from django.contrib.auth import get_user_model from workouts.models import Workout from django.utils.timezone import now +from rest_framework_simplejwt.tokens import AccessToken User = get_user_model() -class TestCoachViewAthleteWorkouts(APITestCase): # Use APITestCase instead of TestCase +class TestCoachViewAthleteWorkouts(TestCase): def setUp(self): - # Initialize the API client - self.client = APIClient() - # Create coach and athlete self.coach = User.objects.create_user(username="test_coach", password="password123", isCoach=True) self.athlete = User.objects.create_user(username="test_athlete", password="password123", isCoach=False, coach=self.coach) @@ -25,8 +33,8 @@ def setUp(self): self.other_workout1 = Workout.objects.create(name="Other Workout 1", owner=self.other_athlete, date=now()) self.other_workout2 = Workout.objects.create(name="Other Workout 2", owner=self.other_athlete, date=now()) - # Force authentication for the coach - self.client.force_authenticate(user=self.coach) + # Generate a JWT token for the coach + self.token = str(AccessToken.for_user(self.coach)) def test_coach_can_view_assigned_athlete_workouts(self): ''' @@ -43,10 +51,10 @@ def test_coach_can_view_assigned_athlete_workouts(self): 5. Verify the response status code is 200. 6. Check that the response contains only the workouts assigned to the coach's athlete. ''' - # 4. Access the API endpoint to view workouts - response = self.client.get("/api/workouts/") + # 4 + response = self.client.get("/api/workouts/", HTTP_AUTHORIZATION=f"Bearer {self.token}") - # 5. Verify response status code + # 5 self.assertEqual(response.status_code, 200, "The response status code is not 200") # Check that the response contains the correct workout details @@ -63,5 +71,5 @@ def test_coach_can_view_assigned_athlete_workouts(self): # Workouts are filtered based on the coach's ID? for workout in response_data: - owner_id = int(workout["owner"].split("/")[-2]) - self.assertEqual(owner_id, self.athlete.id, "The workout is not associated with the correct athlete") \ No newline at end of file + owner_id = int(workout["owner"].split("/")[-2]) + self.assertEqual(owner_id, self.athlete.id, "The workout is not associated with the correct athlete")