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/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 12:19:10 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-10-20 13:36:52 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-12-02 09:31:44 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestProjectMemberProjection_reduces(t *testing.T) {
|
|
|
|
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: "project MemberAddedType",
|
2021-12-02 09:31:44 +01:00
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
project.MemberAddedType,
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{
|
2021-12-02 09:31:44 +01:00
|
|
|
"userId": "user-id",
|
|
|
|
"roles": ["role"]
|
|
|
|
}`),
|
2023-10-19 12:19:10 +02:00
|
|
|
), project.MemberAddedEventMapper),
|
2021-12-02 09:31:44 +01:00
|
|
|
},
|
2022-11-30 17:01:17 +01:00
|
|
|
reduce: (&projectMemberProjection{
|
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: project.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-02 09:31:44 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "INSERT INTO projections.project_members3 (user_id, user_resource_owner, user_owner_removed, roles, creation_date, change_date, sequence, resource_owner, instance_id, owner_removed, project_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
|
2021-12-02 09:31:44 +01:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"user-id",
|
2022-11-30 17:01:17 +01:00
|
|
|
"org1",
|
|
|
|
false,
|
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",
|
2022-11-30 17:01:17 +01:00
|
|
|
false,
|
2021-12-02 09:31:44 +01:00
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 11:59:33 +01:00
|
|
|
name: "project MemberChangedType",
|
2021-12-02 09:31:44 +01:00
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
project.MemberChangedType,
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{
|
2021-12-02 09:31:44 +01:00
|
|
|
"userId": "user-id",
|
|
|
|
"roles": ["role", "changed"]
|
|
|
|
}`),
|
2023-10-19 12:19:10 +02:00
|
|
|
), project.MemberChangedEventMapper),
|
2021-12-02 09:31:44 +01:00
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&projectMemberProjection{}).reduceChanged,
|
2021-12-02 09:31:44 +01:00
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-02 09:31:44 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "UPDATE projections.project_members3 SET (roles, change_date, sequence) = ($1, $2, $3) WHERE (instance_id = $4) AND (user_id = $5) AND (project_id = $6)",
|
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",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 11:59:33 +01:00
|
|
|
name: "project MemberCascadeRemovedType",
|
2021-12-02 09:31:44 +01:00
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
project.MemberCascadeRemovedType,
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{
|
2021-12-02 09:31:44 +01:00
|
|
|
"userId": "user-id"
|
|
|
|
}`),
|
2023-10-19 12:19:10 +02:00
|
|
|
), project.MemberCascadeRemovedEventMapper),
|
2021-12-02 09:31:44 +01:00
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&projectMemberProjection{}).reduceCascadeRemoved,
|
2021-12-02 09:31:44 +01:00
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-02 09:31:44 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members3 WHERE (instance_id = $1) AND (user_id = $2) AND (project_id = $3)",
|
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",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 11:59:33 +01:00
|
|
|
name: "project MemberRemovedType",
|
2021-12-02 09:31:44 +01:00
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
project.MemberRemovedType,
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{
|
2021-12-02 09:31:44 +01:00
|
|
|
"userId": "user-id"
|
|
|
|
}`),
|
2023-10-19 12:19:10 +02:00
|
|
|
), project.MemberRemovedEventMapper),
|
2021-12-02 09:31:44 +01:00
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&projectMemberProjection{}).reduceRemoved,
|
2021-12-02 09:31:44 +01:00
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-02 09:31:44 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members3 WHERE (instance_id = $1) AND (user_id = $2) AND (project_id = $3)",
|
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",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-16 14:25:38 +01:00
|
|
|
{
|
2022-11-10 11:59:33 +01:00
|
|
|
name: "user UserRemovedEventType",
|
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: (&projectMemberProjection{}).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{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members3 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-16 14:25:38 +01:00
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
name: "project ProjectRemovedEventType",
|
2021-12-16 14:25:38 +01:00
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
project.ProjectRemovedType,
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{}`),
|
|
|
|
), project.ProjectRemovedEventMapper),
|
2021-12-16 14:25:38 +01:00
|
|
|
},
|
2022-11-30 17:01:17 +01:00
|
|
|
reduce: (&projectMemberProjection{}).reduceProjectRemoved,
|
2021-12-16 14:25:38 +01:00
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-16 14:25:38 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members3 WHERE (instance_id = $1) AND (project_id = $2)",
|
2021-12-16 14:25:38 +01:00
|
|
|
expectedArgs: []interface{}{
|
2022-11-10 11:59:33 +01:00
|
|
|
"instance-id",
|
2021-12-16 14:25:38 +01:00
|
|
|
"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{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members3 WHERE (instance_id = $1)",
|
2022-10-20 13:36:52 +01:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-16 14:25:38 +01:00
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
name: "org OrgRemovedEventType",
|
2021-12-16 14:25:38 +01:00
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
org.OrgRemovedEventType,
|
|
|
|
org.AggregateType,
|
|
|
|
[]byte(`{}`),
|
|
|
|
), org.OrgRemovedEventMapper),
|
2021-12-16 14:25:38 +01:00
|
|
|
},
|
2022-11-30 17:01:17 +01:00
|
|
|
reduce: (&projectMemberProjection{}).reduceOrgRemoved,
|
2021-12-16 14:25:38 +01:00
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: org.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-16 14:25:38 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-09-07 06:54:51 +02:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members3 WHERE (instance_id = $1) AND (resource_owner = $2)",
|
2021-12-16 14:25:38 +01:00
|
|
|
expectedArgs: []interface{}{
|
2022-11-30 17:01:17 +01:00
|
|
|
"instance-id",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-09-07 06:54:51 +02:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members3 WHERE (instance_id = $1) AND (user_resource_owner = $2)",
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedArgs: []interface{}{
|
2022-11-10 11:59:33 +01:00
|
|
|
"instance-id",
|
2021-12-16 14:25:38 +01:00
|
|
|
"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)
|
|
|
|
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)
|
2022-10-20 13:36:52 +01:00
|
|
|
assertReduce(t, got, err, ProjectMemberProjectionTable, tt.want)
|
2021-12-02 09:31:44 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|