2021-12-02 09:31:44 +01:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
2022-11-30 17:01:17 +01:00
|
|
|
"context"
|
2021-12-02 09:31:44 +01:00
|
|
|
"testing"
|
|
|
|
|
2022-11-30 17:01:17 +01:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2022-08-31 09:52:43 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2022-11-30 17:01:17 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 12:19:10 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-11-30 17:01:17 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-12-02 09:31:44 +01:00
|
|
|
)
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func TestInstanceMemberProjection_reduces(t *testing.T) {
|
2021-12-02 09:31:44 +01:00
|
|
|
type args struct {
|
2022-01-03 09:19:07 +01:00
|
|
|
event func(t *testing.T) eventstore.Event
|
2021-12-02 09:31:44 +01:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2022-01-03 09:19:07 +01:00
|
|
|
reduce func(event eventstore.Event) (*handler.Statement, error)
|
2021-12-02 09:31:44 +01:00
|
|
|
want wantReduce
|
|
|
|
}{
|
|
|
|
{
|
2022-11-10 11:59:33 +01:00
|
|
|
name: "instance MemberAddedType",
|
2021-12-02 09:31:44 +01:00
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
instance.MemberAddedEventType,
|
|
|
|
instance.AggregateType,
|
|
|
|
[]byte(`{
|
2021-12-02 09:31:44 +01:00
|
|
|
"userId": "user-id",
|
|
|
|
"roles": ["role"]
|
|
|
|
}`),
|
2023-10-19 12:19:10 +02:00
|
|
|
), instance.MemberAddedEventMapper),
|
2021-12-02 09:31:44 +01:00
|
|
|
},
|
2022-11-30 17:01:17 +01:00
|
|
|
reduce: (&instanceMemberProjection{
|
2023-10-19 12:19:10 +02:00
|
|
|
es: newMockEventStore().appendFilterResponse(
|
|
|
|
[]eventstore.Event{
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user-id", "org1").Aggregate,
|
|
|
|
"username1",
|
|
|
|
"firstname1",
|
|
|
|
"lastname1",
|
|
|
|
"nickname1",
|
|
|
|
"displayname1",
|
|
|
|
language.German,
|
|
|
|
domain.GenderMale,
|
|
|
|
"email1",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}).reduceAdded,
|
2021-12-02 09:31:44 +01:00
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: instance.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-02 09:31:44 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-11-20 16:21:08 +01:00
|
|
|
expectedStmt: "INSERT INTO projections.instance_members4 (user_id, user_resource_owner, roles, creation_date, change_date, sequence, resource_owner, instance_id, id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
|
2021-12-02 09:31:44 +01:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"user-id",
|
2022-11-30 17:01:17 +01:00
|
|
|
"org1",
|
2023-10-19 12:19:10 +02:00
|
|
|
database.TextArray[string]{"role"},
|
2021-12-02 09:31:44 +01:00
|
|
|
anyArg{},
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
"ro-id",
|
2022-03-23 09:02:39 +01:00
|
|
|
"instance-id",
|
2021-12-02 09:31:44 +01:00
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 11:59:33 +01:00
|
|
|
name: "instance MemberChangedType",
|
2021-12-02 09:31:44 +01:00
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
instance.MemberChangedEventType,
|
|
|
|
instance.AggregateType,
|
|
|
|
[]byte(`{
|
2021-12-02 09:31:44 +01:00
|
|
|
"userId": "user-id",
|
|
|
|
"roles": ["role", "changed"]
|
|
|
|
}`),
|
2023-10-19 12:19:10 +02:00
|
|
|
), instance.MemberChangedEventMapper),
|
2021-12-02 09:31:44 +01:00
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&instanceMemberProjection{}).reduceChanged,
|
2021-12-02 09:31:44 +01:00
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: instance.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-02 09:31:44 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-11-20 16:21:08 +01:00
|
|
|
expectedStmt: "UPDATE projections.instance_members4 SET (roles, change_date, sequence) = ($1, $2, $3) WHERE (instance_id = $4) AND (user_id = $5)",
|
2021-12-02 09:31:44 +01:00
|
|
|
expectedArgs: []interface{}{
|
2023-10-19 12:19:10 +02:00
|
|
|
database.TextArray[string]{"role", "changed"},
|
2021-12-02 09:31:44 +01:00
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
2022-11-10 11:59:33 +01:00
|
|
|
"instance-id",
|
2021-12-02 09:31:44 +01:00
|
|
|
"user-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 11:59:33 +01:00
|
|
|
name: "instance MemberCascadeRemovedType",
|
2021-12-02 09:31:44 +01:00
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
instance.MemberCascadeRemovedEventType,
|
|
|
|
instance.AggregateType,
|
|
|
|
[]byte(`{
|
2021-12-02 09:31:44 +01:00
|
|
|
"userId": "user-id"
|
|
|
|
}`),
|
2023-10-19 12:19:10 +02:00
|
|
|
), instance.MemberCascadeRemovedEventMapper),
|
2021-12-02 09:31:44 +01:00
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&instanceMemberProjection{}).reduceCascadeRemoved,
|
2021-12-02 09:31:44 +01:00
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: instance.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-02 09:31:44 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-11-20 16:21:08 +01:00
|
|
|
expectedStmt: "DELETE FROM projections.instance_members4 WHERE (instance_id = $1) AND (user_id = $2)",
|
2021-12-02 09:31:44 +01:00
|
|
|
expectedArgs: []interface{}{
|
2022-11-10 11:59:33 +01:00
|
|
|
"instance-id",
|
2021-12-02 09:31:44 +01:00
|
|
|
"user-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 11:59:33 +01:00
|
|
|
name: "instance MemberRemovedType",
|
2021-12-02 09:31:44 +01:00
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
instance.MemberRemovedEventType,
|
|
|
|
instance.AggregateType,
|
|
|
|
[]byte(`{
|
2021-12-02 09:31:44 +01:00
|
|
|
"userId": "user-id"
|
|
|
|
}`),
|
2023-10-19 12:19:10 +02:00
|
|
|
), instance.MemberRemovedEventMapper),
|
2021-12-02 09:31:44 +01:00
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&instanceMemberProjection{}).reduceRemoved,
|
2021-12-02 09:31:44 +01:00
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: instance.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-02 09:31:44 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-11-20 16:21:08 +01:00
|
|
|
expectedStmt: "DELETE FROM projections.instance_members4 WHERE (instance_id = $1) AND (user_id = $2)",
|
2021-12-02 09:31:44 +01:00
|
|
|
expectedArgs: []interface{}{
|
2022-11-10 11:59:33 +01:00
|
|
|
"instance-id",
|
2021-12-02 09:31:44 +01:00
|
|
|
"user-id",
|
2021-12-16 14:25:38 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 11:59:33 +01:00
|
|
|
name: "user UserRemoved",
|
2021-12-16 14:25:38 +01:00
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
user.UserRemovedType,
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{}`),
|
|
|
|
), user.UserRemovedEventMapper),
|
2021-12-16 14:25:38 +01:00
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&instanceMemberProjection{}).reduceUserRemoved,
|
2021-12-16 14:25:38 +01:00
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: user.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-16 14:25:38 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-11-20 16:21:08 +01:00
|
|
|
expectedStmt: "DELETE FROM projections.instance_members4 WHERE (instance_id = $1) AND (user_id = $2)",
|
2021-12-16 14:25:38 +01:00
|
|
|
expectedArgs: []interface{}{
|
2022-11-10 11:59:33 +01:00
|
|
|
"instance-id",
|
2021-12-02 09:31:44 +01:00
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-30 17:01:17 +01:00
|
|
|
{
|
|
|
|
name: "org.OrgRemoved",
|
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
org.OrgRemovedEventType,
|
|
|
|
org.AggregateType,
|
|
|
|
nil,
|
|
|
|
), org.OrgRemovedEventMapper),
|
2022-11-30 17:01:17 +01:00
|
|
|
},
|
|
|
|
reduce: (&instanceMemberProjection{}).reduceUserOwnerRemoved,
|
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: org.AggregateType,
|
|
|
|
sequence: 15,
|
2022-11-30 17:01:17 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-11-20 16:21:08 +01:00
|
|
|
expectedStmt: "DELETE FROM projections.instance_members4 WHERE (instance_id = $1) AND (user_resource_owner = $2)",
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"instance-id",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-20 13:36:52 +01:00
|
|
|
{
|
2022-11-10 11:59:33 +01:00
|
|
|
name: "instance reduceInstanceRemoved",
|
2022-10-20 13:36:52 +01:00
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
instance.InstanceRemovedEventType,
|
|
|
|
instance.AggregateType,
|
|
|
|
nil,
|
|
|
|
), instance.InstanceRemovedEventMapper),
|
2022-10-20 13:36:52 +01:00
|
|
|
},
|
|
|
|
reduce: reduceInstanceRemovedHelper(MemberInstanceID),
|
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
|
|
|
sequence: 15,
|
2022-10-20 13:36:52 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-11-20 16:21:08 +01:00
|
|
|
expectedStmt: "DELETE FROM projections.instance_members4 WHERE (instance_id = $1)",
|
2022-10-20 13:36:52 +01:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-02 09:31:44 +01:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
event := baseEvent(t)
|
|
|
|
got, err := tt.reduce(event)
|
2023-12-08 16:30:55 +02:00
|
|
|
if ok := zerrors.IsErrorInvalidArgument(err); !ok {
|
2021-12-02 09:31:44 +01:00
|
|
|
t.Errorf("no wrong event mapping: %v, got: %v", err, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
event = tt.args.event(t)
|
|
|
|
got, err = tt.reduce(event)
|
2022-10-20 13:36:52 +01:00
|
|
|
assertReduce(t, got, err, InstanceMemberProjectionTable, tt.want)
|
2021-12-02 09:31:44 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|