diff --git a/userausfall/migrations/0012_auto_20211021_0901.py b/userausfall/migrations/0012_auto_20211021_0901.py deleted file mode 100644 index c462dd6..0000000 --- a/userausfall/migrations/0012_auto_20211021_0901.py +++ /dev/null @@ -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'), - ), - ] diff --git a/userausfall/migrations/0013_trustbridge_trust_giver.py b/userausfall/migrations/0013_trustbridge_trust_giver.py deleted file mode 100644 index 8b773ec..0000000 --- a/userausfall/migrations/0013_trustbridge_trust_giver.py +++ /dev/null @@ -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), - ), - ] diff --git a/userausfall/models.py b/userausfall/models.py index dbeaca5..7331b19 100644 --- a/userausfall/models.py +++ b/userausfall/models.py @@ -97,14 +97,7 @@ class User(PermissionsMixin, AbstractBaseUser): raise PasswordMismatch("The given password does not match the user's 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): + user = models.OneToOneField("User", on_delete=models.CASCADE, related_name="trust_bridge") 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") diff --git a/userausfall/rest_api/serializers.py b/userausfall/rest_api/serializers.py index 3643390..a9a6479 100644 --- a/userausfall/rest_api/serializers.py +++ b/userausfall/rest_api/serializers.py @@ -1,10 +1,30 @@ from rest_framework import serializers -from userausfall.models import TrustBridge +from userausfall.models import TrustBridge, User class TrustBridgeSerializer(serializers.ModelSerializer): class Meta: model = TrustBridge - fields = ["is_trusted", "trust_giver"] - read_only_fields = ["is_trusted"] + 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) diff --git a/userausfall/rest_api/tests/__init__.py b/userausfall/rest_api/tests/__init__.py index f7cf7fe..78ced3d 100644 --- a/userausfall/rest_api/tests/__init__.py +++ b/userausfall/rest_api/tests/__init__.py @@ -1,2 +1 @@ from .auth import * # noqa: F401, F403 -from .trust_bridges import * # noqa: F401, F403 diff --git a/userausfall/rest_api/tests/trust_bridges.py b/userausfall/rest_api/tests/trust_bridges.py deleted file mode 100644 index b2b9a43..0000000 --- a/userausfall/rest_api/tests/trust_bridges.py +++ /dev/null @@ -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) diff --git a/userausfall/rest_api/urls.py b/userausfall/rest_api/urls.py index 0d476a2..a3a4ed8 100644 --- a/userausfall/rest_api/urls.py +++ b/userausfall/rest_api/urls.py @@ -2,8 +2,6 @@ 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 TrustBridgeView - router = routers.SimpleRouter() urlpatterns = [ @@ -12,5 +10,4 @@ urlpatterns = [ path("schema/", SpectacularAPIView.as_view(), name="schema"), 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("trust-bridge/", TrustBridgeView.as_view()), ] diff --git a/userausfall/rest_api/views.py b/userausfall/rest_api/views.py index 1bed186..b64d9eb 100644 --- a/userausfall/rest_api/views.py +++ b/userausfall/rest_api/views.py @@ -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.response import Response 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): diff --git a/userausfall/tests.py b/userausfall/tests.py index d7d0276..ceb2f8b 100644 --- a/userausfall/tests.py +++ b/userausfall/tests.py @@ -10,7 +10,6 @@ class UserMixin: 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"): diff --git a/userausfall/views.py b/userausfall/views.py deleted file mode 100644 index 4552667..0000000 --- a/userausfall/views.py +++ /dev/null @@ -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