Skip to content

Commit

Permalink
TC_005
Browse files Browse the repository at this point in the history
  • Loading branch information
anabar3 committed Mar 31, 2025
1 parent 3bc7a37 commit ecdba2d
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 19 deletions.
1 change: 1 addition & 0 deletions backend/media/users/2/file_a_0NHVzHH.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions backend/media/users/2/file_a_1wrH3vY.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions backend/media/users/2/file_a_67CtuBp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions backend/media/users/2/file_a_94cIg3T.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions backend/media/users/2/file_a_CHepTi2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions backend/media/users/2/file_a_FGN1pdv.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions backend/media/users/2/file_a_GyNvAlP.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions backend/media/users/2/file_a_TbgaYSG.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions backend/media/users/2/file_a_UASK90p.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions backend/media/users/2/file_a_tgxosyw.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions backend/media/users/2/file_a_vskAVv6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions backend/media/users/2/file_a_wasCFXR.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 4 additions & 19 deletions backend/tests/test_004.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,16 @@ class TestSpecialCharacterFileName(TestCase):
def setUp(self):
# Create a test user (coach)
User = get_user_model()
self.coach = User.objects.create_user(
username="test_coach",
password="password123",
isCoach=True
)
self.coach = User.objects.create_user( username="test_coach", password="password123", isCoach=True)

# Create an athlete and assign the coach
self.athlete = User.objects.create_user(
username="test_athlete",
password="password123",
isCoach=False, # Ensure the user is an athlete
coach=self.coach # Assign the coach
)
self.athlete = User.objects.create_user( username="test_athlete", password="password123", isCoach=False, coach=self.coach)

# Generate a JWT token for the coach
self.token = str(AccessToken.for_user(self.coach))

# Create a workout for the athlete
self.workout = Workout.objects.create(
owner=self.athlete,
name="Test Workout",
date=now()
)
self.workout = Workout.objects.create(owner=self.athlete, name="Test Workout", date=now())

def test_upload_file_with_special_characters_in_name(self):
"""
Expand All @@ -59,9 +46,7 @@ def test_upload_file_with_special_characters_in_name(self):
5. Verify the file is associated with the correct owner and athlete.
"""
# Create a file with special characters in its name
special_char_file = SimpleUploadedFile(
"file@#$ %&()a.png", b"dummy content", content_type="image/png"
)
special_char_file = SimpleUploadedFile("file@#$ %&()a.png", b"dummy content", content_type="image/png")

#Upload file
response = self.client.post(
Expand Down
75 changes: 75 additions & 0 deletions backend/tests/test_005.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'''
# 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(TestCase):
def setUp(self):
# 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)

# Create workouts for the athlete
self.workout1 = Workout.objects.create(name="Workout 1", owner=self.athlete, date=now())
self.workout2 = Workout.objects.create(name="Workout 2", owner=self.athlete, date=now())

# Create another athlete not assigned to the coach
self.other_athlete = User.objects.create_user(username="other_athlete", password="password123", isCoach=False)

# Create workouts for the other athlete
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())

# Generate a JWT token for the coach
self.token = str(AccessToken.for_user(self.coach))

def test_coach_can_view_assigned_athlete_workouts(self):
'''
Test that the coach can view workouts assigned to their athlete.
Expected behavior:
1. The coach should be able to view all workouts assigned to their athlete.
2. The workouts should be filtered based on the coach's ID.
3. The response should include the correct workout details.
Steps:
1. Create a coach and an athlete.
2. Assign workouts to the athlete.
3. Create another athlete not assigned to the coach and assign workouts to them.
4. Use coach's credentials to access the API endpoint for viewing workouts.
5. Verify the response status code is 200.
6. Check that the response contains only the workouts assigned to the coach's athlete.
'''
# 4
response = self.client.get("/api/workouts/", HTTP_AUTHORIZATION=f"Bearer {self.token}")

# 5
self.assertEqual(response.status_code, 200, "The response status code is not 200")

# Check that the response contains the correct workout details
response_data = response.json()
workout_names = [workout["name"] for workout in response_data]

# Coach can see their athlete's workouts?
self.assertIn("Workout 1", workout_names, "Workout 1 is not in the response")
self.assertIn("Workout 2", workout_names, "Workout 2 is not in the response")

# Coach cannot see workouts of other athletes?
self.assertNotIn("Other Workout 1", workout_names, "Other Workout 1 should not be in the response")
self.assertNotIn("Other Workout 2", workout_names, "Other Workout 2 should not be in the response")

# 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")

0 comments on commit ecdba2d

Please sign in to comment.