Skip to content

Commit

Permalink
general tests and relocate login methods
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Oct 22, 2023
1 parent e8e4e3b commit 5e19077
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 25 deletions.
36 changes: 18 additions & 18 deletions codeforlife/tests/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ def login(self, **credentials):

return user

def login_teacher(self, **credentials):
user = self.login(**credentials)
assert user.teacher
assert user.teacher.school
return user

def login_student(self, **credentials):
user = self.login(**credentials)
assert user.student
assert user.student.class_field.teacher.school
return user

def login_indy_student(self, **credentials):
user = self.login(**credentials)
assert user.student
assert not user.student.class_field
return user

@staticmethod
def assert_data_equals_model(
data: t.Dict[str, t.Any],
Expand Down Expand Up @@ -145,24 +163,6 @@ class APITestCase(_APITestCase):
client: APIClient
client_class = APIClient

def login_teacher(self, **credentials):
user = self.client.login(**credentials)
assert user.teacher
assert user.teacher.school
return user

def login_student(self, **credentials):
user = self.client.login(**credentials)
assert user.student
assert user.student.class_field.teacher.school
return user

def login_indy_student(self, **credentials):
user = self.client.login(**credentials)
assert user.student
assert not user.student.class_field
return user

def get_other_user(
self,
user: User,
Expand Down
5 changes: 5 additions & 0 deletions codeforlife/user/tests/views/test_klass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from ....tests import APITestCase


class TestClassViewSet(APITestCase):
pass # TODO
28 changes: 25 additions & 3 deletions codeforlife/user/tests/views/test_school.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import typing as t

from rest_framework import status
from rest_framework.permissions import IsAuthenticated

from ....tests import APITestCase, APIClient
from ...serializers import SchoolSerializer
from ...views import SchoolViewSet
from ...models import User, School, Teacher, Student, Class, UserProfile


Expand Down Expand Up @@ -60,19 +62,19 @@ def setUp(self):
)

def _login_teacher(self):
return self.login_teacher(
return self.client.login_teacher(
email="[email protected]",
password="Password1",
)

def _login_student(self):
return self.login_student(
return self.client.login_student(
email="[email protected]",
password="Password1",
)

def _login_indy_student(self):
return self.login_indy_student(
return self.client.login_indy_student(
email="[email protected]",
password="Password1",
)
Expand Down Expand Up @@ -220,3 +222,23 @@ def test_list__student(self):
user = self._login_student()

self._list_schools([user.student.class_field.teacher.school])

"""
General tests that apply to all actions.
"""

def test_all__requires_authentication(self):
"""
User must be authenticated to call any endpoint.
"""

assert IsAuthenticated in SchoolViewSet.permission_classes

def test_all__only_http_get(self):
"""
These model are read-only.
"""

assert [name.lower() for name in SchoolViewSet.http_method_names] == [
"get"
]
30 changes: 26 additions & 4 deletions codeforlife/user/tests/views/test_user.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import typing as t

from django.db.models.query import QuerySet
from rest_framework import status
from rest_framework.permissions import IsAuthenticated

from ....tests import APITestCase, APIClient
from ...serializers import UserSerializer
from ...views import UserViewSet

from ...models import User, School, Teacher, Student, Class, UserProfile


Expand Down Expand Up @@ -61,19 +63,19 @@ def setUp(self):
)

def _login_teacher(self):
return self.login_teacher(
return self.client.login_teacher(
email="[email protected]",
password="Password1",
)

def _login_student(self):
return self.login_student(
return self.client.login_student(
email="[email protected]",
password="Password1",
)

def _login_indy_student(self):
return self.login_indy_student(
return self.client.login_indy_student(
email="[email protected]",
password="Password1",
)
Expand Down Expand Up @@ -400,3 +402,23 @@ def test_list__indy_student(self):
user = self._login_indy_student()

self._list_users([user])

"""
General tests that apply to all actions.
"""

def test_all__requires_authentication(self):
"""
User must be authenticated to call any endpoint.
"""

assert IsAuthenticated in UserViewSet.permission_classes

def test_all__only_http_get(self):
"""
These model are read-only.
"""

assert [name.lower() for name in UserViewSet.http_method_names] == [
"get"
]

0 comments on commit 5e19077

Please sign in to comment.