zitadel/internal/eventstore/example_test.go

304 lines
7.7 KiB
Go
Raw Normal View History

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