Add tests, add username to api/profile endpoint

This commit is contained in:
Paul Konstantin Gerke 2022-08-30 22:49:54 +02:00
parent 7424155ba4
commit 878ff64fad
4 changed files with 40 additions and 1 deletions

View File

@ -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",

View File

@ -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)

View File

@ -87,7 +87,7 @@
<div class="form-group row">
<label class="col-sm-2 col-form-label">{% trans "Login QR code" %}</label>
<div class="col-sm-10">
<div style="display:inline-block;background-color:white;">
<div style="display:inline-block;background-color:white;" data-qr-code-content="{{ qr_code_data }}">
{% qr_from_text qr_code_data size="s" %}
</div>
</div>

View File

@ -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""".*<div [^>]* data-qr-code-content="[^"]+"[^>]*>.*""",
)
def test_user_views(self):
# Staff setting is required to access user management.
page = self.c.get("/users/")