From 8c7e1978e039a523c626a833464f38e92c2f084e Mon Sep 17 00:00:00 2001 From: SKairinos Date: Thu, 14 Dec 2023 16:31:08 +0000 Subject: [PATCH] create unit tests --- .../user/tests/permissions/__init__.py | 0 .../user/tests/permissions/test_in_class.py | 97 +++++++++++++ .../user/tests/permissions/test_in_school.py | 29 ++++ .../tests/permissions/test_is_independent.py | 42 ++++++ .../user/tests/permissions/test_is_student.py | 76 ++++++++++ .../user/tests/permissions/test_is_teacher.py | 137 ++++++++++++++++++ 6 files changed, 381 insertions(+) create mode 100644 codeforlife/user/tests/permissions/__init__.py create mode 100644 codeforlife/user/tests/permissions/test_in_class.py create mode 100644 codeforlife/user/tests/permissions/test_in_school.py create mode 100644 codeforlife/user/tests/permissions/test_is_independent.py create mode 100644 codeforlife/user/tests/permissions/test_is_student.py create mode 100644 codeforlife/user/tests/permissions/test_is_teacher.py diff --git a/codeforlife/user/tests/permissions/__init__.py b/codeforlife/user/tests/permissions/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/codeforlife/user/tests/permissions/test_in_class.py b/codeforlife/user/tests/permissions/test_in_class.py new file mode 100644 index 00000000..baf0e14c --- /dev/null +++ b/codeforlife/user/tests/permissions/test_in_class.py @@ -0,0 +1,97 @@ +""" +© Ocado Group +Created on 14/12/2023 at 14:26:20(+00:00). +""" + +from rest_framework.views import APIView + +from ....tests import APITestCase +from ...permissions import InClass + + +class TestInClass(APITestCase): + """ + Naming convention: + test_{class_id}__{user_type} + + class_id: The id of a class. Options: + - any_class: Any class. + - in_class: A specific class the user is in. + - not_in_class: A specific class the user is not in. + + user_type: The type of user. Options: + - non_class_teacher: A teacher not in a class. + - class_teacher: A teacher in a class. + - student: A student. + - indy: An independent. + """ + + def test_any_class__non_class_teacher(self): + """ + Teacher without a class is not in any class. + """ + + raise NotImplementedError() # TODO + + def test_any_class__class_teacher(self): + """ + Teacher with a class is in any class. + """ + + raise NotImplementedError() # TODO + + def test_any_class__student(self): + """ + Student is in any class. + """ + + raise NotImplementedError() # TODO + + def test_any_class__indy(self): + """ + Independent is not in any class. + """ + + raise NotImplementedError() # TODO + + def test_not_in_class__non_class_teacher(self): + """ + Teacher without a class is not in a specific class. + """ + + raise NotImplementedError() # TODO + + def test_not_in_class__class_teacher(self): + """ + Teacher with a class is not in a specific class. + """ + + raise NotImplementedError() # TODO + + def test_not_in_class__student(self): + """ + Student is not in a specific class. + """ + + raise NotImplementedError() # TODO + + def test_not_in_class__indy(self): + """ + Independent is not in a specific class. + """ + + raise NotImplementedError() # TODO + + def test_in_class__class_teacher(self): + """ + Teacher with a class is in a specific class. + """ + + raise NotImplementedError() # TODO + + def test_in_class__student(self): + """ + Student is in a specific class. + """ + + raise NotImplementedError() # TODO diff --git a/codeforlife/user/tests/permissions/test_in_school.py b/codeforlife/user/tests/permissions/test_in_school.py new file mode 100644 index 00000000..4ef30fe7 --- /dev/null +++ b/codeforlife/user/tests/permissions/test_in_school.py @@ -0,0 +1,29 @@ +""" +© Ocado Group +Created on 14/12/2023 at 14:26:20(+00:00). +""" + +from rest_framework.views import APIView + +from ....tests import APITestCase +from ...permissions import InSchool + + +class TestInSchool(APITestCase): + """ + Naming convention: + test_{school_id}__{user_type} + + school_id: The id of a school. Options: + - any_school: Any school. + - in_school: A specific school the user is in. + - not_in_school: A specific school the user is not in. + + user_type: The type of user. Options: + - non_school_teacher: A teacher not in a school. + - school_teacher: A teacher in a school. + - student: A student. + - indy: An independent. + """ + + # TODO: define unit tests diff --git a/codeforlife/user/tests/permissions/test_is_independent.py b/codeforlife/user/tests/permissions/test_is_independent.py new file mode 100644 index 00000000..6270fafe --- /dev/null +++ b/codeforlife/user/tests/permissions/test_is_independent.py @@ -0,0 +1,42 @@ +""" +© Ocado Group +Created on 14/12/2023 at 14:26:20(+00:00). +""" + +from rest_framework.views import APIView + +from ....tests import APITestCase +from ...permissions import IsIndependent + + +class TestIsIndependent(APITestCase): + """ + Naming convention: + test_{user_type} + + user_type: The type of user. Options: + - teacher: A teacher. + - student: A student. + - indy: An independent. + """ + + def test_teacher(self): + """ + Teacher is not any independent. + """ + + raise NotImplementedError() # TODO + + def test_student(self): + """ + Student is not any independent. + """ + + raise NotImplementedError() # TODO + + def test_indy(self): + """ + Independent is any independent. + """ + + raise NotImplementedError() # TODO diff --git a/codeforlife/user/tests/permissions/test_is_student.py b/codeforlife/user/tests/permissions/test_is_student.py new file mode 100644 index 00000000..b2aa4583 --- /dev/null +++ b/codeforlife/user/tests/permissions/test_is_student.py @@ -0,0 +1,76 @@ +""" +© Ocado Group +Created on 14/12/2023 at 14:26:20(+00:00). +""" + +from rest_framework.views import APIView + +from ....tests import APITestCase +from ...permissions import IsStudent + + +class TestIsStudent(APITestCase): + """ + Naming convention: + test_{user_type}__{student_id} + + user_type: The type of user. Options: + - teacher: A teacher. + - student: A student. + - indy: An independent. + + student_id: The ID of a student. Options: + - any_student: User is any student. + - not_any_student: User is not any student. + - specific_student: User is a specific student. + - not_specific_student User is not a specific student. + """ + + def test_teacher__not_any_student(self): + """ + Teacher is not any student. + """ + + raise NotImplementedError() # TODO + + def test_teacher__not_specific_student(self): + """ + Teacher is not a specific student. + """ + + raise NotImplementedError() # TODO + + def test_indy__not_any_student(self): + """ + Independent is not any student. + """ + + raise NotImplementedError() # TODO + + def test_indy__not_specific_student(self): + """ + Independent is not a specific student. + """ + + raise NotImplementedError() # TODO + + def test_student__any_student(self): + """ + Student is any student. + """ + + raise NotImplementedError() # TODO + + def test_student__specific_student(self): + """ + Student is a specific student. + """ + + raise NotImplementedError() # TODO + + def test_student__not_specific_student(self): + """ + Student is not a specific student. + """ + + raise NotImplementedError() # TODO diff --git a/codeforlife/user/tests/permissions/test_is_teacher.py b/codeforlife/user/tests/permissions/test_is_teacher.py new file mode 100644 index 00000000..feec8a4c --- /dev/null +++ b/codeforlife/user/tests/permissions/test_is_teacher.py @@ -0,0 +1,137 @@ +""" +© Ocado Group +Created on 14/12/2023 at 14:26:20(+00:00). +""" + +from rest_framework.test import APIRequestFactory +from rest_framework.views import APIView + +from ....tests import APITestCase +from ...models import User +from ...permissions import IsTeacher + + +class TestIsTeacher(APITestCase): + # TODO: test that students and independents do not get permission. + """ + Naming convention: + test_{teacher_id}__{is_admin} + + teacher_id: The id of a teacher. Options: + - id: A specific teacher. + - none: Any teacher. + + is_admin: A flag for if the teacher is an admin. Options: + - true: Teacher is an admin. + - false: Teacher is a non-admin. + - none: Teacher is an admin or a non-admin. + """ + + fixtures = [ + "users", + "teachers", + "schools", + "classes", + "students", + ] + + def setUp(self): + self.user__1 = User.objects.get(pk=1) + self.user__2 = User.objects.get(pk=2) + + assert self.user__1.teacher and self.user__1.teacher.is_admin + assert self.user__2.teacher and not self.user__2.teacher.is_admin + + self.user__1__teacher = self.user__1.teacher + self.user__2__teacher = self.user__2.teacher + + request_factory = APIRequestFactory() + self.request__1 = request_factory.get("/") + self.request__1.user = self.user__1 + self.request__2 = request_factory.get("/") + self.request__2.user = self.user__2 + + def test_none__none(self): + """ + Is any teacher. + """ + + assert IsTeacher( + teacher_id=None, + is_admin=None, + ).has_permission(self.request__1, APIView()) + + def test_none__true(self): + """ + Is any admin teacher. + """ + + assert IsTeacher( + teacher_id=None, + is_admin=True, + ).has_permission(self.request__1, APIView()) + + assert not IsTeacher( + teacher_id=None, + is_admin=True, + ).has_permission(self.request__2, APIView()) + + def test_none__false(self): + """ + Is any non-admin teacher. + """ + + assert not IsTeacher( + teacher_id=None, + is_admin=False, + ).has_permission(self.request__1, APIView()) + + assert IsTeacher( + teacher_id=None, + is_admin=False, + ).has_permission(self.request__2, APIView()) + + def test_id__none(self): + """ + Is a specific teacher. + """ + + assert IsTeacher( + teacher_id=self.user__1__teacher.id, + is_admin=None, + ).has_permission(self.request__1, APIView()) + + assert not IsTeacher( + teacher_id=self.user__2__teacher.id, + is_admin=None, + ).has_permission(self.request__1, APIView()) + + def test_id__true(self): + """ + Is a specific admin teacher. + """ + + assert IsTeacher( + teacher_id=self.user__1__teacher.id, + is_admin=True, + ).has_permission(self.request__1, APIView()) + + assert not IsTeacher( + teacher_id=self.user__2__teacher.id, + is_admin=True, + ).has_permission(self.request__2, APIView()) + + def test_id__false(self): + """ + Is a specific non-admin teacher. + """ + + assert not IsTeacher( + teacher_id=self.user__1__teacher.id, + is_admin=False, + ).has_permission(self.request__1, APIView()) + + assert IsTeacher( + teacher_id=self.user__2__teacher.id, + is_admin=False, + ).has_permission(self.request__2, APIView())