2021-12-02 08:31:44 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-08-31 07:52:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/repository"
|
2022-10-20 12:36:52 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-04-26 23:01:45 +00: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 08:31:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestProjectMemberProjection_reduces(t *testing.T) {
|
|
|
|
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: "project MemberAddedType",
|
2021-12-02 08:31:44 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.MemberAddedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id",
|
|
|
|
"roles": ["role"]
|
|
|
|
}`),
|
|
|
|
), project.MemberAddedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&projectMemberProjection{}).reduceAdded,
|
2021-12-02 08:31:44 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 07:52:43 +00:00
|
|
|
expectedStmt: "INSERT INTO projections.project_members2 (user_id, roles, creation_date, change_date, sequence, resource_owner, instance_id, project_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
|
2021-12-02 08:31:44 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"user-id",
|
2022-08-31 07:52:43 +00:00
|
|
|
database.StringArray{"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: "project MemberChangedType",
|
2021-12-02 08:31:44 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.MemberChangedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id",
|
|
|
|
"roles": ["role", "changed"]
|
|
|
|
}`),
|
|
|
|
), project.MemberChangedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&projectMemberProjection{}).reduceChanged,
|
2021-12-02 08:31:44 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
expectedStmt: "UPDATE projections.project_members2 SET (roles, change_date, sequence) = ($1, $2, $3) WHERE (instance_id = $4) AND (user_id = $5) AND (project_id = $6)",
|
2021-12-02 08:31:44 +00:00
|
|
|
expectedArgs: []interface{}{
|
2022-08-31 07:52:43 +00:00
|
|
|
database.StringArray{"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",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
name: "project MemberCascadeRemovedType",
|
2021-12-02 08:31:44 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.MemberCascadeRemovedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id"
|
|
|
|
}`),
|
|
|
|
), project.MemberCascadeRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&projectMemberProjection{}).reduceCascadeRemoved,
|
2021-12-02 08:31:44 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members2 WHERE (instance_id = $1) AND (user_id = $2) AND (project_id = $3)",
|
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",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
name: "project MemberRemovedType",
|
2021-12-02 08:31:44 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.MemberRemovedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id"
|
|
|
|
}`),
|
|
|
|
), project.MemberRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&projectMemberProjection{}).reduceRemoved,
|
2021-12-02 08:31:44 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members2 WHERE (instance_id = $1) AND (user_id = $2) AND (project_id = $3)",
|
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",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-16 13:25:38 +00:00
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
name: "user UserRemovedEventType",
|
2021-12-16 13:25:38 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(user.UserRemovedType),
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{}`),
|
|
|
|
), user.UserRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&projectMemberProjection{}).reduceUserRemoved,
|
2021-12-16 13:25:38 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: user.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members2 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-16 13:25:38 +00:00
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
name: "org OrgRemovedEventType",
|
2021-12-16 13:25:38 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(org.OrgRemovedEventType),
|
|
|
|
org.AggregateType,
|
|
|
|
[]byte(`{}`),
|
|
|
|
), org.OrgRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&projectMemberProjection{}).reduceOrgRemoved,
|
2021-12-16 13:25:38 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: org.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members2 WHERE (instance_id = $1) AND (resource_owner = $2)",
|
2021-12-16 13:25:38 +00:00
|
|
|
expectedArgs: []interface{}{
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2021-12-16 13:25:38 +00:00
|
|
|
"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{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(instance.InstanceRemovedEventType),
|
|
|
|
instance.AggregateType,
|
2022-10-26 13:06:48 +00:00
|
|
|
nil,
|
2022-10-20 12:36:52 +00:00
|
|
|
), instance.InstanceRemovedEventMapper),
|
|
|
|
},
|
|
|
|
reduce: reduceInstanceRemovedHelper(MemberInstanceID),
|
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
|
|
|
expectedStmt: "DELETE FROM projections.project_members2 WHERE (instance_id = $1)",
|
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-16 13:25:38 +00:00
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
name: "project ProjectRemovedEventType",
|
2021-12-16 13:25:38 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.ProjectRemovedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{}`),
|
|
|
|
), project.ProjectRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&projectMemberProjection{}).reduceProjectRemoved,
|
2021-12-16 13:25:38 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members2 WHERE (instance_id = $1) AND (project_id = $2)",
|
2021-12-16 13:25:38 +00:00
|
|
|
expectedArgs: []interface{}{
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2021-12-16 13:25:38 +00:00
|
|
|
"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)
|
|
|
|
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 12:36:52 +00:00
|
|
|
assertReduce(t, got, err, ProjectMemberProjectionTable, tt.want)
|
2021-12-02 08:31:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|