2021-09-14 13:15:01 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/repository"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/idpconfig"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2021-09-14 13:15:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommandSide_ChangeDefaultIDPJWTConfig(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
secretCrypto crypto.EncryptionAlgorithm
|
|
|
|
}
|
|
|
|
type (
|
|
|
|
args struct {
|
2022-04-05 05:58:09 +00:00
|
|
|
ctx context.Context
|
|
|
|
instanceID string
|
|
|
|
config *domain.JWTIDPConfig
|
2021-09-14 13:15:01 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
type res struct {
|
|
|
|
want *domain.JWTIDPConfig
|
|
|
|
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{
|
2022-04-05 05:58:09 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
instanceID: "INSTANCE",
|
|
|
|
config: &domain.JWTIDPConfig{},
|
2021-09-14 13:15:01 +00:00
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: caos_errs.IsErrorInvalidArgument,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "idp config not existing, not found error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2022-04-05 05:58:09 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
instanceID: "INSTANCE",
|
2021-09-14 13:15:01 +00:00
|
|
|
config: &domain.JWTIDPConfig{
|
|
|
|
IDPConfigID: "config1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: caos_errs.IsNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "idp config removed, not found error",
|
|
|
|
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-09-14 13:15:01 +00:00
|
|
|
"config1",
|
|
|
|
"name1",
|
|
|
|
domain.IDPConfigTypeJWT,
|
|
|
|
domain.IDPConfigStylingTypeGoogle,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewIDPJWTConfigAddedEvent(context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-09-14 13:15:01 +00:00
|
|
|
"config1",
|
|
|
|
"jwt-endpoint",
|
|
|
|
"issuer",
|
|
|
|
"keys-endpoint",
|
|
|
|
"auth",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewIDPConfigRemovedEvent(context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-09-14 13:15:01 +00:00
|
|
|
"config1",
|
|
|
|
"name",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2022-04-05 05:58:09 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
instanceID: "INSTANCE",
|
2021-09-14 13:15:01 +00:00
|
|
|
config: &domain.JWTIDPConfig{
|
|
|
|
IDPConfigID: "config1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: caos_errs.IsNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no changes, precondition error",
|
|
|
|
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-09-14 13:15:01 +00:00
|
|
|
"config1",
|
|
|
|
"name1",
|
|
|
|
domain.IDPConfigTypeJWT,
|
|
|
|
domain.IDPConfigStylingTypeGoogle,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewIDPJWTConfigAddedEvent(context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-09-14 13:15:01 +00:00
|
|
|
"config1",
|
|
|
|
"jwt-endpoint",
|
|
|
|
"issuer",
|
|
|
|
"keys-endpoint",
|
|
|
|
"auth",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
secretCrypto: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
args: args{
|
2022-04-05 05:58:09 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
instanceID: "INSTANCE",
|
2021-09-14 13:15:01 +00:00
|
|
|
config: &domain.JWTIDPConfig{
|
|
|
|
IDPConfigID: "config1",
|
|
|
|
JWTEndpoint: "jwt-endpoint",
|
|
|
|
Issuer: "issuer",
|
|
|
|
KeysEndpoint: "keys-endpoint",
|
|
|
|
HeaderName: "auth",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: caos_errs.IsPreconditionFailed,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "idp config jwt add, 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-09-14 13:15:01 +00:00
|
|
|
"config1",
|
|
|
|
"name1",
|
|
|
|
domain.IDPConfigTypeJWT,
|
|
|
|
domain.IDPConfigStylingTypeGoogle,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewIDPJWTConfigAddedEvent(context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-09-14 13:15:01 +00:00
|
|
|
"config1",
|
|
|
|
"jwt-endpoint",
|
|
|
|
"issuer",
|
|
|
|
"keys-endpoint",
|
|
|
|
"auth",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
[]*repository.Event{
|
|
|
|
eventFromEventPusher(
|
|
|
|
newDefaultIDPJWTConfigChangedEvent(context.Background(),
|
|
|
|
"config1",
|
|
|
|
"jwt-endpoint-changed",
|
|
|
|
"issuer-changed",
|
|
|
|
"keys-endpoint-changed",
|
|
|
|
"auth-changed",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
secretCrypto: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
args: args{
|
2022-04-05 05:58:09 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
instanceID: "INSTANCE",
|
2021-09-14 13:15:01 +00:00
|
|
|
config: &domain.JWTIDPConfig{
|
|
|
|
IDPConfigID: "config1",
|
|
|
|
JWTEndpoint: "jwt-endpoint-changed",
|
|
|
|
Issuer: "issuer-changed",
|
|
|
|
KeysEndpoint: "keys-endpoint-changed",
|
|
|
|
HeaderName: "auth-changed",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.JWTIDPConfig{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
2022-04-05 05:58:09 +00:00
|
|
|
AggregateID: "INSTANCE",
|
|
|
|
ResourceOwner: "INSTANCE",
|
2021-09-14 13:15:01 +00:00
|
|
|
},
|
|
|
|
IDPConfigID: "config1",
|
|
|
|
JWTEndpoint: "jwt-endpoint-changed",
|
|
|
|
Issuer: "issuer-changed",
|
|
|
|
KeysEndpoint: "keys-endpoint-changed",
|
|
|
|
HeaderName: "auth-changed",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
idpConfigEncryption: tt.fields.secretCrypto,
|
2021-09-14 13:15:01 +00:00
|
|
|
}
|
|
|
|
got, err := r.ChangeDefaultIDPJWTConfig(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 newDefaultIDPJWTConfigChangedEvent(ctx context.Context, configID, jwtEndpoint, issuer, keysEndpoint, headerName string) *instance.IDPJWTConfigChangedEvent {
|
|
|
|
event, _ := instance.NewIDPJWTConfigChangedEvent(ctx,
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-09-14 13:15:01 +00:00
|
|
|
configID,
|
|
|
|
[]idpconfig.JWTConfigChanges{
|
|
|
|
idpconfig.ChangeJWTEndpoint(jwtEndpoint),
|
|
|
|
idpconfig.ChangeJWTIssuer(issuer),
|
|
|
|
idpconfig.ChangeKeysEndpoint(keysEndpoint),
|
|
|
|
idpconfig.ChangeHeaderName(headerName),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return event
|
|
|
|
}
|