Skip to content

Commit

Permalink
Update test_TC004.py
Browse files Browse the repository at this point in the history
changed from forced authentication to JWT token
  • Loading branch information
Gargi Ketan Chauhan authored and GitHub Enterprise committed Apr 3, 2025
1 parent f6c261a commit c82a377
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions backend/tests/test_TC004.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -51,18 +48,21 @@ 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/",
{
"file": special_char_file,
"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()
Expand All @@ -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)
self.assertEqual(saved_file.athlete, self.athlete)

0 comments on commit c82a377

Please sign in to comment.