2022-03-28 08:05:09 +00:00
|
|
|
// this is a helper file for tests
|
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2024-08-12 20:32:01 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
2024-08-12 20:32:01 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-03-28 08:05:09 +00:00
|
|
|
)
|
|
|
|
|
2024-04-03 10:43:43 +00:00
|
|
|
// Want represents the expected values for each step
|
2022-03-28 08:05:09 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
// AssertValidation checks if the validation works as intended
|
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 {
|
2024-04-05 09:35:49 +00:00
|
|
|
t.Errorf("verification failed on command: =\n%v\nwant\n%v", cmds[i], cmd)
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2024-08-12 20:32:01 +00:00
|
|
|
|
|
|
|
func assertObjectDetails(t *testing.T, want, got *domain.ObjectDetails) {
|
|
|
|
if want == nil {
|
|
|
|
assert.Nil(t, got)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(t, got.CreationDate, want.CreationDate)
|
|
|
|
assert.Equal(t, got.EventDate, want.EventDate)
|
|
|
|
assert.Equal(t, got.ResourceOwner, want.ResourceOwner)
|
|
|
|
assert.Equal(t, got.Sequence, want.Sequence)
|
|
|
|
if want.ID != "" {
|
|
|
|
assert.Equal(t, got.ID, want.ID)
|
|
|
|
}
|
|
|
|
}
|