2021-03-15 11:51:15 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-24 16:21:34 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-03-15 11:51:15 +00:00
|
|
|
"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"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/repository"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2021-03-15 11:51:15 +00:00
|
|
|
)
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func TestCommandSide_AddDefaultDomainPolicy(t *testing.T) {
|
2021-03-15 11:51:15 +00:00
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
2022-05-24 09:28:17 +00:00
|
|
|
ctx context.Context
|
|
|
|
userLoginMustBeDomain bool
|
|
|
|
validateOrgDomains bool
|
|
|
|
smtpSenderAddressMatchesInstanceDomain bool
|
2021-03-15 11:51:15 +00:00
|
|
|
}
|
|
|
|
type res struct {
|
2022-05-24 09:28:17 +00:00
|
|
|
want *domain.ObjectDetails
|
2021-03-15 11:51:15 +00:00
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
2022-03-28 08:05:09 +00:00
|
|
|
name: "domain policy already existing, already exists error",
|
2021-03-15 11:51:15 +00:00
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2022-03-28 08:05:09 +00:00
|
|
|
instance.NewDomainPolicyAddedEvent(context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-03-15 11:51:15 +00:00
|
|
|
true,
|
2022-04-13 09:24:03 +00:00
|
|
|
true,
|
2022-05-16 14:08:47 +00:00
|
|
|
true,
|
2021-03-15 11:51:15 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2022-05-24 09:28:17 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
userLoginMustBeDomain: true,
|
|
|
|
validateOrgDomains: true,
|
|
|
|
smtpSenderAddressMatchesInstanceDomain: true,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: caos_errs.IsErrorAlreadyExists,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "add policy,ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
expectPush(
|
|
|
|
[]*repository.Event{
|
2022-04-05 05:58:09 +00:00
|
|
|
eventFromEventPusherWithInstanceID(
|
|
|
|
"INSTANCE",
|
2022-03-28 08:05:09 +00:00
|
|
|
instance.NewDomainPolicyAddedEvent(context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-03-15 11:51:15 +00:00
|
|
|
true,
|
2022-04-13 09:24:03 +00:00
|
|
|
true,
|
2022-05-16 14:08:47 +00:00
|
|
|
true,
|
2021-03-15 11:51:15 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2022-05-24 09:28:17 +00:00
|
|
|
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
|
|
|
userLoginMustBeDomain: true,
|
|
|
|
validateOrgDomains: true,
|
|
|
|
smtpSenderAddressMatchesInstanceDomain: true,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
res: res{
|
2022-05-24 09:28:17 +00:00
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "INSTANCE",
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
}
|
2022-05-24 09:28:17 +00:00
|
|
|
got, err := r.AddDefaultDomainPolicy(tt.args.ctx, tt.args.userLoginMustBeDomain, tt.args.validateOrgDomains, tt.args.smtpSenderAddressMatchesInstanceDomain)
|
2021-03-15 11:51:15 +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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
func TestCommandSide_ChangeDefaultDomainPolicy(t *testing.T) {
|
2021-03-15 11:51:15 +00:00
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
2022-03-24 16:21:34 +00:00
|
|
|
policy *domain.DomainPolicy
|
2021-03-15 11:51:15 +00:00
|
|
|
}
|
|
|
|
type res struct {
|
2022-03-24 16:21:34 +00:00
|
|
|
want *domain.DomainPolicy
|
2021-03-15 11:51:15 +00:00
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
2022-03-28 08:05:09 +00:00
|
|
|
name: "domain policy not existing, not found error",
|
2021-03-15 11:51:15 +00:00
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
2022-03-24 16:21:34 +00:00
|
|
|
policy: &domain.DomainPolicy{
|
2022-05-16 14:08:47 +00:00
|
|
|
UserLoginMustBeDomain: true,
|
|
|
|
ValidateOrgDomains: true,
|
|
|
|
SMTPSenderAddressMatchesInstanceDomain: true,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: caos_errs.IsNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no changes, precondition error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2022-03-28 08:05:09 +00:00
|
|
|
instance.NewDomainPolicyAddedEvent(context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-03-15 11:51:15 +00:00
|
|
|
true,
|
2022-04-13 09:24:03 +00:00
|
|
|
true,
|
2022-05-16 14:08:47 +00:00
|
|
|
true,
|
2021-03-15 11:51:15 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
2022-03-24 16:21:34 +00:00
|
|
|
policy: &domain.DomainPolicy{
|
2022-05-16 14:08:47 +00:00
|
|
|
UserLoginMustBeDomain: true,
|
|
|
|
ValidateOrgDomains: true,
|
|
|
|
SMTPSenderAddressMatchesInstanceDomain: true,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: caos_errs.IsPreconditionFailed,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "change, ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2022-03-28 08:05:09 +00:00
|
|
|
instance.NewDomainPolicyAddedEvent(context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-03-15 11:51:15 +00:00
|
|
|
true,
|
2022-04-13 09:24:03 +00:00
|
|
|
true,
|
2022-05-16 14:08:47 +00:00
|
|
|
true,
|
2021-03-15 11:51:15 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
[]*repository.Event{
|
|
|
|
eventFromEventPusher(
|
2022-05-16 14:08:47 +00:00
|
|
|
newDefaultDomainPolicyChangedEvent(context.Background(), false, false, false),
|
2021-03-15 11:51:15 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
2022-03-24 16:21:34 +00:00
|
|
|
policy: &domain.DomainPolicy{
|
2022-05-16 14:08:47 +00:00
|
|
|
UserLoginMustBeDomain: false,
|
|
|
|
ValidateOrgDomains: false,
|
|
|
|
SMTPSenderAddressMatchesInstanceDomain: false,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
2022-03-24 16:21:34 +00:00
|
|
|
want: &domain.DomainPolicy{
|
2021-03-15 11:51:15 +00:00
|
|
|
ObjectRoot: models.ObjectRoot{
|
2022-04-05 05:58:09 +00:00
|
|
|
AggregateID: "INSTANCE",
|
|
|
|
ResourceOwner: "INSTANCE",
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
2022-05-16 14:08:47 +00:00
|
|
|
UserLoginMustBeDomain: false,
|
|
|
|
ValidateOrgDomains: false,
|
|
|
|
SMTPSenderAddressMatchesInstanceDomain: false,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
got, err := r.ChangeDefaultDomainPolicy(tt.args.ctx, tt.args.policy)
|
2021-03-15 11:51:15 +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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 14:08:47 +00:00
|
|
|
func newDefaultDomainPolicyChangedEvent(ctx context.Context, userLoginMustBeDomain, validateOrgDomains, smtpSenderAddressMatchesInstanceDomain bool) *instance.DomainPolicyChangedEvent {
|
2022-03-28 08:05:09 +00:00
|
|
|
event, _ := instance.NewDomainPolicyChangedEvent(ctx,
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2022-04-13 09:24:03 +00:00
|
|
|
[]policy.DomainPolicyChanges{
|
2021-03-15 11:51:15 +00:00
|
|
|
policy.ChangeUserLoginMustBeDomain(userLoginMustBeDomain),
|
2022-04-13 09:24:03 +00:00
|
|
|
policy.ChangeValidateOrgDomains(validateOrgDomains),
|
2022-05-16 14:08:47 +00:00
|
|
|
policy.ChangeSMTPSenderAddressMatchesInstanceDomain(smtpSenderAddressMatchesInstanceDomain),
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
return event
|
|
|
|
}
|