From 2c46b97cf2027f5e48f0b9f23360e724a75d9447 Mon Sep 17 00:00:00 2001 From: EwoutV Date: Tue, 21 May 2024 23:14:56 +0200 Subject: [PATCH] fix: backend tests --- backend/api/logic/parse_zip_files.py | 6 ++- backend/api/serializers/checks_serializer.py | 19 ++++---- backend/api/tests/test_project.py | 51 +++++++++++++++----- backend/api/views/project_view.py | 6 ++- 4 files changed, 56 insertions(+), 26 deletions(-) diff --git a/backend/api/logic/parse_zip_files.py b/backend/api/logic/parse_zip_files.py index 5d84b582..cc21f531 100644 --- a/backend/api/logic/parse_zip_files.py +++ b/backend/api/logic/parse_zip_files.py @@ -12,12 +12,13 @@ def parse_zip(project: Project, zip_file: InMemoryUploadedFile) -> bool: zip_file.seek(0) - with zipfile.ZipFile(zip_file, 'r') as file: - files = file.namelist() + with zipfile.ZipFile(zip_file, 'r') as zip_file: + files = zip_file.namelist() directories = [file for file in files if file.endswith('/')] # Check if all directories start the same common_prefix = os.path.commonprefix(directories) + if '/' in common_prefix: prefixes = common_prefix.split('/') if common_prefix[-1] != '/': @@ -31,6 +32,7 @@ def parse_zip(project: Project, zip_file: InMemoryUploadedFile) -> bool: # Add potential top level files top_level_files = [file for file in files if '/' not in file] + if top_level_files: create_check(project, '', files) diff --git a/backend/api/serializers/checks_serializer.py b/backend/api/serializers/checks_serializer.py index 289265df..54454815 100644 --- a/backend/api/serializers/checks_serializer.py +++ b/backend/api/serializers/checks_serializer.py @@ -18,20 +18,17 @@ class Meta: class StructureCheckSerializer(serializers.ModelSerializer): - project = serializers.HyperlinkedIdentityField( + project = serializers.HyperlinkedRelatedField( + read_only=True, view_name="project-detail" ) obligated_extensions = FileExtensionSerializer( - many=True, - required=False, - default=[] + many=True ) blocked_extensions = FileExtensionSerializer( - many=True, - required=False, - default=[] + many=True ) def validate(self, attrs): @@ -47,7 +44,7 @@ def validate(self, attrs): obligated = set([ext["extension"] for ext in attrs["obligated_extensions"]]) if blocked.intersection(obligated): - raise ValidationError(_("project.error.structure_checks.blocked_obligated")) + raise ValidationError(_("project.error.structure_checks.extension_blocked_and_obligated")) return attrs @@ -82,11 +79,13 @@ class Meta: class ExtraCheckSerializer(serializers.ModelSerializer): - project = serializers.HyperlinkedIdentityField( + project = serializers.HyperlinkedRelatedField( + read_only=True, view_name="project-detail" ) - docker_image = serializers.HyperlinkedIdentityField( + docker_image = serializers.HyperlinkedRelatedField( + read_only=True, view_name="docker-image-detail" ) diff --git a/backend/api/tests/test_project.py b/backend/api/tests/test_project.py index 0684088e..d4d48c3d 100644 --- a/backend/api/tests/test_project.py +++ b/backend/api/tests/test_project.py @@ -5,6 +5,7 @@ from api.models.project import Project from api.models.student import Student from api.models.teacher import Teacher +from api.serializers.checks_serializer import FileExtensionSerializer from api.tests.helpers import (create_admin, create_course, create_file_extension, create_group, create_project, create_structure_check, @@ -417,22 +418,30 @@ def test_project_structure_checks_post(self): course=course, ) + obligated_extensions = FileExtensionSerializer( + [file_extension1, file_extension4], many=True + ) + + blocked_extensions = FileExtensionSerializer( + [file_extension2, file_extension3], many=True + ) + response = self.client.post( reverse("project-structure-checks", args=[str(project.id)]), - { + json.dumps({ "path": ".", - "obligated_extensions": [file_extension1.extension, file_extension4.extension], - "blocked_extensions": [file_extension2.extension, file_extension3.extension]}, + "obligated_extensions": obligated_extensions.data, + "blocked_extensions": blocked_extensions.data}), follow=True, + content_type="application/json" ) project.refresh_from_db() - self.assertEqual(response.status_code, 200) self.assertEqual(response.accepted_media_type, "application/json") # type: ignore # self.assertEqual(json.loads(response.content), {'message': gettext('project.success.structure_check.add')}) - upd: StructureCheck = project.structure_checks.all()[0] + upd: StructureCheck = project.structure_checks.first() retrieved_obligated_extensions = upd.obligated_extensions.all() retrieved_blocked_file_extensions = upd.blocked_extensions.all() @@ -472,7 +481,6 @@ def test_project_structure_checks_post_already_existing(self): days=7, course=course, ) - create_structure_check( path=".", project=project, @@ -480,13 +488,22 @@ def test_project_structure_checks_post_already_existing(self): blocked_extensions=[file_extension2, file_extension3], ) + obligated_extensions = FileExtensionSerializer( + [file_extension1, file_extension4], many=True + ) + + blocked_extensions = FileExtensionSerializer( + [file_extension2, file_extension3], many=True + ) + response = self.client.post( reverse("project-structure-checks", args=[str(project.id)]), - { + json.dumps({ "path": ".", - "obligated_extensions": [file_extension1.extension, file_extension4.extension], - "blocked_extensions": [file_extension2.extension, file_extension3.extension]}, + "obligated_extensions": obligated_extensions.data, + "blocked_extensions": blocked_extensions.data}), follow=True, + content_type="application/json" ) self.assertEqual(response.status_code, 400) @@ -513,14 +530,22 @@ def test_project_structure_checks_post_blocked_and_obligated(self): course=course, ) + obligated_extensions = FileExtensionSerializer( + [file_extension1, file_extension4], many=True + ) + + blocked_extensions = FileExtensionSerializer( + [file_extension1, file_extension2, file_extension3], many=True + ) + response = self.client.post( reverse("project-structure-checks", args=[str(project.id)]), - { + json.dumps({ "path": ".", - "obligated_extensions": [file_extension1.extension, file_extension4.extension], - "blocked_extensions": [file_extension1.extension, file_extension2.extension, - file_extension3.extension]}, + "obligated_extensions": obligated_extensions.data, + "blocked_extensions": blocked_extensions.data}), follow=True, + content_type="application/json" ) self.assertEqual(response.status_code, 400) diff --git a/backend/api/views/project_view.py b/backend/api/views/project_view.py index 42217e30..180dd5d1 100644 --- a/backend/api/views/project_view.py +++ b/backend/api/views/project_view.py @@ -93,7 +93,11 @@ def structure_checks(self, request, **_): # Serialize the check objects serializer = StructureCheckSerializer( - checks, many=True, context={"request": request} + checks, + many=True, + context={ + "request": request + } ) return Response(serializer.data)