Skip to content

Commit

Permalink
create unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Dec 14, 2023
1 parent dbc3df6 commit 8c7e197
Show file tree
Hide file tree
Showing 6 changed files with 381 additions and 0 deletions.
Empty file.
97 changes: 97 additions & 0 deletions codeforlife/user/tests/permissions/test_in_class.py
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions codeforlife/user/tests/permissions/test_in_school.py
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions codeforlife/user/tests/permissions/test_is_independent.py
Original file line number Diff line number Diff line change
@@ -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
76 changes: 76 additions & 0 deletions codeforlife/user/tests/permissions/test_is_student.py
Original file line number Diff line number Diff line change
@@ -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
137 changes: 137 additions & 0 deletions codeforlife/user/tests/permissions/test_is_teacher.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit 8c7e197

Please sign in to comment.