Skip to content

Commit

Permalink
Merge pull request #179 from ISPP-G5/feature/142-tests-for-educator
Browse files Browse the repository at this point in the history
Feature/142 tests for educator
  • Loading branch information
pabpercab1 authored Apr 20, 2024
2 parents 2da90f1 + 9836ee3 commit 0f7be7d
Show file tree
Hide file tree
Showing 6 changed files with 686 additions and 3 deletions.
88 changes: 87 additions & 1 deletion nexong/api/Authentication/tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,94 @@
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 django.test import TestCase
from nexong.api.helpers.testsSetup import testSetupEducator


class EducatorApiViewSetTestCase(APITestCase):
def setUp(self):
testSetupEducator(self)
self.userAdmin = User.objects.create(
username="testAdminUserForEducator",
email="exampleAdmin@outlook.com",
role=ADMIN,
)
self.token2 = Token.objects.create(user=self.userAdmin)

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.token2.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",
)

count = Educator.objects.count()
response = self.client.post(
"/api/educator/",
self.educator_data,
format="multipart",
HTTP_AUTHORIZATION=f"Token {self.token2.key}",
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Educator.objects.count(), count + 1)
self.assertEqual(response.data["description"], "Test description")
self.assertEqual(response.data["birthdate"], "1969-06-09")

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.token2.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.token2.key}",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["description"], "Updated description")
self.assertEqual(response.data["birthdate"], "1970-07-10")

def test_delete_educator(self):
educator = Educator.objects.create(**self.educator_data)
count = Educator.objects.count()

response = self.client.delete(
f"/api/educator/{educator.id}/",
HTTP_AUTHORIZATION=f"Token {self.token2.key}",
)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(Educator.objects.count(), count - 1)


class FamilyApiViewSetTestCase(TestCase):
Expand Down
45 changes: 44 additions & 1 deletion nexong/api/CenterExit/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
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):
Expand Down Expand Up @@ -117,7 +120,6 @@ def test_create_center_exit(self):
},
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
print(response)
self.assertEqual(response.status_code, 201)
self.assertEqual(
CenterExitAuthorization.objects.count(), center_exit_creadas + 1
Expand Down Expand Up @@ -153,3 +155,44 @@ def test_delete_center_authorization(self):
)
self.assertEqual(response.status_code, 204)
self.assertEqual(CenterExitAuthorization.objects.count(), initial_count - 1)


class EducatorCenterExitApiViewSetTestCase(APITestCase):
def setUp(self):
testSetupEducator(self)

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)
191 changes: 191 additions & 0 deletions nexong/api/Evaluation/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,197 @@
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


class EducatorEvaluationTypeTestCase(APITestCase):
def setUp(self):
testSetupEducator(self)

def test_create_evaluation_type_by_educator(self):
self.evaluation_type_data["lesson"] = self.lesson.id
self.evaluation_type_data_bad_request["lesson"] = self.lesson.id
response = self.client.post(
"/api/evaluation-type/",
self.evaluation_type_data,
format="multipart",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

response2 = self.client.post(
"/api/evaluation-type/",
self.evaluation_type_data_bad_request,
format="multipart",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response2.status_code, status.HTTP_400_BAD_REQUEST)

def test_obtain_evaluation_type_by_educator(self):
eval_type = EvaluationType.objects.create(**self.evaluation_type_data)
eval_type_forbid = EvaluationType.objects.create(
**self.evaluation_type_data_forbiden
)
response = self.client.get(
f"/api/evaluation-type/{eval_type.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["name"], "Asistencia")
self.assertEqual(response.data["description"], "asitencia diaria")

response2 = self.client.get(
f"/api/evaluation-type/{eval_type_forbid.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response2.status_code, status.HTTP_403_FORBIDDEN)

def test_update_evaluation_type_by_educator(self):
eval_type = EvaluationType.objects.create(**self.evaluation_type_data)
self.evaluation_type_data["name"] = "Participacion en clase"
self.evaluation_type_data["lesson"] = self.lesson.id
self.evaluation_type_data_bad_request["lesson"] = self.lesson.id

response = self.client.put(
f"/api/evaluation-type/{eval_type.id}/",
self.evaluation_type_data,
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["name"], "Participacion en clase")

eval_type_forbid = EvaluationType.objects.create(
**self.evaluation_type_data_forbiden
)
self.evaluation_type_data_forbiden["lesson"] = self.lesson2.id
response2 = self.client.put(
f"/api/evaluation-type/{eval_type_forbid.id}/",
self.evaluation_type_data,
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response2.status_code, status.HTTP_403_FORBIDDEN)

response3 = self.client.put(
f"/api/evaluation-type/{eval_type.id}/",
self.evaluation_type_data_bad_request,
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response3.status_code, status.HTTP_400_BAD_REQUEST)

def test_delete_evaluation_type_by_educator(self):
eval_type = EvaluationType.objects.create(**self.evaluation_type_data)
eval_type_forbid = EvaluationType.objects.create(
**self.evaluation_type_data_forbiden
)
count = EvaluationType.objects.count()
response = self.client.delete(
f"/api/evaluation-type/{eval_type.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
response2 = self.client.delete(
f"/api/evaluation-type/{eval_type_forbid.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(EvaluationType.objects.count(), count - 1)
self.assertEqual(response2.status_code, status.HTTP_403_FORBIDDEN)


class EducatorStudentEvaluationTestCase(APITestCase):
def setUp(self):
testSetupEducator(self)

def test_create_student_evaluation_by_educator(self):
self.student_eval_data["student"] = self.student.id
self.student_eval_data["evaluation_type"] = self.evaluation_type.id
self.student_eval_data_bad_request["student"] = self.student.id
self.student_eval_data_bad_request["evaluation_type"] = self.evaluation_type2.id
response = self.client.post(
"/api/student-evaluation/",
self.student_eval_data,
format="multipart",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

response2 = self.client.post(
"/api/student-evaluation/",
self.student_eval_data_bad_request,
format="multipart",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response2.status_code, status.HTTP_400_BAD_REQUEST)

def test_obtain_student_evaluation_by_educator(self):
student_eval = StudentEvaluation.objects.create(**self.student_eval_data)
student_eval_forbid = StudentEvaluation.objects.create(
**self.student_eval_data_forbiden
)
response = self.client.get(
f"/api/student-evaluation/{student_eval.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["grade"], 0)
self.assertEqual(response.data["comment"], "Puede mejorar")

response2 = self.client.get(
f"/api/student-evaluation/{student_eval_forbid.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response2.status_code, status.HTTP_403_FORBIDDEN)

def test_update_student_evaluation_by_educator(self):
student_eval = StudentEvaluation.objects.create(**self.student_eval_data)
self.student_eval_data["student"] = self.student.id
self.student_eval_data["evaluation_type"] = self.evaluation_type.id
self.student_eval_data["comment"] = "Mejoro tras la revision"

response = self.client.put(
f"/api/student-evaluation/{student_eval.id}/",
self.student_eval_data,
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["comment"], "Mejoro tras la revision")

student_eval_forbid = StudentEvaluation.objects.create(
**self.student_eval_data_forbiden
)
self.student_eval_data_forbiden["student"] = self.student.id
self.student_eval_data_forbiden["evaluation_type"] = self.evaluation_type2.id
response2 = self.client.put(
f"/api/student-evaluation/{student_eval_forbid.id}/",
self.student_eval_data,
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response2.status_code, status.HTTP_403_FORBIDDEN)

response3 = self.client.put(
f"/api/student-evaluation/{student_eval.id}/",
self.student_eval_data_bad_request,
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response3.status_code, status.HTTP_400_BAD_REQUEST)

def test_delete_student_evaluation_by_educator(self):
student_eval = StudentEvaluation.objects.create(**self.student_eval_data)
student_eval_forbid = StudentEvaluation.objects.create(
**self.student_eval_data_forbiden
)
response = self.client.delete(
f"/api/student-evaluation/{student_eval.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
response2 = self.client.delete(
f"/api/student-evaluation/{student_eval_forbid.id}/",
HTTP_AUTHORIZATION=f"Token {self.token.key}",
)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(StudentEvaluation.objects.count(), 1)
self.assertEqual(response2.status_code, status.HTTP_403_FORBIDDEN)


class Student_Evaluation_ApiViewSetTestCase(TestCase):
Expand Down
Loading

0 comments on commit 0f7be7d

Please sign in to comment.