Compare commits

..

No commits in common. "2ebb0b7ee4f6e85a3511e961965e1fed150cd6b7" and "b4c686bfaafe3283ad6c6e7b46d04c0970c3a3d2" have entirely different histories.

10 changed files with 25 additions and 115 deletions

View file

@ -1,28 +0,0 @@
# Generated by Django 3.2.8 on 2021-10-21 09:01
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('userausfall', '0011_trustbridge'),
]
operations = [
migrations.RenameField(
model_name='trustbridge',
old_name='user',
new_name='trust_taker',
),
migrations.AlterField(
model_name='trustbridge',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='user',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
]

View file

@ -1,20 +0,0 @@
# Generated by Django 3.2.8 on 2021-10-21 09:04
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('userausfall', '0012_auto_20211021_0901'),
]
operations = [
migrations.AddField(
model_name='trustbridge',
name='trust_giver',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL),
),
]

View file

@ -97,14 +97,7 @@ class User(PermissionsMixin, AbstractBaseUser):
raise PasswordMismatch("The given password does not match the user's password.") raise PasswordMismatch("The given password does not match the user's password.")
return ldap.create_account(self.username, raw_password) return ldap.create_account(self.username, raw_password)
def get_or_create_trust_bridge(self):
try:
return self.trust_bridge
except TrustBridge.DoesNotExist:
return TrustBridge.objects.create(trust_taker=self)
class TrustBridge(models.Model): class TrustBridge(models.Model):
user = models.OneToOneField("User", on_delete=models.CASCADE, related_name="trust_bridge")
is_trusted = models.BooleanField(default=False) is_trusted = models.BooleanField(default=False)
trust_giver = models.ForeignKey("User", on_delete=models.SET_NULL, null=True)
trust_taker = models.OneToOneField("User", on_delete=models.CASCADE, related_name="trust_bridge")

View file

@ -1,10 +1,30 @@
from rest_framework import serializers from rest_framework import serializers
from userausfall.models import TrustBridge from userausfall.models import TrustBridge, User
class TrustBridgeSerializer(serializers.ModelSerializer): class TrustBridgeSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = TrustBridge model = TrustBridge
fields = ["is_trusted", "trust_giver"] fields = ["is_trusted"]
read_only_fields = ["is_trusted"]
class ActivateUserSerializer(serializers.Serializer):
password = serializers.CharField()
class RetrieveUserSerializer(serializers.ModelSerializer):
trust_bridge = TrustBridgeSerializer(required=False, read_only=True)
class Meta:
model = User
fields = ["pk", "username", "trust_bridge"]
class CreateUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ("username", "password")
def create(self, validated_data):
return User.objects.create_user(**validated_data)

View file

@ -1,2 +1 @@
from .auth import * # noqa: F401, F403 from .auth import * # noqa: F401, F403
from .trust_bridges import * # noqa: F401, F403

View file

@ -1,34 +0,0 @@
from rest_framework import status
from userausfall.rest_api.tests import UserausfallAPITestCase
from userausfall.tests import UserMixin
class TrustBridgeTestCase(UserMixin, UserausfallAPITestCase):
def test_retrieve_trust_bridge(self):
"""
Retrieve the trust bridge information of a user without an ldap account.
"""
url = "/trust-bridge/"
self.authenticate_user()
response = self.client.get(self.get_api_url(url))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.data,
{
"is_trusted": False,
"trust_giver": None,
},
)
def test_update_trust_bridge(self):
"""
Update the trust giver of the user's trust bridge.
"""
url = "/trust-bridge/"
other_user = self.create_user()
self.create_user()
self.authenticate_user()
response = self.client.put(self.get_api_url(url), {"trust_giver": other_user.pk})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(self.user.trust_bridge.trust_giver, other_user)

View file

@ -2,8 +2,6 @@ from django.urls import include, path
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
from rest_framework import routers from rest_framework import routers
from userausfall.rest_api.views import TrustBridgeView
router = routers.SimpleRouter() router = routers.SimpleRouter()
urlpatterns = [ urlpatterns = [
@ -12,5 +10,4 @@ urlpatterns = [
path("schema/", SpectacularAPIView.as_view(), name="schema"), path("schema/", SpectacularAPIView.as_view(), name="schema"),
path("schema/swagger-ui/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger-ui"), path("schema/swagger-ui/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger-ui"),
path("schema/redoc/", SpectacularRedocView.as_view(url_name="schema"), name="redoc"), path("schema/redoc/", SpectacularRedocView.as_view(url_name="schema"), name="redoc"),
path("trust-bridge/", TrustBridgeView.as_view()),
] ]

View file

@ -1,17 +1,8 @@
from rest_framework import generics, status, viewsets from rest_framework import status, viewsets
from rest_framework.decorators import action from rest_framework.decorators import action
from rest_framework.response import Response from rest_framework.response import Response
from userausfall.models import MissingUserAttribute, PasswordMismatch, User from userausfall.models import MissingUserAttribute, PasswordMismatch, User
from userausfall.rest_api.serializers import TrustBridgeSerializer
from userausfall.views import get_authenticated_user
class TrustBridgeView(generics.RetrieveUpdateAPIView):
serializer_class = TrustBridgeSerializer
def get_object(self):
return get_authenticated_user(self.request).get_or_create_trust_bridge()
class UserViewSet(viewsets.GenericViewSet): class UserViewSet(viewsets.GenericViewSet):

View file

@ -10,7 +10,6 @@ class UserMixin:
self.username = f"test{User.objects.count()}" self.username = f"test{User.objects.count()}"
self.password = "test12345" self.password = "test12345"
self.user = User.objects.create_user(self.username, self.password) self.user = User.objects.create_user(self.username, self.password)
return self.user
def ensure_user_exists(self): def ensure_user_exists(self):
if not hasattr(self, "user"): if not hasattr(self, "user"):

View file

@ -1,7 +0,0 @@
from userausfall.models import User
def get_authenticated_user(request) -> User:
if request is not None and request.user.is_authenticated:
return request.user
return None