Skip to content

Commit f8b8390

Browse files
lunikaAntoLC
authored andcommitted
♻️(backend) UserSerializer fallback strategy from UserLightSerializer
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.
1 parent a1463e0 commit f8b8390

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/backend/core/api/serializers.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,13 @@
2525
class UserSerializer(serializers.ModelSerializer):
2626
"""Serialize users."""
2727

28-
class Meta:
29-
model = models.User
30-
fields = ["id", "email", "full_name", "short_name", "language"]
31-
read_only_fields = ["id", "email", "full_name", "short_name"]
32-
33-
34-
class UserLightSerializer(UserSerializer):
35-
"""Serialize users with limited fields."""
36-
3728
full_name = serializers.SerializerMethodField(read_only=True)
3829
short_name = serializers.SerializerMethodField(read_only=True)
3930

4031
class Meta:
4132
model = models.User
42-
fields = ["full_name", "short_name"]
43-
read_only_fields = ["full_name", "short_name"]
33+
fields = ["id", "email", "full_name", "short_name", "language"]
34+
read_only_fields = ["id", "email", "full_name", "short_name"]
4435

4536
def get_full_name(self, instance):
4637
"""Return the full name of the user."""
@@ -59,6 +50,15 @@ def get_short_name(self, instance):
5950
return instance.short_name
6051

6152

53+
class UserLightSerializer(UserSerializer):
54+
"""Serialize users with limited fields."""
55+
56+
class Meta:
57+
model = models.User
58+
fields = ["full_name", "short_name"]
59+
read_only_fields = ["full_name", "short_name"]
60+
61+
6262
class TemplateAccessSerializer(serializers.ModelSerializer):
6363
"""Serialize template accesses."""
6464

src/backend/core/tests/test_api_users.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,35 @@ def test_api_users_retrieve_me_authenticated():
278278
}
279279

280280

281+
def test_api_users_retrieve_me_authenticated_empty_name():
282+
"""
283+
Authenticated users should be able to retrieve their own user via the "/users/me" path.
284+
when no name is provided, the full name and short name should be the email without the domain.
285+
"""
286+
user = factories.UserFactory(
287+
288+
full_name=None,
289+
short_name=None,
290+
)
291+
292+
client = APIClient()
293+
client.force_login(user)
294+
295+
factories.UserFactory.create_batch(2)
296+
response = client.get(
297+
"/api/v1.0/users/me/",
298+
)
299+
300+
assert response.status_code == 200
301+
assert response.json() == {
302+
"id": str(user.id),
303+
"email": "[email protected]",
304+
"full_name": "test_foo",
305+
"language": user.language,
306+
"short_name": "test_foo",
307+
}
308+
309+
281310
def test_api_users_retrieve_anonymous():
282311
"""Anonymous users should not be allowed to retrieve a user."""
283312
client = APIClient()

0 commit comments

Comments
 (0)