Skip to content

Commit

Permalink
TC_004
Browse files Browse the repository at this point in the history
  • Loading branch information
anabar3 committed Mar 31, 2025
1 parent 60e7b88 commit 3bc7a37
Show file tree
Hide file tree
Showing 22 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/media/users/2/file.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_B9uLPrs.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_D2hQUnz.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_L4gNQ4D.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_PcS7vnX.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_UJ72zDl.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.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_6lQPS4J.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_JW42k7Z.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_NqOOFst.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_e9REZty.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_rKYqbxO.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_txEpC2S.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_uKza9X2.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_vTYonXw.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_ftpj6X6.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_kzK8lpG.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_lkfxfeu.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_mk2hHJZ.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_nxCLEPo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
97 changes: 97 additions & 0 deletions backend/tests/test_004.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'''
#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.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
)

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

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

def test_upload_file_with_special_characters_in_name(self):
"""
Test uploading a file with special characters in its name.
Expected behavior:
1. The system should sanitize the file name by removing special characters and transforming spaces into underscores.
2. A unique identifier should be appended to the sanitized name.
3. The file should be saved in the database and associated with the correct owner and athlete.
Steps:
1. Create a file with special characters in its name.
2. Upload the file using the API.
3. Verify the response status code is 201 (successful creation).
4. Check that the file is saved in the database with the sanitized name.
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"
)

#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


# File was saved with a sanitized name and unique identifier?
saved_file = AthleteFile.objects.first()
self.assertIsNotNone(saved_file, "The file was not saved in the database.")

# Extract the base name of the file (without the directory path)
saved_file_name = saved_file.file.name.split("/")[-1]

# Saved file name starts with "file" and ends with ".png"?
self.assertTrue(
saved_file_name.startswith("file_a") and saved_file_name.endswith(".png"),
f"Expected file name to start with 'file_a' and end with '.png', but got '{saved_file_name}'."
)

# File is associated with the correct owner and athlete?
self.assertEqual(saved_file.owner, self.coach)
self.assertEqual(saved_file.athlete, self.athlete)
20 changes: 20 additions & 0 deletions backend/users/migrations/0004_alter_athletefile_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.0.8 on 2025-03-31 10:13

from django.db import migrations, models
import users.models
import users.validators


class Migration(migrations.Migration):

dependencies = [
('users', '0003_user_specialism'),
]

operations = [
migrations.AlterField(
model_name='athletefile',
name='file',
field=models.FileField(upload_to=users.models.athlete_directory_path, validators=[users.validators.FileValidator(allowed_extensions='', allowed_mimetypes='', max_size=5242880)]),
),
]

0 comments on commit 3bc7a37

Please sign in to comment.