2020-10-14 12:43:31 +02:00
|
|
|
package eventstore_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-23 16:16:46 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
2020-10-14 12:43:31 +02:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 12:19:10 +02:00
|
|
|
query_repo "github.com/zitadel/zitadel/internal/eventstore/repository/sql"
|
|
|
|
v3 "github.com/zitadel/zitadel/internal/eventstore/v3"
|
2020-10-14 12:43:31 +02:00
|
|
|
)
|
|
|
|
|
2020-10-23 16:16:46 +02:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
// User aggregate start
|
|
|
|
// ------------------------------------------------------------
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewUserAggregate(id string) *eventstore.Aggregate {
|
|
|
|
return eventstore.NewAggregate(
|
2022-03-15 07:19:02 +01:00
|
|
|
authz.NewMockContext("zitadel", "caos", "adlerhurst"),
|
2021-02-18 14:48:27 +01:00
|
|
|
id,
|
|
|
|
"test.user",
|
|
|
|
"v1",
|
|
|
|
)
|
2020-10-14 12:43:31 +02:00
|
|
|
}
|
|
|
|
|
2020-10-23 16:16:46 +02:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
// User added event start
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2020-10-14 12:43:31 +02:00
|
|
|
type UserAddedEvent struct {
|
2021-02-23 17:05:47 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
2020-11-06 13:47:27 +01:00
|
|
|
|
2020-10-23 16:16:46 +02:00
|
|
|
FirstName string `json:"firstName"`
|
2020-11-06 13:47:27 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewUserAddedEvent(id string, firstName string) *UserAddedEvent {
|
2020-11-06 13:47:27 +01:00
|
|
|
return &UserAddedEvent{
|
|
|
|
FirstName: firstName,
|
2021-02-18 14:48:27 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
context.Background(),
|
|
|
|
NewUserAggregate(id),
|
|
|
|
"user.added"),
|
2020-11-06 13:47:27 +01:00
|
|
|
}
|
2020-10-23 16:16:46 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func UserAddedEventMapper() (eventstore.AggregateType, eventstore.EventType, func(eventstore.Event) (eventstore.Event, error)) {
|
|
|
|
return "user", "user.added", func(event eventstore.Event) (eventstore.Event, error) {
|
2020-10-23 16:16:46 +02:00
|
|
|
e := &UserAddedEvent{
|
2020-11-06 13:47:27 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
2020-10-23 16:16:46 +02:00
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
|
|
|
|
err := event.Unmarshal(e)
|
2020-10-23 16:16:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return e, nil
|
|
|
|
}
|
2020-10-14 12:43:31 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserAddedEvent) Payload() interface{} {
|
2020-10-14 12:43:31 +02:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *UserAddedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-23 16:16:46 +02:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
// User first name changed event start
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2020-10-14 12:43:31 +02:00
|
|
|
type UserFirstNameChangedEvent struct {
|
2020-11-06 13:47:27 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
FirstName string `json:"firstName"`
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewUserFirstNameChangedEvent(id, firstName string) *UserFirstNameChangedEvent {
|
2020-11-06 13:47:27 +01:00
|
|
|
return &UserFirstNameChangedEvent{
|
|
|
|
FirstName: firstName,
|
2021-02-18 14:48:27 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
context.Background(),
|
|
|
|
NewUserAggregate(id),
|
|
|
|
"user.firstname.changed"),
|
2020-11-06 13:47:27 +01:00
|
|
|
}
|
2020-10-23 16:16:46 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func UserFirstNameChangedMapper() (eventstore.AggregateType, eventstore.EventType, func(eventstore.Event) (eventstore.Event, error)) {
|
|
|
|
return "user", "user.firstName.changed", func(event eventstore.Event) (eventstore.Event, error) {
|
2020-10-23 16:16:46 +02:00
|
|
|
e := &UserFirstNameChangedEvent{
|
2020-11-06 13:47:27 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
2020-10-23 16:16:46 +02:00
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2020-10-23 16:16:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return e, nil
|
|
|
|
}
|
2020-10-14 12:43:31 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserFirstNameChangedEvent) Payload() interface{} {
|
2020-10-14 12:43:31 +02:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserFirstNameChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *UserFirstNameChangedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-23 16:16:46 +02:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
// User password checked event start
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
type UserPasswordCheckedEvent struct {
|
2020-11-06 13:47:27 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewUserPasswordCheckedEvent(id string) *UserPasswordCheckedEvent {
|
2020-11-06 13:47:27 +01:00
|
|
|
return &UserPasswordCheckedEvent{
|
2021-02-18 14:48:27 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
context.Background(),
|
|
|
|
NewUserAggregate(id),
|
|
|
|
"user.password.checked"),
|
2020-11-06 13:47:27 +01:00
|
|
|
}
|
2020-10-23 16:16:46 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func UserPasswordCheckedMapper() (eventstore.AggregateType, eventstore.EventType, func(eventstore.Event) (eventstore.Event, error)) {
|
|
|
|
return "user", "user.password.checked", func(event eventstore.Event) (eventstore.Event, error) {
|
2020-10-23 16:16:46 +02:00
|
|
|
return &UserPasswordCheckedEvent{
|
2020-11-06 13:47:27 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
2020-10-23 16:16:46 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserPasswordCheckedEvent) Payload() interface{} {
|
2020-10-23 16:16:46 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserPasswordCheckedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *UserPasswordCheckedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-27 16:03:17 +01:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
// User deleted event
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
type UserDeletedEvent struct {
|
2020-11-06 13:47:27 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewUserDeletedEvent(id string) *UserDeletedEvent {
|
2020-11-06 13:47:27 +01:00
|
|
|
return &UserDeletedEvent{
|
2021-02-18 14:48:27 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
context.Background(),
|
|
|
|
NewUserAggregate(id),
|
|
|
|
"user.deleted"),
|
2020-11-06 13:47:27 +01:00
|
|
|
}
|
2020-10-27 16:03:17 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func UserDeletedMapper() (eventstore.AggregateType, eventstore.EventType, func(eventstore.Event) (eventstore.Event, error)) {
|
|
|
|
return "user", "user.deleted", func(event eventstore.Event) (eventstore.Event, error) {
|
2020-10-27 16:03:17 +01:00
|
|
|
return &UserDeletedEvent{
|
2020-11-06 13:47:27 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
2020-10-27 16:03:17 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserDeletedEvent) Payload() interface{} {
|
2020-10-27 16:03:17 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserDeletedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *UserDeletedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-23 16:16:46 +02:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
// Users read model start
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
type UsersReadModel struct {
|
|
|
|
eventstore.ReadModel
|
2020-11-11 17:51:44 +01:00
|
|
|
|
2020-10-23 16:16:46 +02:00
|
|
|
Users []*UserReadModel
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (rm *UsersReadModel) AppendEvents(events ...eventstore.Event) {
|
2020-10-23 16:16:46 +02:00
|
|
|
rm.ReadModel.AppendEvents(events...)
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *UserAddedEvent:
|
2020-10-26 14:49:42 +01:00
|
|
|
//insert
|
2021-02-18 14:48:27 +01:00
|
|
|
user := NewUserReadModel(e.Aggregate().ID)
|
2020-10-23 16:16:46 +02:00
|
|
|
rm.Users = append(rm.Users, user)
|
2020-11-26 09:19:14 +01:00
|
|
|
user.AppendEvents(e)
|
2020-10-23 16:16:46 +02:00
|
|
|
case *UserFirstNameChangedEvent, *UserPasswordCheckedEvent:
|
2020-10-26 14:49:42 +01:00
|
|
|
//update
|
2021-02-18 14:48:27 +01:00
|
|
|
_, user := rm.userByID(e.Aggregate().ID)
|
2020-10-23 16:16:46 +02:00
|
|
|
if user == nil {
|
2020-11-26 09:19:14 +01:00
|
|
|
return
|
2020-10-23 16:16:46 +02:00
|
|
|
}
|
2020-11-26 09:19:14 +01:00
|
|
|
user.AppendEvents(e)
|
2020-10-27 16:03:17 +01:00
|
|
|
case *UserDeletedEvent:
|
2021-02-18 14:48:27 +01:00
|
|
|
idx, _ := rm.userByID(e.Aggregate().ID)
|
2020-10-27 16:03:17 +01:00
|
|
|
if idx < 0 {
|
2020-11-26 09:19:14 +01:00
|
|
|
return
|
2020-10-27 16:03:17 +01:00
|
|
|
}
|
|
|
|
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]
|
2020-10-23 16:16:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *UsersReadModel) Reduce() error {
|
|
|
|
for _, user := range rm.Users {
|
|
|
|
err := user.Reduce()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rm.ReadModel.Reduce()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *UsersReadModel) userByID(id string) (idx int, user *UserReadModel) {
|
|
|
|
for idx, user = range rm.Users {
|
2020-11-11 17:51:44 +01:00
|
|
|
if user.ID == id {
|
2020-10-23 16:16:46 +02:00
|
|
|
return idx, user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
// User read model start
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2020-10-14 12:43:31 +02:00
|
|
|
type UserReadModel struct {
|
|
|
|
eventstore.ReadModel
|
2020-11-11 17:51:44 +01:00
|
|
|
|
|
|
|
ID string
|
2020-10-23 16:16:46 +02:00
|
|
|
FirstName string
|
|
|
|
pwCheckCount int
|
|
|
|
lastPasswordCheck time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserReadModel(id string) *UserReadModel {
|
|
|
|
return &UserReadModel{
|
2020-11-26 09:19:14 +01:00
|
|
|
ID: id,
|
2020-10-23 16:16:46 +02:00
|
|
|
}
|
2020-10-14 12:43:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *UserReadModel) Reduce() error {
|
|
|
|
for _, event := range rm.ReadModel.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *UserAddedEvent:
|
|
|
|
rm.FirstName = e.FirstName
|
|
|
|
case *UserFirstNameChangedEvent:
|
|
|
|
rm.FirstName = e.FirstName
|
2020-10-23 16:16:46 +02:00
|
|
|
case *UserPasswordCheckedEvent:
|
|
|
|
rm.pwCheckCount++
|
2020-11-06 17:25:07 +01:00
|
|
|
rm.lastPasswordCheck = e.CreationDate()
|
2020-10-14 12:43:31 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-23 16:16:46 +02:00
|
|
|
rm.ReadModel.Reduce()
|
2020-10-14 12:43:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-10-23 16:16:46 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
// Tests
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
func TestUserReadModel(t *testing.T) {
|
2023-10-19 12:19:10 +02:00
|
|
|
es := eventstore.NewEventstore(
|
|
|
|
&eventstore.Config{
|
|
|
|
Querier: query_repo.NewCRDB(testCRDBClient),
|
|
|
|
Pusher: v3.NewEventstore(testCRDBClient),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-10-23 16:16:46 +02:00
|
|
|
es.RegisterFilterEventMapper(UserAddedEventMapper()).
|
|
|
|
RegisterFilterEventMapper(UserFirstNameChangedMapper()).
|
2020-10-27 16:03:17 +01:00
|
|
|
RegisterFilterEventMapper(UserPasswordCheckedMapper()).
|
|
|
|
RegisterFilterEventMapper(UserDeletedMapper())
|
2020-10-23 16:16:46 +02:00
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
events, err := es.Push(context.Background(),
|
2021-02-18 14:48:27 +01:00
|
|
|
NewUserAddedEvent("1", "hodor"),
|
|
|
|
NewUserAddedEvent("2", "hodor"),
|
|
|
|
NewUserPasswordCheckedEvent("2"),
|
|
|
|
NewUserPasswordCheckedEvent("2"),
|
|
|
|
NewUserFirstNameChangedEvent("2", "ueli"),
|
|
|
|
NewUserDeletedEvent("2"))
|
|
|
|
|
2020-10-23 16:16:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error on push aggregates: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
events = append(events, nil)
|
|
|
|
|
2022-03-15 07:19:02 +01:00
|
|
|
t.Logf("%+v\n", events)
|
2020-10-23 16:16:46 +02:00
|
|
|
|
2020-11-26 09:19:14 +01:00
|
|
|
users := UsersReadModel{}
|
2021-07-06 13:55:57 +02:00
|
|
|
|
|
|
|
err = es.FilterToReducer(context.Background(), eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).AddQuery().AggregateTypes("test.user").Builder(), &users)
|
2020-10-23 16:16:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error on filter to reducer: %v", err)
|
|
|
|
}
|
2022-03-15 07:19:02 +01:00
|
|
|
t.Logf("%+v", users)
|
2020-10-23 16:16:46 +02:00
|
|
|
}
|