2022-01-21 07:52:12 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2022-03-24 16:21:34 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/instance"
|
2022-01-21 07:52:12 +00:00
|
|
|
)
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func TestInstanceProjection_reduces(t *testing.T) {
|
2022-01-21 07:52:12 +00:00
|
|
|
type args struct {
|
|
|
|
event func(t *testing.T) eventstore.Event
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
reduce func(event eventstore.Event) (*handler.Statement, error)
|
|
|
|
want wantReduce
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "reduceGlobalOrgSet",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.GlobalOrgSetEventType),
|
|
|
|
instance.AggregateType,
|
2022-01-21 07:52:12 +00:00
|
|
|
[]byte(`{"globalOrgId": "orgid"}`),
|
2022-03-24 16:21:34 +00:00
|
|
|
), instance.GlobalOrgSetMapper),
|
2022-01-21 07:52:12 +00:00
|
|
|
},
|
2022-03-28 08:05:09 +00:00
|
|
|
reduce: (&InstanceProjection{}).reduceGlobalOrgSet,
|
2022-01-21 07:52:12 +00:00
|
|
|
want: wantReduce{
|
2022-03-24 16:21:34 +00:00
|
|
|
projection: InstanceProjectionTable,
|
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
2022-01-21 07:52:12 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-28 08:05:09 +00:00
|
|
|
expectedStmt: "UPSERT INTO projections.instances (id, change_date, sequence, global_org_id) VALUES ($1, $2, $3, $4)",
|
2022-01-21 07:52:12 +00:00
|
|
|
expectedArgs: []interface{}{
|
2022-03-23 08:02:39 +00:00
|
|
|
"instance-id",
|
2022-01-21 07:52:12 +00:00
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
"orgid",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-02-16 15:49:17 +00:00
|
|
|
name: "reduceProjectIDSet",
|
2022-01-21 07:52:12 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.ProjectSetEventType),
|
|
|
|
instance.AggregateType,
|
2022-01-21 07:52:12 +00:00
|
|
|
[]byte(`{"iamProjectId": "project-id"}`),
|
2022-03-24 16:21:34 +00:00
|
|
|
), instance.ProjectSetMapper),
|
2022-01-21 07:52:12 +00:00
|
|
|
},
|
2022-03-28 08:05:09 +00:00
|
|
|
reduce: (&InstanceProjection{}).reduceIAMProjectSet,
|
2022-01-21 07:52:12 +00:00
|
|
|
want: wantReduce{
|
2022-03-24 16:21:34 +00:00
|
|
|
projection: InstanceProjectionTable,
|
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
2022-01-21 07:52:12 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-28 08:05:09 +00:00
|
|
|
expectedStmt: "UPSERT INTO projections.instances (id, change_date, sequence, iam_project_id) VALUES ($1, $2, $3, $4)",
|
2022-01-21 07:52:12 +00:00
|
|
|
expectedArgs: []interface{}{
|
2022-03-23 08:02:39 +00:00
|
|
|
"instance-id",
|
2022-01-21 07:52:12 +00:00
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
"project-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-02-16 15:49:17 +00:00
|
|
|
{
|
|
|
|
name: "reduceDefaultLanguageSet",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.DefaultLanguageSetEventType),
|
|
|
|
instance.AggregateType,
|
2022-02-16 15:49:17 +00:00
|
|
|
[]byte(`{"language": "en"}`),
|
2022-03-24 16:21:34 +00:00
|
|
|
), instance.DefaultLanguageSetMapper),
|
2022-02-16 15:49:17 +00:00
|
|
|
},
|
2022-03-28 08:05:09 +00:00
|
|
|
reduce: (&InstanceProjection{}).reduceDefaultLanguageSet,
|
2022-02-16 15:49:17 +00:00
|
|
|
want: wantReduce{
|
2022-03-24 16:21:34 +00:00
|
|
|
projection: InstanceProjectionTable,
|
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
2022-02-16 15:49:17 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-28 08:05:09 +00:00
|
|
|
expectedStmt: "UPSERT INTO projections.instances (id, change_date, sequence, default_language) VALUES ($1, $2, $3, $4)",
|
2022-02-16 15:49:17 +00:00
|
|
|
expectedArgs: []interface{}{
|
2022-03-23 08:02:39 +00:00
|
|
|
"instance-id",
|
2022-02-16 15:49:17 +00:00
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
"en",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-21 07:52:12 +00:00
|
|
|
{
|
|
|
|
name: "reduceSetupStarted",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.SetupStartedEventType),
|
|
|
|
instance.AggregateType,
|
2022-01-21 07:52:12 +00:00
|
|
|
[]byte(`{"Step": 1}`),
|
2022-03-24 16:21:34 +00:00
|
|
|
), instance.SetupStepMapper),
|
2022-01-21 07:52:12 +00:00
|
|
|
},
|
2022-03-28 08:05:09 +00:00
|
|
|
reduce: (&InstanceProjection{}).reduceSetupEvent,
|
2022-01-21 07:52:12 +00:00
|
|
|
want: wantReduce{
|
2022-03-24 16:21:34 +00:00
|
|
|
projection: InstanceProjectionTable,
|
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
2022-01-21 07:52:12 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-28 08:05:09 +00:00
|
|
|
expectedStmt: "UPSERT INTO projections.instances (id, change_date, sequence, setup_started) VALUES ($1, $2, $3, $4)",
|
2022-01-21 07:52:12 +00:00
|
|
|
expectedArgs: []interface{}{
|
2022-03-23 08:02:39 +00:00
|
|
|
"instance-id",
|
2022-01-21 07:52:12 +00:00
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
domain.Step1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceSetupDone",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.SetupDoneEventType),
|
|
|
|
instance.AggregateType,
|
2022-01-21 07:52:12 +00:00
|
|
|
[]byte(`{"Step": 1}`),
|
2022-03-24 16:21:34 +00:00
|
|
|
), instance.SetupStepMapper),
|
2022-01-21 07:52:12 +00:00
|
|
|
},
|
2022-03-28 08:05:09 +00:00
|
|
|
reduce: (&InstanceProjection{}).reduceSetupEvent,
|
2022-01-21 07:52:12 +00:00
|
|
|
want: wantReduce{
|
2022-03-24 16:21:34 +00:00
|
|
|
projection: InstanceProjectionTable,
|
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
2022-01-21 07:52:12 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-28 08:05:09 +00:00
|
|
|
expectedStmt: "UPSERT INTO projections.instances (id, change_date, sequence, setup_done) VALUES ($1, $2, $3, $4)",
|
2022-01-21 07:52:12 +00:00
|
|
|
expectedArgs: []interface{}{
|
2022-03-23 08:02:39 +00:00
|
|
|
"instance-id",
|
2022-01-21 07:52:12 +00:00
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
domain.Step1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
event := baseEvent(t)
|
|
|
|
got, err := tt.reduce(event)
|
|
|
|
if _, ok := err.(errors.InvalidArgument); !ok {
|
|
|
|
t.Errorf("no wrong event mapping: %v, got: %v", err, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
event = tt.args.event(t)
|
|
|
|
got, err = tt.reduce(event)
|
|
|
|
assertReduce(t, got, err, tt.want)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|