Skip to content

Commit

Permalink
Merge pull request #118 from SELab-2/extra_permission_tests
Browse files Browse the repository at this point in the history
Extra permission tests
  • Loading branch information
EwoutV authored Mar 15, 2024
2 parents 7f136b4 + 79f7a4c commit b1cb2d3
Show file tree
Hide file tree
Showing 8 changed files with 586 additions and 22 deletions.
16 changes: 0 additions & 16 deletions backend/api/permissions/course_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,3 @@ def has_object_permission(self, request: Request, view: ViewSet, course: Course)

# Teachers and assistants can add and remove any student.
return super().has_object_permission(request, view, course)


class CourseProjectPermission(CoursePermission):
"""Permission class for project related endpoints."""
def has_permission(self, request: Request, view: ViewSet) -> bool:
return request.user and request.user.is_authenticated

def has_object_permission(self, request: Request, view: ViewSet, course: Course):
user: User = request.user

# Logged-in users can fetch course projects.
if request.method in SAFE_METHODS:
return user.is_authenticated

# Teachers and assistants can modify projects.
return super().has_object_permission(request, view, course)
12 changes: 8 additions & 4 deletions backend/api/serializers/project_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
from api.models.group import Group
from rest_framework.exceptions import ValidationError
from django.utils import timezone
from api.models.submission import Submission, SubmissionFile
from api.models.checks import FileExtension, StructureCheck
from api.models.checks import FileExtension
from api.serializers.submission_serializer import SubmissionSerializer
from api.serializers.checks_serializer import StructureCheckSerializer
from rest_framework.request import Request


class ProjectSerializer(serializers.ModelSerializer):
Expand All @@ -33,6 +31,11 @@ class ProjectSerializer(serializers.ModelSerializer):
read_only=True
)

submissions = serializers.HyperlinkedIdentityField(
view_name="project-submissions",
read_only=True
)

class Meta:
model = Project
fields = [
Expand All @@ -49,7 +52,8 @@ class Meta:
"structure_checks",
"extra_checks",
"course",
"groups"
"groups",
"submissions"
]

def validate(self, data):
Expand Down
42 changes: 42 additions & 0 deletions backend/api/tests/test_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.urls import reverse
from rest_framework.test import APITestCase
from api.models.assistant import Assistant
from api.models.teacher import Teacher
from api.models.course import Course
from authentication.models import Faculty, User

Expand Down Expand Up @@ -287,3 +288,44 @@ def test_assistant_courses(self):
self.assertEqual(content["name"], course2.name)
self.assertEqual(int(content["academic_startyear"]), course2.academic_startyear)
self.assertEqual(content["description"], course2.description)


class AssitantModelAsTeacherTests(APITestCase):
def setUp(self) -> None:
self.user = Teacher.objects.create(
id=1,
first_name="John",
last_name="Doe",
username="john_doe",
email="[email protected]"
)

self.client.force_authenticate(self.user)

def test_retrieve_assistant_list(self):
"""
Able to retrieve assistant list as a teacher.
"""
# Create an assistant for testing with the name "Bob Peeters"
create_assistant(
id=5, first_name="Bob", last_name="Peeters", email="[email protected]"
)

create_assistant(
id=6, first_name="Jane", last_name="Doe", email="[email protected]"
)

# Make a GET request to retrieve the assistant details
response = self.client.get(reverse("assistant-list"), follow=True)

# Check if the response was successful
self.assertEqual(response.status_code, 200)

# Assert that the response is JSON
self.assertEqual(response.accepted_media_type, "application/json")

# Parse the JSON content from the response
content_json = json.loads(response.content.decode("utf-8"))

# Assert that the parsed JSON is a list with multiple assistant
self.assertEqual(len(content_json), 2)
Loading

0 comments on commit b1cb2d3

Please sign in to comment.