2021-07-05 08:36:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-24 13:00:24 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2021-07-05 08:36:51 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v1/models"
|
2022-03-24 16:21:34 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/instance"
|
2021-07-05 08:36:51 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/policy"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommandSide_AddDefaultPrivacyPolicy(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
policy *domain.PrivacyPolicy
|
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
want *domain.PrivacyPolicy
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "privacy policy already existing, already exists error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewPrivacyPolicyAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate().Aggregate,
|
2021-07-05 08:36:51 +00:00
|
|
|
"TOSLink",
|
|
|
|
"PrivacyLink",
|
2022-03-24 13:00:24 +00:00
|
|
|
"HelpLink",
|
2021-07-05 08:36:51 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
policy: &domain.PrivacyPolicy{
|
|
|
|
TOSLink: "TOSLink",
|
|
|
|
PrivacyLink: "PrivacyLink",
|
2022-03-24 13:00:24 +00:00
|
|
|
HelpLink: "HelpLink",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: caos_errs.IsErrorAlreadyExists,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "add policy,ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
expectPush(
|
|
|
|
[]*repository.Event{
|
|
|
|
eventFromEventPusher(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewPrivacyPolicyAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate().Aggregate,
|
2021-07-05 08:36:51 +00:00
|
|
|
"TOSLink",
|
|
|
|
"PrivacyLink",
|
2022-03-24 13:00:24 +00:00
|
|
|
"HelpLink",
|
2021-07-05 08:36:51 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
policy: &domain.PrivacyPolicy{
|
|
|
|
TOSLink: "TOSLink",
|
|
|
|
PrivacyLink: "PrivacyLink",
|
2022-03-24 13:00:24 +00:00
|
|
|
HelpLink: "HelpLink",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.PrivacyPolicy{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "IAM",
|
|
|
|
ResourceOwner: "IAM",
|
|
|
|
},
|
|
|
|
TOSLink: "TOSLink",
|
|
|
|
PrivacyLink: "PrivacyLink",
|
2022-03-24 13:00:24 +00:00
|
|
|
HelpLink: "HelpLink",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "add empty policy,ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
expectPush(
|
|
|
|
[]*repository.Event{
|
|
|
|
eventFromEventPusher(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewPrivacyPolicyAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate().Aggregate,
|
2021-07-05 08:36:51 +00:00
|
|
|
"",
|
|
|
|
"",
|
2022-03-24 13:00:24 +00:00
|
|
|
"",
|
2021-07-05 08:36:51 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
policy: &domain.PrivacyPolicy{
|
|
|
|
TOSLink: "",
|
|
|
|
PrivacyLink: "",
|
2022-03-24 13:00:24 +00:00
|
|
|
HelpLink: "",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.PrivacyPolicy{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "IAM",
|
|
|
|
ResourceOwner: "IAM",
|
|
|
|
},
|
|
|
|
TOSLink: "",
|
|
|
|
PrivacyLink: "",
|
2022-03-24 13:00:24 +00:00
|
|
|
HelpLink: "",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
}
|
|
|
|
got, err := r.AddDefaultPrivacyPolicy(tt.args.ctx, tt.args.policy)
|
|
|
|
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_ChangeDefaultPrivacyPolicy(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
policy *domain.PrivacyPolicy
|
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
want *domain.PrivacyPolicy
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "privacy policy not existing, not found error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
policy: &domain.PrivacyPolicy{
|
|
|
|
TOSLink: "TOSLink",
|
|
|
|
PrivacyLink: "PrivacyLink",
|
2022-03-24 13:00:24 +00:00
|
|
|
HelpLink: "HelpLink",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
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.NewPrivacyPolicyAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate().Aggregate,
|
2021-07-05 08:36:51 +00:00
|
|
|
"TOSLink",
|
|
|
|
"PrivacyLink",
|
2022-03-24 13:00:24 +00:00
|
|
|
"HelpLink",
|
2021-07-05 08:36:51 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
policy: &domain.PrivacyPolicy{
|
|
|
|
TOSLink: "TOSLink",
|
|
|
|
PrivacyLink: "PrivacyLink",
|
2022-03-24 13:00:24 +00:00
|
|
|
HelpLink: "HelpLink",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: caos_errs.IsPreconditionFailed,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "change, ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewPrivacyPolicyAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate().Aggregate,
|
2021-07-05 08:36:51 +00:00
|
|
|
"TOSLink",
|
|
|
|
"PrivacyLink",
|
2022-03-24 13:00:24 +00:00
|
|
|
"HelpLink",
|
2021-07-05 08:36:51 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
[]*repository.Event{
|
|
|
|
eventFromEventPusher(
|
|
|
|
newDefaultPrivacyPolicyChangedEvent(context.Background(),
|
|
|
|
"TOSLinkChanged",
|
|
|
|
"PrivacyLinkChanged",
|
2022-03-24 13:00:24 +00:00
|
|
|
"HelpLinkChanged",
|
2021-07-05 08:36:51 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
policy: &domain.PrivacyPolicy{
|
|
|
|
TOSLink: "TOSLinkChanged",
|
|
|
|
PrivacyLink: "PrivacyLinkChanged",
|
2022-03-24 13:00:24 +00:00
|
|
|
HelpLink: "HelpLinkChanged",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.PrivacyPolicy{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "IAM",
|
|
|
|
ResourceOwner: "IAM",
|
|
|
|
},
|
|
|
|
TOSLink: "TOSLinkChanged",
|
|
|
|
PrivacyLink: "PrivacyLinkChanged",
|
2022-03-24 13:00:24 +00:00
|
|
|
HelpLink: "HelpLinkChanged",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
}
|
|
|
|
got, err := r.ChangeDefaultPrivacyPolicy(tt.args.ctx, tt.args.policy)
|
|
|
|
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 newDefaultPrivacyPolicyChangedEvent(ctx context.Context, tosLink, privacyLink, helpLink string) *instance.PrivacyPolicyChangedEvent {
|
|
|
|
event, _ := instance.NewPrivacyPolicyChangedEvent(ctx,
|
|
|
|
&instance.NewAggregate().Aggregate,
|
2021-07-05 08:36:51 +00:00
|
|
|
[]policy.PrivacyPolicyChanges{
|
|
|
|
policy.ChangeTOSLink(tosLink),
|
|
|
|
policy.ChangePrivacyLink(privacyLink),
|
2022-03-24 13:00:24 +00:00
|
|
|
policy.ChangeHelpLink(helpLink),
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
return event
|
|
|
|
}
|