Skip to content

Commit

Permalink
chore: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Mar 6, 2024
1 parent 53447e2 commit 46697ae
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
35 changes: 14 additions & 21 deletions backend/authentication/serializers.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
from typing import Tuple
from django.contrib.auth.models import update_last_login

from authentication.cas.client import client
from authentication.models import User
from authentication.signals import user_created, user_login
from django.contrib.auth import login
from django.contrib.auth.models import update_last_login
from rest_framework.serializers import (
CharField,
EmailField,
HyperlinkedRelatedField,
ModelSerializer,
ValidationError,
Serializer,
HyperlinkedIdentityField,
HyperlinkedRelatedField,
ValidationError,
)
from rest_framework_simplejwt.tokens import RefreshToken, AccessToken
from rest_framework_simplejwt.settings import api_settings
from authentication.signals import user_created, user_login
from authentication.models import User, Faculty
from authentication.cas.client import client
from rest_framework_simplejwt.tokens import AccessToken, RefreshToken


class CASTokenObtainSerializer(Serializer):
"""Serializer for CAS ticket validation
This serializer takes the CAS ticket and tries to validate it.
Upon successful validation, create a new user if it doesn't exist.
"""

ticket = CharField(required=True, min_length=49, max_length=49)

def validate(self, data):
Expand All @@ -40,23 +41,15 @@ def validate(self, data):
if "request" in self.context:
login(self.context["request"], user)

user_login.send(
sender=self, user=user
)
user_login.send(sender=self, user=user)

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

# Return access tokens for the now logged-in user.
return {
"access": str(
AccessToken.for_user(user)
),
"refresh": str(
RefreshToken.for_user(user)
),
"access": str(AccessToken.for_user(user)),
"refresh": str(RefreshToken.for_user(user)),
}

def _validate_ticket(self, ticket: str) -> dict:
Expand Down Expand Up @@ -102,7 +95,7 @@ class UserSerializer(ModelSerializer):
many=True, read_only=True, view_name="faculty-detail"
)

notifications = HyperlinkedIdentityField(
notifications = HyperlinkedRelatedField(
view_name="notification-detail",
read_only=True,
)
Expand Down
2 changes: 1 addition & 1 deletion backend/notifications/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
from notifications.views import NotificationView

urlpatterns = [
path("<str:user_id>/", NotificationView.as_view()),
path("<str:user_id>/", NotificationView.as_view(), name="notification-detail"),
]
23 changes: 16 additions & 7 deletions backend/ypovoli/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.urls import path, include
from rest_framework import permissions
from drf_yasg.views import get_schema_view

from django.urls import include, path
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import permissions

schema_view = get_schema_view(
openapi.Info(
title="Ypovoli API",
default_version="v1",
),
public=True,
permission_classes=[permissions.AllowAny,],
permission_classes=[
permissions.AllowAny,
],
)


Expand All @@ -34,8 +37,14 @@
path("", include("api.urls")),
# Authentication endpoints.
path("auth/", include("authentication.urls")),
path("notifications/", include("notifications.urls")),
path("notifications/", include("notifications.urls"), name="notifications"),
# Swagger documentation.
path("swagger/", schema_view.with_ui("swagger", cache_timeout=0), name="schema-swagger-ui"),
path("swagger<format>/", schema_view.without_ui(cache_timeout=0), name="schema-json"),
path(
"swagger/",
schema_view.with_ui("swagger", cache_timeout=0),
name="schema-swagger-ui",
),
path(
"swagger<format>/", schema_view.without_ui(cache_timeout=0), name="schema-json"
),
]

0 comments on commit 46697ae

Please sign in to comment.