Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/142 tests for educator #179

Merged
merged 17 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 93 additions & 2 deletions nexong/api/Authentication/tests.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,100 @@
from django.test import TestCase
from rest_framework.exceptions import ValidationError
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 rest_framework.authtoken.models import Token
from django.test import TestCase


class EducatorApiViewSetTestCase(APITestCase):
def setUp(self):
self.factory = APIRequestFactory()
self.educator_data = {
"description": "Test description",
"birthdate": "1969-06-09",
}
self.educator_error_desc = {
"description": "",
"birthdate": "1969-06-09",
}
self.educator_error_date = {
"description": "Test description",
"birthdate": "2069-06-09",
}
self.user = User.objects.create(
username="testuser", email="example@gmail.com", role=ADMIN
)
self.token = Token.objects.create(user=self.user)

def test_create_educator(self):
serializerE1 = EducatorSerializer(data=self.educator_error_desc)
serializerE2 = EducatorSerializer(data=self.educator_error_date)

response_error = self.client.post(
"/api/educator/",
self.educator_error_date,
format="multipart",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response_error.status_code, status.HTTP_400_BAD_REQUEST)

with self.assertRaises(ValidationError) as context1:
serializerE1.is_valid(raise_exception=True)
self.assertEqual(
context1.exception.detail["description"][0], "This field may not be blank."
)

with self.assertRaises(ValidationError) as context2:
serializerE2.is_valid(raise_exception=True)
self.assertEqual(
context2.exception.detail["non_field_errors"][0],
"Birthdate can't be greater than today",
)

response = self.client.post(
"/api/educator/",
self.educator_data,
format="multipart",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Educator.objects.count(), 1)
self.assertEqual(Educator.objects.get().description, "Test description")
self.assertEqual(Educator.objects.get().birthdate, datetime.date(1969, 6, 9))

def test_retrieve_educator(self):
educator = Educator.objects.create(**self.educator_data)
response = self.client.get(
f"/api/educator/{educator.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["description"], "Test description")
self.assertEqual(response.data["birthdate"], "1969-06-09")

def test_update_educator(self):
educator = Educator.objects.create(**self.educator_data)
self.educator_data["description"] = "Updated description"
self.educator_data["birthdate"] = "1970-07-10"
response = self.client.put(
f"/api/educator/{educator.id}/",
self.educator_data,
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(Educator.objects.get().description, "Updated description")
self.assertEqual(Educator.objects.get().birthdate, datetime.date(1970, 7, 10))

def test_delete_educator(self):
educator = Educator.objects.create(**self.educator_data)
response = self.client.delete(
f"/api/educator/{educator.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(Educator.objects.count(), 0)


class AdminUserApiViewSetTestCase(TestCase):
Expand Down
Empty file.
142 changes: 142 additions & 0 deletions nexong/api/CenterExit/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
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 django.core.files.uploadedfile import SimpleUploadedFile


class EducatorCenterExitApiViewSetTestCase(APITestCase):
def setUp(self):
self.family = Family.objects.create(name="Los Pedraz")
self.educator = Educator.objects.create(
birthdate="1969-06-09", description="EL profesor de lengua"
)
self.factory = APIRequestFactory()
self.user = User.objects.create(
username="testuser",
email="example@gmail.com",
role=EDUCATOR,
educator=self.educator,
)
self.token = Token.objects.create(user=self.user)
self.educator2 = Educator.objects.create(birthdate="2000-04-21")
self.educator3 = 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.educator2, self.educator3)
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,
}
file_content = b"Test file content"
self.center_exit["authorization"] = SimpleUploadedFile(
"centerexit_authorization.pdf", file_content
)

def test_create_center_exit_by_educator(self):
response = self.client.post(
"/api/center-exit/",
self.center_exit,
format="multipart",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_obtain_center_authorization_by_educator(self):
center_exit = CenterExitAuthorization.objects.create(**self.center_exit)
response = self.client.get(
f"/api/center-exit/{center_exit.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(center_exit.is_authorized, True)

def test_update_center_authorization_by_educator(self):
center_exit = CenterExitAuthorization.objects.create(**self.center_exit)
self.center_exit["is_authorized"] = False
response = self.client.put(
f"/api/center-exit/{center_exit.id}/",
self.center_exit,
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_delete_center_authorization_by_educator(self):
center_exit = CenterExitAuthorization.objects.create(**self.center_exit)
response = self.client.delete(
f"/api/center-exit/{center_exit.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
Empty file.
Loading
Loading