2021-12-02 09:31:44 +01:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-08-31 09:52:43 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2022-04-27 01:01:45 +02: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"
|
|
|
|
"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
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "project.MemberAddedType",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.MemberAddedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id",
|
|
|
|
"roles": ["role"]
|
|
|
|
}`),
|
|
|
|
), project.MemberAddedEventMapper),
|
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&projectMemberProjection{}).reduceAdded,
|
2021-12-02 09:31:44 +01:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
projection: ProjectMemberProjectionTable,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 09:52:43 +02: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 09:31:44 +01:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"user-id",
|
2022-08-31 09:52:43 +02:00
|
|
|
database.StringArray{"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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "project.MemberChangedType",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.MemberChangedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id",
|
|
|
|
"roles": ["role", "changed"]
|
|
|
|
}`),
|
|
|
|
), project.MemberChangedEventMapper),
|
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&projectMemberProjection{}).reduceChanged,
|
2021-12-02 09:31:44 +01:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
projection: ProjectMemberProjectionTable,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 09:52:43 +02:00
|
|
|
expectedStmt: "UPDATE projections.project_members2 SET (roles, change_date, sequence) = ($1, $2, $3) WHERE (user_id = $4) AND (project_id = $5)",
|
2021-12-02 09:31:44 +01:00
|
|
|
expectedArgs: []interface{}{
|
2022-08-31 09:52:43 +02:00
|
|
|
database.StringArray{"role", "changed"},
|
2021-12-02 09:31:44 +01:00
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
"user-id",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "project.MemberCascadeRemovedType",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.MemberCascadeRemovedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id"
|
|
|
|
}`),
|
|
|
|
), project.MemberCascadeRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&projectMemberProjection{}).reduceCascadeRemoved,
|
2021-12-02 09:31:44 +01:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
projection: ProjectMemberProjectionTable,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 09:52:43 +02:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members2 WHERE (user_id = $1) AND (project_id = $2)",
|
2021-12-02 09:31:44 +01:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"user-id",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "project.MemberRemovedType",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.MemberRemovedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id"
|
|
|
|
}`),
|
|
|
|
), project.MemberRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&projectMemberProjection{}).reduceRemoved,
|
2021-12-02 09:31:44 +01:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
projection: ProjectMemberProjectionTable,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 09:52:43 +02:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members2 WHERE (user_id = $1) AND (project_id = $2)",
|
2021-12-02 09:31:44 +01:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"user-id",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-16 14:25:38 +01:00
|
|
|
{
|
|
|
|
name: "user.UserRemovedEventType",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(user.UserRemovedType),
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{}`),
|
|
|
|
), user.UserRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&projectMemberProjection{}).reduceUserRemoved,
|
2021-12-16 14:25:38 +01:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: user.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
projection: ProjectMemberProjectionTable,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 09:52:43 +02:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members2 WHERE (user_id = $1)",
|
2021-12-16 14:25:38 +01:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "org.OrgRemovedEventType",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(org.OrgRemovedEventType),
|
|
|
|
org.AggregateType,
|
|
|
|
[]byte(`{}`),
|
|
|
|
), org.OrgRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&projectMemberProjection{}).reduceOrgRemoved,
|
2021-12-16 14:25:38 +01:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: org.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
projection: ProjectMemberProjectionTable,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 09:52:43 +02:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members2 WHERE (resource_owner = $1)",
|
2021-12-16 14:25:38 +01:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "project.ProjectRemovedEventType",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.ProjectRemovedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{}`),
|
|
|
|
), project.ProjectRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&projectMemberProjection{}).reduceProjectRemoved,
|
2021-12-16 14:25:38 +01:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
projection: ProjectMemberProjectionTable,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 09:52:43 +02:00
|
|
|
expectedStmt: "DELETE FROM projections.project_members2 WHERE (project_id = $1)",
|
2021-12-16 14:25:38 +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)
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|