2022-03-28 08:05:09 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-03-28 08:05:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_customPasswordComplexityPolicy(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
filter preparation.FilterToQueryReducer
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2022-04-12 14:20:17 +00:00
|
|
|
want *PasswordComplexityPolicyWriteModel
|
2022-03-28 08:05:09 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "err from filter",
|
|
|
|
args: args{
|
|
|
|
filter: func(_ context.Context, _ *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(nil, "USER-IgYlN", "Errors.Internal")
|
2022-03-28 08:05:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no events",
|
|
|
|
args: args{
|
|
|
|
filter: func(_ context.Context, _ *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
|
|
|
return []eventstore.Event{}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "policy found",
|
|
|
|
args: args{
|
|
|
|
filter: func(_ context.Context, _ *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
|
|
|
return []eventstore.Event{
|
|
|
|
org.NewPasswordComplexityPolicyAddedEvent(
|
|
|
|
context.Background(),
|
2022-04-20 14:59:37 +00:00
|
|
|
&org.NewAggregate("id").Aggregate,
|
2022-03-28 08:05:09 +00:00
|
|
|
8,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
2022-04-12 14:20:17 +00:00
|
|
|
want: &PasswordComplexityPolicyWriteModel{
|
2022-03-28 08:05:09 +00:00
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: "id",
|
2022-04-20 14:59:37 +00:00
|
|
|
ResourceOwner: "id",
|
2022-03-28 08:05:09 +00:00
|
|
|
Events: []eventstore.Event{},
|
|
|
|
},
|
|
|
|
MinLength: 8,
|
|
|
|
HasLowercase: true,
|
|
|
|
HasUppercase: true,
|
|
|
|
HasNumber: true,
|
|
|
|
HasSymbol: true,
|
|
|
|
State: domain.PolicyStateActive,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := customPasswordComplexityPolicy(context.Background(), tt.args.filter)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("customPasswordComplexityPolicy() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("customPasswordComplexityPolicy() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_defaultPasswordComplexityPolicy(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
filter preparation.FilterToQueryReducer
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2022-04-12 14:20:17 +00:00
|
|
|
want *PasswordComplexityPolicyWriteModel
|
2022-03-28 08:05:09 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "err from filter",
|
|
|
|
args: args{
|
|
|
|
filter: func(_ context.Context, _ *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(nil, "USER-IgYlN", "Errors.Internal")
|
2022-03-28 08:05:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no events",
|
|
|
|
args: args{
|
|
|
|
filter: func(_ context.Context, _ *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
|
|
|
return []eventstore.Event{}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "policy found",
|
|
|
|
args: args{
|
|
|
|
filter: func(_ context.Context, _ *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
|
|
|
return []eventstore.Event{
|
|
|
|
instance.NewPasswordComplexityPolicyAddedEvent(
|
|
|
|
context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2022-03-28 08:05:09 +00:00
|
|
|
8,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
2022-04-12 14:20:17 +00:00
|
|
|
want: &PasswordComplexityPolicyWriteModel{
|
2022-03-28 08:05:09 +00:00
|
|
|
WriteModel: eventstore.WriteModel{
|
2022-04-05 05:58:09 +00:00
|
|
|
AggregateID: "INSTANCE",
|
|
|
|
ResourceOwner: "INSTANCE",
|
2022-03-28 08:05:09 +00:00
|
|
|
Events: []eventstore.Event{},
|
2022-09-27 06:58:50 +00:00
|
|
|
InstanceID: "INSTANCE",
|
2022-03-28 08:05:09 +00:00
|
|
|
},
|
|
|
|
MinLength: 8,
|
|
|
|
HasLowercase: true,
|
|
|
|
HasUppercase: true,
|
|
|
|
HasNumber: true,
|
|
|
|
HasSymbol: true,
|
|
|
|
State: domain.PolicyStateActive,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-04-05 05:58:09 +00:00
|
|
|
got, err := defaultPasswordComplexityPolicy(authz.WithInstanceID(context.Background(), "INSTANCE"), tt.args.filter)
|
2022-03-28 08:05:09 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("defaultPasswordComplexityPolicy() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("defaultPasswordComplexityPolicy() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_passwordComplexityPolicy(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
filter preparation.FilterToQueryReducer
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2022-04-12 14:20:17 +00:00
|
|
|
want *PasswordComplexityPolicyWriteModel
|
2022-03-28 08:05:09 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "err from filter custom",
|
|
|
|
args: args{
|
|
|
|
filter: func(_ context.Context, _ *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(nil, "USER-IgYlN", "Errors.Internal")
|
2022-03-28 08:05:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "custom found",
|
|
|
|
args: args{
|
|
|
|
filter: func(_ context.Context, _ *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
|
|
|
return []eventstore.Event{
|
|
|
|
org.NewPasswordComplexityPolicyAddedEvent(
|
|
|
|
context.Background(),
|
2022-04-20 14:59:37 +00:00
|
|
|
&org.NewAggregate("id").Aggregate,
|
2022-03-28 08:05:09 +00:00
|
|
|
8,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
2022-04-12 14:20:17 +00:00
|
|
|
want: &PasswordComplexityPolicyWriteModel{
|
2022-03-28 08:05:09 +00:00
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: "id",
|
2022-04-20 14:59:37 +00:00
|
|
|
ResourceOwner: "id",
|
2022-03-28 08:05:09 +00:00
|
|
|
Events: []eventstore.Event{},
|
|
|
|
},
|
|
|
|
MinLength: 8,
|
|
|
|
HasLowercase: true,
|
|
|
|
HasUppercase: true,
|
|
|
|
HasNumber: true,
|
|
|
|
HasSymbol: true,
|
|
|
|
State: domain.PolicyStateActive,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "err from filter default",
|
|
|
|
args: args{
|
|
|
|
filter: NewMultiFilter().
|
|
|
|
Append(func(ctx context.Context, queryFactory *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
|
|
|
return nil, nil
|
|
|
|
}).
|
|
|
|
Append(func(ctx context.Context, queryFactory *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(nil, "USER-6HnsD", "Errors.Internal")
|
2022-03-28 08:05:09 +00:00
|
|
|
}).
|
|
|
|
Filter(),
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "default found",
|
|
|
|
args: args{
|
|
|
|
filter: NewMultiFilter().
|
|
|
|
Append(func(ctx context.Context, queryFactory *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
|
|
|
return nil, nil
|
|
|
|
}).
|
|
|
|
Append(func(_ context.Context, _ *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
|
|
|
return []eventstore.Event{
|
|
|
|
instance.NewPasswordComplexityPolicyAddedEvent(
|
|
|
|
context.Background(),
|
2022-04-05 05:58:09 +00:00
|
|
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
2022-03-28 08:05:09 +00:00
|
|
|
8,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
}).
|
|
|
|
Filter(),
|
|
|
|
},
|
2022-04-12 14:20:17 +00:00
|
|
|
want: &PasswordComplexityPolicyWriteModel{
|
2022-03-28 08:05:09 +00:00
|
|
|
WriteModel: eventstore.WriteModel{
|
2022-04-05 05:58:09 +00:00
|
|
|
AggregateID: "INSTANCE",
|
|
|
|
ResourceOwner: "INSTANCE",
|
2022-03-28 08:05:09 +00:00
|
|
|
Events: []eventstore.Event{},
|
2022-09-27 06:58:50 +00:00
|
|
|
InstanceID: "INSTANCE",
|
2022-03-28 08:05:09 +00:00
|
|
|
},
|
|
|
|
MinLength: 8,
|
|
|
|
HasLowercase: true,
|
|
|
|
HasUppercase: true,
|
|
|
|
HasNumber: true,
|
|
|
|
HasSymbol: true,
|
|
|
|
State: domain.PolicyStateActive,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no policy found",
|
|
|
|
args: args{
|
|
|
|
filter: func(_ context.Context, _ *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
|
|
|
return nil, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-04-05 05:58:09 +00:00
|
|
|
got, err := passwordComplexityPolicyWriteModel(authz.WithInstanceID(context.Background(), "INSTANCE"), tt.args.filter)
|
2022-03-28 08:05:09 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("defaultPasswordComplexityPolicy() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("defaultPasswordComplexityPolicy() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|