Skip to content

Commit

Permalink
fix: login as user
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Feb 16, 2024
1 parent 91404b8 commit 6987ffe
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
82 changes: 75 additions & 7 deletions codeforlife/tests/model_view_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
SchoolTeacherUser,
StudentUser,
TeacherUser,
TypedUser,
User,
)
from ..views import ModelViewSet
Expand Down Expand Up @@ -609,76 +610,143 @@ def login(self, **credentials):
"""
return self._login_user_type(User, **credentials)

def login_teacher(self, email: str, password: str):
def login_teacher(self, email: str, password: str = "password"):
"""Log in a user and assert they are a teacher.
Args:
email: The user's email address.
password: The user's password.
Returns:
The teacher-user.
"""
return self._login_user_type(
TeacherUser, email=email, password=password
)

def login_school_teacher(self, email: str, password: str):
def login_school_teacher(self, email: str, password: str = "password"):
"""Log in a user and assert they are a school-teacher.
Args:
email: The user's email address.
password: The user's password.
Returns:
The school-teacher-user.
"""
return self._login_user_type(
SchoolTeacherUser, email=email, password=password
)

def login_admin_school_teacher(self, email: str, password: str):
def login_admin_school_teacher(
self, email: str, password: str = "password"
):
"""Log in a user and assert they are an admin-school-teacher.
Args:
email: The user's email address.
password: The user's password.
Returns:
The admin-school-teacher-user.
"""
return self._login_user_type(
AdminSchoolTeacherUser, email=email, password=password
)

def login_non_admin_school_teacher(self, email: str, password: str):
def login_non_admin_school_teacher(
self, email: str, password: str = "password"
):
"""Log in a user and assert they are a non-admin-school-teacher.
Args:
email: The user's email address.
password: The user's password.
Returns:
The non-admin-school-teacher-user.
"""
return self._login_user_type(
NonAdminSchoolTeacherUser, email=email, password=password
)

def login_non_school_teacher(self, email: str, password: str):
def login_non_school_teacher(self, email: str, password: str = "password"):
"""Log in a user and assert they are a non-school-teacher.
Args:
email: The user's email address.
password: The user's password.
Returns:
The non-school-teacher-user.
"""
return self._login_user_type(
NonSchoolTeacherUser, email=email, password=password
)

def login_student(self, username: str, password: str, class_id: str):
def login_student(
self, class_id: str, username: str, password: str = "password"
):
"""Log in a user and assert they are a student.
Args:
class_id: The ID of the class the student belongs to.
username: The user's username.
password: The user's password.
Returns:
The student-user.
"""
return self._login_user_type(
StudentUser, username=username, password=password, class_id=class_id
)

def login_indy(self, email: str, password: str):
def login_indy(self, email: str, password: str = "password"):
"""Log in a user and assert they are an independent.
Args:
email: The user's email address.
password: The user's password.
Returns:
The independent-user.
"""
return self._login_user_type(
IndependentUser, email=email, password=password
)

def login_as(self, user: TypedUser, password: str = "password"):
"""Log in as a user. The user instance needs to be a user proxy in order
to know which credentials are required.
Args:
user: The user to log in as.
password: The user's password.
"""

if isinstance(user, TeacherUser):
auth_user = self.login_teacher(user.email, password)
elif isinstance(user, SchoolTeacherUser):
auth_user = self.login_school_teacher(user.email, password)
elif isinstance(user, AdminSchoolTeacherUser):
auth_user = self.login_admin_school_teacher(user.email, password)
elif isinstance(user, NonAdminSchoolTeacherUser):
auth_user = self.login_non_admin_school_teacher(
user.email, password
)
elif isinstance(user, NonSchoolTeacherUser):
auth_user = self.login_non_school_teacher(user.email, password)
elif isinstance(user, StudentUser):
auth_user = self.login_student(
user.student.class_field.access_code,
user.username,
password,
)
elif isinstance(user, IndependentUser):
auth_user = self.login_indy(user.email, password)

assert user == auth_user


class ModelViewSetTestCase(APITestCase, t.Generic[AnyModel]):
"""Base for all model view set test cases."""
Expand Down
2 changes: 2 additions & 0 deletions codeforlife/user/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
)
from .user import ( # TODO: remove UserProfile
AdminSchoolTeacherUser,
AnyTypedUser,
AnyUser,
IndependentUser,
NonAdminSchoolTeacherUser,
NonSchoolTeacherUser,
SchoolTeacherUser,
StudentUser,
TeacherUser,
TypedUser,
User,
UserProfile,
)
13 changes: 13 additions & 0 deletions codeforlife/user/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,16 @@ class Meta(TypedModelMeta):
objects: IndependentUserManager = ( # type: ignore[misc]
IndependentUserManager() # type: ignore[assignment]
)


TypedUser = t.Union[
TeacherUser,
SchoolTeacherUser,
AdminSchoolTeacherUser,
NonAdminSchoolTeacherUser,
NonSchoolTeacherUser,
StudentUser,
IndependentUser,
]

AnyTypedUser = t.TypeVar("AnyTypedUser", bound=TypedUser)

0 comments on commit 6987ffe

Please sign in to comment.