From c82a377b53cb49f469ef434613969b6f17c3e221 Mon Sep 17 00:00:00 2001 From: Gargi Ketan Chauhan Date: Thu, 3 Apr 2025 23:08:24 +0200 Subject: [PATCH] Update test_TC004.py changed from forced authentication to JWT token --- backend/tests/test_TC004.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/backend/tests/test_TC004.py b/backend/tests/test_TC004.py index e3f30e2..94843dd 100644 --- a/backend/tests/test_TC004.py +++ b/backend/tests/test_TC004.py @@ -1,38 +1,35 @@ -# This is for coverage testing independent of manage.py ''' +#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') +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'secfit.settings') django.setup() ''' - - from django.test import TestCase from django.core.files.uploadedfile import SimpleUploadedFile from users.models import AthleteFile from workouts.models import Workout from django.contrib.auth import get_user_model +from rest_framework_simplejwt.tokens import AccessToken # Import for JWT token generation from django.utils.timezone import now 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, coach=self.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()) - # Force authentication for the coach - from rest_framework.test import APIClient - self.client = APIClient() # Use APIClient for forced authentication - self.client.force_authenticate(user=self.coach) - def test_upload_file_with_special_characters_in_name(self): """ Test uploading a file with special characters in its name. @@ -51,7 +48,7 @@ def test_upload_file_with_special_characters_in_name(self): # Create a file with special characters in its name special_char_file = SimpleUploadedFile("file@#$ %&()a.png", b"dummy content", content_type="image/png") - # Upload file + #Upload file response = self.client.post( "/api/athlete-files/", { @@ -59,10 +56,13 @@ def test_upload_file_with_special_characters_in_name(self): "workout": self.workout.id, "athlete": f"/api/users/{self.athlete.id}/", # Include the athlete field }, + HTTP_AUTHORIZATION=f"Bearer {self.token}", # Include the JWT token in the headers format="multipart" ) - self.assertEqual(response.status_code, 201) # Successful creation + + self.assertEqual(response.status_code, 201) #Successful creation + # File was saved with a sanitized name and unique identifier? saved_file = AthleteFile.objects.first() @@ -79,4 +79,4 @@ def test_upload_file_with_special_characters_in_name(self): # File is associated with the correct owner and athlete? self.assertEqual(saved_file.owner, self.coach) - self.assertEqual(saved_file.athlete, self.athlete) \ No newline at end of file + self.assertEqual(saved_file.athlete, self.athlete)