Skip to content

Commit

Permalink
test: implement backend view that logs in as test User #400
Browse files Browse the repository at this point in the history
  • Loading branch information
bsilkyn committed May 13, 2024
1 parent c46cd7c commit d5b1d09
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
5 changes: 3 additions & 2 deletions backend/authentication/urls.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView, TokenVerifyView
from authentication.views import CASViewSet
from authentication.views import CASViewSet, TestUser

router = DefaultRouter()
router.register("cas", CASViewSet, "cas")
router.register("test-user", TestUser, "test-user")

urlpatterns = [
# AUTH endpoints.
path("", include(router.urls)),
# TOKEN endpoints.
path("token/", TokenObtainPairView.as_view(), name="token"),
path("token/refresh/", TokenRefreshView.as_view(), name="token-refresh"),
path("token/verify/", TokenVerifyView.as_view(), name="token-verify")
path("token/verify/", TokenVerifyView.as_view(), name="token-verify"),
]
42 changes: 41 additions & 1 deletion backend/authentication/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from authentication.cas.client import client
from authentication.permissions import IsDebug
from authentication.serializers import CASTokenObtainSerializer, UserSerializer
from django.contrib.auth import logout
from authentication.models import User
from authentication.signals import user_created
from django.contrib.auth import logout, login
from django.shortcuts import redirect
from rest_framework.decorators import action
from rest_framework.exceptions import AuthenticationFailed
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet
from rest_framework.status import HTTP_301_MOVED_PERMANENTLY
from ypovoli import settings


Expand Down Expand Up @@ -59,3 +62,40 @@ def echo(self, request: Request) -> Response:
return Response(token_serializer.validated_data)

raise AuthenticationFailed(token_serializer.errors)


data = {
"id": "1234",
"username": "test",
"email": "test@test",
"first_name": "test",
"last_name": "test",
}

attributes = dict(data)
attributes["ugentStudentID"] = "1234"


def create_user(self, request) -> Response:
user, created = User.objects.get_or_create(id=data["id"], defaults=data)

if created:
user_created.send(sender=self, attributes=attributes, user=user)

login(request, user)

return Response(status=HTTP_301_MOVED_PERMANENTLY, headers={"Location": "/"})


class TestUser(ViewSet):
permission_classes = [IsDebug]

@action(detail=False, methods=['GET'], permission_classes=[IsDebug], url_path='admin')
def login_admin(self, request, *__) -> Response:
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:
data["is_staff"] = False
return create_user(self, request)
1 change: 1 addition & 0 deletions frontend/src/router/guards/authentication.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export async function AuthenticationGuard(to: RouteLocationNormalized): Promise<
const { refreshUser } = useAuthStore();
const { intent, isAuthenticated } = storeToRefs(useAuthStore());

console.log(to.name as string);
if (!isAuthenticated.value && !['login', 'verify'].includes(to.name as string)) {
try {
await refreshUser();
Expand Down

0 comments on commit d5b1d09

Please sign in to comment.