Skip to content

Commit

Permalink
feat: all the unit tests (all 403 fails)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jolan committed Mar 27, 2025
1 parent 10a683e commit dbc58de
Showing 1 changed file with 39 additions and 42 deletions.
81 changes: 39 additions & 42 deletions backend/tests/test_workout_file_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,51 @@
class TestWorkoutFileUpload(TestCase):
def setUp(self):
self.client = APIClient()
self.user = get_user_model().objects.create_user(username=os.getenv("DJANGO_SUPERUSER_USERNAME"), password=os.getenv("DJANGO_SUPERUSER_PASSWORD"))
self.user = get_user_model().objects.create_user(
username="admin",
password="Password"
)
self.client.force_authenticate(self.user)

self.url = reverse("workout-file-list")

def test_upload_multiple_valid_pdf_files(self):
print(self.client.get(self.url).status_code)
# Create mock PDF files under 2MB
files = {}
for i in range(8): # 8 files
# Creating a mock PDF file of size < 2MB (e.g., 1MB each)
file_content = b'%PDF-1.4 example PDF content' * 1024 # Creating a simple PDF-like content
files[f'file{i}'] = SimpleUploadedFile(
f'file{i}.pdf', file_content, content_type='application/pdf'
)

# Make the POST request with files
print(self.url)
files = {f'file{i}': SimpleUploadedFile(
f'file{i}.pdf', b'%PDF-1.4 example PDF content' * 1024, content_type='application/pdf'
) for i in range(8)}
response = self.client.post(self.url, files, format='multipart')

# Check if the response is successful
self.assertEqual(response.status_code, 201) # 201 means created

# Optionally, check that the files have been created in the database
workout_files = WorkoutFile.objects.all()
self.assertEqual(workout_files.count(), 8)

def test_uploading_file_of_maximum_size(self):
#Logic for testing uploading of files with exceeding size limit
pass

def test_uploading_maximum_number_of_files(self):
#Logic for testing uploading of maximum number of files
pass


def test_uploading_file_with_size_just_above_limit(self):
#Logic for testing uploading of files with size just above limit
pass

def test_uploading_too_many_files(self):
#Logic for testing uploading of too many files
pass

def test_uploading_invalid_file_type(self):
#Logic for testing uploading of invalid file types
pass




self.assertEqual(workout_files.count(), 8)

def test_upload_file_of_maximum_size(self):
file_content = b'X' * (2 * 1024 * 1024) # 2MB file
file = SimpleUploadedFile("max_size.pdf", file_content, content_type='application/pdf')
response = self.client.post(self.url, {"file": file}, format='multipart')
self.assertEqual(response.status_code, 201)

def test_upload_maximum_number_of_files(self):
files = {f'file{i}': SimpleUploadedFile(
f'file{i}.pdf', b'%PDF-1.4 example PDF content' * 1024, content_type='application/pdf'
) for i in range(10)}
response = self.client.post(self.url, files, format='multipart')
self.assertEqual(response.status_code, 201)

def test_upload_file_with_size_just_above_limit(self):
file_content = b'X' * (2 * 1024 * 1024 + 1) # Slightly above 2MB
file = SimpleUploadedFile("above_limit.pdf", file_content, content_type='application/pdf')
response = self.client.post(self.url, {"file": file}, format='multipart')
self.assertEqual(response.status_code, 400)

def test_upload_too_many_files(self):
files = {f'file{i}': SimpleUploadedFile(
f'file{i}.pdf', b'%PDF-1.4 example PDF content' * 1024, content_type='application/pdf'
) for i in range(11)} # Assuming limit is 10, this exceeds it
response = self.client.post(self.url, files, format='multipart')
self.assertEqual(response.status_code, 400)

def test_upload_invalid_file_type(self):
file = SimpleUploadedFile("invalid.txt", b'Invalid content', content_type='text/plain')
response = self.client.post(self.url, {"file": file}, format='multipart')
self.assertEqual(response.status_code, 400)

0 comments on commit dbc58de

Please sign in to comment.