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
This commit is contained in:
Stefan Benz
2025-03-20 08:05:01 +01:00
committed by GitHub
parent 5ca76af779
commit 5486ef2627

View File

@@ -6,6 +6,7 @@ import (
"context" "context"
"fmt" "fmt"
"net/http" "net/http"
"slices"
"strings" "strings"
"testing" "testing"
"time" "time"
@@ -141,12 +142,15 @@ func TestListUser(t *testing.T) {
SortBy: gu.Ptr("username"), SortBy: gu.Ptr("username"),
}, },
assert: func(t assert.TestingT, resp *scim.ListResponse[*resources.ScimUser]) { 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, 2, resp.ItemsPerPage)
assert.Equal(t, totalCountOfHumanUsers, resp.TotalResults) assert.Equal(t, totalCountOfHumanUsers, resp.TotalResults)
assert.Equal(t, 5, resp.StartIndex) assert.Equal(t, 5, resp.StartIndex)
assert.Len(t, resp.Resources, 2) assert.Len(t, sortedResources, 2)
assert.True(t, strings.HasPrefix(resp.Resources[0].UserName, "scim-username-1: ")) assert.True(t, strings.HasPrefix(sortedResources[0].UserName, "scim-username-1: "))
assert.True(t, strings.HasPrefix(resp.Resources[1].UserName, "scim-username-2: ")) 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"`), Filter: gu.Ptr(`emails sw "scim-email-1" and emails ew "@example.com"`),
}, },
assert: func(t assert.TestingT, resp *scim.ListResponse[*resources.ScimUser]) { 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, 5, resp.ItemsPerPage)
assert.Equal(t, 2, resp.TotalResults) assert.Equal(t, 2, resp.TotalResults)
assert.Equal(t, 1, resp.StartIndex) assert.Equal(t, 1, resp.StartIndex)
assert.Len(t, resp.Resources, 2) assert.Len(t, sortedResources, 2)
for _, resource := range resp.Resources { for _, resource := range sortedResources {
assert.True(t, strings.HasPrefix(resource.UserName, "scim-username-1")) assert.True(t, strings.HasPrefix(resource.UserName, "scim-username-1"))
assert.Len(t, resource.Emails, 1) assert.Len(t, resource.Emails, 1)
assert.True(t, strings.HasPrefix(resource.Emails[0].Value, "scim-email-1")) assert.True(t, strings.HasPrefix(resource.Emails[0].Value, "scim-email-1"))
@@ -198,11 +205,14 @@ func TestListUser(t *testing.T) {
SendAsPost: true, SendAsPost: true,
}, },
assert: func(t assert.TestingT, resp *scim.ListResponse[*resources.ScimUser]) { 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, 5, resp.ItemsPerPage)
assert.Equal(t, 2, resp.TotalResults) assert.Equal(t, 2, resp.TotalResults)
assert.Equal(t, 1, resp.StartIndex) assert.Equal(t, 1, resp.StartIndex)
assert.Len(t, resp.Resources, 2) assert.Len(t, sortedResources, 2)
for _, resource := range resp.Resources { for _, resource := range sortedResources {
assert.True(t, strings.HasPrefix(resource.UserName, "scim-username-1")) assert.True(t, strings.HasPrefix(resource.UserName, "scim-username-1"))
assert.Len(t, resource.Emails, 1) assert.Len(t, resource.Emails, 1)
assert.True(t, strings.HasPrefix(resource.Emails[0].Value, "scim-email-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 { func createUsers(t *testing.T, ctx context.Context, orgID string) []string {
count := totalCountOfHumanUsers - 1 // zitadel admin is always created by default count := totalCountOfHumanUsers - 1 // zitadel admin is always created by default
createdUserIDs := make([]string, 0, count) createdUserIDs := make([]string, 0, count)