Skip to content

Commit

Permalink
fix: backend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoutV committed May 21, 2024
1 parent 0049d1e commit 2c46b97
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 26 deletions.
6 changes: 4 additions & 2 deletions backend/api/logic/parse_zip_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] != '/':
Expand All @@ -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)

Expand Down
19 changes: 9 additions & 10 deletions backend/api/serializers/checks_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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

Expand Down Expand Up @@ -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"
)

Expand Down
51 changes: 38 additions & 13 deletions backend/api/tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -472,21 +481,29 @@ def test_project_structure_checks_post_already_existing(self):
days=7,
course=course,
)

create_structure_check(
path=".",
project=project,
obligated_extensions=[file_extension1, file_extension4],
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)
Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion backend/api/views/project_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2c46b97

Please sign in to comment.