From 5486ef262710eaed624778ee5c4116ec0bc6d9c8 Mon Sep 17 00:00:00 2001 From: Stefan Benz <46600784+stebenz@users.noreply.github.com> Date: Thu, 20 Mar 2025 08:05:01 +0100 Subject: [PATCH] test: correct sorting of scim list users integration tests (#9568) # Which Problems Are Solved SCIM integration test failed sometimes, as ListUsers with usernames-sort was not reliable if the asserted list is not sorted as well. # How the Problems Are Solved Sort the list of results in the sorted integration tests. # Additional Changes None # Additional Context Relates to https://github.com/zitadel/zitadel/actions/runs/13922326003/job/38960759621 --- .../scim/integration_test/users_list_test.go | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/internal/api/scim/integration_test/users_list_test.go b/internal/api/scim/integration_test/users_list_test.go index 7945d2039d..47030d37e3 100644 --- a/internal/api/scim/integration_test/users_list_test.go +++ b/internal/api/scim/integration_test/users_list_test.go @@ -6,6 +6,7 @@ import ( "context" "fmt" "net/http" + "slices" "strings" "testing" "time" @@ -141,12 +142,15 @@ func TestListUser(t *testing.T) { SortBy: gu.Ptr("username"), }, assert: func(t assert.TestingT, resp *scim.ListResponse[*resources.ScimUser]) { + // sort the created users with usernames instead of creation date + sortedResources := sortScimUserByUsername(resp.Resources) + assert.Equal(t, 2, resp.ItemsPerPage) assert.Equal(t, totalCountOfHumanUsers, resp.TotalResults) assert.Equal(t, 5, resp.StartIndex) - assert.Len(t, resp.Resources, 2) - assert.True(t, strings.HasPrefix(resp.Resources[0].UserName, "scim-username-1: ")) - assert.True(t, strings.HasPrefix(resp.Resources[1].UserName, "scim-username-2: ")) + assert.Len(t, sortedResources, 2) + assert.True(t, strings.HasPrefix(sortedResources[0].UserName, "scim-username-1: ")) + assert.True(t, strings.HasPrefix(sortedResources[1].UserName, "scim-username-2: ")) }, }, { @@ -174,11 +178,14 @@ func TestListUser(t *testing.T) { Filter: gu.Ptr(`emails sw "scim-email-1" and emails ew "@example.com"`), }, assert: func(t assert.TestingT, resp *scim.ListResponse[*resources.ScimUser]) { + // sort the created users with usernames instead of creation date + sortedResources := sortScimUserByUsername(resp.Resources) + assert.Equal(t, 5, resp.ItemsPerPage) assert.Equal(t, 2, resp.TotalResults) assert.Equal(t, 1, resp.StartIndex) - assert.Len(t, resp.Resources, 2) - for _, resource := range resp.Resources { + assert.Len(t, sortedResources, 2) + for _, resource := range sortedResources { assert.True(t, strings.HasPrefix(resource.UserName, "scim-username-1")) assert.Len(t, resource.Emails, 1) assert.True(t, strings.HasPrefix(resource.Emails[0].Value, "scim-email-1")) @@ -198,11 +205,14 @@ func TestListUser(t *testing.T) { SendAsPost: true, }, assert: func(t assert.TestingT, resp *scim.ListResponse[*resources.ScimUser]) { + // sort the created users with usernames instead of creation date + sortedResources := sortScimUserByUsername(resp.Resources) + assert.Equal(t, 5, resp.ItemsPerPage) assert.Equal(t, 2, resp.TotalResults) assert.Equal(t, 1, resp.StartIndex) - assert.Len(t, resp.Resources, 2) - for _, resource := range resp.Resources { + assert.Len(t, sortedResources, 2) + for _, resource := range sortedResources { assert.True(t, strings.HasPrefix(resource.UserName, "scim-username-1")) assert.Len(t, resource.Emails, 1) assert.True(t, strings.HasPrefix(resource.Emails[0].Value, "scim-email-1")) @@ -433,6 +443,14 @@ func TestListUser(t *testing.T) { } } +func sortScimUserByUsername(users []*resources.ScimUser) []*resources.ScimUser { + sortedResources := users + slices.SortFunc(sortedResources, func(a, b *resources.ScimUser) int { + return strings.Compare(a.UserName, b.UserName) + }) + return sortedResources +} + func createUsers(t *testing.T, ctx context.Context, orgID string) []string { count := totalCountOfHumanUsers - 1 // zitadel admin is always created by default createdUserIDs := make([]string, 0, count)