From b58501f5869043dc63acd3e1e64f1e21fccf2762 Mon Sep 17 00:00:00 2001 From: Bram Meir Date: Sat, 9 Mar 2024 22:36:27 +0100 Subject: [PATCH] chore: check score is below max_score project --- backend/api/serializers/group_serializer.py | 13 +++++++++++-- backend/api/tests/test_group.py | 19 ++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/backend/api/serializers/group_serializer.py b/backend/api/serializers/group_serializer.py index 2e8f33ed..92847e7f 100644 --- a/backend/api/serializers/group_serializer.py +++ b/backend/api/serializers/group_serializer.py @@ -20,6 +20,15 @@ class Meta: model = Group fields = ["id", "project", "students", "score"] + def validate(self, data): + # Make sure the score of the group is lower or equal to the maximum score + group: Group = self.instance + + if "score" in data and data["score"] > group.project.max_score: + raise ValidationError(gettext("group.errors.score_exceeds_max")) + + return data + class StudentJoinGroupSerializer(StudentIDSerializer): @@ -34,7 +43,7 @@ def validate(self, data): # Make sure the group is not already full if group.is_full(): - raise serializers.ValidationError(gettext("group.errors.full")) + raise ValidationError(gettext("group.errors.full")) # Make sure the student is part of the course if not group.project.course.students.filter(id=student.id).exists(): @@ -42,7 +51,7 @@ def validate(self, data): # Make sure the student is not already in a group if student.is_enrolled_in_group(group.project): - raise serializers.ValidationError(gettext("group.errors.already_in_group")) + raise ValidationError(gettext("group.errors.already_in_group")) return data diff --git a/backend/api/tests/test_group.py b/backend/api/tests/test_group.py index 8f988a8e..0005626c 100644 --- a/backend/api/tests/test_group.py +++ b/backend/api/tests/test_group.py @@ -24,11 +24,11 @@ def create_course(name, academic_startyear, description=None, parent_course=None ) -def create_project(name, description, days, course, group_size=2): +def create_project(name, description, days, course, group_size=2, max_score=20): """Create a Project with the given arguments.""" deadline = timezone.now() + timedelta(days=days) return Project.objects.create( - name=name, description=description, deadline=deadline, course=course, group_size=group_size + name=name, description=description, deadline=deadline, course=course, group_size=group_size, max_score=max_score ) @@ -347,7 +347,7 @@ def test_update_score_of_group(self): course = create_course(name="sel2", academic_startyear=2023) project = create_project( - name="Project 1", description="Description 1", days=7, course=course + name="Project 1", description="Description 1", days=7, course=course, max_score=20 ) # Add this teacher to the course @@ -368,6 +368,19 @@ def test_update_score_of_group(self): group.refresh_from_db() self.assertEqual(group.score, 20) + # Try to update the score of a group to a score higher than the maximum score + response = self.client.patch( + reverse("group-detail", args=[str(group.id)]), + {"score": 30}, + follow=True, + ) + + self.assertEqual(response.status_code, 400) + + # Make sure the score of the group is not updated + group.refresh_from_db() + self.assertEqual(group.score, 20) + class GroupModelTestsAsStudent(APITestCase): def setUp(self) -> None: