From 2ebb0b7ee4f6e85a3511e961965e1fed150cd6b7 Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 21 Oct 2021 12:23:02 +0200 Subject: [PATCH] feat: Update user's trust bridge information --- .../migrations/0012_auto_20211021_0901.py | 28 +++++++++++++++++++ .../0013_trustbridge_trust_giver.py | 20 +++++++++++++ userausfall/rest_api/serializers.py | 1 + userausfall/rest_api/tests/trust_bridges.py | 12 ++++++++ userausfall/rest_api/views.py | 2 +- userausfall/tests.py | 1 + 6 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 userausfall/migrations/0012_auto_20211021_0901.py create mode 100644 userausfall/migrations/0013_trustbridge_trust_giver.py diff --git a/userausfall/migrations/0012_auto_20211021_0901.py b/userausfall/migrations/0012_auto_20211021_0901.py new file mode 100644 index 0000000..c462dd6 --- /dev/null +++ b/userausfall/migrations/0012_auto_20211021_0901.py @@ -0,0 +1,28 @@ +# 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 new file mode 100644 index 0000000..8b773ec --- /dev/null +++ b/userausfall/migrations/0013_trustbridge_trust_giver.py @@ -0,0 +1,20 @@ +# 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/rest_api/serializers.py b/userausfall/rest_api/serializers.py index 92d2f9e..3643390 100644 --- a/userausfall/rest_api/serializers.py +++ b/userausfall/rest_api/serializers.py @@ -7,3 +7,4 @@ class TrustBridgeSerializer(serializers.ModelSerializer): class Meta: model = TrustBridge fields = ["is_trusted", "trust_giver"] + read_only_fields = ["is_trusted"] diff --git a/userausfall/rest_api/tests/trust_bridges.py b/userausfall/rest_api/tests/trust_bridges.py index 24a7f9c..b2b9a43 100644 --- a/userausfall/rest_api/tests/trust_bridges.py +++ b/userausfall/rest_api/tests/trust_bridges.py @@ -20,3 +20,15 @@ class TrustBridgeTestCase(UserMixin, UserausfallAPITestCase): "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/views.py b/userausfall/rest_api/views.py index f6bf7da..1bed186 100644 --- a/userausfall/rest_api/views.py +++ b/userausfall/rest_api/views.py @@ -7,7 +7,7 @@ from userausfall.rest_api.serializers import TrustBridgeSerializer from userausfall.views import get_authenticated_user -class TrustBridgeView(generics.RetrieveAPIView): +class TrustBridgeView(generics.RetrieveUpdateAPIView): serializer_class = TrustBridgeSerializer def get_object(self): diff --git a/userausfall/tests.py b/userausfall/tests.py index ceb2f8b..d7d0276 100644 --- a/userausfall/tests.py +++ b/userausfall/tests.py @@ -10,6 +10,7 @@ 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"):