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
60e7b88
commit 3bc7a37
Showing
22 changed files
with
137 additions
and
0 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.
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
| 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) |
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,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)]), | ||
| ), | ||
| ] |