Skip to content

Commit

Permalink
Update test_TC005.py
Browse files Browse the repository at this point in the history
changed authentication process
  • Loading branch information
Gargi Ketan Chauhan authored and GitHub Enterprise committed Apr 3, 2025
1 parent c82a377 commit 374aae1
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions backend/tests/test_TC005.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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):
'''
Expand All @@ -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
Expand All @@ -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")
owner_id = int(workout["owner"].split("/")[-2])
self.assertEqual(owner_id, self.athlete.id, "The workout is not associated with the correct athlete")

0 comments on commit 374aae1

Please sign in to comment.