diff --git a/internal/command/org_idp_config.go b/internal/command/org_idp_config.go index 73f824241..b105db8f7 100644 --- a/internal/command/org_idp_config.go +++ b/internal/command/org_idp_config.go @@ -177,17 +177,14 @@ func (c *Commands) removeIDPConfig(ctx context.Context, existingIDP *OrgIDPConfi if existingIDP.State == domain.IDPConfigStateRemoved || existingIDP.State == domain.IDPConfigStateUnspecified { return nil, caos_errs.ThrowNotFound(nil, "Org-Yx9vd", "Errors.Org.IDPConfig.NotExisting") } - if existingIDP.State != domain.IDPConfigStateInactive { - return nil, caos_errs.ThrowPreconditionFailed(nil, "Org-5Mo0d", "Errors.Org.IDPConfig.NotInactive") - } orgAgg := OrgAggregateFromWriteModel(&existingIDP.WriteModel) events := []eventstore.EventPusher{ - org_repo.NewIDPConfigRemovedEvent(ctx, orgAgg, existingIDP.AggregateID, existingIDP.Name), + org_repo.NewIDPConfigRemovedEvent(ctx, orgAgg, existingIDP.ConfigID, existingIDP.Name), } if cascadeRemoveProvider { - removeIDPEvents := c.removeIDPProviderFromLoginPolicy(ctx, orgAgg, existingIDP.AggregateID, true, cascadeExternalIDPs...) + removeIDPEvents := c.removeIDPProviderFromLoginPolicy(ctx, orgAgg, existingIDP.ConfigID, true, cascadeExternalIDPs...) events = append(events, removeIDPEvents...) } return events, nil diff --git a/internal/command/org_idp_config_test.go b/internal/command/org_idp_config_test.go index b19f418e6..6aa4bce90 100644 --- a/internal/command/org_idp_config_test.go +++ b/internal/command/org_idp_config_test.go @@ -6,6 +6,7 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" + "golang.org/x/text/language" "github.com/caos/zitadel/internal/crypto" "github.com/caos/zitadel/internal/domain" @@ -17,6 +18,7 @@ import ( id_mock "github.com/caos/zitadel/internal/id/mock" "github.com/caos/zitadel/internal/repository/idpconfig" "github.com/caos/zitadel/internal/repository/org" + "github.com/caos/zitadel/internal/repository/user" ) func TestCommandSide_AddIDPConfig(t *testing.T) { @@ -414,3 +416,190 @@ func newIDPConfigChangedEvent(ctx context.Context, orgID, configID, oldName, new ) return event } + +func TestCommands_RemoveIDPConfig(t *testing.T) { + type fields struct { + eventstore *eventstore.Eventstore + } + type args struct { + ctx context.Context + idpID string + orgID string + cascadeRemoveProvider bool + cascadeExternalIDPs []*domain.ExternalIDP + } + type res struct { + want *domain.ObjectDetails + err func(error) bool + } + tests := []struct { + name string + fields fields + args args + res res + }{ + { + "not existing, error", + fields{ + eventstore: eventstoreExpect(t, + expectFilter(), + ), + }, + args{ + context.Background(), + "idp1", + "org1", + false, + nil, + }, + res{ + nil, + caos_errs.IsNotFound, + }, + }, + { + "no cascade, ok", + fields{ + eventstore: eventstoreExpect(t, + expectFilter( + eventFromEventPusher( + org.NewIDPConfigAddedEvent(context.Background(), + &org.NewAggregate("org1", "org1").Aggregate, + "idp1", + "name1", + domain.IDPConfigTypeOIDC, + domain.IDPConfigStylingTypeGoogle, + false, + ), + ), + ), + expectPush( + eventPusherToEvents( + org.NewIDPConfigRemovedEvent(context.Background(), + &org.NewAggregate("org1", "org1").Aggregate, + "idp1", + "name1", + ), + ), + uniqueConstraintsFromEventConstraint(idpconfig.NewRemoveIDPConfigNameUniqueConstraint("name1", "org1")), + ), + ), + }, + args{ + context.Background(), + "idp1", + "org1", + false, + nil, + }, + res{ + &domain.ObjectDetails{ + ResourceOwner: "org1", + }, + nil, + }, + }, + { + "cascade, ok", + fields{ + eventstore: eventstoreExpect(t, + expectFilter( + eventFromEventPusher( + org.NewIDPConfigAddedEvent(context.Background(), + &org.NewAggregate("org1", "org1").Aggregate, + "idp1", + "name1", + domain.IDPConfigTypeOIDC, + domain.IDPConfigStylingTypeGoogle, + false, + ), + ), + ), + expectFilter( + eventFromEventPusher( + user.NewHumanAddedEvent(context.Background(), + &org.NewAggregate("user1", "org1").Aggregate, + "username", + "firstname", + "lastname", + "nickname", + "displayName", + language.German, + domain.GenderUnspecified, + "email@test.com", + true, + ), + ), + eventFromEventPusher( + user.NewHumanExternalIDPAddedEvent(context.Background(), + &org.NewAggregate("user1", "org1").Aggregate, + "idp1", + "name", + "id1", + ), + ), + ), + expectPush( + eventPusherToEvents( + org.NewIDPConfigRemovedEvent(context.Background(), + &org.NewAggregate("org1", "org1").Aggregate, + "idp1", + "name1", + ), + org.NewIdentityProviderCascadeRemovedEvent(context.Background(), + &org.NewAggregate("org1", "org1").Aggregate, + "idp1", + ), + user.NewHumanExternalIDPCascadeRemovedEvent(context.Background(), + &user.NewAggregate("user1", "org1").Aggregate, + "idp1", + "id1", + ), + ), + uniqueConstraintsFromEventConstraint(idpconfig.NewRemoveIDPConfigNameUniqueConstraint("name1", "org1")), + uniqueConstraintsFromEventConstraint(user.NewRemoveExternalIDPUniqueConstraint("idp1", "id1")), + ), + ), + }, + args{ + context.Background(), + "idp1", + "org1", + true, + []*domain.ExternalIDP{ + { + ObjectRoot: models.ObjectRoot{ + AggregateID: "user1", + }, + IDPConfigID: "idp1", + ExternalUserID: "id1", + DisplayName: "name", + }, + }, + }, + res{ + &domain.ObjectDetails{ + ResourceOwner: "org1", + }, + nil, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &Commands{ + eventstore: tt.fields.eventstore, + } + got, err := c.RemoveIDPConfig(tt.args.ctx, tt.args.idpID, tt.args.orgID, tt.args.cascadeRemoveProvider, tt.args.cascadeExternalIDPs...) + 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) + } + }) + } +} diff --git a/internal/repository/idpconfig/idp_config.go b/internal/repository/idpconfig/idp_config.go index 5ce71f4ba..9951effcb 100644 --- a/internal/repository/idpconfig/idp_config.go +++ b/internal/repository/idpconfig/idp_config.go @@ -2,10 +2,10 @@ package idpconfig import ( "encoding/json" - "github.com/caos/zitadel/internal/eventstore" "github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/errors" + "github.com/caos/zitadel/internal/eventstore" "github.com/caos/zitadel/internal/eventstore/repository" ) @@ -232,7 +232,7 @@ type IDPConfigRemovedEvent struct { eventstore.BaseEvent `json:"-"` ConfigID string `json:"idpConfigId"` - Name string + name string } func NewIDPConfigRemovedEvent( @@ -244,7 +244,7 @@ func NewIDPConfigRemovedEvent( return &IDPConfigRemovedEvent{ BaseEvent: *base, ConfigID: configID, - Name: name, + name: name, } } @@ -253,7 +253,7 @@ func (e *IDPConfigRemovedEvent) Data() interface{} { } func (e *IDPConfigRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint { - return []*eventstore.EventUniqueConstraint{NewRemoveIDPConfigNameUniqueConstraint(e.Name, e.Aggregate().ResourceOwner)} + return []*eventstore.EventUniqueConstraint{NewRemoveIDPConfigNameUniqueConstraint(e.name, e.Aggregate().ResourceOwner)} } func IDPConfigRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {