feat: Add user view set

This commit is contained in:
aldrin 2021-10-22 12:38:25 +02:00
parent 330e3c89ea
commit 9449f7e665
8 changed files with 80 additions and 35 deletions

View file

@ -7,17 +7,23 @@ from userausfall.views import get_authenticated_user
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ["username"]
fields = ["id", "trust_bridge", "url"]
class TrustBridgeUserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ["id", "url", "username"]
# the UniqueValidator for username prevents successful validation for existing users
extra_kwargs = {"username": {"validators": []}}
class TrustBridgeSerializer(serializers.HyperlinkedModelSerializer):
trust_giver = UserSerializer()
trust_giver = TrustBridgeUserSerializer()
class Meta:
model = TrustBridge
fields = ["is_trusted", "trust_giver"]
fields = ["id", "is_trusted", "trust_giver", "url"]
read_only_fields = ["is_trusted"]
def create(self, validated_data):

View file

@ -1,2 +1,3 @@
from .auth import * # noqa: F401, F403
from .trust_bridges import * # noqa: F401, F403
from .auth import AuthenticationTestCase # noqa: F401, F403
from .trust_bridges import TrustBridgeTestCase # noqa: F401, F403
from .users import UserTestCase # noqa: F401, F403

View file

@ -2,32 +2,10 @@ from rest_framework import status
from userausfall.models import User
from userausfall.rest_api.tests.userausfall import UserausfallAPITestCase
from userausfall.rest_api.tests.users import UserMixin
class UserMixin:
user: User
password: str
username: str
def create_user(self):
self.username = f"test{User.objects.count()}"
self.password = "test12345"
self.user = User.objects.create_user(self.username, self.password)
return self.user
def ensure_user_exists(self):
if not hasattr(self, "user"):
self.create_user()
def authenticate_user(self):
self.ensure_user_exists()
if hasattr(self.client, "force_authentication"):
self.client.force_authenticate(user=self.user)
else:
self.client.force_login(user=self.user)
class UserTestCase(UserMixin, UserausfallAPITestCase):
class AuthenticationTestCase(UserMixin, UserausfallAPITestCase):
base_url = "/api/auth"
def test_signup(self):

View file

@ -2,7 +2,8 @@ from django.core import mail
from rest_framework import status
from userausfall.models import TrustBridge
from userausfall.rest_api.tests import UserausfallAPITestCase, UserMixin
from userausfall.rest_api.tests.userausfall import get_url, UserausfallAPITestCase
from userausfall.rest_api.tests.users import UserMixin
class TrustBridgeTestCase(UserMixin, UserausfallAPITestCase):
@ -37,13 +38,17 @@ class TrustBridgeTestCase(UserMixin, UserausfallAPITestCase):
self.authenticate_user()
response = self.client.get(self.get_api_url(url, pk=self.trust_bridge.pk))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
self.assertDictEqual(
response.data,
{
"id": self.trust_bridge.id,
"is_trusted": False,
"trust_giver": {
"id": self.trust_giver.id,
"username": self.trust_giver.username,
"url": get_url(response, "user", self.trust_giver),
},
"url": get_url(response, "trustbridge", self.trust_bridge),
},
)

View file

@ -1,6 +1,11 @@
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase
def get_url(response, basename, instance):
return reverse(f"{basename}-detail", [instance.pk], request=response.wsgi_request)
class UserausfallAPITestCase(APITestCase):
base_url = "/api"

View file

@ -0,0 +1,44 @@
from rest_framework import status
from userausfall.models import User
from userausfall.rest_api.tests.userausfall import get_url, UserausfallAPITestCase
class UserMixin:
user: User
password: str
username: str
def create_user(self):
self.username = f"test{User.objects.count()}"
self.password = "test12345"
self.user = User.objects.create_user(self.username, self.password)
return self.user
def ensure_user_exists(self):
if not hasattr(self, "user"):
self.create_user()
def authenticate_user(self):
self.ensure_user_exists()
if hasattr(self.client, "force_authentication"):
self.client.force_authenticate(user=self.user)
else:
self.client.force_login(user=self.user)
class UserTestCase(UserMixin, UserausfallAPITestCase):
def test_retrieve_user(self):
"""Retrieve the details of the current user."""
url = "/users/{pk}/"
self.authenticate_user()
response = self.client.get(self.get_api_url(url, pk=self.user.pk))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertDictEqual(
response.data,
{
"id": self.user.id,
"trust_bridge": None,
"url": get_url(response, "user", self.user),
},
)

View file

@ -2,10 +2,11 @@ from django.urls import include, path
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
from rest_framework import routers
from userausfall.rest_api.views import TrustBridgeViewSet
from userausfall.rest_api.views import TrustBridgeViewSet, UserViewSet
router = routers.SimpleRouter()
router.register(r"trust-bridges", TrustBridgeViewSet, "trust-bridge")
router.register(r"trust-bridges", TrustBridgeViewSet, "trustbridge")
router.register(r"users", UserViewSet, "user")
urlpatterns = [
path("", include(router.urls)),

View file

@ -4,7 +4,7 @@ from rest_framework.decorators import action
from rest_framework.response import Response
from userausfall.models import MissingUserAttribute, PasswordMismatch, TrustBridge, User
from userausfall.rest_api.serializers import TrustBridgeSerializer
from userausfall.rest_api.serializers import TrustBridgeSerializer, UserSerializer
from userausfall.views import get_authenticated_user
@ -18,7 +18,12 @@ class TrustBridgeViewSet(
return self.queryset.filter(trust_taker=get_authenticated_user(self.request))
class UserViewSet(viewsets.GenericViewSet):
class UserViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
serializer_class = UserSerializer
def get_queryset(self):
return User.objects.filter(pk=get_authenticated_user(self.request).pk)
@action(detail=True, methods=["post"])
def activate(self, request, pk=None):
"""Create the corresponding LDAP account."""