2024-02-28 10:21:11 +00:00
|
|
|
//go:build integration
|
|
|
|
|
|
|
|
package admin_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2024-09-06 12:47:57 +00:00
|
|
|
"time"
|
2024-02-28 10:21:11 +00:00
|
|
|
|
|
|
|
"github.com/brianvoe/gofakeit/v6"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-04-03 10:43:43 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
|
2024-02-28 10:21:11 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/integration"
|
|
|
|
admin_pb "github.com/zitadel/zitadel/pkg/grpc/admin"
|
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/member"
|
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/object"
|
|
|
|
)
|
|
|
|
|
|
|
|
var iamRoles = []string{
|
|
|
|
"IAM_OWNER",
|
|
|
|
"IAM_OWNER_VIEWER",
|
|
|
|
"IAM_ORG_MANAGER",
|
|
|
|
"IAM_USER_MANAGER",
|
|
|
|
"IAM_ADMIN_IMPERSONATOR",
|
|
|
|
"IAM_END_USER_IMPERSONATOR",
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_ListIAMMemberRoles(t *testing.T) {
|
|
|
|
got, err := Client.ListIAMMemberRoles(AdminCTX, &admin_pb.ListIAMMemberRolesRequest{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.ElementsMatch(t, iamRoles, got.GetRoles())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_ListIAMMembers(t *testing.T) {
|
2024-09-06 12:47:57 +00:00
|
|
|
user := Instance.CreateHumanUserVerified(AdminCTX, Instance.DefaultOrg.Id, gofakeit.Email())
|
2024-02-28 10:21:11 +00:00
|
|
|
_, err := Client.AddIAMMember(AdminCTX, &admin_pb.AddIAMMemberRequest{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
Roles: iamRoles,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
req *admin_pb.ListIAMMembersRequest
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *admin_pb.ListIAMMembersResponse
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "permission error",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: Instance.WithAuthorization(CTX, integration.UserTypeOrgOwner),
|
2024-02-28 10:21:11 +00:00
|
|
|
req: &admin_pb.ListIAMMembersRequest{
|
|
|
|
Query: &object.ListQuery{},
|
|
|
|
Queries: []*member.SearchQuery{{
|
|
|
|
Query: &member.SearchQuery_UserIdQuery{
|
|
|
|
UserIdQuery: &member.UserIDQuery{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
args: args{
|
|
|
|
ctx: AdminCTX,
|
|
|
|
req: &admin_pb.ListIAMMembersRequest{
|
|
|
|
Query: &object.ListQuery{},
|
|
|
|
Queries: []*member.SearchQuery{{
|
|
|
|
Query: &member.SearchQuery_UserIdQuery{
|
|
|
|
UserIdQuery: &member.UserIDQuery{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &admin_pb.ListIAMMembersResponse{
|
|
|
|
Result: []*member.Member{{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
Roles: iamRoles,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-09-06 12:47:57 +00:00
|
|
|
assert.EventuallyWithT(t, func(ct *assert.CollectT) {
|
|
|
|
got, err := Client.ListIAMMembers(tt.args.ctx, tt.args.req)
|
|
|
|
if tt.wantErr {
|
|
|
|
assert.Error(ct, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(ct, err)
|
|
|
|
wantResult := tt.want.GetResult()
|
|
|
|
gotResult := got.GetResult()
|
2024-02-28 10:21:11 +00:00
|
|
|
|
2024-09-06 12:47:57 +00:00
|
|
|
if assert.Len(ct, gotResult, len(wantResult)) {
|
|
|
|
for i, want := range wantResult {
|
|
|
|
assert.Equal(ct, want.GetUserId(), gotResult[i].GetUserId())
|
|
|
|
assert.ElementsMatch(ct, want.GetRoles(), gotResult[i].GetRoles())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, time.Minute, time.Second)
|
2024-02-28 10:21:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_AddIAMMember(t *testing.T) {
|
2024-09-06 12:47:57 +00:00
|
|
|
user := Instance.CreateHumanUserVerified(AdminCTX, Instance.DefaultOrg.Id, gofakeit.Email())
|
2024-02-28 10:21:11 +00:00
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
req *admin_pb.AddIAMMemberRequest
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *admin_pb.AddIAMMemberResponse
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "permission error",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: Instance.WithAuthorization(CTX, integration.UserTypeOrgOwner),
|
2024-02-28 10:21:11 +00:00
|
|
|
req: &admin_pb.AddIAMMemberRequest{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
Roles: iamRoles,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
args: args{
|
|
|
|
ctx: AdminCTX,
|
|
|
|
req: &admin_pb.AddIAMMemberRequest{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
Roles: iamRoles,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &admin_pb.AddIAMMemberResponse{
|
|
|
|
Details: &object.ObjectDetails{
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: Instance.ID(),
|
2024-02-28 10:21:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unknown roles error",
|
|
|
|
args: args{
|
|
|
|
ctx: AdminCTX,
|
|
|
|
req: &admin_pb.AddIAMMemberRequest{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
Roles: []string{"FOO", "BAR"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "org role error",
|
|
|
|
args: args{
|
|
|
|
ctx: AdminCTX,
|
|
|
|
req: &admin_pb.AddIAMMemberRequest{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
Roles: []string{"ORG_OWNER"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := Client.AddIAMMember(tt.args.ctx, tt.args.req)
|
|
|
|
if tt.wantErr {
|
|
|
|
assert.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
integration.AssertDetails(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_UpdateIAMMember(t *testing.T) {
|
2024-09-06 12:47:57 +00:00
|
|
|
user := Instance.CreateHumanUserVerified(AdminCTX, Instance.DefaultOrg.Id, gofakeit.Email())
|
2024-02-28 10:21:11 +00:00
|
|
|
_, err := Client.AddIAMMember(AdminCTX, &admin_pb.AddIAMMemberRequest{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
Roles: []string{"IAM_OWNER"},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
req *admin_pb.UpdateIAMMemberRequest
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *admin_pb.UpdateIAMMemberResponse
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "permission error",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: Instance.WithAuthorization(CTX, integration.UserTypeOrgOwner),
|
2024-02-28 10:21:11 +00:00
|
|
|
req: &admin_pb.UpdateIAMMemberRequest{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
Roles: iamRoles,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
args: args{
|
|
|
|
ctx: AdminCTX,
|
|
|
|
req: &admin_pb.UpdateIAMMemberRequest{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
Roles: iamRoles,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &admin_pb.UpdateIAMMemberResponse{
|
|
|
|
Details: &object.ObjectDetails{
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: Instance.ID(),
|
2024-02-28 10:21:11 +00:00
|
|
|
ChangeDate: timestamppb.Now(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unknown roles error",
|
|
|
|
args: args{
|
|
|
|
ctx: AdminCTX,
|
|
|
|
req: &admin_pb.UpdateIAMMemberRequest{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
Roles: []string{"FOO", "BAR"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "org role error",
|
|
|
|
args: args{
|
|
|
|
ctx: AdminCTX,
|
|
|
|
req: &admin_pb.UpdateIAMMemberRequest{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
Roles: []string{"ORG_OWNER"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := Client.UpdateIAMMember(tt.args.ctx, tt.args.req)
|
|
|
|
if tt.wantErr {
|
|
|
|
assert.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
integration.AssertDetails(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_RemoveIAMMember(t *testing.T) {
|
2024-09-06 12:47:57 +00:00
|
|
|
user := Instance.CreateHumanUserVerified(AdminCTX, Instance.DefaultOrg.Id, gofakeit.Email())
|
2024-02-28 10:21:11 +00:00
|
|
|
_, err := Client.AddIAMMember(AdminCTX, &admin_pb.AddIAMMemberRequest{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
Roles: []string{"IAM_OWNER"},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
req *admin_pb.RemoveIAMMemberRequest
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *admin_pb.RemoveIAMMemberResponse
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "permission error",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: Instance.WithAuthorization(CTX, integration.UserTypeOrgOwner),
|
2024-02-28 10:21:11 +00:00
|
|
|
req: &admin_pb.RemoveIAMMemberRequest{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
args: args{
|
|
|
|
ctx: AdminCTX,
|
|
|
|
req: &admin_pb.RemoveIAMMemberRequest{
|
|
|
|
UserId: user.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &admin_pb.RemoveIAMMemberResponse{
|
|
|
|
Details: &object.ObjectDetails{
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: Instance.ID(),
|
2024-02-28 10:21:11 +00:00
|
|
|
ChangeDate: timestamppb.Now(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := Client.RemoveIAMMember(tt.args.ctx, tt.args.req)
|
|
|
|
if tt.wantErr {
|
|
|
|
assert.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
integration.AssertDetails(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|