fix(eventstore): tests

This commit is contained in:
adlerhurst 2020-11-26 09:19:14 +01:00
parent 4bb9650f27
commit 246d4294cf
10 changed files with 97 additions and 65 deletions

View File

@ -110,6 +110,9 @@ func (repo *IAMRepository) IDPConfigByID(ctx context.Context, idpConfigID string
func (repo *IAMRepository) AddOIDCIDPConfig(ctx context.Context, idp *iam_model.IDPConfig) (*iam_model.IDPConfig, error) {
idp.AggregateID = repo.SystemDefaults.IamID
if repo.IAMV2 != nil {
return repo.IAMV2.AddIDPConfig(ctx, idp)
}
return repo.IAMEventstore.AddIDPConfig(ctx, idp)
}

View File

@ -6,8 +6,8 @@ type serviceKey struct{}
var key *serviceKey = (*serviceKey)(nil)
func WithService(partent context.Context, serviceName string) context.Context {
return context.WithValue(partent, key, serviceName)
func WithService(parent context.Context, serviceName string) context.Context {
return context.WithValue(parent, key, serviceName)
}
func FromContext(ctx context.Context) string {

View File

@ -125,8 +125,8 @@ func (es *Eventstore) mapEvents(events []*repository.Event) (mappedEvents []Even
interceptors, ok := es.eventInterceptors[EventType(event.Type)]
if !ok || interceptors.eventMapper == nil {
mappedEvents[i] = BaseEventFromRepo(event)
continue
// return nil, errors.ThrowPreconditionFailed(nil, "V2-usujB", "event mapper not defined")
// continue
return nil, errors.ThrowPreconditionFailed(nil, "V2-usujB", "event mapper not defined")
}
mappedEvents[i], err = interceptors.eventMapper(event)
if err != nil {

View File

@ -8,6 +8,7 @@ import (
"testing"
"github.com/caos/zitadel/internal/api/authz"
"github.com/caos/zitadel/internal/api/service"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
)
@ -57,7 +58,7 @@ func newTestEvent(description string, data func() interface{}, checkPrevious boo
data: data,
shouldCheckPrevious: checkPrevious,
BaseEvent: *NewBaseEventForPush(
authz.NewMockContext("resourceOwner", "editorUser"),
service.WithService(authz.NewMockContext("resourceOwner", "editorUser"), "editorService"),
"test.event",
),
}
@ -1142,12 +1143,8 @@ func (r *testReducer) Reduce() error {
return nil
}
func (r *testReducer) AppendEvents(e ...EventReader) error {
if r.err != nil {
return r.err
}
func (r *testReducer) AppendEvents(e ...EventReader) {
r.events = append(r.events, e...)
return nil
}
func TestEventstore_FilterToReducer(t *testing.T) {
@ -1234,10 +1231,7 @@ func TestEventstore_FilterToReducer(t *testing.T) {
{
name: "found events",
args: args{
query: &SearchQueryFactory{
aggregateTypes: []AggregateType{"test.aggregate"},
columns: repository.ColumnsEvent,
},
query: NewSearchQueryFactory(repository.ColumnsEvent, "test.aggregate"),
readModel: &testReducer{
t: t,
expectedLength: 1,
@ -1268,8 +1262,9 @@ func TestEventstore_FilterToReducer(t *testing.T) {
columns: repository.ColumnsEvent,
},
readModel: &testReducer{
t: t,
err: errors.ThrowInvalidArgument(nil, "V2-W06TG", "test err"),
t: t,
err: errors.ThrowInvalidArgument(nil, "V2-W06TG", "test err"),
expectedLength: 1,
},
},
fields: fields{
@ -1399,6 +1394,8 @@ func TestEventstore_mapEvents(t *testing.T) {
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){},
},
res: res{
//TODO: as long as not all events are implemented in v2 eventstore doesn't return an error
// afterwards it will return an error on un
wantErr: true,
},
},

View File

@ -3,7 +3,6 @@ package eventstore_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
"time"
@ -209,14 +208,7 @@ type UsersReadModel struct {
Users []*UserReadModel
}
func NewUsersReadModel() *UsersReadModel {
return &UsersReadModel{
ReadModel: *eventstore.NewReadModel(),
Users: []*UserReadModel{},
}
}
func (rm *UsersReadModel) AppendEvents(events ...eventstore.EventReader) (err error) {
func (rm *UsersReadModel) AppendEvents(events ...eventstore.EventReader) {
rm.ReadModel.AppendEvents(events...)
for _, event := range events {
switch e := event.(type) {
@ -224,29 +216,24 @@ func (rm *UsersReadModel) AppendEvents(events ...eventstore.EventReader) (err er
//insert
user := NewUserReadModel(e.AggregateID())
rm.Users = append(rm.Users, user)
err = user.AppendEvents(e)
user.AppendEvents(e)
case *UserFirstNameChangedEvent, *UserPasswordCheckedEvent:
//update
_, user := rm.userByID(e.AggregateID())
if user == nil {
return errors.New("user not found")
return
}
err = user.AppendEvents(e)
user.AppendEvents(e)
case *UserDeletedEvent:
idx, _ := rm.userByID(e.AggregateID())
if idx < 0 {
return nil
return
}
copy(rm.Users[idx:], rm.Users[idx+1:])
rm.Users[len(rm.Users)-1] = nil // or the zero value of T
rm.Users = rm.Users[:len(rm.Users)-1]
}
if err != nil {
return err
}
}
return nil
}
func (rm *UsersReadModel) Reduce() error {
@ -285,16 +272,10 @@ type UserReadModel struct {
func NewUserReadModel(id string) *UserReadModel {
return &UserReadModel{
ReadModel: *eventstore.NewReadModel(),
ID: id,
ID: id,
}
}
func (rm *UserReadModel) AppendEvents(events ...eventstore.EventReader) error {
rm.ReadModel.AppendEvents(events...)
return nil
}
func (rm *UserReadModel) Reduce() error {
for _, event := range rm.ReadModel.Events {
switch e := event.(type) {
@ -335,8 +316,8 @@ func TestUserReadModel(t *testing.T) {
fmt.Printf("%+v\n", events)
users := NewUsersReadModel()
err = es.FilterToReducer(context.Background(), eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, "test.user"), users)
users := UsersReadModel{}
err = es.FilterToReducer(context.Background(), eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, "test.user"), &users)
if err != nil {
t.Errorf("unexpected error on filter to reducer: %v", err)
}

View File

@ -2,12 +2,6 @@ package eventstore
import "time"
func NewReadModel() *ReadModel {
return &ReadModel{
Events: []EventReader{},
}
}
//ReadModel is the minimum representation of a View model.
// It implements a basic reducer
// it might be saved in a database or in memory

View File

@ -28,3 +28,12 @@ func (r *Repository) IDPConfigByID(ctx context.Context, idpConfigID string) (*ia
return readModelToIDPConfigView(idpConfig), nil
}
func (r *Repository) AddIDPConfig(ctx context.Context, config *iam_model.IDPConfig) (*iam_model.IDPConfig, error) {
iam, err := r.iamByID(ctx, config.AggregateID)
if err != nil {
return nil, err
}
return nil, nil
}

View File

@ -0,0 +1,56 @@
package idp
import (
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/v2/repository/idp/oidc"
)
type ConfigsReadModel struct {
eventstore.ReadModel
Configs []*ConfigReadModel
}
func (rm *ConfigsReadModel) AppendEvents(events ...eventstore.EventReader) {
rm.ReadModel.AppendEvents(events...)
for _, event := range events {
switch event.(type) {
case *oidc.ConfigAddedEvent:
rm.OIDCConfig = &oidc.ConfigReadModel{}
rm.OIDCConfig.AppendEvents(event)
case *oidc.ConfigChangedEvent:
rm.OIDCConfig.AppendEvents(event)
}
}
}
func (rm *ConfigsReadModel) Reduce() error {
for _, event := range rm.Events {
switch e := event.(type) {
case *ConfigAddedEvent:
rm.ConfigID = e.ConfigID
rm.Name = e.Name
rm.StylingType = e.StylingType
rm.State = ConfigStateActive
case *ConfigChangedEvent:
if e.Name != "" {
rm.Name = e.Name
}
if e.StylingType.Valid() {
rm.StylingType = e.StylingType
}
case *ConfigDeactivatedEvent:
rm.State = ConfigStateInactive
case *ConfigReactivatedEvent:
rm.State = ConfigStateActive
case *ConfigRemovedEvent:
rm.State = ConfigStateRemoved
case *oidc.ConfigAddedEvent:
rm.Type = ConfigTypeOIDC
}
}
if err := rm.OIDCConfig.Reduce(); err != nil {
return err
}
return rm.ReadModel.Reduce()
}

View File

@ -14,8 +14,7 @@ type ReadModel struct {
//NewMemberReadModel is the default constructor of ReadModel
func NewMemberReadModel(userID string) *ReadModel {
return &ReadModel{
ReadModel: *eventstore.NewReadModel(),
UserID: userID,
UserID: userID,
}
}

View File

@ -11,13 +11,6 @@ type ReadModel struct {
Members []*member.ReadModel
}
func NewMembersReadModel() *ReadModel {
return &ReadModel{
ReadModel: *eventstore.NewReadModel(),
Members: []*member.ReadModel{},
}
}
func (rm *ReadModel) MemberByUserID(id string) (idx int, member *member.ReadModel) {
for idx, member = range rm.Members {
if member.UserID == id {
@ -31,12 +24,12 @@ func (rm *ReadModel) AppendEvents(events ...eventstore.EventReader) {
for _, event := range events {
switch e := event.(type) {
case *member.AddedEvent:
member := member.NewMemberReadModel(e.UserID)
rm.Members = append(rm.Members, member)
member.AppendEvents(e)
m := member.NewMemberReadModel(e.UserID)
rm.Members = append(rm.Members, m)
m.AppendEvents(e)
case *member.ChangedEvent:
_, member := rm.MemberByUserID(e.UserID)
member.AppendEvents(e)
_, m := rm.MemberByUserID(e.UserID)
m.AppendEvents(e)
case *member.RemovedEvent:
idx, _ := rm.MemberByUserID(e.UserID)
if idx < 0 {
@ -50,8 +43,8 @@ func (rm *ReadModel) AppendEvents(events ...eventstore.EventReader) {
}
func (rm *ReadModel) Reduce() (err error) {
for _, member := range rm.Members {
err = member.Reduce()
for _, m := range rm.Members {
err = m.Reduce()
if err != nil {
return err
}