mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 02:54:20 +00:00
fix(eventstore): tests
This commit is contained in:
parent
4bb9650f27
commit
246d4294cf
@ -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) {
|
func (repo *IAMRepository) AddOIDCIDPConfig(ctx context.Context, idp *iam_model.IDPConfig) (*iam_model.IDPConfig, error) {
|
||||||
idp.AggregateID = repo.SystemDefaults.IamID
|
idp.AggregateID = repo.SystemDefaults.IamID
|
||||||
|
if repo.IAMV2 != nil {
|
||||||
|
return repo.IAMV2.AddIDPConfig(ctx, idp)
|
||||||
|
}
|
||||||
return repo.IAMEventstore.AddIDPConfig(ctx, idp)
|
return repo.IAMEventstore.AddIDPConfig(ctx, idp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,8 +6,8 @@ type serviceKey struct{}
|
|||||||
|
|
||||||
var key *serviceKey = (*serviceKey)(nil)
|
var key *serviceKey = (*serviceKey)(nil)
|
||||||
|
|
||||||
func WithService(partent context.Context, serviceName string) context.Context {
|
func WithService(parent context.Context, serviceName string) context.Context {
|
||||||
return context.WithValue(partent, key, serviceName)
|
return context.WithValue(parent, key, serviceName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromContext(ctx context.Context) string {
|
func FromContext(ctx context.Context) string {
|
||||||
|
@ -125,8 +125,8 @@ func (es *Eventstore) mapEvents(events []*repository.Event) (mappedEvents []Even
|
|||||||
interceptors, ok := es.eventInterceptors[EventType(event.Type)]
|
interceptors, ok := es.eventInterceptors[EventType(event.Type)]
|
||||||
if !ok || interceptors.eventMapper == nil {
|
if !ok || interceptors.eventMapper == nil {
|
||||||
mappedEvents[i] = BaseEventFromRepo(event)
|
mappedEvents[i] = BaseEventFromRepo(event)
|
||||||
continue
|
// continue
|
||||||
// return nil, errors.ThrowPreconditionFailed(nil, "V2-usujB", "event mapper not defined")
|
return nil, errors.ThrowPreconditionFailed(nil, "V2-usujB", "event mapper not defined")
|
||||||
}
|
}
|
||||||
mappedEvents[i], err = interceptors.eventMapper(event)
|
mappedEvents[i], err = interceptors.eventMapper(event)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/caos/zitadel/internal/api/authz"
|
"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/errors"
|
||||||
"github.com/caos/zitadel/internal/eventstore/v2/repository"
|
"github.com/caos/zitadel/internal/eventstore/v2/repository"
|
||||||
)
|
)
|
||||||
@ -57,7 +58,7 @@ func newTestEvent(description string, data func() interface{}, checkPrevious boo
|
|||||||
data: data,
|
data: data,
|
||||||
shouldCheckPrevious: checkPrevious,
|
shouldCheckPrevious: checkPrevious,
|
||||||
BaseEvent: *NewBaseEventForPush(
|
BaseEvent: *NewBaseEventForPush(
|
||||||
authz.NewMockContext("resourceOwner", "editorUser"),
|
service.WithService(authz.NewMockContext("resourceOwner", "editorUser"), "editorService"),
|
||||||
"test.event",
|
"test.event",
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
@ -1142,12 +1143,8 @@ func (r *testReducer) Reduce() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *testReducer) AppendEvents(e ...EventReader) error {
|
func (r *testReducer) AppendEvents(e ...EventReader) {
|
||||||
if r.err != nil {
|
|
||||||
return r.err
|
|
||||||
}
|
|
||||||
r.events = append(r.events, e...)
|
r.events = append(r.events, e...)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEventstore_FilterToReducer(t *testing.T) {
|
func TestEventstore_FilterToReducer(t *testing.T) {
|
||||||
@ -1234,10 +1231,7 @@ func TestEventstore_FilterToReducer(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "found events",
|
name: "found events",
|
||||||
args: args{
|
args: args{
|
||||||
query: &SearchQueryFactory{
|
query: NewSearchQueryFactory(repository.ColumnsEvent, "test.aggregate"),
|
||||||
aggregateTypes: []AggregateType{"test.aggregate"},
|
|
||||||
columns: repository.ColumnsEvent,
|
|
||||||
},
|
|
||||||
readModel: &testReducer{
|
readModel: &testReducer{
|
||||||
t: t,
|
t: t,
|
||||||
expectedLength: 1,
|
expectedLength: 1,
|
||||||
@ -1268,8 +1262,9 @@ func TestEventstore_FilterToReducer(t *testing.T) {
|
|||||||
columns: repository.ColumnsEvent,
|
columns: repository.ColumnsEvent,
|
||||||
},
|
},
|
||||||
readModel: &testReducer{
|
readModel: &testReducer{
|
||||||
t: t,
|
t: t,
|
||||||
err: errors.ThrowInvalidArgument(nil, "V2-W06TG", "test err"),
|
err: errors.ThrowInvalidArgument(nil, "V2-W06TG", "test err"),
|
||||||
|
expectedLength: 1,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
fields: fields{
|
fields: fields{
|
||||||
@ -1399,6 +1394,8 @@ func TestEventstore_mapEvents(t *testing.T) {
|
|||||||
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){},
|
eventMapper: map[EventType]func(*repository.Event) (EventReader, error){},
|
||||||
},
|
},
|
||||||
res: res{
|
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,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -3,7 +3,6 @@ package eventstore_test
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -209,14 +208,7 @@ type UsersReadModel struct {
|
|||||||
Users []*UserReadModel
|
Users []*UserReadModel
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUsersReadModel() *UsersReadModel {
|
func (rm *UsersReadModel) AppendEvents(events ...eventstore.EventReader) {
|
||||||
return &UsersReadModel{
|
|
||||||
ReadModel: *eventstore.NewReadModel(),
|
|
||||||
Users: []*UserReadModel{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rm *UsersReadModel) AppendEvents(events ...eventstore.EventReader) (err error) {
|
|
||||||
rm.ReadModel.AppendEvents(events...)
|
rm.ReadModel.AppendEvents(events...)
|
||||||
for _, event := range events {
|
for _, event := range events {
|
||||||
switch e := event.(type) {
|
switch e := event.(type) {
|
||||||
@ -224,29 +216,24 @@ func (rm *UsersReadModel) AppendEvents(events ...eventstore.EventReader) (err er
|
|||||||
//insert
|
//insert
|
||||||
user := NewUserReadModel(e.AggregateID())
|
user := NewUserReadModel(e.AggregateID())
|
||||||
rm.Users = append(rm.Users, user)
|
rm.Users = append(rm.Users, user)
|
||||||
err = user.AppendEvents(e)
|
user.AppendEvents(e)
|
||||||
case *UserFirstNameChangedEvent, *UserPasswordCheckedEvent:
|
case *UserFirstNameChangedEvent, *UserPasswordCheckedEvent:
|
||||||
//update
|
//update
|
||||||
_, user := rm.userByID(e.AggregateID())
|
_, user := rm.userByID(e.AggregateID())
|
||||||
if user == nil {
|
if user == nil {
|
||||||
return errors.New("user not found")
|
return
|
||||||
}
|
}
|
||||||
err = user.AppendEvents(e)
|
user.AppendEvents(e)
|
||||||
case *UserDeletedEvent:
|
case *UserDeletedEvent:
|
||||||
idx, _ := rm.userByID(e.AggregateID())
|
idx, _ := rm.userByID(e.AggregateID())
|
||||||
if idx < 0 {
|
if idx < 0 {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
copy(rm.Users[idx:], rm.Users[idx+1:])
|
copy(rm.Users[idx:], rm.Users[idx+1:])
|
||||||
rm.Users[len(rm.Users)-1] = nil // or the zero value of T
|
rm.Users[len(rm.Users)-1] = nil // or the zero value of T
|
||||||
rm.Users = rm.Users[:len(rm.Users)-1]
|
rm.Users = rm.Users[:len(rm.Users)-1]
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rm *UsersReadModel) Reduce() error {
|
func (rm *UsersReadModel) Reduce() error {
|
||||||
@ -285,16 +272,10 @@ type UserReadModel struct {
|
|||||||
|
|
||||||
func NewUserReadModel(id string) *UserReadModel {
|
func NewUserReadModel(id string) *UserReadModel {
|
||||||
return &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 {
|
func (rm *UserReadModel) Reduce() error {
|
||||||
for _, event := range rm.ReadModel.Events {
|
for _, event := range rm.ReadModel.Events {
|
||||||
switch e := event.(type) {
|
switch e := event.(type) {
|
||||||
@ -335,8 +316,8 @@ func TestUserReadModel(t *testing.T) {
|
|||||||
|
|
||||||
fmt.Printf("%+v\n", events)
|
fmt.Printf("%+v\n", events)
|
||||||
|
|
||||||
users := NewUsersReadModel()
|
users := UsersReadModel{}
|
||||||
err = es.FilterToReducer(context.Background(), eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, "test.user"), users)
|
err = es.FilterToReducer(context.Background(), eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, "test.user"), &users)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error on filter to reducer: %v", err)
|
t.Errorf("unexpected error on filter to reducer: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,6 @@ package eventstore
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
func NewReadModel() *ReadModel {
|
|
||||||
return &ReadModel{
|
|
||||||
Events: []EventReader{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//ReadModel is the minimum representation of a View model.
|
//ReadModel is the minimum representation of a View model.
|
||||||
// It implements a basic reducer
|
// It implements a basic reducer
|
||||||
// it might be saved in a database or in memory
|
// it might be saved in a database or in memory
|
||||||
|
@ -28,3 +28,12 @@ func (r *Repository) IDPConfigByID(ctx context.Context, idpConfigID string) (*ia
|
|||||||
|
|
||||||
return readModelToIDPConfigView(idpConfig), nil
|
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
|
||||||
|
}
|
||||||
|
56
internal/v2/repository/idp/configs_read_model.go
Normal file
56
internal/v2/repository/idp/configs_read_model.go
Normal 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()
|
||||||
|
}
|
@ -14,8 +14,7 @@ type ReadModel struct {
|
|||||||
//NewMemberReadModel is the default constructor of ReadModel
|
//NewMemberReadModel is the default constructor of ReadModel
|
||||||
func NewMemberReadModel(userID string) *ReadModel {
|
func NewMemberReadModel(userID string) *ReadModel {
|
||||||
return &ReadModel{
|
return &ReadModel{
|
||||||
ReadModel: *eventstore.NewReadModel(),
|
UserID: userID,
|
||||||
UserID: userID,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,13 +11,6 @@ type ReadModel struct {
|
|||||||
Members []*member.ReadModel
|
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) {
|
func (rm *ReadModel) MemberByUserID(id string) (idx int, member *member.ReadModel) {
|
||||||
for idx, member = range rm.Members {
|
for idx, member = range rm.Members {
|
||||||
if member.UserID == id {
|
if member.UserID == id {
|
||||||
@ -31,12 +24,12 @@ func (rm *ReadModel) AppendEvents(events ...eventstore.EventReader) {
|
|||||||
for _, event := range events {
|
for _, event := range events {
|
||||||
switch e := event.(type) {
|
switch e := event.(type) {
|
||||||
case *member.AddedEvent:
|
case *member.AddedEvent:
|
||||||
member := member.NewMemberReadModel(e.UserID)
|
m := member.NewMemberReadModel(e.UserID)
|
||||||
rm.Members = append(rm.Members, member)
|
rm.Members = append(rm.Members, m)
|
||||||
member.AppendEvents(e)
|
m.AppendEvents(e)
|
||||||
case *member.ChangedEvent:
|
case *member.ChangedEvent:
|
||||||
_, member := rm.MemberByUserID(e.UserID)
|
_, m := rm.MemberByUserID(e.UserID)
|
||||||
member.AppendEvents(e)
|
m.AppendEvents(e)
|
||||||
case *member.RemovedEvent:
|
case *member.RemovedEvent:
|
||||||
idx, _ := rm.MemberByUserID(e.UserID)
|
idx, _ := rm.MemberByUserID(e.UserID)
|
||||||
if idx < 0 {
|
if idx < 0 {
|
||||||
@ -50,8 +43,8 @@ func (rm *ReadModel) AppendEvents(events ...eventstore.EventReader) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rm *ReadModel) Reduce() (err error) {
|
func (rm *ReadModel) Reduce() (err error) {
|
||||||
for _, member := range rm.Members {
|
for _, m := range rm.Members {
|
||||||
err = member.Reduce()
|
err = m.Reduce()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user