From 9ffc9d9330c116ff45df8d21fb436ef8e24da113 Mon Sep 17 00:00:00 2001 From: Livio Amstutz Date: Fri, 4 Jun 2021 15:44:45 +0200 Subject: [PATCH] fix: check if org exists when setting features (#1813) * fix: check if org exists when setting features * fix tests --- internal/command/org_features.go | 6 ++- internal/command/org_features_test.go | 77 +++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/internal/command/org_features.go b/internal/command/org_features.go index 37cd5f162d..2e551f85d0 100644 --- a/internal/command/org_features.go +++ b/internal/command/org_features.go @@ -13,8 +13,12 @@ func (c *Commands) SetOrgFeatures(ctx context.Context, resourceOwner string, fea if resourceOwner == "" { return nil, caos_errs.ThrowInvalidArgument(nil, "Features-G5tg", "Errors.ResourceOwnerMissing") } + err := c.checkOrgExists(ctx, resourceOwner) + if err != nil { + return nil, err + } existingFeatures := NewOrgFeaturesWriteModel(resourceOwner) - err := c.eventstore.FilterToQueryReducer(ctx, existingFeatures) + err = c.eventstore.FilterToQueryReducer(ctx, existingFeatures) if err != nil { return nil, err } diff --git a/internal/command/org_features_test.go b/internal/command/org_features_test.go index d8e3ddffe3..afcc73448c 100644 --- a/internal/command/org_features_test.go +++ b/internal/command/org_features_test.go @@ -104,11 +104,51 @@ func TestCommandSide_SetOrgFeatures(t *testing.T) { err: caos_errs.IsPreconditionFailed, }, }, + { + name: "org does not exist, error", + fields: fields{ + eventstore: eventstoreExpect( + t, + expectFilter(), + ), + }, + args: args{ + ctx: context.Background(), + resourceOwner: "org1", + features: &domain.Features{ + TierName: "Test", + State: domain.FeaturesStateActive, + AuditLogRetention: time.Hour, + LoginPolicyFactors: false, + LoginPolicyIDP: false, + LoginPolicyPasswordless: false, + LoginPolicyRegistration: false, + LoginPolicyUsernameLogin: false, + LoginPolicyPasswordReset: false, + PasswordComplexityPolicy: false, + LabelPolicyPrivateLabel: false, + LabelPolicyWatermark: false, + CustomDomain: false, + }, + }, + res: res{ + err: caos_errs.IsPreconditionFailed, + }, + }, { name: "set with default policies, ok", fields: fields{ eventstore: eventstoreExpect( t, + expectFilter( + eventFromEventPusher( + org.NewOrgAddedEvent( + context.Background(), + &org.NewAggregate("org1", "org1").Aggregate, + "org1", + ), + ), + ), expectFilter(), expectFilter( eventFromEventPusher( @@ -226,6 +266,15 @@ func TestCommandSide_SetOrgFeatures(t *testing.T) { fields: fields{ eventstore: eventstoreExpect( t, + expectFilter( + eventFromEventPusher( + org.NewOrgAddedEvent( + context.Background(), + &org.NewAggregate("org1", "org1").Aggregate, + "org1", + ), + ), + ), expectFilter(), expectFilter( eventFromEventPusher( @@ -371,6 +420,15 @@ func TestCommandSide_SetOrgFeatures(t *testing.T) { fields: fields{ eventstore: eventstoreExpect( t, + expectFilter( + eventFromEventPusher( + org.NewOrgAddedEvent( + context.Background(), + &org.NewAggregate("org1", "org1").Aggregate, + "org1", + ), + ), + ), expectFilter(), expectFilter( eventFromEventPusher( @@ -526,6 +584,15 @@ func TestCommandSide_SetOrgFeatures(t *testing.T) { fields: fields{ eventstore: eventstoreExpect( t, + expectFilter( + eventFromEventPusher( + org.NewOrgAddedEvent( + context.Background(), + &org.NewAggregate("org1", "org1").Aggregate, + "org1", + ), + ), + ), expectFilter(), expectFilter( eventFromEventPusher( @@ -691,6 +758,16 @@ func TestCommandSide_SetOrgFeatures(t *testing.T) { fields: fields{ eventstore: eventstoreExpect( t, + //checkOrgExists + expectFilter( + eventFromEventPusher( + org.NewOrgAddedEvent( + context.Background(), + &org.NewAggregate("org1", "org1").Aggregate, + "org1", + ), + ), + ), //NewOrgFeaturesWriteModel expectFilter(), //begin ensureOrgSettingsToFeatures