diff --git a/apps/tasks/tests.py b/apps/tasks/tests.py index a780723..9529f7a 100644 --- a/apps/tasks/tests.py +++ b/apps/tasks/tests.py @@ -7,14 +7,13 @@ InMemoryUploadedFile, SimpleUploadedFile, ) -from django.http import HttpRequest from django.test import TestCase from django.test.client import RequestFactory from django.urls import resolve, reverse from django.utils import timezone from apps.contests.models import Contest -from apps.submissions.forms import SubmissionForm +from apps.submissions.forms import SubmissionForm, UploadFileForm from apps.submissions.models import Submission, SubmissionStatus from apps.tasks.admin import TaskAdmin, TaskModelForm from apps.tasks.models import Task @@ -524,8 +523,8 @@ def test_handle_uploaded_file(self) -> None: file_content = b"Test file content" uploaded_file = SimpleUploadedFile("test_file.txt", file_content) - request = HttpRequest() - request.FILES["file"] = uploaded_file + request = self.factory.post(self.url, data={"file": uploaded_file}) + request.user = self.user submission = Submission._default_manager.create( author=self.user, @@ -540,10 +539,43 @@ def test_handle_uploaded_file(self) -> None: self.assertTrue(os.path.exists(destination_path)) submission.refresh_from_db() - self.assertEqual(submission.status, "WJ") + self.assertEqual(submission.status, SubmissionStatus.WAITING_JUDGE) os.remove(destination_path) + def test_handle_uploaded_file_with_invalid_form(self) -> None: + file_content = b"Test file content" + uploaded_file = SimpleUploadedFile("test_file.txt", file_content) + + invalid_request_data = {"file": uploaded_file} + invalid_request = self.factory.post( + self.url, data=invalid_request_data + ) + invalid_request.user = self.user + + destination_path = os.path.join("apps/tasks/uploads/", "test_file.txt") + + submission = Submission._default_manager.create( + author=self.user, + task=self.task, + code="print('Hello, World!')", + status=SubmissionStatus.WAITING_JUDGE, + ) + + upload_form = UploadFileForm( + invalid_request.POST, invalid_request.FILES + ) + self.assertFalse(upload_form.is_valid()) + handle_uploaded_file(invalid_request, self.task.id, submission.id) + + print(f"File exists: {os.path.exists(destination_path)}") + print(f"File path: {destination_path}") + + self.assertFalse(os.path.exists(destination_path)) + + submission.refresh_from_db() + self.assertEqual(submission.status, SubmissionStatus.WAITING_JUDGE) + class BackgroundJobTaskTest(TestCase): def setUp(self) -> None: diff --git a/apps/tasks/views.py b/apps/tasks/views.py index 4471597..0a12673 100644 --- a/apps/tasks/views.py +++ b/apps/tasks/views.py @@ -77,14 +77,16 @@ def handle_uploaded_file( uploaded_file = request.FILES.get("file") if uploaded_file: - with open( - os.path.join(destination_dir, uploaded_file.name or ""), "wb+" - ) as destination: - for chunk in uploaded_file.chunks(): - destination.write(chunk) - - submission.status = SubmissionStatus.WAITING_JUDGE - submission.save() + upload_form = UploadFileForm(request.POST, request.FILES) + if upload_form.is_valid(): + with open( + os.path.join(destination_dir, uploaded_file.name or ""), "wb+" + ) as destination: + for chunk in uploaded_file.chunks(): + destination.write(chunk) + + submission.status = SubmissionStatus.WAITING_JUDGE + submission.save() class DetailView(FormMixinBase, DetailViewBase):