mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-19 19:48:45 +00:00
feat: List users by metadata (#10415)
# Which Problems Are Solved Some users have reported the need of retrieving users given a metadata key, metadata value or both. This change introduces metadata search filter on the `ListUsers()` endpoint to allow Zitadel users to search for user records by metadata. The changes affect only v2 APIs. # How the Problems Are Solved - Add new search filter to `ListUserRequest`: `MetaKey` and `MetaValue` - Add SQL indices on metadata key and metadata value - Update query to left join `user_metadata` table # Additional Context - Closes #9053 - Depends on https://github.com/zitadel/zitadel/pull/10567 --------- Co-authored-by: Silvan <[email protected]> Co-authored-by: Stefan Benz <[email protected]>
This commit is contained in:
co-authored by
Silvan
Stefan Benz
parent
5517fe234f
commit
8df402fb4f
@@ -49,6 +49,17 @@ func TimestampMethodPbToQuery(method filter.TimestampFilterMethod) query.Timesta
|
||||
}
|
||||
}
|
||||
|
||||
func ByteMethodPbToQuery(method filter.ByteFilterMethod) query.BytesComparison {
|
||||
switch method {
|
||||
case filter.ByteFilterMethod_BYTE_FILTER_METHOD_EQUALS:
|
||||
return query.BytesEquals
|
||||
case filter.ByteFilterMethod_BYTE_FILTER_METHOD_NOT_EQUALS:
|
||||
return query.BytesNotEquals
|
||||
default:
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
func PaginationPbToQuery(defaults systemdefaults.SystemDefaults, query *filter.PaginationRequest) (offset, limit uint64, asc bool, err error) {
|
||||
limit = defaults.DefaultQueryLimit
|
||||
if query == nil {
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
package filter
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/config/systemdefaults"
|
||||
"github.com/zitadel/zitadel/internal/query"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/filter/v2"
|
||||
)
|
||||
|
||||
func TestTextMethodPbToQuery(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tt := []struct {
|
||||
name string
|
||||
input filter.TextFilterMethod
|
||||
output query.TextComparison
|
||||
}{
|
||||
{"Equals", filter.TextFilterMethod_TEXT_FILTER_METHOD_EQUALS, query.TextEquals},
|
||||
{"EqualsIgnoreCase", filter.TextFilterMethod_TEXT_FILTER_METHOD_EQUALS_IGNORE_CASE, query.TextEqualsIgnoreCase},
|
||||
{"StartsWith", filter.TextFilterMethod_TEXT_FILTER_METHOD_STARTS_WITH, query.TextStartsWith},
|
||||
{"StartsWithIgnoreCase", filter.TextFilterMethod_TEXT_FILTER_METHOD_STARTS_WITH_IGNORE_CASE, query.TextStartsWithIgnoreCase},
|
||||
{"Contains", filter.TextFilterMethod_TEXT_FILTER_METHOD_CONTAINS, query.TextContains},
|
||||
{"ContainsIgnoreCase", filter.TextFilterMethod_TEXT_FILTER_METHOD_CONTAINS_IGNORE_CASE, query.TextContainsIgnoreCase},
|
||||
{"EndsWith", filter.TextFilterMethod_TEXT_FILTER_METHOD_ENDS_WITH, query.TextEndsWith},
|
||||
{"EndsWithIgnoreCase", filter.TextFilterMethod_TEXT_FILTER_METHOD_ENDS_WITH_IGNORE_CASE, query.TextEndsWithIgnoreCase},
|
||||
{"Unknown", filter.TextFilterMethod(999), -1},
|
||||
}
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := TextMethodPbToQuery(tc.input)
|
||||
|
||||
assert.Equal(t, tc.output, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimestampMethodPbToQuery(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tt := []struct {
|
||||
name string
|
||||
input filter.TimestampFilterMethod
|
||||
output query.TimestampComparison
|
||||
}{
|
||||
{"Equals", filter.TimestampFilterMethod_TIMESTAMP_FILTER_METHOD_EQUALS, query.TimestampEquals},
|
||||
{"Before", filter.TimestampFilterMethod_TIMESTAMP_FILTER_METHOD_BEFORE, query.TimestampLess},
|
||||
{"After", filter.TimestampFilterMethod_TIMESTAMP_FILTER_METHOD_AFTER, query.TimestampGreater},
|
||||
{"BeforeOrEquals", filter.TimestampFilterMethod_TIMESTAMP_FILTER_METHOD_BEFORE_OR_EQUALS, query.TimestampLessOrEquals},
|
||||
{"AfterOrEquals", filter.TimestampFilterMethod_TIMESTAMP_FILTER_METHOD_AFTER_OR_EQUALS, query.TimestampGreaterOrEquals},
|
||||
{"Unknown", filter.TimestampFilterMethod(999), -1},
|
||||
}
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := TimestampMethodPbToQuery(tc.input)
|
||||
|
||||
assert.Equal(t, tc.output, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestByteMethodPbToQuery(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tt := []struct {
|
||||
name string
|
||||
input filter.ByteFilterMethod
|
||||
output query.BytesComparison
|
||||
}{
|
||||
{"Equals", filter.ByteFilterMethod_BYTE_FILTER_METHOD_EQUALS, query.BytesEquals},
|
||||
{"NotEquals", filter.ByteFilterMethod_BYTE_FILTER_METHOD_NOT_EQUALS, query.BytesNotEquals},
|
||||
{"Unknown", filter.ByteFilterMethod(999), -1},
|
||||
}
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := ByteMethodPbToQuery(tc.input)
|
||||
|
||||
assert.Equal(t, tc.output, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPaginationPbToQuery(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
defaults := systemdefaults.SystemDefaults{
|
||||
DefaultQueryLimit: 10,
|
||||
MaxQueryLimit: 100,
|
||||
}
|
||||
tt := []struct {
|
||||
name string
|
||||
query *filter.PaginationRequest
|
||||
wantOff uint64
|
||||
wantLim uint64
|
||||
wantAsc bool
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "nil query",
|
||||
query: nil,
|
||||
wantOff: 0,
|
||||
wantLim: 10,
|
||||
wantAsc: false,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "limit not set",
|
||||
query: &filter.PaginationRequest{Offset: 5, Limit: 0, Asc: true},
|
||||
wantOff: 5,
|
||||
wantLim: 10,
|
||||
wantAsc: true,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "limit set below max",
|
||||
query: &filter.PaginationRequest{Offset: 2, Limit: 50, Asc: false},
|
||||
wantOff: 2,
|
||||
wantLim: 50,
|
||||
wantAsc: false,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "limit exceeds max",
|
||||
query: &filter.PaginationRequest{Offset: 1, Limit: 101, Asc: true},
|
||||
wantOff: 0,
|
||||
wantLim: 0,
|
||||
wantAsc: false,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
off, lim, asc, err := PaginationPbToQuery(defaults, tc.query)
|
||||
|
||||
require.Equal(t, tc.wantErr, err != nil)
|
||||
|
||||
assert.Equal(t, tc.wantOff, off)
|
||||
assert.Equal(t, tc.wantLim, lim)
|
||||
assert.Equal(t, tc.wantAsc, asc)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryToPaginationPb(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
req := query.SearchRequest{Limit: 20}
|
||||
resp := query.SearchResponse{Count: 123}
|
||||
got := QueryToPaginationPb(req, resp)
|
||||
|
||||
assert.Equal(t, req.Limit, got.AppliedLimit)
|
||||
assert.Equal(t, resp.Count, got.TotalResult)
|
||||
}
|
||||
@@ -7,13 +7,12 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/brianvoe/gofakeit/v6"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/integration"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/filter/v2beta"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/settings/v2beta"
|
||||
filter "github.com/zitadel/zitadel/pkg/grpc/filter/v2beta"
|
||||
settings "github.com/zitadel/zitadel/pkg/grpc/settings/v2beta"
|
||||
)
|
||||
|
||||
func TestServer_ListOrganizationSettings(t *testing.T) {
|
||||
@@ -36,7 +35,7 @@ func TestServer_ListOrganizationSettings(t *testing.T) {
|
||||
args: args{
|
||||
ctx: CTX,
|
||||
dep: func(request *settings.ListOrganizationSettingsRequest, response *settings.ListOrganizationSettingsResponse) {
|
||||
orgResp := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), gofakeit.Email())
|
||||
orgResp := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), integration.Email())
|
||||
instance.SetOrganizationSettings(iamOwnerCtx, t, orgResp.GetOrganizationId(), true)
|
||||
|
||||
request.Filters[0].Filter = &settings.OrganizationSettingsSearchFilter_InOrganizationIdsFilter{
|
||||
@@ -56,7 +55,7 @@ func TestServer_ListOrganizationSettings(t *testing.T) {
|
||||
args: args{
|
||||
ctx: instance.WithAuthorizationToken(CTX, integration.UserTypeNoPermission),
|
||||
dep: func(request *settings.ListOrganizationSettingsRequest, response *settings.ListOrganizationSettingsResponse) {
|
||||
orgResp := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), gofakeit.Email())
|
||||
orgResp := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), integration.Email())
|
||||
instance.SetOrganizationSettings(iamOwnerCtx, t, orgResp.GetOrganizationId(), true)
|
||||
|
||||
request.Filters[0].Filter = &settings.OrganizationSettingsSearchFilter_InOrganizationIdsFilter{
|
||||
@@ -82,7 +81,7 @@ func TestServer_ListOrganizationSettings(t *testing.T) {
|
||||
args: args{
|
||||
ctx: instance.WithAuthorizationToken(CTX, integration.UserTypeOrgOwner),
|
||||
dep: func(request *settings.ListOrganizationSettingsRequest, response *settings.ListOrganizationSettingsResponse) {
|
||||
orgResp := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), gofakeit.Email())
|
||||
orgResp := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), integration.Email())
|
||||
instance.SetOrganizationSettings(iamOwnerCtx, t, orgResp.GetOrganizationId(), true)
|
||||
|
||||
request.Filters[0].Filter = &settings.OrganizationSettingsSearchFilter_InOrganizationIdsFilter{
|
||||
@@ -131,7 +130,7 @@ func TestServer_ListOrganizationSettings(t *testing.T) {
|
||||
args: args{
|
||||
ctx: iamOwnerCtx,
|
||||
dep: func(request *settings.ListOrganizationSettingsRequest, response *settings.ListOrganizationSettingsResponse) {
|
||||
orgResp := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), gofakeit.Email())
|
||||
orgResp := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), integration.Email())
|
||||
settingsResp := instance.SetOrganizationSettings(iamOwnerCtx, t, orgResp.GetOrganizationId(), true)
|
||||
|
||||
request.Filters[0].Filter = &settings.OrganizationSettingsSearchFilter_InOrganizationIdsFilter{
|
||||
@@ -163,11 +162,11 @@ func TestServer_ListOrganizationSettings(t *testing.T) {
|
||||
args: args{
|
||||
ctx: iamOwnerCtx,
|
||||
dep: func(request *settings.ListOrganizationSettingsRequest, response *settings.ListOrganizationSettingsResponse) {
|
||||
orgResp1 := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), gofakeit.Email())
|
||||
orgResp1 := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), integration.Email())
|
||||
settingsResp1 := instance.SetOrganizationSettings(iamOwnerCtx, t, orgResp1.GetOrganizationId(), true)
|
||||
orgResp2 := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), gofakeit.Email())
|
||||
orgResp2 := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), integration.Email())
|
||||
settingsResp2 := instance.SetOrganizationSettings(iamOwnerCtx, t, orgResp2.GetOrganizationId(), true)
|
||||
orgResp3 := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), gofakeit.Email())
|
||||
orgResp3 := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), integration.Email())
|
||||
settingsResp3 := instance.SetOrganizationSettings(iamOwnerCtx, t, orgResp3.GetOrganizationId(), true)
|
||||
|
||||
request.Filters[0].Filter = &settings.OrganizationSettingsSearchFilter_InOrganizationIdsFilter{
|
||||
@@ -211,11 +210,11 @@ func TestServer_ListOrganizationSettings(t *testing.T) {
|
||||
args: args{
|
||||
ctx: iamOwnerCtx,
|
||||
dep: func(request *settings.ListOrganizationSettingsRequest, response *settings.ListOrganizationSettingsResponse) {
|
||||
orgResp1 := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), gofakeit.Email())
|
||||
orgResp1 := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), integration.Email())
|
||||
instance.SetOrganizationSettings(iamOwnerCtx, t, orgResp1.GetOrganizationId(), false)
|
||||
orgResp2 := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), gofakeit.Email())
|
||||
orgResp2 := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), integration.Email())
|
||||
settingsResp2 := instance.SetOrganizationSettings(iamOwnerCtx, t, orgResp2.GetOrganizationId(), true)
|
||||
orgResp3 := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), gofakeit.Email())
|
||||
orgResp3 := instance.CreateOrganization(iamOwnerCtx, integration.OrganizationName(), integration.Email())
|
||||
instance.SetOrganizationSettings(iamOwnerCtx, t, orgResp3.GetOrganizationId(), false)
|
||||
|
||||
request.Filters[0].Filter = &settings.OrganizationSettingsSearchFilter_InOrganizationIdsFilter{
|
||||
|
||||
@@ -23,7 +23,7 @@ import (
|
||||
|
||||
func TestServer_Limits_AuditLogRetention(t *testing.T) {
|
||||
isoInstance := integration.NewInstance(CTX)
|
||||
iamOwnerCtx := isoInstance.WithAuthorization(CTX, integration.UserTypeIAMOwner)
|
||||
iamOwnerCtx := isoInstance.WithAuthorizationToken(CTX, integration.UserTypeIAMOwner)
|
||||
userID, projectID, appID, projectGrantID := seedObjects(iamOwnerCtx, t, isoInstance.Client)
|
||||
beforeTime := time.Now()
|
||||
farPast := timestamppb.New(beforeTime.Add(-10 * time.Hour).UTC())
|
||||
@@ -37,7 +37,7 @@ func TestServer_Limits_AuditLogRetention(t *testing.T) {
|
||||
}, "wait for added event assertions to pass")
|
||||
_, err := integration.SystemClient().SetLimits(CTX, &system.SetLimitsRequest{
|
||||
InstanceId: isoInstance.ID(),
|
||||
AuditLogRetention: durationpb.New(time.Now().Sub(beforeTime)),
|
||||
AuditLogRetention: durationpb.New(time.Since(beforeTime)),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
var limitedCounts *eventCounts
|
||||
|
||||
@@ -4,6 +4,7 @@ package user_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"slices"
|
||||
"testing"
|
||||
@@ -17,6 +18,8 @@ import (
|
||||
|
||||
"github.com/zitadel/zitadel/internal/integration"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/feature/v2"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/filter/v2"
|
||||
v2 "github.com/zitadel/zitadel/pkg/grpc/metadata/v2"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/object/v2"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/session/v2"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/user/v2"
|
||||
@@ -413,7 +416,7 @@ func createUsers(ctx context.Context, orgID string, count int, passwordChangeReq
|
||||
}
|
||||
|
||||
func createUser(ctx context.Context, orgID string, passwordChangeRequired bool) userAttr {
|
||||
username := gofakeit.Email()
|
||||
username := integration.Email()
|
||||
return createUserWithUserName(ctx, username, orgID, passwordChangeRequired)
|
||||
}
|
||||
|
||||
@@ -432,10 +435,10 @@ func createUserWithUserName(ctx context.Context, username string, orgID string,
|
||||
}
|
||||
|
||||
func TestServer_ListUsers(t *testing.T) {
|
||||
defer func() {
|
||||
t.Cleanup(func() {
|
||||
_, err := Instance.Client.FeatureV2.ResetInstanceFeatures(IamCTX, &feature.ResetInstanceFeaturesRequest{})
|
||||
require.NoError(t, err)
|
||||
}()
|
||||
})
|
||||
|
||||
orgResp := Instance.CreateOrganization(IamCTX, integration.OrganizationName(), gofakeit.Email())
|
||||
type args struct {
|
||||
@@ -443,7 +446,7 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
req *user.ListUsersRequest
|
||||
dep func(ctx context.Context, request *user.ListUsersRequest) userAttrs
|
||||
}
|
||||
tests := []struct {
|
||||
tt := []struct {
|
||||
name string
|
||||
args args
|
||||
want *user.ListUsersResponse
|
||||
@@ -622,7 +625,7 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "list user by id multiple, ok",
|
||||
name: "list user by id and meta key multiple, ok",
|
||||
args: args{
|
||||
IamCTX,
|
||||
&user.ListUsersRequest{},
|
||||
@@ -631,6 +634,13 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
request.Queries = []*user.SearchQuery{}
|
||||
request.Queries = append(request.Queries, OrganizationIdQuery(orgResp.OrganizationId))
|
||||
request.Queries = append(request.Queries, InUserIDsQuery(infos.userIDs()))
|
||||
|
||||
Instance.SetUserMetadata(ctx, infos[0].UserID, "my meta", "my value 1")
|
||||
Instance.SetUserMetadata(ctx, infos[1].UserID, "my meta 2", "my value 3")
|
||||
Instance.SetUserMetadata(ctx, infos[2].UserID, "my meta", "my value 2")
|
||||
|
||||
request.Queries = append(request.Queries, MetadataKeyContainsQuery("my meta"))
|
||||
request.SortingColumn = user.UserFieldName_USER_FIELD_NAME_CREATION_DATE
|
||||
return infos
|
||||
},
|
||||
},
|
||||
@@ -639,7 +649,7 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
TotalResult: 3,
|
||||
Timestamp: timestamppb.Now(),
|
||||
},
|
||||
SortingColumn: 0,
|
||||
SortingColumn: user.UserFieldName_USER_FIELD_NAME_CREATION_DATE,
|
||||
Result: []*user.User{
|
||||
{
|
||||
State: user.UserState_USER_STATE_ACTIVE,
|
||||
@@ -717,6 +727,8 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
request.Queries = []*user.SearchQuery{}
|
||||
request.Queries = append(request.Queries, OrganizationIdQuery(orgResp.OrganizationId))
|
||||
request.Queries = append(request.Queries, UsernameQuery(info.Username))
|
||||
|
||||
request.SortingColumn = user.UserFieldName_USER_FIELD_NAME_CREATION_DATE
|
||||
return []userAttr{info}
|
||||
},
|
||||
},
|
||||
@@ -725,7 +737,7 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
TotalResult: 1,
|
||||
Timestamp: timestamppb.Now(),
|
||||
},
|
||||
SortingColumn: 0,
|
||||
SortingColumn: user.UserFieldName_USER_FIELD_NAME_CREATION_DATE,
|
||||
Result: []*user.User{
|
||||
{
|
||||
State: user.UserState_USER_STATE_ACTIVE,
|
||||
@@ -761,6 +773,7 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
request.Queries = []*user.SearchQuery{}
|
||||
request.Queries = append(request.Queries, OrganizationIdQuery(orgResp.OrganizationId))
|
||||
request.Queries = append(request.Queries, InUserEmailsQuery([]string{info.Username}))
|
||||
request.SortingColumn = user.UserFieldName_USER_FIELD_NAME_CREATION_DATE
|
||||
return []userAttr{info}
|
||||
},
|
||||
},
|
||||
@@ -769,7 +782,7 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
TotalResult: 1,
|
||||
Timestamp: timestamppb.Now(),
|
||||
},
|
||||
SortingColumn: 0,
|
||||
SortingColumn: user.UserFieldName_USER_FIELD_NAME_CREATION_DATE,
|
||||
Result: []*user.User{
|
||||
{
|
||||
State: user.UserState_USER_STATE_ACTIVE,
|
||||
@@ -796,7 +809,7 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "list user in emails multiple, ok",
|
||||
name: "list user by emails and meta value multiple, ok",
|
||||
args: args{
|
||||
IamCTX,
|
||||
&user.ListUsersRequest{},
|
||||
@@ -805,6 +818,15 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
request.Queries = []*user.SearchQuery{}
|
||||
request.Queries = append(request.Queries, OrganizationIdQuery(orgResp.OrganizationId))
|
||||
request.Queries = append(request.Queries, InUserEmailsQuery(infos.emails()))
|
||||
|
||||
Instance.SetUserMetadata(ctx, infos[0].UserID, "my meta 1", "my value")
|
||||
Instance.SetUserMetadata(ctx, infos[0].UserID, "my meta 2", "my value")
|
||||
Instance.SetUserMetadata(ctx, infos[1].UserID, "my meta 2", "my value")
|
||||
Instance.SetUserMetadata(ctx, infos[2].UserID, "my meta", "my value")
|
||||
|
||||
request.Queries = append(request.Queries, MetadataValueQuery("my value"))
|
||||
request.SortingColumn = user.UserFieldName_USER_FIELD_NAME_CREATION_DATE
|
||||
|
||||
return infos
|
||||
},
|
||||
},
|
||||
@@ -813,7 +835,7 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
TotalResult: 3,
|
||||
Timestamp: timestamppb.Now(),
|
||||
},
|
||||
SortingColumn: 0,
|
||||
SortingColumn: user.UserFieldName_USER_FIELD_NAME_CREATION_DATE,
|
||||
Result: []*user.User{
|
||||
{
|
||||
State: user.UserState_USER_STATE_ACTIVE,
|
||||
@@ -981,6 +1003,7 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
request.Queries = []*user.SearchQuery{}
|
||||
request.Queries = append(request.Queries, OrganizationIdQuery(orgResp.OrganizationId))
|
||||
request.Queries = append(request.Queries, InUserEmailsQuery(infos.emails()))
|
||||
request.SortingColumn = user.UserFieldName_USER_FIELD_NAME_CREATION_DATE
|
||||
return infos
|
||||
},
|
||||
},
|
||||
@@ -989,7 +1012,7 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
TotalResult: 3,
|
||||
Timestamp: timestamppb.Now(),
|
||||
},
|
||||
SortingColumn: 0,
|
||||
SortingColumn: user.UserFieldName_USER_FIELD_NAME_CREATION_DATE,
|
||||
Result: []*user.User{
|
||||
{
|
||||
State: user.UserState_USER_STATE_ACTIVE,
|
||||
@@ -1065,6 +1088,7 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
info := createUser(ctx, orgRespForOrgTests.OrganizationId, false)
|
||||
request.Queries = []*user.SearchQuery{}
|
||||
request.Queries = append(request.Queries, OrganizationIdQuery(orgRespForOrgTests.OrganizationId))
|
||||
request.SortingColumn = user.UserFieldName_USER_FIELD_NAME_CREATION_DATE
|
||||
return []userAttr{info, {}}
|
||||
},
|
||||
},
|
||||
@@ -1073,7 +1097,7 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
TotalResult: 2,
|
||||
Timestamp: timestamppb.Now(),
|
||||
},
|
||||
SortingColumn: 0,
|
||||
SortingColumn: user.UserFieldName_USER_FIELD_NAME_CREATION_DATE,
|
||||
Result: []*user.User{
|
||||
{
|
||||
State: user.UserState_USER_STATE_ACTIVE,
|
||||
@@ -1109,7 +1133,6 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
func(ctx context.Context, request *user.ListUsersRequest) userAttrs {
|
||||
orgRespForOrgTests := Instance.CreateOrganization(IamCTX, integration.OrganizationName(), gofakeit.Email())
|
||||
orgRespForOrgTests2 := Instance.CreateOrganization(IamCTX, integration.OrganizationName(), gofakeit.Email())
|
||||
// info := createUser(ctx, orgRespForOrgTests.OrganizationId, false)
|
||||
createUser(ctx, orgRespForOrgTests.OrganizationId, false)
|
||||
request.Queries = []*user.SearchQuery{}
|
||||
request.Queries = append(request.Queries, OrganizationIdQuery(orgRespForOrgTests2.OrganizationId))
|
||||
@@ -1128,54 +1151,77 @@ func TestServer_ListUsers(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "when no users matching meta key should return empty list",
|
||||
args: args{
|
||||
IamCTX,
|
||||
&user.ListUsersRequest{},
|
||||
func(ctx context.Context, request *user.ListUsersRequest) userAttrs {
|
||||
createUser(ctx, orgResp.OrganizationId, false)
|
||||
request.Queries = []*user.SearchQuery{}
|
||||
request.Queries = append(request.Queries, OrganizationIdQuery(orgResp.OrganizationId))
|
||||
request.Queries = append(request.Queries, MetadataKeyContainsQuery("some non-existent meta"))
|
||||
|
||||
request.SortingColumn = user.UserFieldName_USER_FIELD_NAME_CREATION_DATE
|
||||
return []userAttr{}
|
||||
},
|
||||
},
|
||||
want: &user.ListUsersResponse{
|
||||
Details: &object.ListDetails{
|
||||
TotalResult: 0,
|
||||
Timestamp: timestamppb.Now(),
|
||||
},
|
||||
SortingColumn: user.UserFieldName_USER_FIELD_NAME_CREATION_DATE,
|
||||
Result: []*user.User{},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, f := range permissionCheckV2Settings {
|
||||
f := f
|
||||
for _, tt := range tests {
|
||||
t.Run(f.TestNamePrependString+tt.name, func(t *testing.T) {
|
||||
for _, tc := range tt {
|
||||
t.Run(f.TestNamePrependString+tc.name, func(t1 *testing.T) {
|
||||
setPermissionCheckV2Flag(t, f.SetFlag)
|
||||
infos := tt.args.dep(IamCTX, tt.args.req)
|
||||
infos := tc.args.dep(IamCTX, tc.args.req)
|
||||
|
||||
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(tt.args.ctx, 10*time.Minute)
|
||||
require.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
||||
got, err := Client.ListUsers(tt.args.ctx, tt.args.req)
|
||||
if tt.wantErr {
|
||||
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(tc.args.ctx, 20*time.Second)
|
||||
require.EventuallyWithT(t1, func(ttt *assert.CollectT) {
|
||||
got, err := Client.ListUsers(tc.args.ctx, tc.args.req)
|
||||
if tc.wantErr {
|
||||
require.Error(ttt, err)
|
||||
return
|
||||
}
|
||||
require.NoError(ttt, err)
|
||||
|
||||
// always only give back dependency infos which are required for the response
|
||||
require.Len(ttt, tt.want.Result, len(infos))
|
||||
if assert.Len(ttt, got.Result, len(tt.want.Result)) {
|
||||
tt.want.Details.TotalResult = got.Details.TotalResult
|
||||
require.Len(ttt, tc.want.Result, len(infos))
|
||||
if assert.Len(ttt, got.Result, len(tc.want.Result)) {
|
||||
tc.want.Details.TotalResult = got.Details.TotalResult
|
||||
|
||||
// fill in userid and username as it is generated
|
||||
for i := range infos {
|
||||
if tt.want.Result[i] == nil {
|
||||
if tc.want.Result[i] == nil {
|
||||
continue
|
||||
}
|
||||
tt.want.Result[i].UserId = infos[i].UserID
|
||||
tt.want.Result[i].Username = infos[i].Username
|
||||
tt.want.Result[i].PreferredLoginName = infos[i].Username
|
||||
tt.want.Result[i].LoginNames = []string{infos[i].Username}
|
||||
if human := tt.want.Result[i].GetHuman(); human != nil {
|
||||
tc.want.Result[i].UserId = infos[i].UserID
|
||||
tc.want.Result[i].Username = infos[i].Username
|
||||
tc.want.Result[i].PreferredLoginName = infos[i].Username
|
||||
tc.want.Result[i].LoginNames = []string{infos[i].Username}
|
||||
if human := tc.want.Result[i].GetHuman(); human != nil {
|
||||
human.Email.Email = infos[i].Username
|
||||
human.Phone.Phone = infos[i].Phone
|
||||
if tt.want.Result[i].GetHuman().GetPasswordChanged() != nil {
|
||||
if tc.want.Result[i].GetHuman().GetPasswordChanged() != nil {
|
||||
human.PasswordChanged = infos[i].Changed
|
||||
}
|
||||
}
|
||||
tt.want.Result[i].Details = infos[i].Details
|
||||
tc.want.Result[i].Details = infos[i].Details
|
||||
}
|
||||
for i := range tt.want.Result {
|
||||
if tt.want.Result[i] == nil {
|
||||
for i := range tc.want.Result {
|
||||
if tc.want.Result[i] == nil {
|
||||
continue
|
||||
}
|
||||
assert.EqualExportedValues(ttt, got.Result[i], tt.want.Result[i])
|
||||
assert.EqualExportedValues(ttt, got.Result[i], tc.want.Result[i])
|
||||
}
|
||||
}
|
||||
integration.AssertListDetails(ttt, tt.want, got)
|
||||
integration.AssertListDetails(ttt, tc.want, got)
|
||||
}, retryDuration, tick, "timeout waiting for expected user result")
|
||||
})
|
||||
}
|
||||
@@ -1322,3 +1368,44 @@ func OrganizationIdQuery(resourceowner string) *user.SearchQuery {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func OrQuery(queries []*user.SearchQuery) *user.SearchQuery {
|
||||
return &user.SearchQuery{
|
||||
Query: &user.SearchQuery_OrQuery{
|
||||
OrQuery: &user.OrQuery{
|
||||
Queries: queries,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func MetadataKeyContainsQuery(metadataKey string) *user.SearchQuery {
|
||||
return &user.SearchQuery{
|
||||
Query: &user.SearchQuery_MetadataKeyFilter{
|
||||
MetadataKeyFilter: &v2.MetadataKeyFilter{
|
||||
Key: metadataKey,
|
||||
Method: filter.TextFilterMethod_TEXT_FILTER_METHOD_STARTS_WITH},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func MetakeyEqualsQuery(metaKey string) *user.SearchQuery {
|
||||
return &user.SearchQuery{
|
||||
Query: &user.SearchQuery_MetadataKeyFilter{
|
||||
MetadataKeyFilter: &v2.MetadataKeyFilter{
|
||||
Key: metaKey,
|
||||
Method: filter.TextFilterMethod_TEXT_FILTER_METHOD_EQUALS},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func MetadataValueQuery(metaValue string) *user.SearchQuery {
|
||||
return &user.SearchQuery{
|
||||
Query: &user.SearchQuery_MetadataValueFilter{
|
||||
MetadataValueFilter: &v2.MetadataValueFilter{
|
||||
Value: []byte(base64.StdEncoding.EncodeToString([]byte(metaValue))),
|
||||
Method: filter.ByteFilterMethod_BYTE_FILTER_METHOD_EQUALS,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -45,11 +45,11 @@ func TestMain(m *testing.M) {
|
||||
|
||||
Instance = integration.NewInstance(ctx)
|
||||
|
||||
UserCTX = Instance.WithAuthorization(ctx, integration.UserTypeNoPermission)
|
||||
IamCTX = Instance.WithAuthorization(ctx, integration.UserTypeIAMOwner)
|
||||
LoginCTX = Instance.WithAuthorization(ctx, integration.UserTypeLogin)
|
||||
UserCTX = Instance.WithAuthorizationToken(ctx, integration.UserTypeNoPermission)
|
||||
IamCTX = Instance.WithAuthorizationToken(ctx, integration.UserTypeIAMOwner)
|
||||
LoginCTX = Instance.WithAuthorizationToken(ctx, integration.UserTypeLogin)
|
||||
SystemCTX = integration.WithSystemAuthorization(ctx)
|
||||
CTX = Instance.WithAuthorization(ctx, integration.UserTypeOrgOwner)
|
||||
CTX = Instance.WithAuthorizationToken(ctx, integration.UserTypeOrgOwner)
|
||||
Client = Instance.Client.UserV2beta
|
||||
return m.Run()
|
||||
}())
|
||||
|
||||
Reference in New Issue
Block a user