Skip to content

Commit

Permalink
tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jan 16, 2024
1 parent 16f57ce commit 9e3f870
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 56 deletions.
51 changes: 25 additions & 26 deletions codeforlife/user/tests/permissions/test_is_student.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.contrib.auth.models import AnonymousUser

from ....tests import PermissionTestCase
from ...models import User
from ...models import Student, User
from ...permissions import IsStudent


Expand All @@ -23,26 +23,22 @@ class TestIsStudent(PermissionTestCase[IsStudent]):
def setUp(self):
super().setUp()

self.user__1 = User.objects.get(pk=1)
self.user__3 = User.objects.get(pk=3)
self.user__4 = User.objects.get(pk=4)
self.user__5 = User.objects.get(pk=5)
self.teacher_user = User.objects.get(pk=1)
assert self.teacher_user.teacher
self.teacher = self.teacher_user.teacher
self.teacher_request = self.request_factory.get("/")
self.teacher_request.user = self.teacher_user

assert self.user__1.teacher
assert self.user__3.student
assert self.user__4.student
assert not self.user__5.student and not self.user__5.teacher
self.student_user = User.objects.get(pk=3)
assert self.student_user.student
self.student = self.student_user.student
self.student_request = self.request_factory.get("/")
self.student_request.user = self.student_user

self.user__1__teacher = self.user__1.teacher
self.user__3__student = self.user__3.student
self.user__4__student = self.user__4.student

self.request__1 = self.request_factory.get("/")
self.request__1.user = self.user__1
self.request__3 = self.request_factory.get("/")
self.request__3.user = self.user__3
self.request__5 = self.request_factory.get("/")
self.request__5.user = self.user__5
self.indy_user = User.objects.get(pk=5)
assert not self.indy_user.student and not self.indy_user.teacher
self.indy_request = self.request_factory.get("/")
self.indy_request.user = self.indy_user

# pylint: disable-next=pointless-string-statement
"""
Expand All @@ -68,7 +64,7 @@ def test_teacher__not_any_student(self):
"""

self.assert_not_has_permission(
self.request__1,
self.teacher_request,
init_kwargs={
"student_id": None,
},
Expand All @@ -80,7 +76,7 @@ def test_indy__not_any_student(self):
"""

self.assert_not_has_permission(
self.request__5,
self.indy_request,
init_kwargs={
"student_id": None,
},
Expand All @@ -102,7 +98,7 @@ def test_student__any_student(self):
"""

self.assert_has_permission(
self.request__3,
self.student_request,
init_kwargs={
"student_id": None,
},
Expand All @@ -114,9 +110,9 @@ def test_student__specific_student(self):
"""

self.assert_has_permission(
self.request__3,
self.student_request,
init_kwargs={
"student_id": self.user__3__student.id,
"student_id": self.student.id,
},
)

Expand All @@ -125,9 +121,12 @@ def test_student__not_specific_student(self):
Student is not a specific student.
"""

student = Student.objects.exclude(id=self.student.id).first()
assert student is not None

self.assert_not_has_permission(
self.request__3,
self.student_request,
init_kwargs={
"student_id": self.user__4__student.id,
"student_id": student.id,
},
)
64 changes: 34 additions & 30 deletions codeforlife/user/tests/permissions/test_is_teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ class TestIsTeacher(PermissionTestCase[IsTeacher]):
def setUp(self):
super().setUp()

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

self.request__1 = self.request_factory.get("/")
self.request__1.user = self.user__1
self.request__2 = self.request_factory.get("/")
self.request__2.user = self.user__2
self.admin_teacher_user = User.objects.get(pk=1)
assert (
self.admin_teacher_user.teacher
and self.admin_teacher_user.teacher.is_admin
)
self.admin_teacher = self.admin_teacher_user.teacher
self.admin_teacher_request = self.request_factory.get("/")
self.admin_teacher_request.user = self.admin_teacher_user

self.non_admin_teacher_user = User.objects.get(pk=2)
assert (
self.non_admin_teacher_user.teacher
and not self.non_admin_teacher_user.teacher.is_admin
)
self.non_admin_teacher = self.non_admin_teacher_user.teacher
self.non_admin_teacher_request = self.request_factory.get("/")
self.non_admin_teacher_request.user = self.non_admin_teacher_user

# pylint: disable-next=pointless-string-statement
"""
Expand All @@ -58,7 +62,7 @@ def test_none__none(self):
"""

self.assert_has_permission(
self.request__1,
self.admin_teacher_request,
init_kwargs={
"teacher_id": None,
"is_admin": None,
Expand All @@ -71,15 +75,15 @@ def test_none__true(self):
"""

self.assert_has_permission(
self.request__1,
self.admin_teacher_request,
init_kwargs={
"teacher_id": None,
"is_admin": True,
},
)

self.assert_not_has_permission(
self.request__2,
self.non_admin_teacher_request,
init_kwargs={
"teacher_id": None,
"is_admin": True,
Expand All @@ -92,15 +96,15 @@ def test_none__false(self):
"""

self.assert_not_has_permission(
self.request__1,
self.admin_teacher_request,
init_kwargs={
"teacher_id": None,
"is_admin": False,
},
)

self.assert_has_permission(
self.request__2,
self.non_admin_teacher_request,
init_kwargs={
"teacher_id": None,
"is_admin": False,
Expand All @@ -113,17 +117,17 @@ def test_id__none(self):
"""

self.assert_has_permission(
self.request__1,
self.admin_teacher_request,
init_kwargs={
"teacher_id": self.user__1__teacher.id,
"teacher_id": self.admin_teacher.id,
"is_admin": None,
},
)

self.assert_not_has_permission(
self.request__1,
self.admin_teacher_request,
init_kwargs={
"teacher_id": self.user__2__teacher.id,
"teacher_id": self.non_admin_teacher.id,
"is_admin": None,
},
)
Expand All @@ -134,17 +138,17 @@ def test_id__true(self):
"""

self.assert_has_permission(
self.request__1,
self.admin_teacher_request,
init_kwargs={
"teacher_id": self.user__1__teacher.id,
"teacher_id": self.admin_teacher.id,
"is_admin": True,
},
)

self.assert_not_has_permission(
self.request__2,
self.non_admin_teacher_request,
init_kwargs={
"teacher_id": self.user__2__teacher.id,
"teacher_id": self.non_admin_teacher.id,
"is_admin": True,
},
)
Expand All @@ -155,17 +159,17 @@ def test_id__false(self):
"""

self.assert_not_has_permission(
self.request__1,
self.admin_teacher_request,
init_kwargs={
"teacher_id": self.user__1__teacher.id,
"teacher_id": self.admin_teacher.id,
"is_admin": False,
},
)

self.assert_has_permission(
self.request__2,
self.non_admin_teacher_request,
init_kwargs={
"teacher_id": self.user__2__teacher.id,
"teacher_id": self.non_admin_teacher.id,
"is_admin": False,
},
)
Expand Down

0 comments on commit 9e3f870

Please sign in to comment.