Skip to content

Commit

Permalink
chore: document test login shortcut more #241
Browse files Browse the repository at this point in the history
  • Loading branch information
bsilkyn committed May 13, 2024
1 parent e70c1f9 commit d741051
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions backend/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,35 @@ def echo(self, request: Request) -> Response:


def create_user(self, request) -> Response:
"""General function to create a user, log them in and which returns an empty html page"""
# log in user, or retrieve if they already exist
user, created = User.objects.get_or_create(id=data["id"], defaults=data)

# if it has just been created, send the signal to user_created Signal(), to also activate it as a student
if created:
user_created.send(sender=self, attributes=attributes, user=user)

# login the user
login(request, user)

# return Response with empty html page
return Response('<!DOCTYPE html><html></html>',
status=HTTP_200_OK, headers={"Location": "/"}, content_type="text/html")


class TestUser(ViewSet):
"""View meant to be able to log in quickly for tests on server in debug mode"""

permission_classes = [IsDebug]

@action(detail=False, methods=['GET'], permission_classes=[IsDebug], url_path='admin')
def login_admin(self, request, *__) -> Response:
"""This endpoint lets you log in an admin"""
data["is_staff"] = True
return create_user(self, request)

@action(detail=False, methods=['GET'], permission_classes=[IsDebug], url_path='student')
def login_student(self, request, *__) -> Response:
"""This endpoint lets you log in as a student who's not an admin"""
data["is_staff"] = False
return create_user(self, request)

0 comments on commit d741051

Please sign in to comment.