Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/ISPP-G5/NexONG_Backend i…
Browse files Browse the repository at this point in the history
…nto feature/142-tests-for-educator
  • Loading branch information
JuanluRM committed Apr 20, 2024
2 parents b96ea08 + 9f06d24 commit 86c62eb
Show file tree
Hide file tree
Showing 7 changed files with 1,120 additions and 15 deletions.
87 changes: 87 additions & 0 deletions nexong/api/Authentication/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,93 @@ def test_delete_educator(self):
self.assertEqual(Educator.objects.count(), count - 1)

Check notice on line 91 in nexong/api/Authentication/tests.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

nexong/api/Authentication/tests.py#L91

'Educator' may be undefined, or defined from star imports: nexong.api.Authentication.views, nexong.models (F405)


class FamilyApiViewSetTestCase(TestCase):
def setUp(self):
self.factory = APIRequestFactory()
self.user = User.objects.create(
username="testuser", email="example@gmail.com", role=ADMIN
)
education = EducationCenter.objects.create(name="San Francisco Solano")
self.user1 = User.objects.create(
username="testuser1",
email="example1@gmail.com",
role=EDUCATION_CENTER,
education_center=education,
)
family = Family.objects.create(name="Familia López")
self.user2 = User.objects.create(
username="testuser2", email="example2@gmail.com", role=FAMILY, family=family
)
self.token = Token.objects.create(user=self.user)
self.token1 = Token.objects.create(user=self.user1)
self.token2 = Token.objects.create(user=self.user2)

def test_create_family_admin(self):
familias_creadas = Family.objects.count()
response = self.client.post(
"/api/family/",
{"name": "Familia López"},
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, 201)
self.assertEqual(Family.objects.count(), familias_creadas + 1)
family = Family.objects.first()
self.assertEqual(family.name, "Familia López")

def test_obtain_family(self):
family = Family.objects.create(name="Familia Lopez")
response = self.client.get(
f"/api/family/{family.id}/", HTTP_AUTHORIZATION=f"Token {self.token.key}"
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["name"], "Familia Lopez")

def test_update_family(self):
family = Family.objects.create(name="Familia López")
response = self.client.put(
f"/api/family/{family.id}/",
data={"id": family.id, "name": "Familia Ruz"},
content_type="application/json",
HTTP_AUTHORIZATION=f"Token {self.token2.key}",
)
self.assertEqual(response.status_code, 200)
family.refresh_from_db()
self.assertEqual(family.name, "Familia Ruz")

def test_delete_family(self):
family = Family.objects.create(name="Familia Lopez")
initial_count = Family.objects.count()
response = self.client.delete(
f"/api/family/{family.id}/", HTTP_AUTHORIZATION=f"Token {self.token.key}"
)
self.assertEqual(response.status_code, 204)
self.assertEqual(Family.objects.count(), initial_count - 1)

def test_obtain_family_permissions_error(self):
family = Family.objects.create(name="Familia Lopez")
response = self.client.get(
f"/api/family/{family.id}/", HTTP_AUTHORIZATION=f"Token {self.token1.key}"
)
self.assertEqual(response.status_code, 403)

def test_update_family_permissions_error(self):
family = Family.objects.create(name="Familia Lopez")
response = self.client.put(
f"/api/family/{family.id}/",
data={"id": family.id, "name": "Familia Ruz"},
content_type="application/json",
HTTP_AUTHORIZATION=f"Token {self.token1.key}",
)
self.assertEqual(response.status_code, 403)

def test_delete_family_permissions_error(self):
family = Family.objects.create(name="Familia Lopez")
response = self.client.delete(
f"/api/family/{family.id}/", HTTP_AUTHORIZATION=f"Token {self.token1.key}"
)
self.assertEqual(response.status_code, 403)


class AdminUserApiViewSetTestCase(TestCase):
def setUp(self):
self.factory = APIRequestFactory()
Expand Down
151 changes: 151 additions & 0 deletions nexong/api/CenterExit/tests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,161 @@
from django.test import TestCase
from nexong.api.Authentication.views import *
from nexong.models import *
from rest_framework.authtoken.models import Token
from rest_framework.test import APIRequestFactory
from django.core.files.uploadedfile import SimpleUploadedFile
from rest_framework.test import APITestCase
from rest_framework import status
from nexong.api.helpers.testsSetup import testSetupEducator


class CenterExitApiViewSetTestCase(TestCase):
def setUp(self):
self.family = Family.objects.create(name="Familia López")
self.factory = APIRequestFactory()
self.user = User.objects.create(
username="testuser",
email="example@gmail.com",
role=FAMILY,
family=self.family,
)
self.user2 = User.objects.create(
username="testuser2", email="example2@gmail.com", role=ADMIN
)
self.token = Token.objects.create(user=self.user)
self.token2 = Token.objects.create(user=self.user2)
file_content = b"Test file content"
self.authorization = SimpleUploadedFile(
"centerexit_authorization.pdf", file_content
)
self.educator = Educator.objects.create(birthdate="2000-04-21")
self.educator2 = Educator.objects.create(birthdate="2000-04-22")
self.education_center = EducationCenter.objects.create(
name="San Francisco Solano"
)
self.voluntario = Volunteer.objects.create(
academic_formation="Prueba Voluntario",
motivation="Test voluntario",
status="ACEPTADO",
address="Test voluntario address",
postal_code=12359,
birthdate="1956-07-05",
start_date="1955-07-05",
end_date="1956-07-05",
)
self.voluntario2 = Volunteer.objects.create(
academic_formation="Test vol2",
motivation="Test vol",
status="ACEPTADO",
address="Test advol",
postal_code=12356,
birthdate="1956-07-05",
start_date="1956-07-05",
end_date="1956-07-05",
)
self.student = Student.objects.create(
name="Amadeo",
surname="Portillo",
education_center=self.education_center,
is_morning_student=True,
activities_during_exit="",
status="ACEPTADO",
current_education_year="TRES AÑOS",
education_center_tutor="Don Carlos Perez",
nationality="Alemania",
birthdate="2015-04-21",
family=self.family,
)
self.student2 = Student.objects.create(
name="Pablo",
surname="Portillo",
education_center=self.education_center,
is_morning_student=False,
activities_during_exit="",
status="ACEPTADO",
current_education_year="TRES AÑOS",
education_center_tutor="Don Carlos Perez",
nationality="Alemania",
birthdate="2015-06-21",
family=self.family,
)
self.lesson = Lesson.objects.create(
name="PRIMER Materia",
description="Módulo C, segunda planta",
capacity=50,
is_morning_lesson=True,
educator=self.educator,
start_date="2024-01-28",
end_date="2024-05-28",
)
self.lesson_event = LessonEvent.objects.create(
name="Excursión a La Giralda",
description="Vamos al centro a ver La Giralda",
place="Centro Sevilla",
price=25,
max_volunteers=10,
start_date="2024-04-18 12:00-00:00",
end_date="2024-04-18 18:00-00:00",
lesson=self.lesson,
)
self.lesson_event.educators.add(self.educator, self.educator2)
self.lesson_event.attendees.add(self.student, self.student2)
self.lesson_event.volunteers.add(self.voluntario, self.voluntario2)

self.center_exit = {
"student": self.student,
"is_authorized": True,
"lesson_event": self.lesson_event,
}

def test_create_center_exit(self):
center_exit_creadas = CenterExitAuthorization.objects.count()
response = self.client.post(
"/api/center-exit/",
{
"authorization": self.authorization,
"student": self.student.id,
"is_authorized": True,
"lesson_event": self.lesson_event.id,
},
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, 201)
self.assertEqual(
CenterExitAuthorization.objects.count(), center_exit_creadas + 1
)
center_exit = CenterExitAuthorization.objects.first()
self.assertEqual(center_exit.is_authorized, True)

def test_obtain_center_authorization(self):
center_exit = CenterExitAuthorization.objects.create(
authorization=self.authorization,
student=self.student,
is_authorized=True,
lesson_event=self.lesson_event,
)
response = self.client.get(
f"/api/center-exit/{center_exit.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, 200)
self.assertEqual(center_exit.is_authorized, True)

def test_delete_center_authorization(self):
center_exit1 = CenterExitAuthorization.objects.create(
authorization=self.authorization,
student=self.student,
is_authorized=False,
lesson_event=self.lesson_event,
)
initial_count = CenterExitAuthorization.objects.count()
response = self.client.delete(
f"/api/center-exit/{center_exit1.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, 204)
self.assertEqual(CenterExitAuthorization.objects.count(), initial_count - 1)

class EducatorCenterExitApiViewSetTestCase(APITestCase):
def setUp(self):
testSetupEducator(self)
Expand Down
114 changes: 114 additions & 0 deletions nexong/api/Evaluation/test.py → nexong/api/Evaluation/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from django.test import TestCase
from nexong.api.Authentication.views import *
from nexong.models import *
from rest_framework.authtoken.models import Token
from rest_framework.test import APIRequestFactory
from rest_framework.test import APITestCase
from rest_framework import status
from nexong.api.helpers.testsSetup import testSetupEducator
Expand Down Expand Up @@ -191,3 +194,114 @@ def test_delete_student_evaluation_by_educator(self):
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(StudentEvaluation.objects.count(), 1)

Check notice on line 195 in nexong/api/Evaluation/tests.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

nexong/api/Evaluation/tests.py#L195

'StudentEvaluation' may be undefined, or defined from star imports: nexong.api.Authentication.views, nexong.models (F405)
self.assertEqual(response2.status_code, status.HTTP_403_FORBIDDEN)

class Student_Evaluation_ApiViewSetTestCase(TestCase):
def setUp(self):
self.factory = APIRequestFactory()

self.family = Family.objects.create(name="Familia Ruz")
self.education_center = EducationCenter.objects.create(
name="San Francisco Solano"
)
self.educator = Educator.objects.create(birthdate="2000-04-21")
self.user = User.objects.create(
username="testuser",
email="example@gmail.com",
role=FAMILY,
family=self.family,
)
self.token = Token.objects.create(user=self.user)

self.student = Student.objects.create(
name="Alvaro",
surname="Mejias",
education_center=self.education_center,
is_morning_student=True,
activities_during_exit="",
status="ACEPTADO",
current_education_year="TRES AÑOS",
education_center_tutor="Don Perez",
nationality="España",
birthdate="2015-04-21",
family=self.family,
)
self.lesson = Lesson.objects.create(
name="PRIMER CICLO 1",
description="Módulo I, segunda planta",
capacity=4,
is_morning_lesson=True,
educator=self.educator,
start_date="2024-01-28",
end_date="2024-05-28",
)
self.evaluation_type = EvaluationType.objects.create(
name="Asistencia diaria",
description="Campo Prueba",
evaluation_type="DIARIO",
grade_system="CERO A UNO",
lesson=self.lesson,
)

def test_obtain_student_evaluation_by_family(self):
student_evaluation = StudentEvaluation.objects.create(
grade=8,
date="2024-02-17",
comment="Muy bien",
evaluation_type=self.evaluation_type,
student=self.student,
)
response = self.client.get(
f"/api/student-evaluation/{student_evaluation.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, 200)

def test_create_student_evaluation_by_family_error(self):
response = self.client.post(
f"/api/student-evaluation/",
data={
"grade": 8,
"date": "2024-02-17",
"comment": "Muy bien",
"evaluation_type": self.evaluation_type.id,
"student": self.student.id,
},
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_update_student_evaluation_permissions_error(self):
student_evaluation = StudentEvaluation.objects.create(
grade=8,
date="2024-02-17",
comment="Muy bien",
evaluation_type=self.evaluation_type,
student=self.student,
)
response = self.client.put(
f"/api/student-evaluation/{student_evaluation.id}/",
data={
"grade": 5,
"date": "2024-02-17",
"comment": "Muy bien",
"evaluation_type": self.evaluation_type.id,
"student": self.student.id,
},
content_type="application/json",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_delete_student_evaluation_permissions_error(self):
student_evaluation2 = StudentEvaluation.objects.create(
grade=4,
date="2024-02-17",
comment="Muy bien",
evaluation_type=self.evaluation_type,
student=self.student,
)
response = self.client.delete(
f"/api/student-evaluation/{student_evaluation2.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
Loading

0 comments on commit 86c62eb

Please sign in to comment.