feat: Converter tests (#9452)

This commit is contained in:
Marco Ardizzone
2025-04-29 10:47:04 +02:00
parent ef57a3de41
commit 1490ba9860
2 changed files with 201 additions and 24 deletions

View File

@@ -120,21 +120,21 @@ func instanceQueryToModel(searchQuery *instance.Query) (query.SearchQuery, error
func CreateInstancePbToSetupInstance(req *instance.CreateInstanceRequest, defaultInstance command.InstanceSetup, externalDomain string) *command.InstanceSetup {
instance := defaultInstance
if req.InstanceName != "" {
instance.InstanceName = req.InstanceName
instance.Org.Name = req.InstanceName
if trimmed := strings.TrimSpace(req.InstanceName); trimmed != "" {
instance.InstanceName = trimmed
instance.Org.Name = trimmed
}
if req.CustomDomain != "" {
instance.CustomDomain = req.CustomDomain
if trimmed := strings.TrimSpace(req.CustomDomain); trimmed != "" {
instance.CustomDomain = trimmed
}
if req.FirstOrgName != "" {
instance.Org.Name = req.FirstOrgName
if trimmed := strings.TrimSpace(req.FirstOrgName); trimmed != "" {
instance.Org.Name = trimmed
}
if user := req.GetMachine(); user != nil {
defaultMachine := instance.Org.Machine
if defaultMachine == nil {
defaultMachine = new(command.AddMachine)
defaultMachine = &command.AddMachine{}
}
instance.Org.Machine = createInstancePbToAddMachine(user, *defaultMachine)
@@ -142,14 +142,14 @@ func CreateInstancePbToSetupInstance(req *instance.CreateInstanceRequest, defaul
} else if user := req.GetHuman(); user != nil {
defaultHuman := instance.Org.Human
if instance.Org.Human != nil {
defaultHuman = new(command.AddHuman)
defaultHuman = &command.AddHuman{}
}
instance.Org.Human = createInstancePbToAddHuman(user, *defaultHuman, instance.DomainPolicy.UserLoginMustBeDomain, instance.Org.Name, externalDomain)
instance.Org.Machine = nil
}
if lang := language.Make(req.DefaultLanguage); !lang.IsRoot() {
if lang := language.Make(strings.TrimSpace(req.DefaultLanguage)); !lang.IsRoot() {
instance.DefaultLanguage = lang
}
@@ -163,14 +163,14 @@ func createInstancePbToAddHuman(req *instance.CreateInstanceRequest_Human, defau
user.Email.Verified = req.Email.IsEmailVerified
}
if req.Profile != nil {
if req.Profile.FirstName != "" {
user.FirstName = req.Profile.FirstName
if firstName := strings.TrimSpace(req.Profile.FirstName); firstName != "" {
user.FirstName = firstName
}
if req.Profile.LastName != "" {
user.LastName = req.Profile.LastName
if lastName := strings.TrimSpace(req.Profile.LastName); lastName != "" {
user.LastName = lastName
}
if req.Profile.PreferredLanguage != "" {
lang, err := language.Parse(req.Profile.PreferredLanguage)
if lang := strings.TrimSpace(req.Profile.PreferredLanguage); lang != "" {
lang, err := language.Parse(lang)
if err == nil {
user.PreferredLanguage = lang
}
@@ -182,8 +182,8 @@ func createInstancePbToAddHuman(req *instance.CreateInstanceRequest_Human, defau
orgDomain, _ := domain.NewIAMDomainName(org, externalDomain)
user.Username = user.Username + "@" + orgDomain
}
if req.UserName != "" {
user.Username = req.UserName
if username := strings.TrimSpace(req.UserName); username != "" {
user.Username = username
}
if req.Password != nil {
@@ -194,19 +194,19 @@ func createInstancePbToAddHuman(req *instance.CreateInstanceRequest_Human, defau
}
func createInstancePbToAddMachine(req *instance.CreateInstanceRequest_Machine, defaultMachine command.AddMachine) (machine *command.AddMachine) {
machine = new(command.AddMachine)
machine = &command.AddMachine{}
if defaultMachine.Machine != nil {
machineCopy := *defaultMachine.Machine
machine.Machine = &machineCopy
} else {
machine.Machine = new(command.Machine)
machine.Machine = &command.Machine{}
}
if req.UserName != "" {
machine.Machine.Username = req.UserName
if username := strings.TrimSpace(req.UserName); username != "" {
machine.Machine.Username = username
}
if req.Name != "" {
machine.Machine.Name = req.Name
if name := strings.TrimSpace(req.Name); name != "" {
machine.Machine.Name = name
}
if defaultMachine.Pat != nil || req.PersonalAccessToken != nil {

View File

@@ -7,13 +7,19 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zitadel/oidc/v3/pkg/oidc"
"github.com/zitadel/zitadel/cmd/build"
z_oidc "github.com/zitadel/zitadel/internal/api/oidc"
"github.com/zitadel/zitadel/internal/command"
"github.com/zitadel/zitadel/internal/config/systemdefaults"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/query"
"github.com/zitadel/zitadel/internal/zerrors"
authn "github.com/zitadel/zitadel/pkg/grpc/authn/v2beta"
filter "github.com/zitadel/zitadel/pkg/grpc/filter/v2beta"
instance "github.com/zitadel/zitadel/pkg/grpc/instance/v2beta"
"github.com/zitadel/zitadel/pkg/grpc/object/v2"
"golang.org/x/text/language"
"google.golang.org/protobuf/types/known/timestamppb"
)
@@ -226,3 +232,174 @@ func Test_instanceQueryToModel(t *testing.T) {
})
}
}
func Test_CreateInstancePbToSetupInstance(t *testing.T) {
t.Parallel()
nowTS := timestamppb.Now()
tests := []struct {
name string
req *instance.CreateInstanceRequest
defaultInstance command.InstanceSetup
externalDomain string
want *command.InstanceSetup
}{
{
name: "Set instance name, custom domain and organization name",
req: &instance.CreateInstanceRequest{
InstanceName: " TestInstance",
CustomDomain: "test.com ",
FirstOrgName: " org ",
},
defaultInstance: command.InstanceSetup{},
externalDomain: "external.com",
want: &command.InstanceSetup{
InstanceName: "TestInstance",
CustomDomain: "test.com",
Org: command.InstanceOrgSetup{
Name: "org",
},
},
},
{
name: "Set machine user with PAT",
req: &instance.CreateInstanceRequest{
Owner: &instance.CreateInstanceRequest_Machine_{
Machine: &instance.CreateInstanceRequest_Machine{
UserName: "machine-user",
Name: "Machine Name",
PersonalAccessToken: &instance.CreateInstanceRequest_PersonalAccessToken{
ExpirationDate: nowTS,
},
MachineKey: &instance.CreateInstanceRequest_MachineKey{
Type: authn.KeyType(authn.KeyType_KEY_TYPE_JSON),
ExpirationDate: nowTS,
},
},
},
},
defaultInstance: command.InstanceSetup{
Org: command.InstanceOrgSetup{},
},
externalDomain: "external.com",
want: &command.InstanceSetup{
Org: command.InstanceOrgSetup{
Machine: &command.AddMachine{
Machine: &command.Machine{
Username: "machine-user",
Name: "Machine Name",
},
Pat: &command.AddPat{
ExpirationDate: nowTS.AsTime(),
Scopes: []string{oidc.ScopeOpenID, oidc.ScopeProfile, z_oidc.ScopeUserMetaData, z_oidc.ScopeResourceOwner},
},
MachineKey: &command.AddMachineKey{
Type: domain.AuthNKeyTypeJSON,
ExpirationDate: nowTS.AsTime(),
},
},
},
},
},
{
name: "Set machine user with default machine PAT",
req: &instance.CreateInstanceRequest{
Owner: &instance.CreateInstanceRequest_Machine_{
Machine: &instance.CreateInstanceRequest_Machine{
UserName: "machine-user",
Name: "Machine Name",
PersonalAccessToken: &instance.CreateInstanceRequest_PersonalAccessToken{},
MachineKey: &instance.CreateInstanceRequest_MachineKey{},
},
},
},
defaultInstance: command.InstanceSetup{
Org: command.InstanceOrgSetup{
Machine: &command.AddMachine{
Pat: &command.AddPat{
ExpirationDate: nowTS.AsTime(),
},
MachineKey: &command.AddMachineKey{
Type: domain.AuthNKeyTypeJSON,
ExpirationDate: nowTS.AsTime(),
},
},
},
},
externalDomain: "external.com",
want: &command.InstanceSetup{
Org: command.InstanceOrgSetup{
Machine: &command.AddMachine{
Machine: &command.Machine{
Username: "machine-user",
Name: "Machine Name",
},
Pat: &command.AddPat{
ExpirationDate: nowTS.AsTime(),
Scopes: []string{oidc.ScopeOpenID, oidc.ScopeProfile, z_oidc.ScopeUserMetaData, z_oidc.ScopeResourceOwner},
},
MachineKey: &command.AddMachineKey{
Type: domain.AuthNKeyTypeJSON,
ExpirationDate: nowTS.AsTime(),
},
},
},
},
},
{
name: "Set human user",
req: &instance.CreateInstanceRequest{
Owner: &instance.CreateInstanceRequest_Human_{
Human: &instance.CreateInstanceRequest_Human{
UserName: "human-user ",
Email: &instance.CreateInstanceRequest_Email{
Email: "john.doe@example.com",
},
Profile: &instance.CreateInstanceRequest_Profile{
FirstName: "John ",
LastName: " Doe",
PreferredLanguage: "it ",
},
Password: &instance.CreateInstanceRequest_Password{},
},
},
},
defaultInstance: command.InstanceSetup{
Org: command.InstanceOrgSetup{Human: &command.AddHuman{}},
},
externalDomain: "external.com",
want: &command.InstanceSetup{
Org: command.InstanceOrgSetup{
Human: &command.AddHuman{
Username: "human-user",
FirstName: "John",
LastName: "Doe",
Email: command.Email{
Address: "john.doe@example.com",
},
PreferredLanguage: language.Italian,
},
},
},
},
{
name: "Set default language",
req: &instance.CreateInstanceRequest{
DefaultLanguage: " en ",
},
defaultInstance: command.InstanceSetup{},
externalDomain: "external.com",
want: &command.InstanceSetup{
DefaultLanguage: language.English,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := CreateInstancePbToSetupInstance(tt.req, tt.defaultInstance, tt.externalDomain)
assert.Equal(t, tt.want, got)
})
}
}