fix: resolve missing users in <UserCombobox /> (#21822)

Closes #21044

This pull-request addresses an issue we were seeing where we would
attempt to filter the `<UserCombobox />` by the users username or email
not their username (which the rendered options would show).

To highlight this I created three different users. Each with a username
that did not contain their `email` or `name` and attempted to filter.
Attempting to search for `John` wouldn't actually show the user as his
username was `x`, and infact whereas a subset of users might be returned
from the backend for having `john` in the `email` it would've been
filtered by the frontend for not being in the `name` field.

| Name | Username |
| --- | --- |
| `Jake` | `z` |  
| `Jeff` | `y` |
| `John` | `x` |

| Previously | Now |
| --- | --- |
| <img width="560" height="547" alt="OLD_USER_COMBOBOX"
src="https://github.com/user-attachments/assets/a0567264-0034-42ac-aba0-95b05c4f92dd"
/> | <img width="580" height="548" alt="NEW_USER_COMBOBOX"
src="https://github.com/user-attachments/assets/1aa0c942-d340-4b1c-8dde-b97879525bfb"
/> |
This commit is contained in:
Jake Howell
2026-02-03 00:13:41 +11:00
committed by GitHub
parent 3e369c0b04
commit 052bd114a4
9 changed files with 193 additions and 23 deletions
+102
View File
@@ -2110,6 +2110,108 @@ func TestGetUsers(t *testing.T) {
require.Equal(t, res.Users[0].Username, "bob")
require.Equal(t, res.Users[0].LoginType, codersdk.LoginTypeOIDC)
})
t.Run("NameFilter", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
first := coderdtest.CreateFirstUser(t, client)
ctx := testutil.Context(t, testutil.WaitLong)
// Create users with different display names
_, err := client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
Email: "alice@email.com",
Username: "alice",
Name: "Alice Smith",
OrganizationIDs: []uuid.UUID{first.OrganizationID},
UserLoginType: codersdk.LoginTypeNone,
})
require.NoError(t, err)
_, err = client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
Email: "bob@email.com",
Username: "bob",
Name: "Bob Johnson",
OrganizationIDs: []uuid.UUID{first.OrganizationID},
UserLoginType: codersdk.LoginTypeNone,
})
require.NoError(t, err)
_, err = client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
Email: "charlie@email.com",
Username: "charlie",
Name: "Charlie Smith",
OrganizationIDs: []uuid.UUID{first.OrganizationID},
UserLoginType: codersdk.LoginTypeNone,
})
require.NoError(t, err)
// Filter by name "Smith" should return Alice and Charlie
res, err := client.Users(ctx, codersdk.UsersRequest{
Name: "Smith",
})
require.NoError(t, err)
require.Len(t, res.Users, 2)
usernames := []string{res.Users[0].Username, res.Users[1].Username}
require.ElementsMatch(t, []string{"alice", "charlie"}, usernames)
// Filter by name "Alice" should return only Alice
res, err = client.Users(ctx, codersdk.UsersRequest{
Name: "Alice",
})
require.NoError(t, err)
require.Len(t, res.Users, 1)
require.Equal(t, "alice", res.Users[0].Username)
// Filter by name "Johnson" should return only Bob
res, err = client.Users(ctx, codersdk.UsersRequest{
Name: "Johnson",
})
require.NoError(t, err)
require.Len(t, res.Users, 1)
require.Equal(t, "bob", res.Users[0].Username)
// Filter by name that doesn't exist should return no users
res, err = client.Users(ctx, codersdk.UsersRequest{
Name: "Nonexistent",
})
require.NoError(t, err)
require.Len(t, res.Users, 0)
})
t.Run("NameFilterWithSearchFilter", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
first := coderdtest.CreateFirstUser(t, client)
ctx := testutil.Context(t, testutil.WaitLong)
// Create users with different display names and usernames
_, err := client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
Email: "alice@email.com",
Username: "alice",
Name: "Alice Developer",
OrganizationIDs: []uuid.UUID{first.OrganizationID},
UserLoginType: codersdk.LoginTypeNone,
})
require.NoError(t, err)
_, err = client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
Email: "bob@email.com",
Username: "bobdev",
Name: "Bob Developer",
OrganizationIDs: []uuid.UUID{first.OrganizationID},
UserLoginType: codersdk.LoginTypeNone,
})
require.NoError(t, err)
// Filter by name "Developer" and search "alice" should return only Alice
// because name matches both but search matches only alice's username
res, err := client.Users(ctx, codersdk.UsersRequest{
SearchQuery: "name:Developer search:alice",
})
require.NoError(t, err)
require.Len(t, res.Users, 1)
require.Equal(t, "alice", res.Users[0].Username)
})
}
func TestGetUsersPagination(t *testing.T) {