diff --git a/backend/.coverage b/backend/.coverage index 73625672..6a61e8a5 100644 Binary files a/backend/.coverage and b/backend/.coverage differ diff --git a/backend/api/tests/test_file_structure.py b/backend/api/tests/test_file_structure.py index f29f6a17..47ec1c3e 100644 --- a/backend/api/tests/test_file_structure.py +++ b/backend/api/tests/test_file_structure.py @@ -71,7 +71,7 @@ def tearDown(self): # Restore the original MEDIA_ROOT after tests settings.MEDIA_ROOT = self.old_media_root - def test_your_parsing(self): + def test_parsing(self): course = create_course(id=3, name="test course", academic_startyear=2024) project = create_project( name="test", @@ -148,7 +148,7 @@ def test_your_parsing(self): self.assertEqual(len(content["obligated_extensions"]), 2) self.assertEqual(len(content["blocked_extensions"]), 0) - def test_your_checking(self): + def test_checking(self): course = create_course(id=3, name="test course", academic_startyear=2024) project = create_project( name="test", @@ -213,3 +213,266 @@ def test_your_checking(self): blocked=[]) self.assertTrue(check_zip_file(project=project, dir_path="structures/zip_struct1.zip")[0]) + + def test_checking_obligated_not_found(self): + course = create_course(id=3, name="test course", academic_startyear=2024) + project = create_project( + name="test", + description="descr", + visible=True, + archived=False, + days=100, + course=course, + ) + + fileExtensionHS = create_file_extension(extension="hs") + fileExtensionPDF = create_file_extension(extension="pdf") + fileExtensionDOCX = create_file_extension(extension="docx") + fileExtensionLATEX = create_file_extension(extension="latex") + fileExtensionMD = create_file_extension(extension="md") + fileExtensionPY = create_file_extension(extension="py") + fileExtensionHPP = create_file_extension(extension="hpp") + fileExtensionCPP = create_file_extension(extension="cpp") + fileExtensionTS = create_file_extension(extension="ts") + fileExtensionTSX = create_file_extension(extension="tsx") + + create_structure_check( + name=".", + project=project, + obligated=[], + blocked=[fileExtensionDOCX]) + + create_structure_check( + name="folder_struct1", + project=project, + obligated=[fileExtensionHS], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap1", + project=project, + obligated=[fileExtensionPDF, fileExtensionDOCX], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap1/templates", + project=project, + obligated=[fileExtensionLATEX], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap2", + project=project, + obligated=[fileExtensionMD], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap2/src", + project=project, + obligated=[fileExtensionPY, fileExtensionHPP, fileExtensionCPP], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap3", + project=project, + obligated=[fileExtensionTS, fileExtensionTSX], + blocked=[]) + self.assertFalse(check_zip_file(project=project, dir_path="tests/test_zip2struct1.zip")[0]) + + def test_checking_obligated_directory_not_found(self): + course = create_course(id=3, name="test course", academic_startyear=2024) + project = create_project( + name="test", + description="descr", + visible=True, + archived=False, + days=100, + course=course, + ) + + fileExtensionHS = create_file_extension(extension="hs") + fileExtensionPDF = create_file_extension(extension="pdf") + fileExtensionDOCX = create_file_extension(extension="docx") + fileExtensionLATEX = create_file_extension(extension="latex") + fileExtensionMD = create_file_extension(extension="md") + fileExtensionPY = create_file_extension(extension="py") + fileExtensionHPP = create_file_extension(extension="hpp") + fileExtensionCPP = create_file_extension(extension="cpp") + fileExtensionTS = create_file_extension(extension="ts") + fileExtensionTSX = create_file_extension(extension="tsx") + + create_structure_check( + name=".", + project=project, + obligated=[], + blocked=[fileExtensionDOCX]) + + create_structure_check( + name="folder_struct1", + project=project, + obligated=[fileExtensionHS], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap1", + project=project, + obligated=[fileExtensionPDF, fileExtensionDOCX], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap1/templates", + project=project, + obligated=[fileExtensionLATEX], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap2", + project=project, + obligated=[fileExtensionMD], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap2/src", + project=project, + obligated=[fileExtensionPY, fileExtensionHPP, fileExtensionCPP], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap3", + project=project, + obligated=[fileExtensionTS, fileExtensionTSX], + blocked=[]) + self.assertFalse(check_zip_file(project=project, dir_path="tests/test_zip4struct1.zip")[0]) + + def test_checking_blocked_extension_found(self): + course = create_course(id=3, name="test course", academic_startyear=2024) + project = create_project( + name="test", + description="descr", + visible=True, + archived=False, + days=100, + course=course, + ) + + fileExtensionHS = create_file_extension(extension="hs") + fileExtensionPDF = create_file_extension(extension="pdf") + fileExtensionDOCX = create_file_extension(extension="docx") + fileExtensionLATEX = create_file_extension(extension="latex") + fileExtensionMD = create_file_extension(extension="md") + fileExtensionPY = create_file_extension(extension="py") + fileExtensionHPP = create_file_extension(extension="hpp") + fileExtensionCPP = create_file_extension(extension="cpp") + fileExtensionTS = create_file_extension(extension="ts") + fileExtensionTSX = create_file_extension(extension="tsx") + + create_structure_check( + name=".", + project=project, + obligated=[], + blocked=[fileExtensionDOCX]) + + create_structure_check( + name="folder_struct1", + project=project, + obligated=[fileExtensionHS], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap1", + project=project, + obligated=[fileExtensionDOCX], + blocked=[fileExtensionPDF]) + + create_structure_check( + name="folder_struct1/submap1/templates", + project=project, + obligated=[fileExtensionLATEX], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap2", + project=project, + obligated=[fileExtensionMD], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap2/src", + project=project, + obligated=[fileExtensionPY, fileExtensionHPP, fileExtensionCPP], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap3", + project=project, + obligated=[fileExtensionTS, fileExtensionTSX], + blocked=[]) + + self.assertFalse(check_zip_file(project=project, dir_path="tests/test_zip1struct1.zip")[0]) + + def test_checking_extra_directory_found(self): + course = create_course(id=3, name="test course", academic_startyear=2024) + project = create_project( + name="test", + description="descr", + visible=True, + archived=False, + days=100, + course=course, + ) + + fileExtensionHS = create_file_extension(extension="hs") + fileExtensionPDF = create_file_extension(extension="pdf") + fileExtensionDOCX = create_file_extension(extension="docx") + fileExtensionLATEX = create_file_extension(extension="latex") + fileExtensionMD = create_file_extension(extension="md") + fileExtensionPY = create_file_extension(extension="py") + fileExtensionHPP = create_file_extension(extension="hpp") + fileExtensionCPP = create_file_extension(extension="cpp") + fileExtensionTS = create_file_extension(extension="ts") + fileExtensionTSX = create_file_extension(extension="tsx") + + create_structure_check( + name=".", + project=project, + obligated=[], + blocked=[fileExtensionDOCX]) + + create_structure_check( + name="folder_struct1", + project=project, + obligated=[fileExtensionHS], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap1", + project=project, + obligated=[fileExtensionPDF, fileExtensionDOCX], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap1/templates", + project=project, + obligated=[fileExtensionLATEX], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap2", + project=project, + obligated=[fileExtensionMD], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap2/src", + project=project, + obligated=[fileExtensionPY, fileExtensionHPP, fileExtensionCPP], + blocked=[]) + + create_structure_check( + name="folder_struct1/submap3", + project=project, + obligated=[fileExtensionTS, fileExtensionTSX], + blocked=[]) + + self.assertFalse( + check_zip_file(project=project, dir_path="tests/test_zip3struct1.zip", restrict_extra_folders=True)[0]) diff --git a/backend/api/tests/test_project.py b/backend/api/tests/test_project.py index bf17f5ec..df356c94 100644 --- a/backend/api/tests/test_project.py +++ b/backend/api/tests/test_project.py @@ -1,6 +1,7 @@ import json from django.utils import timezone from django.urls import reverse +from django.utils.translation import gettext from rest_framework.test import APITestCase from authentication.models import User from api.models.project import Project @@ -490,6 +491,136 @@ def test_project_structure_checks(self): fileExtension3.extension, ) + def test_project_structure_checks_post(self): + """ + Able to retrieve a structure check of a project after posting it. + """ + + course = create_course(id=3, name="test course", academic_startyear=2024) + fileExtension1 = create_fileExtension(id=1, extension="jpg") + fileExtension2 = create_fileExtension(id=2, extension="png") + fileExtension3 = create_fileExtension(id=3, extension="tar") + fileExtension4 = create_fileExtension(id=4, extension="wfp") + project = create_project( + name="test project", + description="test description", + visible=True, + archived=False, + days=7, + course=course, + ) + + response = self.client.post( + reverse("project-structure-checks", args=[str(project.id)]), + { + "name": ".", + "obligated_extensions": [fileExtension1.extension, fileExtension4.extension], + "blocked_extensions": [fileExtension2.extension, fileExtension3.extension]}, + follow=True, + ) + + project.refresh_from_db() + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.accepted_media_type, "application/json") + self.assertEqual(json.loads(response.content), {'message': 'project.success.structure_check.add'}) + + upd: StructureCheck = project.structure_checks.all()[0] + retrieved_obligated_extensions = upd.obligated_extensions.all() + retrieved_blocked_file_extensions = upd.blocked_extensions.all() + + self.assertEqual(len(retrieved_obligated_extensions), 2) + self.assertEqual( + retrieved_obligated_extensions[0].extension, fileExtension1.extension + ) + self.assertEqual( + retrieved_obligated_extensions[1].extension, fileExtension4.extension + ) + + self.assertEqual(len(retrieved_blocked_file_extensions), 2) + self.assertEqual( + retrieved_blocked_file_extensions[0].extension, + fileExtension2.extension, + ) + self.assertEqual( + retrieved_blocked_file_extensions[1].extension, + fileExtension3.extension, + ) + + def test_project_structure_checks_post_already_existing(self): + """ + Able to retrieve a structure check of a project after posting it. + """ + + course = create_course(id=3, name="test course", academic_startyear=2024) + fileExtension1 = create_fileExtension(id=1, extension="jpg") + fileExtension2 = create_fileExtension(id=2, extension="png") + fileExtension3 = create_fileExtension(id=3, extension="tar") + fileExtension4 = create_fileExtension(id=4, extension="wfp") + project = create_project( + name="test project", + description="test description", + visible=True, + archived=False, + days=7, + course=course, + ) + + create_structure_check( + id=5, + name=".", + project=project, + obligated_extensions=[fileExtension1, fileExtension4], + blocked_extensions=[fileExtension2, fileExtension3], + ) + + response = self.client.post( + reverse("project-structure-checks", args=[str(project.id)]), + { + "name": ".", + "obligated_extensions": [fileExtension1.extension, fileExtension4.extension], + "blocked_extensions": [fileExtension2.extension, fileExtension3.extension]}, + follow=True, + ) + + self.assertEqual(response.status_code, 400) + self.assertEqual(response.accepted_media_type, "application/json") + self.assertEqual(json.loads(response.content), { + 'non_field_errors': [gettext("project.error.structure_checks.already_existing")]}) + + def test_project_structure_checks_post_blocked_and_obligated(self): + """ + Able to retrieve a structure check of a project after posting it. + """ + + course = create_course(id=3, name="test course", academic_startyear=2024) + fileExtension1 = create_fileExtension(id=1, extension="jpg") + fileExtension2 = create_fileExtension(id=2, extension="png") + fileExtension3 = create_fileExtension(id=3, extension="tar") + fileExtension4 = create_fileExtension(id=4, extension="wfp") + project = create_project( + name="test project", + description="test description", + visible=True, + archived=False, + days=7, + course=course, + ) + + response = self.client.post( + reverse("project-structure-checks", args=[str(project.id)]), + { + "name": ".", + "obligated_extensions": [fileExtension1.extension, fileExtension4.extension], + "blocked_extensions": [fileExtension1.extension, fileExtension2.extension, fileExtension3.extension]}, + follow=True, + ) + + self.assertEqual(response.status_code, 400) + self.assertEqual(response.accepted_media_type, "application/json") + self.assertEqual(json.loads(response.content), { + 'non_field_errors': [gettext("project.error.structure_checks.extension_blocked_and_obligated")]}) + def test_project_extra_checks(self): """ Able to retrieve an extra check of a project after creating it.