2022-03-28 08:05:09 +00:00
|
|
|
// this is a helper file for tests
|
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-03-28 08:05:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//Want represents the expected values for each step
|
|
|
|
type Want struct {
|
|
|
|
ValidationErr error
|
|
|
|
CreateErr error
|
|
|
|
Commands []eventstore.Command
|
|
|
|
}
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
type CommandVerifier interface {
|
|
|
|
Validate(eventstore.Command) bool
|
|
|
|
}
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
//AssertValidation checks if the validation works as inteded
|
2022-07-28 11:18:31 +00:00
|
|
|
func AssertValidation(t *testing.T, ctx context.Context, validation preparation.Validation, filter preparation.FilterToQueryReducer, want Want) {
|
2022-03-28 08:05:09 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
creates, err := validation()
|
|
|
|
if !errors.Is(err, want.ValidationErr) {
|
2022-04-12 14:20:17 +00:00
|
|
|
t.Errorf("wrong validation err = (%[1]T): %[1]v, want (%[2]T): %[2]v", err, want.ValidationErr)
|
2022-03-28 08:05:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-07-28 11:18:31 +00:00
|
|
|
cmds, err := creates(ctx, filter)
|
2022-03-28 08:05:09 +00:00
|
|
|
if !errors.Is(err, want.CreateErr) {
|
2022-04-12 14:20:17 +00:00
|
|
|
t.Errorf("wrong create err = (%[1]T): %[1]v, want (%[2]T): %[2]v", err, want.CreateErr)
|
2022-03-28 08:05:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cmds) != len(want.Commands) {
|
|
|
|
t.Errorf("wrong length of commands = %v, want %v", eventTypes(cmds), eventTypes(want.Commands))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, cmd := range want.Commands {
|
2022-04-12 14:20:17 +00:00
|
|
|
if v, ok := cmd.(CommandVerifier); ok {
|
|
|
|
if verified := v.Validate(cmds[i]); !verified {
|
|
|
|
t.Errorf("verification failed on command: = %v, want %v", cmds[i], cmd)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
if !reflect.DeepEqual(cmd, cmds[i]) {
|
|
|
|
t.Errorf("unexpected command: = %v, want %v", cmds[i], cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func eventTypes(cmds []eventstore.Command) []eventstore.EventType {
|
|
|
|
types := make([]eventstore.EventType, len(cmds))
|
|
|
|
for i, cmd := range cmds {
|
|
|
|
types[i] = cmd.Type()
|
|
|
|
}
|
|
|
|
return types
|
|
|
|
}
|
|
|
|
|
|
|
|
type MultiFilter struct {
|
|
|
|
count int
|
|
|
|
filters []preparation.FilterToQueryReducer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMultiFilter() *MultiFilter {
|
|
|
|
return new(MultiFilter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mf *MultiFilter) Append(filter preparation.FilterToQueryReducer) *MultiFilter {
|
|
|
|
mf.filters = append(mf.filters, filter)
|
|
|
|
return mf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mf *MultiFilter) Filter() preparation.FilterToQueryReducer {
|
|
|
|
return func(ctx context.Context, queryFactory *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
|
|
|
mf.count++
|
|
|
|
return mf.filters[mf.count-1](ctx, queryFactory)
|
|
|
|
}
|
|
|
|
}
|