package command import ( "context" "testing" "github.com/stretchr/testify/assert" "github.com/zitadel/zitadel/internal/domain" "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/repository/org" "github.com/zitadel/zitadel/internal/repository/policy" "github.com/zitadel/zitadel/internal/zerrors" ) func TestCommandSide_AddNotificationPolicy(t *testing.T) { type fields struct { eventstore *eventstore.Eventstore } type args struct { ctx context.Context orgID string passwordChange bool } type res struct { want *domain.ObjectDetails err func(error) bool } tests := []struct { name string fields fields args args res res }{ { name: "org id missing, invalid argument error", fields: fields{ eventstore: eventstoreExpect( t, ), }, args: args{ ctx: context.Background(), orgID: "", passwordChange: true, }, res: res{ err: zerrors.IsErrorInvalidArgument, }, }, { name: "policy already existing, already exists error", fields: fields{ eventstore: eventstoreExpect( t, expectFilter( eventFromEventPusher( org.NewNotificationPolicyAddedEvent(context.Background(), &org.NewAggregate("org1").Aggregate, true, ), ), ), ), }, args: args{ ctx: context.Background(), orgID: "org1", passwordChange: true, }, res: res{ err: zerrors.IsErrorAlreadyExists, }, }, { name: "add policy,ok", fields: fields{ eventstore: eventstoreExpect( t, expectFilter(), expectPush( org.NewNotificationPolicyAddedEvent(context.Background(), &org.NewAggregate("org1").Aggregate, true, ), ), ), }, args: args{ ctx: context.Background(), orgID: "org1", passwordChange: true, }, res: res{ want: &domain.ObjectDetails{ ResourceOwner: "org1", }, }, }, { name: "add policy empty, ok", fields: fields{ eventstore: eventstoreExpect( t, expectFilter(), expectPush( org.NewNotificationPolicyAddedEvent(context.Background(), &org.NewAggregate("org1").Aggregate, false, ), ), ), }, args: args{ ctx: context.Background(), orgID: "org1", passwordChange: false, }, res: res{ want: &domain.ObjectDetails{ ResourceOwner: "org1", }, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { r := &Commands{ eventstore: tt.fields.eventstore, } got, err := r.AddNotificationPolicy(tt.args.ctx, tt.args.orgID, tt.args.passwordChange) 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_ChangeNotificationPolicy(t *testing.T) { type fields struct { eventstore *eventstore.Eventstore } type args struct { ctx context.Context orgID string passwordChange bool } type res struct { want *domain.ObjectDetails err func(error) bool } tests := []struct { name string fields fields args args res res }{ { name: "org id missing, invalid argument error", fields: fields{ eventstore: eventstoreExpect( t, ), }, args: args{ ctx: context.Background(), passwordChange: true, }, res: res{ err: zerrors.IsErrorInvalidArgument, }, }, { name: "policy not existing, not found error", fields: fields{ eventstore: eventstoreExpect( t, expectFilter(), ), }, args: args{ ctx: context.Background(), orgID: "org1", passwordChange: true, }, res: res{ err: zerrors.IsNotFound, }, }, { name: "no changes, precondition error", fields: fields{ eventstore: eventstoreExpect( t, expectFilter( eventFromEventPusher( org.NewNotificationPolicyAddedEvent(context.Background(), &org.NewAggregate("org1").Aggregate, true, ), ), ), ), }, args: args{ ctx: context.Background(), orgID: "org1", passwordChange: true, }, res: res{ err: zerrors.IsPreconditionFailed, }, }, { name: "change, ok", fields: fields{ eventstore: eventstoreExpect( t, expectFilter( eventFromEventPusher( org.NewNotificationPolicyAddedEvent(context.Background(), &org.NewAggregate("org1").Aggregate, true, ), ), ), expectPush( newNotificationPolicyChangedEvent(context.Background(), "org1", false), ), ), }, args: args{ ctx: context.Background(), orgID: "org1", passwordChange: false, }, res: res{ want: &domain.ObjectDetails{ ResourceOwner: "org1", }, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { r := &Commands{ eventstore: tt.fields.eventstore, } got, err := r.ChangeNotificationPolicy(tt.args.ctx, tt.args.orgID, tt.args.passwordChange) 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_RemoveNotificationPolicy(t *testing.T) { type fields struct { eventstore *eventstore.Eventstore } type args struct { ctx context.Context orgID string } type res struct { want *domain.ObjectDetails err func(error) bool } tests := []struct { name string fields fields args args res res }{ { name: "org id missing, invalid argument error", fields: fields{ eventstore: eventstoreExpect( t, ), }, args: args{ ctx: context.Background(), }, res: res{ err: zerrors.IsErrorInvalidArgument, }, }, { name: "policy not existing, not found error", fields: fields{ eventstore: eventstoreExpect( t, expectFilter(), ), }, args: args{ ctx: context.Background(), orgID: "org1", }, res: res{ err: zerrors.IsNotFound, }, }, { name: "remove, ok", fields: fields{ eventstore: eventstoreExpect( t, expectFilter( eventFromEventPusher( org.NewNotificationPolicyAddedEvent(context.Background(), &org.NewAggregate("org1").Aggregate, true, ), ), ), expectPush( org.NewNotificationPolicyRemovedEvent(context.Background(), &org.NewAggregate("org1").Aggregate), ), ), }, args: args{ ctx: context.Background(), orgID: "org1", }, res: res{ want: &domain.ObjectDetails{ ResourceOwner: "org1", }, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { r := &Commands{ eventstore: tt.fields.eventstore, } got, err := r.RemoveNotificationPolicy(tt.args.ctx, tt.args.orgID) 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 newNotificationPolicyChangedEvent(ctx context.Context, orgID string, passwordChange bool) *org.NotificationPolicyChangedEvent { event, _ := org.NewNotificationPolicyChangedEvent(ctx, &org.NewAggregate(orgID).Aggregate, []policy.NotificationPolicyChanges{ policy.ChangePasswordChange(passwordChange), }, ) return event }