Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions dandiapi/api/tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,30 @@ def test_user_search_extra_data(api_client):
assert api_client.get('/api/users/search/?', {'username': 'odysseus'}).data == []


@pytest.mark.django_db
def test_user_search_extra_data_no_name(api_client):
"""Test that a `null` name field in extra_data doesn't result in a `null` in the output."""
user = UserFactory.create(social_account__extra_data__name=None)

api_client.force_authenticate(user=user)

resp = api_client.get('/api/users/search/?', {'username': user.username})
assert len(resp.data) == 1
assert resp.data[0]['name'] == user.get_full_name()


@pytest.mark.django_db
def test_user_search_extra_data_no_username(api_client):
"""Test that a `null` username field in extra_data doesn't result in a `null` in the output."""
user = UserFactory.create(social_account__extra_data__login=None)

api_client.force_authenticate(user=user)

resp = api_client.get('/api/users/search/?', {'username': user.username})
assert len(resp.data) == 1
assert resp.data[0]['username'] == user.username


@pytest.mark.parametrize(
('status', 'expected_status_code', 'expected_search_results_value'),
[
Expand Down
6 changes: 3 additions & 3 deletions dandiapi/api/views/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ def social_account_to_dict(social_account: SocialAccount):
def serialize_user(user: User):
"""Serialize a user that's been annotated with a `social_account_data` field."""
username = user.username
name = f'{user.first_name} {user.last_name}'.strip()
name = user.get_full_name()
Copy link
Member Author

@jjnesbitt jjnesbitt Dec 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function on the User model is exactly the same as what we're doing here.


# Prefer social account info if present
if user.social_account_data is not None:
username = user.social_account_data.get('login', username)
name = user.social_account_data.get('name', name)
username = user.social_account_data.get('login') or username
name = user.social_account_data.get('name') or name

return {
'admin': user.is_superuser,
Expand Down
Loading