2021-03-19 10:12:56 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-22 10:56:43 +00:00
|
|
|
"go.uber.org/mock/gomock"
|
2021-03-19 10:12:56 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/zitadel/internal/id"
|
|
|
|
id_mock "github.com/zitadel/zitadel/internal/id/mock"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/idpconfig"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-03-19 10:12:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommandSide_AddDefaultIDPConfig(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
idGenerator id.Generator
|
|
|
|
secretCrypto crypto.EncryptionAlgorithm
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
config *domain.IDPConfig
|
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
want *domain.IDPConfig
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "invalid config, error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
config: &domain.IDPConfig{},
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "idp config oidc add, ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
instance.NewIDPConfigAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
|
|
|
"config1",
|
|
|
|
"name1",
|
|
|
|
domain.IDPConfigTypeOIDC,
|
|
|
|
domain.IDPConfigStylingTypeGoogle,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
instance.NewIDPOIDCConfigAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
|
|
|
"clientid1",
|
|
|
|
"config1",
|
|
|
|
"issuer",
|
|
|
|
"authorization-endpoint",
|
|
|
|
"token-endpoint",
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("secret"),
|
|
|
|
},
|
|
|
|
domain.OIDCMappingFieldEmail,
|
|
|
|
domain.OIDCMappingFieldEmail,
|
|
|
|
"scope",
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "config1"),
|
|
|
|
secretCrypto: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
args: args{
|
2022-04-05 05:58:09 +00:00
|
|
|
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
2021-03-19 10:12:56 +00:00
|
|
|
config: &domain.IDPConfig{
|
2021-09-10 07:49:49 +00:00
|
|
|
Name: "name1",
|
|
|
|
StylingType: domain.IDPConfigStylingTypeGoogle,
|
|
|
|
AutoRegister: true,
|
2021-03-19 10:12:56 +00:00
|
|
|
OIDCConfig: &domain.OIDCIDPConfig{
|
|
|
|
ClientID: "clientid1",
|
|
|
|
Issuer: "issuer",
|
2021-07-06 14:39:48 +00:00
|
|
|
AuthorizationEndpoint: "authorization-endpoint",
|
|
|
|
TokenEndpoint: "token-endpoint",
|
2021-03-19 10:12:56 +00:00
|
|
|
ClientSecretString: "secret",
|
|
|
|
Scopes: []string{"scope"},
|
|
|
|
IDPDisplayNameMapping: domain.OIDCMappingFieldEmail,
|
|
|
|
UsernameMapping: domain.OIDCMappingFieldEmail,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.IDPConfig{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
2022-04-05 05:58:09 +00:00
|
|
|
InstanceID: "INSTANCE",
|
|
|
|
AggregateID: "INSTANCE",
|
|
|
|
ResourceOwner: "INSTANCE",
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
2021-09-10 07:49:49 +00:00
|
|
|
IDPConfigID: "config1",
|
|
|
|
Name: "name1",
|
|
|
|
StylingType: domain.IDPConfigStylingTypeGoogle,
|
|
|
|
State: domain.IDPConfigStateActive,
|
|
|
|
AutoRegister: true,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-09-14 13:15:01 +00:00
|
|
|
{
|
|
|
|
name: "idp config jwt add, ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
instance.NewIDPConfigAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
|
|
|
"config1",
|
|
|
|
"name1",
|
|
|
|
domain.IDPConfigTypeOIDC,
|
|
|
|
domain.IDPConfigStylingTypeGoogle,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
instance.NewIDPJWTConfigAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
|
|
|
"config1",
|
|
|
|
"jwt-endpoint",
|
|
|
|
"issuer",
|
|
|
|
"keys-endpoint",
|
|
|
|
"auth",
|
|
|
|
),
|
2021-09-14 13:15:01 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "config1"),
|
|
|
|
},
|
|
|
|
args: args{
|
2022-04-05 05:58:09 +00:00
|
|
|
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
2021-09-14 13:15:01 +00:00
|
|
|
config: &domain.IDPConfig{
|
|
|
|
Name: "name1",
|
|
|
|
StylingType: domain.IDPConfigStylingTypeGoogle,
|
|
|
|
JWTConfig: &domain.JWTIDPConfig{
|
|
|
|
JWTEndpoint: "jwt-endpoint",
|
|
|
|
Issuer: "issuer",
|
|
|
|
KeysEndpoint: "keys-endpoint",
|
|
|
|
HeaderName: "auth",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.IDPConfig{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
2022-04-05 05:58:09 +00:00
|
|
|
InstanceID: "INSTANCE",
|
|
|
|
AggregateID: "INSTANCE",
|
|
|
|
ResourceOwner: "INSTANCE",
|
2021-09-14 13:15:01 +00:00
|
|
|
},
|
|
|
|
IDPConfigID: "config1",
|
|
|
|
Name: "name1",
|
|
|
|
StylingType: domain.IDPConfigStylingTypeGoogle,
|
|
|
|
State: domain.IDPConfigStateActive,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2022-04-20 14:59:37 +00:00
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
idGenerator: tt.fields.idGenerator,
|
|
|
|
idpConfigEncryption: tt.fields.secretCrypto,
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
got, err := r.AddDefaultIDPConfig(tt.args.ctx, tt.args.config)
|
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.res.err != nil && !tt.res.err(err) {
|
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.Equal(t, tt.res.want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_ChangeDefaultIDPConfig(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
config *domain.IDPConfig
|
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
want *domain.IDPConfig
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "invalid config, error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
config: &domain.IDPConfig{},
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "config not existing, not found error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
config: &domain.IDPConfig{
|
|
|
|
IDPConfigID: "config1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsNotFound,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "idp config change, ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewIDPConfigAddedEvent(context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-03-19 10:12:56 +00:00
|
|
|
"config1",
|
|
|
|
"name1",
|
|
|
|
domain.IDPConfigTypeOIDC,
|
|
|
|
domain.IDPConfigStylingTypeGoogle,
|
2021-09-10 07:49:49 +00:00
|
|
|
true,
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewIDPOIDCConfigAddedEvent(context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-03-19 10:12:56 +00:00
|
|
|
"clientid1",
|
|
|
|
"config1",
|
|
|
|
"issuer",
|
2021-07-06 14:39:48 +00:00
|
|
|
"authorization-endpoint",
|
|
|
|
"token-endpoint",
|
2021-03-19 10:12:56 +00:00
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
domain.OIDCMappingFieldEmail,
|
|
|
|
domain.OIDCMappingFieldEmail,
|
|
|
|
"scope",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
newDefaultIDPConfigChangedEvent(context.Background(), "config1", "name1", "name2", domain.IDPConfigStylingTypeUnspecified, false),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
config: &domain.IDPConfig{
|
2021-09-10 07:49:49 +00:00
|
|
|
IDPConfigID: "config1",
|
|
|
|
Name: "name2",
|
|
|
|
StylingType: domain.IDPConfigStylingTypeUnspecified,
|
|
|
|
AutoRegister: false,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.IDPConfig{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
2022-04-05 05:58:09 +00:00
|
|
|
AggregateID: "INSTANCE",
|
|
|
|
ResourceOwner: "INSTANCE",
|
2023-10-19 10:19:10 +00:00
|
|
|
InstanceID: "INSTANCE",
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
2021-09-10 07:49:49 +00:00
|
|
|
IDPConfigID: "config1",
|
|
|
|
Name: "name2",
|
|
|
|
StylingType: domain.IDPConfigStylingTypeUnspecified,
|
|
|
|
State: domain.IDPConfigStateActive,
|
|
|
|
AutoRegister: false,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
}
|
|
|
|
got, err := r.ChangeDefaultIDPConfig(tt.args.ctx, tt.args.config)
|
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.res.err != nil && !tt.res.err(err) {
|
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.Equal(t, tt.res.want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func newDefaultIDPConfigChangedEvent(ctx context.Context, configID, oldName, newName string, stylingType domain.IDPConfigStylingType, autoRegister bool) *instance.IDPConfigChangedEvent {
|
|
|
|
event, _ := instance.NewIDPConfigChangedEvent(ctx,
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-03-19 10:12:56 +00:00
|
|
|
configID,
|
|
|
|
oldName,
|
|
|
|
[]idpconfig.IDPConfigChanges{
|
|
|
|
idpconfig.ChangeName(newName),
|
|
|
|
idpconfig.ChangeStyleType(stylingType),
|
2021-09-10 07:49:49 +00:00
|
|
|
idpconfig.ChangeAutoRegister(autoRegister),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
return event
|
|
|
|
}
|