From 878ff64fade4c27d0a0d658899f2901acff497e8 Mon Sep 17 00:00:00 2001 From: Paul Konstantin Gerke Date: Tue, 30 Aug 2022 22:49:54 +0200 Subject: [PATCH] Add tests, add username to api/profile endpoint --- api/serializers.py | 5 ++++ api/tests.py | 29 +++++++++++++++++++ .../babybuddy/user_settings_form.html | 2 +- babybuddy/tests/tests_views.py | 5 ++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/api/serializers.py b/api/serializers.py index a6981af1..102f14d5 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -273,6 +273,7 @@ class WeightSerializer(CoreModelSerializer, TaggableSerializer): class ProfileSerializer(serializers.ModelSerializer): api_key = serializers.SerializerMethodField("get_api_key") + username = serializers.SerializerMethodField("get_username") first_name = serializers.SerializerMethodField("get_first_name") last_name = serializers.SerializerMethodField("get_last_name") email = serializers.SerializerMethodField("get_email") @@ -281,6 +282,9 @@ class ProfileSerializer(serializers.ModelSerializer): def get_api_key(self, value): return self.instance.api_key().key + def get_username(self, value): + return self.instance.user.username + def get_first_name(self, value): return self.instance.user.first_name @@ -297,6 +301,7 @@ class ProfileSerializer(serializers.ModelSerializer): model = babybuddy_models.Settings fields = ( "user", + "username", "first_name", "last_name", "email", diff --git a/api/tests.py b/api/tests.py index c8ee0e4b..65d60d1a 100644 --- a/api/tests.py +++ b/api/tests.py @@ -874,3 +874,32 @@ class WeightAPITestCase(TestBase.BabyBuddyAPITestCaseBase): ) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, entry) + + +class TestProfileAPITestCase(APITestCase): + endpoint = reverse("api:profile") + + def setUp(self): + self.client.login(username="admin", password="admin") + + def test_get(self): + response = self.client.get(self.endpoint) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertDictContainsSubset( + { + "user": 1, + "username": "admin", + "first_name": "", + "last_name": "", + "email": "", + "staff": True, + "language": "en-US", + "timezone": "UTC", + }, + response.data, + ) + + # Test that api_key is in the mix and "some long string" + self.assertIn("api_key", response.data) + self.assertTrue(isinstance(response.data["api_key"], str)) + self.assertGreater(len(response.data["api_key"]), 30) diff --git a/babybuddy/templates/babybuddy/user_settings_form.html b/babybuddy/templates/babybuddy/user_settings_form.html index d91091d0..24e80c0c 100644 --- a/babybuddy/templates/babybuddy/user_settings_form.html +++ b/babybuddy/templates/babybuddy/user_settings_form.html @@ -87,7 +87,7 @@
-
+
{% qr_from_text qr_code_data size="s" %}
diff --git a/babybuddy/tests/tests_views.py b/babybuddy/tests/tests_views.py index 90c9cb4b..25ac7a7b 100644 --- a/babybuddy/tests/tests_views.py +++ b/babybuddy/tests/tests_views.py @@ -52,6 +52,11 @@ class ViewsTestCase(TestCase): page = self.c.get("/user/settings/") self.assertEqual(page.status_code, 200) + self.assertRegex( + page.content.decode(), + r""".*
]* data-qr-code-content="[^"]+"[^>]*>.*""", + ) + def test_user_views(self): # Staff setting is required to access user management. page = self.c.get("/users/")