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"
|
2022-05-24 09:28:17 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-07-05 08:36:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommandSide_AddDefaultPrivacyPolicy(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
2023-03-28 19:36:52 +00:00
|
|
|
ctx context.Context
|
|
|
|
tosLink string
|
|
|
|
privacyLink string
|
|
|
|
helpLink string
|
|
|
|
supportEmail domain.EmailAddress
|
2021-07-05 08:36:51 +00:00
|
|
|
}
|
|
|
|
type res struct {
|
2022-05-24 09:28:17 +00:00
|
|
|
want *domain.ObjectDetails
|
2021-07-05 08:36:51 +00:00
|
|
|
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(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-07-05 08:36:51 +00:00
|
|
|
"TOSLink",
|
|
|
|
"PrivacyLink",
|
2022-03-24 13:00:24 +00:00
|
|
|
"HelpLink",
|
2023-03-28 19:36:52 +00:00
|
|
|
"support@example.com",
|
2021-07-05 08:36:51 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2023-03-28 19:36:52 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
tosLink: "TOSLink",
|
|
|
|
privacyLink: "PrivacyLink",
|
|
|
|
helpLink: "HelpLink",
|
|
|
|
supportEmail: "support@example.com",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorAlreadyExists,
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "add policy,ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
instance.NewPrivacyPolicyAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
|
|
|
"TOSLink",
|
|
|
|
"PrivacyLink",
|
|
|
|
"HelpLink",
|
|
|
|
"support@example.com",
|
|
|
|
),
|
2021-07-05 08:36:51 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2023-03-28 19:36:52 +00:00
|
|
|
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
|
|
|
tosLink: "TOSLink",
|
|
|
|
privacyLink: "PrivacyLink",
|
|
|
|
helpLink: "HelpLink",
|
|
|
|
supportEmail: "support@example.com",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
res: res{
|
2022-05-24 09:28:17 +00:00
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "INSTANCE",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-03-28 19:36:52 +00:00
|
|
|
{
|
|
|
|
name: "wrong email, can't add policy",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
|
|
|
tosLink: "TOSLink",
|
|
|
|
privacyLink: "PrivacyLink",
|
|
|
|
helpLink: "HelpLink",
|
|
|
|
supportEmail: "wrong email",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2023-03-28 19:36:52 +00:00
|
|
|
},
|
|
|
|
},
|
2021-07-05 08:36:51 +00:00
|
|
|
{
|
|
|
|
name: "add empty policy,ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
instance.NewPrivacyPolicyAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
),
|
2021-07-05 08:36:51 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2023-03-28 19:36:52 +00:00
|
|
|
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
|
|
|
tosLink: "",
|
|
|
|
privacyLink: "",
|
|
|
|
helpLink: "",
|
|
|
|
supportEmail: "",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
res: res{
|
2022-05-24 09:28:17 +00:00
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "INSTANCE",
|
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,
|
|
|
|
}
|
2023-03-28 19:36:52 +00:00
|
|
|
got, err := r.AddDefaultPrivacyPolicy(tt.args.ctx, tt.args.tosLink, tt.args.privacyLink, tt.args.helpLink, tt.args.supportEmail)
|
2021-07-05 08:36:51 +00:00
|
|
|
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{
|
2023-03-28 19:36:52 +00:00
|
|
|
TOSLink: "TOSLink",
|
|
|
|
PrivacyLink: "PrivacyLink",
|
|
|
|
HelpLink: "HelpLink",
|
|
|
|
SupportEmail: "support@example.com",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsNotFound,
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no changes, precondition error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewPrivacyPolicyAddedEvent(context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-07-05 08:36:51 +00:00
|
|
|
"TOSLink",
|
|
|
|
"PrivacyLink",
|
2022-03-24 13:00:24 +00:00
|
|
|
"HelpLink",
|
2023-03-28 19:36:52 +00:00
|
|
|
"support@example.com",
|
2021-07-05 08:36:51 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
policy: &domain.PrivacyPolicy{
|
2023-03-28 19:36:52 +00:00
|
|
|
TOSLink: "TOSLink",
|
|
|
|
PrivacyLink: "PrivacyLink",
|
|
|
|
HelpLink: "HelpLink",
|
|
|
|
SupportEmail: "support@example.com",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
2023-03-28 19:36:52 +00:00
|
|
|
{
|
|
|
|
name: "wrong email, can't change policy",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
policy: &domain.PrivacyPolicy{
|
|
|
|
TOSLink: "TOSLink",
|
|
|
|
PrivacyLink: "PrivacyLink",
|
|
|
|
HelpLink: "HelpLink",
|
|
|
|
SupportEmail: "wrong email",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2023-03-28 19:36:52 +00:00
|
|
|
},
|
|
|
|
},
|
2021-07-05 08:36:51 +00:00
|
|
|
{
|
|
|
|
name: "change, ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewPrivacyPolicyAddedEvent(context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-07-05 08:36:51 +00:00
|
|
|
"TOSLink",
|
|
|
|
"PrivacyLink",
|
2022-03-24 13:00:24 +00:00
|
|
|
"HelpLink",
|
2023-03-28 19:36:52 +00:00
|
|
|
"support@example.com",
|
2021-07-05 08:36:51 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
newDefaultPrivacyPolicyChangedEvent(context.Background(),
|
|
|
|
"TOSLinkChanged",
|
|
|
|
"PrivacyLinkChanged",
|
|
|
|
"HelpLinkChanged",
|
|
|
|
"support2@example.com",
|
|
|
|
),
|
2021-07-05 08:36:51 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
policy: &domain.PrivacyPolicy{
|
2023-03-28 19:36:52 +00:00
|
|
|
TOSLink: "TOSLinkChanged",
|
|
|
|
PrivacyLink: "PrivacyLinkChanged",
|
|
|
|
HelpLink: "HelpLinkChanged",
|
|
|
|
SupportEmail: "support2@example.com",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.PrivacyPolicy{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
2022-04-05 05:58:09 +00:00
|
|
|
AggregateID: "INSTANCE",
|
|
|
|
ResourceOwner: "INSTANCE",
|
2023-10-19 10:19:10 +00:00
|
|
|
InstanceID: "INSTANCE",
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
2023-03-28 19:36:52 +00:00
|
|
|
TOSLink: "TOSLinkChanged",
|
|
|
|
PrivacyLink: "PrivacyLinkChanged",
|
|
|
|
HelpLink: "HelpLinkChanged",
|
|
|
|
SupportEmail: "support2@example.com",
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-28 19:36:52 +00:00
|
|
|
func newDefaultPrivacyPolicyChangedEvent(ctx context.Context, tosLink, privacyLink, helpLink, supportEmail string) *instance.PrivacyPolicyChangedEvent {
|
2022-03-24 16:21:34 +00:00
|
|
|
event, _ := instance.NewPrivacyPolicyChangedEvent(ctx,
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").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),
|
2023-03-28 19:36:52 +00:00
|
|
|
policy.ChangeSupportEmail(domain.EmailAddress(supportEmail)),
|
2021-07-05 08:36:51 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
return event
|
|
|
|
}
|