2024-02-28 10:21:11 +00:00
|
|
|
//go:build integration
|
|
|
|
|
|
|
|
package admin_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2024-10-17 21:20:57 +00:00
|
|
|
"time"
|
2024-02-28 10:21:11 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/integration"
|
|
|
|
admin_pb "github.com/zitadel/zitadel/pkg/grpc/admin"
|
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/object"
|
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/settings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServer_GetSecurityPolicy(t *testing.T) {
|
2024-09-06 12:47:57 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
instance := integration.NewInstance(CTX)
|
|
|
|
adminCtx := instance.WithAuthorization(CTX, integration.UserTypeIAMOwner)
|
|
|
|
|
|
|
|
_, err := instance.Client.Admin.SetSecurityPolicy(adminCtx, &admin_pb.SetSecurityPolicyRequest{
|
2024-02-28 10:21:11 +00:00
|
|
|
EnableIframeEmbedding: true,
|
|
|
|
AllowedOrigins: []string{"foo.com", "bar.com"},
|
|
|
|
EnableImpersonation: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
ctx context.Context
|
|
|
|
want *admin_pb.GetSecurityPolicyResponse
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "permission error",
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: instance.WithAuthorization(CTX, integration.UserTypeOrgOwner),
|
2024-02-28 10:21:11 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: adminCtx,
|
2024-02-28 10:21:11 +00:00
|
|
|
want: &admin_pb.GetSecurityPolicyResponse{
|
|
|
|
Policy: &settings.SecurityPolicy{
|
|
|
|
EnableIframeEmbedding: true,
|
|
|
|
AllowedOrigins: []string{"foo.com", "bar.com"},
|
|
|
|
EnableImpersonation: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-10-17 21:20:57 +00:00
|
|
|
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(tt.ctx, 5*time.Second)
|
|
|
|
require.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
|
|
|
resp, err := instance.Client.Admin.GetSecurityPolicy(tt.ctx, &admin_pb.GetSecurityPolicyRequest{})
|
|
|
|
if tt.wantErr {
|
|
|
|
require.Error(ttt, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(ttt, err)
|
|
|
|
got, want := resp.GetPolicy(), tt.want.GetPolicy()
|
|
|
|
assert.Equal(ttt, want.GetEnableIframeEmbedding(), got.GetEnableIframeEmbedding(), "enable iframe embedding")
|
|
|
|
assert.Equal(ttt, want.GetAllowedOrigins(), got.GetAllowedOrigins(), "allowed origins")
|
|
|
|
assert.Equal(ttt, want.GetEnableImpersonation(), got.GetEnableImpersonation(), "enable impersonation")
|
|
|
|
}, retryDuration, tick, "timeout waiting for expected target result")
|
2024-02-28 10:21:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_SetSecurityPolicy(t *testing.T) {
|
2024-09-06 12:47:57 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
instance := integration.NewInstance(CTX)
|
|
|
|
adminCtx := instance.WithAuthorization(CTX, integration.UserTypeIAMOwner)
|
|
|
|
|
2024-02-28 10:21:11 +00:00
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
req *admin_pb.SetSecurityPolicyRequest
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *admin_pb.SetSecurityPolicyResponse
|
|
|
|
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.SetSecurityPolicyRequest{
|
|
|
|
EnableIframeEmbedding: true,
|
|
|
|
AllowedOrigins: []string{"foo.com", "bar.com"},
|
|
|
|
EnableImpersonation: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success allowed origins",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: adminCtx,
|
2024-02-28 10:21:11 +00:00
|
|
|
req: &admin_pb.SetSecurityPolicyRequest{
|
|
|
|
AllowedOrigins: []string{"foo.com", "bar.com"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &admin_pb.SetSecurityPolicyResponse{
|
|
|
|
Details: &object.ObjectDetails{
|
|
|
|
ChangeDate: timestamppb.Now(),
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: instance.ID(),
|
2024-02-28 10:21:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success iframe embedding",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: adminCtx,
|
2024-02-28 10:21:11 +00:00
|
|
|
req: &admin_pb.SetSecurityPolicyRequest{
|
|
|
|
EnableIframeEmbedding: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &admin_pb.SetSecurityPolicyResponse{
|
|
|
|
Details: &object.ObjectDetails{
|
|
|
|
ChangeDate: timestamppb.Now(),
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: instance.ID(),
|
2024-02-28 10:21:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success impersonation",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: adminCtx,
|
2024-02-28 10:21:11 +00:00
|
|
|
req: &admin_pb.SetSecurityPolicyRequest{
|
|
|
|
EnableImpersonation: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &admin_pb.SetSecurityPolicyResponse{
|
|
|
|
Details: &object.ObjectDetails{
|
|
|
|
ChangeDate: timestamppb.Now(),
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: instance.ID(),
|
2024-02-28 10:21:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success all",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: adminCtx,
|
2024-02-28 10:21:11 +00:00
|
|
|
req: &admin_pb.SetSecurityPolicyRequest{
|
|
|
|
EnableIframeEmbedding: true,
|
|
|
|
AllowedOrigins: []string{"foo.com", "bar.com"},
|
|
|
|
EnableImpersonation: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &admin_pb.SetSecurityPolicyResponse{
|
|
|
|
Details: &object.ObjectDetails{
|
|
|
|
ChangeDate: timestamppb.Now(),
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: instance.ID(),
|
2024-02-28 10:21:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-09-06 12:47:57 +00:00
|
|
|
got, err := instance.Client.Admin.SetSecurityPolicy(tt.args.ctx, tt.args.req)
|
2024-02-28 10:21:11 +00:00
|
|
|
if tt.wantErr {
|
2024-10-17 21:20:57 +00:00
|
|
|
require.Error(t, err)
|
2024-02-28 10:21:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
integration.AssertDetails(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|