From f8b839075810dd4ed4dc12f66741e20f723fe40c Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Thu, 20 Nov 2025 10:10:49 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(backend)=20UserSerializer=20?= =?UTF-8?q?fallback=20strategy=20from=20UserLightSerializer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the UserLightSerializer we were fallbacking on a strategy to never have a full_name or short_name empty. We use the part of the email befire the @. We are doing the same thing now in the main UserSerializer. --- src/backend/core/api/serializers.py | 22 +++++++++--------- src/backend/core/tests/test_api_users.py | 29 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/backend/core/api/serializers.py b/src/backend/core/api/serializers.py index e9d49aac..47754efe 100644 --- a/src/backend/core/api/serializers.py +++ b/src/backend/core/api/serializers.py @@ -25,22 +25,13 @@ from core.services.converter_services import ( class UserSerializer(serializers.ModelSerializer): """Serialize users.""" - class Meta: - model = models.User - fields = ["id", "email", "full_name", "short_name", "language"] - read_only_fields = ["id", "email", "full_name", "short_name"] - - -class UserLightSerializer(UserSerializer): - """Serialize users with limited fields.""" - full_name = serializers.SerializerMethodField(read_only=True) short_name = serializers.SerializerMethodField(read_only=True) class Meta: model = models.User - fields = ["full_name", "short_name"] - read_only_fields = ["full_name", "short_name"] + fields = ["id", "email", "full_name", "short_name", "language"] + read_only_fields = ["id", "email", "full_name", "short_name"] def get_full_name(self, instance): """Return the full name of the user.""" @@ -59,6 +50,15 @@ class UserLightSerializer(UserSerializer): return instance.short_name +class UserLightSerializer(UserSerializer): + """Serialize users with limited fields.""" + + class Meta: + model = models.User + fields = ["full_name", "short_name"] + read_only_fields = ["full_name", "short_name"] + + class TemplateAccessSerializer(serializers.ModelSerializer): """Serialize template accesses.""" diff --git a/src/backend/core/tests/test_api_users.py b/src/backend/core/tests/test_api_users.py index 2968432a..a0a43552 100644 --- a/src/backend/core/tests/test_api_users.py +++ b/src/backend/core/tests/test_api_users.py @@ -278,6 +278,35 @@ def test_api_users_retrieve_me_authenticated(): } +def test_api_users_retrieve_me_authenticated_empty_name(): + """ + Authenticated users should be able to retrieve their own user via the "/users/me" path. + when no name is provided, the full name and short name should be the email without the domain. + """ + user = factories.UserFactory( + email="test_foo@test.com", + full_name=None, + short_name=None, + ) + + client = APIClient() + client.force_login(user) + + factories.UserFactory.create_batch(2) + response = client.get( + "/api/v1.0/users/me/", + ) + + assert response.status_code == 200 + assert response.json() == { + "id": str(user.id), + "email": "test_foo@test.com", + "full_name": "test_foo", + "language": user.language, + "short_name": "test_foo", + } + + def test_api_users_retrieve_anonymous(): """Anonymous users should not be allowed to retrieve a user.""" client = APIClient()