2021-12-02 08:31:44 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
2022-11-30 16:01:17 +00:00
|
|
|
"context"
|
2021-12-02 08:31:44 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-11-30 16:01:17 +00:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2022-08-31 07:52:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2022-11-30 16:01:17 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-11-30 16:01:17 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-12-02 08:31:44 +00:00
|
|
|
)
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func TestInstanceMemberProjection_reduces(t *testing.T) {
|
2021-12-02 08:31:44 +00:00
|
|
|
type args struct {
|
2022-01-03 08:19:07 +00:00
|
|
|
event func(t *testing.T) eventstore.Event
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2022-01-03 08:19:07 +00:00
|
|
|
reduce func(event eventstore.Event) (*handler.Statement, error)
|
2021-12-02 08:31:44 +00:00
|
|
|
want wantReduce
|
|
|
|
}{
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
name: "instance MemberAddedType",
|
2021-12-02 08:31:44 +00:00
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
instance.MemberAddedEventType,
|
|
|
|
instance.AggregateType,
|
|
|
|
[]byte(`{
|
2021-12-02 08:31:44 +00:00
|
|
|
"userId": "user-id",
|
|
|
|
"roles": ["role"]
|
|
|
|
}`),
|
2023-10-19 10:19:10 +00:00
|
|
|
), instance.MemberAddedEventMapper),
|
2021-12-02 08:31:44 +00:00
|
|
|
},
|
2022-11-30 16:01:17 +00:00
|
|
|
reduce: (&instanceMemberProjection{
|
2023-10-19 10:19:10 +00: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 08:31:44 +00:00
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: instance.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-02 08:31:44 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-11-20 15:21:08 +00: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 08:31:44 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"user-id",
|
2022-11-30 16:01:17 +00:00
|
|
|
"org1",
|
2023-10-19 10:19:10 +00:00
|
|
|
database.TextArray[string]{"role"},
|
2021-12-02 08:31:44 +00:00
|
|
|
anyArg{},
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
"ro-id",
|
2022-03-23 08:02:39 +00:00
|
|
|
"instance-id",
|
2021-12-02 08:31:44 +00:00
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
name: "instance MemberChangedType",
|
2021-12-02 08:31:44 +00:00
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
instance.MemberChangedEventType,
|
|
|
|
instance.AggregateType,
|
|
|
|
[]byte(`{
|
2021-12-02 08:31:44 +00:00
|
|
|
"userId": "user-id",
|
|
|
|
"roles": ["role", "changed"]
|
|
|
|
}`),
|
2023-10-19 10:19:10 +00:00
|
|
|
), instance.MemberChangedEventMapper),
|
2021-12-02 08:31:44 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&instanceMemberProjection{}).reduceChanged,
|
2021-12-02 08:31:44 +00:00
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: instance.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-02 08:31:44 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-11-20 15:21:08 +00: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 08:31:44 +00:00
|
|
|
expectedArgs: []interface{}{
|
2023-10-19 10:19:10 +00:00
|
|
|
database.TextArray[string]{"role", "changed"},
|
2021-12-02 08:31:44 +00:00
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2021-12-02 08:31:44 +00:00
|
|
|
"user-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
name: "instance MemberCascadeRemovedType",
|
2021-12-02 08:31:44 +00:00
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
instance.MemberCascadeRemovedEventType,
|
|
|
|
instance.AggregateType,
|
|
|
|
[]byte(`{
|
2021-12-02 08:31:44 +00:00
|
|
|
"userId": "user-id"
|
|
|
|
}`),
|
2023-10-19 10:19:10 +00:00
|
|
|
), instance.MemberCascadeRemovedEventMapper),
|
2021-12-02 08:31:44 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&instanceMemberProjection{}).reduceCascadeRemoved,
|
2021-12-02 08:31:44 +00:00
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: instance.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-02 08:31:44 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-11-20 15:21:08 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.instance_members4 WHERE (instance_id = $1) AND (user_id = $2)",
|
2021-12-02 08:31:44 +00:00
|
|
|
expectedArgs: []interface{}{
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2021-12-02 08:31:44 +00:00
|
|
|
"user-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
name: "instance MemberRemovedType",
|
2021-12-02 08:31:44 +00:00
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
instance.MemberRemovedEventType,
|
|
|
|
instance.AggregateType,
|
|
|
|
[]byte(`{
|
2021-12-02 08:31:44 +00:00
|
|
|
"userId": "user-id"
|
|
|
|
}`),
|
2023-10-19 10:19:10 +00:00
|
|
|
), instance.MemberRemovedEventMapper),
|
2021-12-02 08:31:44 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&instanceMemberProjection{}).reduceRemoved,
|
2021-12-02 08:31:44 +00:00
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: instance.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-02 08:31:44 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-11-20 15:21:08 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.instance_members4 WHERE (instance_id = $1) AND (user_id = $2)",
|
2021-12-02 08:31:44 +00:00
|
|
|
expectedArgs: []interface{}{
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2021-12-02 08:31:44 +00:00
|
|
|
"user-id",
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
name: "user UserRemoved",
|
2021-12-16 13:25:38 +00:00
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
user.UserRemovedType,
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{}`),
|
|
|
|
), user.UserRemovedEventMapper),
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&instanceMemberProjection{}).reduceUserRemoved,
|
2021-12-16 13:25:38 +00:00
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: user.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-16 13:25:38 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-11-20 15:21:08 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.instance_members4 WHERE (instance_id = $1) AND (user_id = $2)",
|
2021-12-16 13:25:38 +00:00
|
|
|
expectedArgs: []interface{}{
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2021-12-02 08:31:44 +00:00
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-30 16:01:17 +00:00
|
|
|
{
|
|
|
|
name: "org.OrgRemoved",
|
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
org.OrgRemovedEventType,
|
|
|
|
org.AggregateType,
|
|
|
|
nil,
|
|
|
|
), org.OrgRemovedEventMapper),
|
2022-11-30 16:01:17 +00:00
|
|
|
},
|
|
|
|
reduce: (&instanceMemberProjection{}).reduceUserOwnerRemoved,
|
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: org.AggregateType,
|
|
|
|
sequence: 15,
|
2022-11-30 16:01:17 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-11-20 15:21:08 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.instance_members4 WHERE (instance_id = $1) AND (user_resource_owner = $2)",
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"instance-id",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
name: "instance reduceInstanceRemoved",
|
2022-10-20 12:36:52 +00:00
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
instance.InstanceRemovedEventType,
|
|
|
|
instance.AggregateType,
|
|
|
|
nil,
|
|
|
|
), instance.InstanceRemovedEventMapper),
|
2022-10-20 12:36:52 +00:00
|
|
|
},
|
|
|
|
reduce: reduceInstanceRemovedHelper(MemberInstanceID),
|
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
|
|
|
sequence: 15,
|
2022-10-20 12:36:52 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-11-20 15:21:08 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.instance_members4 WHERE (instance_id = $1)",
|
2022-10-20 12:36:52 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
event := baseEvent(t)
|
|
|
|
got, err := tt.reduce(event)
|
2023-12-08 14:30:55 +00:00
|
|
|
if ok := zerrors.IsErrorInvalidArgument(err); !ok {
|
2021-12-02 08:31:44 +00: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 12:36:52 +00:00
|
|
|
assertReduce(t, got, err, InstanceMemberProjectionTable, tt.want)
|
2021-12-02 08:31:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|