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-12-06 08:01:31 +00:00
|
|
|
"golang.org/x/text/language"
|
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/repository/instance"
|
2022-12-06 08:01:31 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2022-12-06 08:01:31 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
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(
|
2023-10-19 10:19:10 +00:00
|
|
|
instance.NewDomainPolicyAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
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 {
|
2022-12-06 08:01:31 +00:00
|
|
|
ctx context.Context
|
|
|
|
userLoginMustBeDomain bool
|
|
|
|
validateOrgDomains bool
|
|
|
|
smtpSenderAddressMatchesInstanceDomain bool
|
2021-03-15 11:51:15 +00:00
|
|
|
}
|
|
|
|
type res struct {
|
2022-12-06 08:01:31 +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 not existing, not found error",
|
2021-03-15 11:51:15 +00:00
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2022-12-06 08:01:31 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
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{
|
2022-12-06 08:01:31 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
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
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2022-12-06 08:01:31 +00:00
|
|
|
// domainPolicyOrgs
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewOrgAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
"org1",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewOrgAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org2").Aggregate,
|
|
|
|
"org2",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewDomainPolicyAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org2").Aggregate,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewOrgAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org3").Aggregate,
|
|
|
|
"org3",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewDomainPolicyAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org3").Aggregate,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewDomainPolicyRemovedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org3").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
// domainPolicyUsernames for each org
|
|
|
|
// org1
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewDomainPrimarySetEvent(
|
|
|
|
context.Background(),
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
"org1.com",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewDomainPrimarySetEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
"org1.com",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"user1",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.English,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"user1@org1.com",
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
// org3
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewDomainPrimarySetEvent(
|
|
|
|
context.Background(),
|
|
|
|
&org.NewAggregate("org3").Aggregate,
|
|
|
|
"org3.com",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewDomainPrimarySetEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org3").Aggregate,
|
|
|
|
"org3.com",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org3").Aggregate,
|
|
|
|
"user1",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.English,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"user1@org3.com",
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2021-03-15 11:51:15 +00:00
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
newDefaultDomainPolicyChangedEvent(context.Background(), false, false, false),
|
|
|
|
user.NewUsernameChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"user1",
|
|
|
|
"user1@org1.com",
|
|
|
|
false,
|
|
|
|
user.UsernameChangedEventWithPolicyChange(),
|
|
|
|
),
|
|
|
|
user.NewUsernameChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org3").Aggregate,
|
|
|
|
"user1",
|
|
|
|
"user1@org3.com",
|
|
|
|
false,
|
|
|
|
user.UsernameChangedEventWithPolicyChange(),
|
|
|
|
),
|
2021-03-15 11:51:15 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2022-12-06 08:01:31 +00:00
|
|
|
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
|
|
|
userLoginMustBeDomain: false,
|
|
|
|
validateOrgDomains: false,
|
|
|
|
smtpSenderAddressMatchesInstanceDomain: false,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
res: res{
|
2022-12-06 08:01:31 +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-12-06 08:01:31 +00:00
|
|
|
got, err := r.ChangeDefaultDomainPolicy(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-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
|
|
|
|
}
|