From fd27d6d4dd50eda3d266ef4a2fceeb418d9e5967 Mon Sep 17 00:00:00 2001 From: HladczukLe Date: Tue, 12 Dec 2023 18:21:34 -0300 Subject: [PATCH] test(apps/tasks): fix test --- apps/tasks/tests.py | 35 +---------------------------------- apps/tasks/views.py | 23 ++++++++--------------- 2 files changed, 9 insertions(+), 49 deletions(-) diff --git a/apps/tasks/tests.py b/apps/tasks/tests.py index 9529f7a..4642b87 100644 --- a/apps/tasks/tests.py +++ b/apps/tasks/tests.py @@ -13,7 +13,7 @@ from django.utils import timezone from apps.contests.models import Contest -from apps.submissions.forms import SubmissionForm, UploadFileForm +from apps.submissions.forms import SubmissionForm from apps.submissions.models import Submission, SubmissionStatus from apps.tasks.admin import TaskAdmin, TaskModelForm from apps.tasks.models import Task @@ -543,39 +543,6 @@ def test_handle_uploaded_file(self) -> None: 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 0a12673..06ba28c 100644 --- a/apps/tasks/views.py +++ b/apps/tasks/views.py @@ -9,7 +9,7 @@ from django.views import generic from django.views.generic.edit import FormMixin -from apps.submissions.forms import SubmissionForm, UploadFileForm +from apps.submissions.forms import SubmissionForm from apps.submissions.models import Submission, SubmissionStatus from apps.tasks.models import Task from server import celery @@ -77,16 +77,14 @@ def handle_uploaded_file( uploaded_file = request.FILES.get("file") if uploaded_file: - 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) + 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() + submission.status = SubmissionStatus.WAITING_JUDGE + submission.save() class DetailView(FormMixinBase, DetailViewBase): @@ -143,9 +141,4 @@ def post(self, request: HttpRequest, *, pk: int) -> HttpResponse: submission.id, ) - if uploaded_file: - upload_form = UploadFileForm(request.POST, request.FILES) - if upload_form.is_valid(): - handle_uploaded_file(request, self.object.id, submission.id) - return redirect(self.get_success_url())