Skip to content

Commit

Permalink
Merge branch 'frontend_testen' of https://github.com/SELab-2/UGent-4
Browse files Browse the repository at this point in the history
…into frontend_testen
  • Loading branch information
mathis2003 committed May 23, 2024
2 parents 84f4b89 + ad78469 commit 9834e93
Show file tree
Hide file tree
Showing 25 changed files with 635 additions and 585 deletions.
14 changes: 13 additions & 1 deletion api/middleware/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from django.contrib.auth.models import User
from api.models.gebruiker import Gebruiker
from api.serializers.gebruiker import GebruikerSerializer
from api.serializers.template import TemplateSerializer
from django.core.files import File
import requests
import os

URL = "https://graph.microsoft.com/v1.0/me"

Expand Down Expand Up @@ -59,6 +62,16 @@ def __call__(self, request):
try:
Gebruiker.objects.get(pk=request.user.id)
except Gebruiker.DoesNotExist:
directory_path = "api/base_templates"
for filename in os.listdir(directory_path):
file_path = os.path.join(directory_path, filename)
with open(file_path, "rb") as f:
django_file = File(f)
template_data = {"user": request.user.id, "bestand": django_file}
serializer = TemplateSerializer(data=template_data)
if serializer.is_valid():
serializer.save()

gebruiker_post_data = {
"user": request.user.id,
"subjects": [],
Expand All @@ -72,7 +85,6 @@ def __call__(self, request):


class DisableCSRFMiddleware(object):

def __init__(self, get_response):
self.get_response = get_response

Expand Down
17 changes: 14 additions & 3 deletions api/middleware/middleware_lesgever_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.contrib.auth.models import User
from api.models.gebruiker import Gebruiker
from api.serializers.gebruiker import GebruikerSerializer

from api.serializers.template import TemplateSerializer
from django.core.files import File
import os


class AuthenticationUserMiddleware:
Expand All @@ -22,7 +24,7 @@ def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):

mail = "[email protected]"
try:
user = User.objects.get(username=mail)
Expand All @@ -39,6 +41,16 @@ def __call__(self, request):
try:
Gebruiker.objects.get(pk=request.user.id)
except Gebruiker.DoesNotExist:
directory_path = "api/base_templates"
for filename in os.listdir(directory_path):
file_path = os.path.join(directory_path, filename)
with open(file_path, "rb") as f:
django_file = File(f)
template_data = {"user": request.user.id, "bestand": django_file}
serializer = TemplateSerializer(data=template_data)
if serializer.is_valid():
serializer.save()

gebruiker_post_data = {
"user": request.user.id,
"subjects": [],
Expand All @@ -52,7 +64,6 @@ def __call__(self, request):


class DisableCSRFMiddleware(object):

def __init__(self, get_response):
self.get_response = get_response

Expand Down
14 changes: 13 additions & 1 deletion api/middleware/middleware_original.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from django.contrib.auth.models import User
from api.models.gebruiker import Gebruiker
from api.serializers.gebruiker import GebruikerSerializer
from api.serializers.template import TemplateSerializer
from django.core.files import File
import requests
import os

URL = "https://graph.microsoft.com/v1.0/me"

Expand Down Expand Up @@ -59,6 +62,16 @@ def __call__(self, request):
try:
Gebruiker.objects.get(pk=request.user.id)
except Gebruiker.DoesNotExist:
directory_path = "api/base_templates"
for filename in os.listdir(directory_path):
file_path = os.path.join(directory_path, filename)
with open(file_path, "rb") as f:
django_file = File(f)
template_data = {"user": request.user.id, "bestand": django_file}
serializer = TemplateSerializer(data=template_data)
if serializer.is_valid():
serializer.save()

gebruiker_post_data = {
"user": request.user.id,
"subjects": [],
Expand All @@ -72,7 +85,6 @@ def __call__(self, request):


class DisableCSRFMiddleware(object):

def __init__(self, get_response):
self.get_response = get_response

Expand Down
17 changes: 14 additions & 3 deletions api/middleware/middleware_student_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.contrib.auth.models import User
from api.models.gebruiker import Gebruiker
from api.serializers.gebruiker import GebruikerSerializer

from api.serializers.template import TemplateSerializer
from django.core.files import File
import os


class AuthenticationUserMiddleware:
Expand All @@ -22,7 +24,7 @@ def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):

mail = "[email protected]"
try:
user = User.objects.get(username=mail)
Expand All @@ -39,6 +41,16 @@ def __call__(self, request):
try:
Gebruiker.objects.get(pk=request.user.id)
except Gebruiker.DoesNotExist:
directory_path = "api/base_templates"
for filename in os.listdir(directory_path):
file_path = os.path.join(directory_path, filename)
with open(file_path, "rb") as f:
django_file = File(f)
template_data = {"user": request.user.id, "bestand": django_file}
serializer = TemplateSerializer(data=template_data)
if serializer.is_valid():
serializer.save()

gebruiker_post_data = {
"user": request.user.id,
"subjects": [],
Expand All @@ -52,7 +64,6 @@ def __call__(self, request):


class DisableCSRFMiddleware(object):

def __init__(self, get_response):
self.get_response = get_response

Expand Down
6 changes: 3 additions & 3 deletions api/models/indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ def __str__(self):
return str(self.indiening_id)

def save(self, *args, **kwargs):
# Update the bestand path if it's still using the temporary path
if "temp" not in self.bestand.name:
super(Indiening, self).save(*args, **kwargs)

if "temp" in self.bestand.name:
old_file = self.bestand
new_path = self.bestand.name.replace(
"temp", f"indiening_{self.indiening_id}"
)
default_storage.save(new_path, ContentFile(old_file.read()))
old_file.storage.delete(old_file.name)
self.bestand.name = new_path

super(Indiening, self).save(*args, **kwargs)
Expand All @@ -95,7 +96,6 @@ def run_tests_async(instance):
indiening_id = instance.indiening_id
project_id = instance.groep.project.project_id
result = run_tests_on(indiening_id, project_id)

with transaction.atomic():
instance.status = -1 if "FAIL" in result else 1
instance.result = result
Expand Down
25 changes: 3 additions & 22 deletions api/serializers/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ def update(self, instance, validated_data):
Groep: De bijgewerkte groep.
"""
students_data = validated_data.pop("studenten", instance.studenten.all())
new_project = validated_data.get("project", instance.project)
validate_students(students_data, new_project, current_group=instance)
validate_project(instance, new_project)
validate_groep_grootte(students_data, new_project)
validated_data.pop("project", instance.project)
validate_students(students_data, instance.project, current_group=instance)
validate_groep_grootte(students_data, instance.project)

super().update(instance=instance, validated_data=validated_data)
instance.studenten.set(students_data)
Expand All @@ -62,24 +61,6 @@ def update(self, instance, validated_data):
return instance


def validate_project(instance, new_project):
"""
Valideert of het project van een groep niet kan worden aangepast.
Args:
instance: De huidige instantie van het project.
new_project: Het nieuwe project waaraan de groep wil worden gekoppeld.
Raises:
serializers.ValidationError: Wordt opgegooid als het project van een groep wordt aangepast.
"""

if instance.project != new_project:
raise serializers.ValidationError(
"Het project van een groep kan niet aangepast worden"
)


def validate_students(students_data, project, current_group=None):
"""
Controleert of de opgegeven gebruikers studenten zijn en of ze al in een andere groep voor dit project zitten.
Expand Down
29 changes: 17 additions & 12 deletions api/views/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from api.models.groep import Groep
from api.serializers.groep import GroepSerializer
from api.utils import has_permissions, contains
from api.utils import has_permissions


@api_view(["GET", "POST"])
Expand All @@ -28,10 +28,7 @@ def groep_list(request, format=None):
Response: Een lijst van groepen of een nieuw aangemaakte groep.
"""
if request.method == "GET":
if has_permissions(request.user):
groepen = Groep.objects.all()
else:
groepen = Groep.objects.filter(studenten=request.user.id)
groepen = Groep.objects.all()

if "project" in request.GET:
try:
Expand Down Expand Up @@ -78,13 +75,13 @@ def groep_detail(request, id, format=None):
except Groep.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == "GET":
if has_permissions(request.user) or contains(groep.studenten, request.user):
serializer = GroepSerializer(groep)
return Response(serializer.data)
return Response(status=status.HTTP_403_FORBIDDEN)
serializer = GroepSerializer(groep)
return Response(serializer.data)

if has_permissions(request.user):
if request.method in ["PUT", "PATCH"]:
if request.method in ["PUT", "PATCH"]:
if has_permissions(request.user) or validate_new_students(
request.user, groep, request.data
):
if request.method == "PUT":
serializer = GroepSerializer(groep, data=request.data)
else:
Expand All @@ -93,8 +90,16 @@ def groep_detail(request, id, format=None):
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response(status=status.HTTP_403_FORBIDDEN)

elif request.method == "DELETE":
if has_permissions(request.user):
if request.method == "DELETE":
groep.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
return Response(status=status.HTTP_403_FORBIDDEN)


def validate_new_students(user, current_groep, data):
old = set(map(lambda student: student.user.id, current_groep.studenten.all()))
new = set(data.get("studenten"))
return (old - new).union(new - old) == {user.id}
5 changes: 0 additions & 5 deletions frontend/frontend/cypress.env.json.example

This file was deleted.

3 changes: 1 addition & 2 deletions frontend/frontend/cypress/e2e/student/group.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ describe('student chooses a group and leaves', () => {
cy.get('#group').click()
cy.contains('Student Testing').should('exist')
cy.get('#leaveGroup').click()
cy.contains('No members yet').should('exist')
})

it('join', () => {
// student should able to choose a group because of the teacher groups test
cy.contains('test course').click()
cy.contains('test project').click()
cy.get('#group').click()
cy.contains('No members yet').should('exist')
cy.get('#joinGroup').click()
cy.contains('Student Testing').should('exist')
})

})
5 changes: 3 additions & 2 deletions frontend/frontend/cypress/e2e/student/submission.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ describe('submit files for an assignment', () => {
it('submit', () => {
cy.contains('test course').click()
cy.contains('test project').click()

// TODO submit files
cy.get('#uploadButton').selectFile('cypress/fixtures/test.pdf')
cy.contains('test.pdf')
cy.get('#submit').click()
})
})
8 changes: 3 additions & 5 deletions frontend/frontend/cypress/e2e/teacher/course.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,26 @@ describe('create and change a course', () => {
cy.get('#addCourse').click()
// fill in the fields
cy.get('#courseName').type('test course')
cy.get('#uploadStudent').get('#email').type('[email protected]')
cy.get('#uploadStudent').get('#add').click()
cy.get('#save').click()
cy.get('#confirm').click()
// check if course was added
cy.get('#logo').click();
cy.contains('test course').should('exist')
cy.contains('Students: 1').should('exist')
cy.contains('Students: 0').should('exist')
})

it('alter course', () => {

cy.contains('test course').click()
cy.get('#editButton').click()
// add a student
cy.get('#uploadStudent').get('#email').type('student1@testing.com') // make sure there is a student 1 in the db
cy.get('#uploadStudent').get('#email').type('student@testing.com')
cy.get('#uploadStudent').get('#add').click()
// save course
cy.get('#save').click()
cy.get('#confirm').click()
// check if the student was added
cy.get('#logo').click();
cy.contains('Students: 2').should('exist')
cy.contains('Students: 1').should('exist')
})
})
5 changes: 5 additions & 0 deletions frontend/frontend/cypress/e2e/teacher/project.cy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { cyan } from "@mui/material/colors"

describe('add and change projects', () => {
beforeEach(() => {
cy.visit('http://localhost:5173')
Expand All @@ -16,6 +18,9 @@ describe('add and change projects', () => {
cy.get('#setVisible').click()
cy.get('#groupSize').clear().type('2')
cy.get('#maxScore').clear().type('30')
// add an assignment
cy.get('#uploadButton').selectFile('cypress/fixtures/test.pdf')
cy.contains('test.pdf').should('exist')
// add a restriction script
cy.get('#addRestrictionButton').click()
// create a new script
Expand Down
7 changes: 5 additions & 2 deletions frontend/frontend/cypress/e2e/teacher/score.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ describe('assign scores to submissions', () => {
cy.contains('test course').click()
cy.contains('test project').click()
cy.get('#adjustScores').click()

// TODO adjust the scores
cy.get('#score').type('10')
cy.get('#saveScores').click()
cy.get('#confirm').click()
// check if the score is assigned
cy.contains('10/30').should('exist')
})
})
Binary file modified frontend/frontend/cypress/fixtures/test.pdf
Binary file not shown.
Loading

0 comments on commit 9834e93

Please sign in to comment.