mirror of
https://github.com/zitadel/zitadel.git
synced 2025-01-10 18:43:40 +00:00
189 lines
5.0 KiB
Go
189 lines
5.0 KiB
Go
|
package eventsourcing
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/caos/zitadel/internal/api/authz"
|
||
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
||
|
"github.com/caos/zitadel/internal/eventstore/models"
|
||
|
iam_es_model "github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
|
||
|
"github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
||
|
)
|
||
|
|
||
|
func TestMailTextAddedAggregate(t *testing.T) {
|
||
|
type args struct {
|
||
|
ctx context.Context
|
||
|
existing *model.Org
|
||
|
new *iam_es_model.MailText
|
||
|
aggCreator *models.AggregateCreator
|
||
|
}
|
||
|
type res struct {
|
||
|
eventLen int
|
||
|
eventTypes []models.EventType
|
||
|
wantErr bool
|
||
|
errFunc func(err error) bool
|
||
|
}
|
||
|
tests := []struct {
|
||
|
name string
|
||
|
args args
|
||
|
res res
|
||
|
}{
|
||
|
{
|
||
|
name: "add mailtext",
|
||
|
args: args{
|
||
|
ctx: authz.NewMockContext("orgID", "userID"),
|
||
|
existing: &model.Org{
|
||
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
||
|
},
|
||
|
new: &iam_es_model.MailText{
|
||
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
||
|
MailTextType: "Type",
|
||
|
Language: "DE",
|
||
|
},
|
||
|
aggCreator: models.NewAggregateCreator("Test"),
|
||
|
},
|
||
|
res: res{
|
||
|
eventLen: 1,
|
||
|
eventTypes: []models.EventType{model.MailTextAdded},
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "existing org nil",
|
||
|
args: args{
|
||
|
ctx: authz.NewMockContext("orgID", "userID"),
|
||
|
existing: nil,
|
||
|
aggCreator: models.NewAggregateCreator("Test"),
|
||
|
},
|
||
|
res: res{
|
||
|
wantErr: true,
|
||
|
errFunc: caos_errs.IsPreconditionFailed,
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "mailtext config nil",
|
||
|
args: args{
|
||
|
ctx: authz.NewMockContext("orgID", "userID"),
|
||
|
existing: &model.Org{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}},
|
||
|
new: nil,
|
||
|
aggCreator: models.NewAggregateCreator("Test"),
|
||
|
},
|
||
|
res: res{
|
||
|
wantErr: true,
|
||
|
errFunc: caos_errs.IsPreconditionFailed,
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
for _, tt := range tests {
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
agg, err := MailTextAddedAggregate(tt.args.aggCreator, tt.args.existing, tt.args.new)(tt.args.ctx)
|
||
|
|
||
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
||
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
||
|
}
|
||
|
for i := 0; i < tt.res.eventLen; i++ {
|
||
|
if !tt.res.wantErr && agg.Events[i].Type != tt.res.eventTypes[i] {
|
||
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventTypes[i], agg.Events[i].Type.String())
|
||
|
}
|
||
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
||
|
t.Errorf("should have data in event")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
||
|
t.Errorf("got wrong err: %v ", err)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestMailTextChangedAggregate(t *testing.T) {
|
||
|
type args struct {
|
||
|
ctx context.Context
|
||
|
existing *model.Org
|
||
|
new *iam_es_model.MailText
|
||
|
aggCreator *models.AggregateCreator
|
||
|
}
|
||
|
type res struct {
|
||
|
eventLen int
|
||
|
eventTypes []models.EventType
|
||
|
wantErr bool
|
||
|
errFunc func(err error) bool
|
||
|
}
|
||
|
tests := []struct {
|
||
|
name string
|
||
|
args args
|
||
|
res res
|
||
|
}{
|
||
|
{
|
||
|
name: "change mailtext",
|
||
|
args: args{
|
||
|
ctx: authz.NewMockContext("orgID", "userID"),
|
||
|
existing: &model.Org{
|
||
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
||
|
MailTexts: []*iam_es_model.MailText{&iam_es_model.MailText{
|
||
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}},
|
||
|
},
|
||
|
},
|
||
|
new: &iam_es_model.MailText{
|
||
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
||
|
MailTextType: "Type",
|
||
|
Language: "DE",
|
||
|
Subject: "Subject",
|
||
|
},
|
||
|
aggCreator: models.NewAggregateCreator("Test"),
|
||
|
},
|
||
|
res: res{
|
||
|
eventLen: 1,
|
||
|
eventTypes: []models.EventType{model.MailTextChanged},
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "existing org nil",
|
||
|
args: args{
|
||
|
ctx: authz.NewMockContext("orgID", "userID"),
|
||
|
existing: nil,
|
||
|
aggCreator: models.NewAggregateCreator("Test"),
|
||
|
},
|
||
|
res: res{
|
||
|
wantErr: true,
|
||
|
errFunc: caos_errs.IsPreconditionFailed,
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "mailtext config nil",
|
||
|
args: args{
|
||
|
ctx: authz.NewMockContext("orgID", "userID"),
|
||
|
existing: &model.Org{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}},
|
||
|
new: nil,
|
||
|
aggCreator: models.NewAggregateCreator("Test"),
|
||
|
},
|
||
|
res: res{
|
||
|
wantErr: true,
|
||
|
errFunc: caos_errs.IsPreconditionFailed,
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
for _, tt := range tests {
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
agg, err := MailTextChangedAggregate(tt.args.aggCreator, tt.args.existing, tt.args.new)(tt.args.ctx)
|
||
|
|
||
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
||
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
||
|
}
|
||
|
for i := 0; i < tt.res.eventLen; i++ {
|
||
|
if !tt.res.wantErr && agg.Events[i].Type != tt.res.eventTypes[i] {
|
||
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventTypes[i], agg.Events[i].Type.String())
|
||
|
}
|
||
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
||
|
t.Errorf("should have data in event")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
||
|
t.Errorf("got wrong err: %v ", err)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|