2021-03-15 11:51:15 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-05 05:58:09 +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"
|
|
|
|
"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-03-15 11:51:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommandSide_AddDefaultPasswordComplexityPolicy(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
2022-05-24 09:28:17 +00:00
|
|
|
ctx context.Context
|
|
|
|
minLength uint64
|
|
|
|
hasLowercase bool
|
|
|
|
hasUppercase bool
|
|
|
|
hasNumber bool
|
|
|
|
hasSymbol 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
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "invalid policy, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2022-05-24 09:28:17 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
minLength: 0,
|
|
|
|
hasUppercase: true,
|
|
|
|
hasLowercase: true,
|
|
|
|
hasNumber: true,
|
|
|
|
hasSymbol: true,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "password complexity policy already existing, already exists error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewPasswordComplexityPolicyAddedEvent(context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-03-15 11:51:15 +00:00
|
|
|
8,
|
|
|
|
true, true, true, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2022-05-24 09:28:17 +00:00
|
|
|
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
|
|
|
minLength: 8,
|
|
|
|
hasUppercase: true,
|
|
|
|
hasLowercase: true,
|
|
|
|
hasNumber: true,
|
|
|
|
hasSymbol: true,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorAlreadyExists,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "add policy,ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
instance.NewPasswordComplexityPolicyAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
|
|
|
8,
|
|
|
|
true, 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"),
|
|
|
|
minLength: 8,
|
|
|
|
hasUppercase: true,
|
|
|
|
hasLowercase: true,
|
|
|
|
hasNumber: true,
|
|
|
|
hasSymbol: 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.AddDefaultPasswordComplexityPolicy(tt.args.ctx, tt.args.minLength, tt.args.hasLowercase, tt.args.hasUppercase, tt.args.hasNumber, tt.args.hasSymbol)
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_ChangeDefaultPasswordComplexityPolicy(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
policy *domain.PasswordComplexityPolicy
|
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
want *domain.PasswordComplexityPolicy
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "invalid policy, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
policy: &domain.PasswordComplexityPolicy{
|
|
|
|
MinLength: 0,
|
|
|
|
HasUppercase: true,
|
|
|
|
HasLowercase: true,
|
|
|
|
HasNumber: true,
|
|
|
|
HasSymbol: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "password complexity policy not existing, not found error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
policy: &domain.PasswordComplexityPolicy{
|
|
|
|
MinLength: 8,
|
|
|
|
HasUppercase: true,
|
|
|
|
HasLowercase: true,
|
|
|
|
HasNumber: true,
|
|
|
|
HasSymbol: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsNotFound,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no changes, precondition error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewPasswordComplexityPolicyAddedEvent(context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-03-15 11:51:15 +00:00
|
|
|
8,
|
|
|
|
true, true, true, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
policy: &domain.PasswordComplexityPolicy{
|
|
|
|
MinLength: 8,
|
|
|
|
HasUppercase: true,
|
|
|
|
HasLowercase: true,
|
|
|
|
HasNumber: true,
|
|
|
|
HasSymbol: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "change, ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewPasswordComplexityPolicyAddedEvent(context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-03-15 11:51:15 +00:00
|
|
|
8,
|
|
|
|
true, true, true, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
newDefaultPasswordComplexityPolicyChangedEvent(context.Background(), 10, false, false, false, false),
|
2021-03-15 11:51:15 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
policy: &domain.PasswordComplexityPolicy{
|
|
|
|
MinLength: 10,
|
|
|
|
HasUppercase: false,
|
|
|
|
HasLowercase: false,
|
|
|
|
HasNumber: false,
|
|
|
|
HasSymbol: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.PasswordComplexityPolicy{
|
|
|
|
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-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
MinLength: 10,
|
|
|
|
HasUppercase: false,
|
|
|
|
HasLowercase: false,
|
|
|
|
HasNumber: false,
|
|
|
|
HasSymbol: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
}
|
|
|
|
got, err := r.ChangeDefaultPasswordComplexityPolicy(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 newDefaultPasswordComplexityPolicyChangedEvent(ctx context.Context, minLength uint64, hasUpper, hasLower, hasNumber, hasSymbol bool) *instance.PasswordComplexityPolicyChangedEvent {
|
|
|
|
event, _ := instance.NewPasswordComplexityPolicyChangedEvent(ctx,
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2021-03-15 11:51:15 +00:00
|
|
|
[]policy.PasswordComplexityPolicyChanges{
|
|
|
|
policy.ChangeMinLength(minLength),
|
|
|
|
policy.ChangeHasUppercase(hasUpper),
|
|
|
|
policy.ChangeHasLowercase(hasLower),
|
|
|
|
policy.ChangeHasSymbol(hasNumber),
|
|
|
|
policy.ChangeHasNumber(hasSymbol),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return event
|
|
|
|
}
|