forked from mathialm/secfit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
anabar3
committed
Mar 31, 2025
1 parent
3bc7a37
commit ecdba2d
Showing
14 changed files
with
91 additions
and
19 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |