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"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
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-03-24 16:21:34 +00:00
|
|
|
name: "instance.MemberAddedType",
|
2021-12-02 08:31:44 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.MemberAddedEventType),
|
|
|
|
instance.AggregateType,
|
2021-12-02 08:31:44 +00:00
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id",
|
|
|
|
"roles": ["role"]
|
|
|
|
}`),
|
2022-03-24 16:21:34 +00:00
|
|
|
), instance.MemberAddedEventMapper),
|
2021-12-02 08:31:44 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&instanceMemberProjection{}).reduceAdded,
|
2021-12-02 08:31:44 +00:00
|
|
|
want: wantReduce{
|
2022-03-24 16:21:34 +00:00
|
|
|
aggregateType: instance.AggregateType,
|
2021-12-02 08:31:44 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
2022-03-24 16:21:34 +00:00
|
|
|
projection: InstanceMemberProjectionTable,
|
2021-12-02 08:31:44 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 07:52:43 +00:00
|
|
|
expectedStmt: "INSERT INTO projections.instance_members2 (user_id, roles, creation_date, change_date, sequence, resource_owner, instance_id, 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-03-24 16:21:34 +00:00
|
|
|
name: "instance.MemberChangedType",
|
2021-12-02 08:31:44 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.MemberChangedEventType),
|
|
|
|
instance.AggregateType,
|
2021-12-02 08:31:44 +00:00
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id",
|
|
|
|
"roles": ["role", "changed"]
|
|
|
|
}`),
|
2022-03-24 16:21:34 +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{
|
2022-03-24 16:21:34 +00:00
|
|
|
aggregateType: instance.AggregateType,
|
2021-12-02 08:31:44 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
2022-03-24 16:21:34 +00:00
|
|
|
projection: InstanceMemberProjectionTable,
|
2021-12-02 08:31:44 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 07:52:43 +00:00
|
|
|
expectedStmt: "UPDATE projections.instance_members2 SET (roles, change_date, sequence) = ($1, $2, $3) WHERE (user_id = $4)",
|
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),
|
|
|
|
"user-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
name: "instance.MemberCascadeRemovedType",
|
2021-12-02 08:31:44 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.MemberCascadeRemovedEventType),
|
|
|
|
instance.AggregateType,
|
2021-12-02 08:31:44 +00:00
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id"
|
|
|
|
}`),
|
2022-03-24 16:21:34 +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{
|
2022-03-24 16:21:34 +00:00
|
|
|
aggregateType: instance.AggregateType,
|
2021-12-02 08:31:44 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
2022-03-24 16:21:34 +00:00
|
|
|
projection: InstanceMemberProjectionTable,
|
2021-12-02 08:31:44 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 07:52:43 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.instance_members2 WHERE (user_id = $1)",
|
2021-12-02 08:31:44 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"user-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
name: "instance.MemberRemovedType",
|
2021-12-02 08:31:44 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.MemberRemovedEventType),
|
|
|
|
instance.AggregateType,
|
2021-12-02 08:31:44 +00:00
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id"
|
|
|
|
}`),
|
2022-03-24 16:21:34 +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{
|
2022-03-24 16:21:34 +00:00
|
|
|
aggregateType: instance.AggregateType,
|
2021-12-02 08:31:44 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
2022-03-24 16:21:34 +00:00
|
|
|
projection: InstanceMemberProjectionTable,
|
2021-12-02 08:31:44 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 07:52:43 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.instance_members2 WHERE (user_id = $1)",
|
2021-12-02 08:31:44 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"user-id",
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user.UserRemoved",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(user.UserRemovedType),
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{}`),
|
|
|
|
), user.UserRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&instanceMemberProjection{}).reduceUserRemoved,
|
2021-12-16 13:25:38 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: user.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
2022-03-24 16:21:34 +00:00
|
|
|
projection: InstanceMemberProjectionTable,
|
2021-12-16 13:25:38 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 07:52:43 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.instance_members2 WHERE (user_id = $1)",
|
2021-12-16 13:25:38 +00:00
|
|
|
expectedArgs: []interface{}{
|
2021-12-02 08:31:44 +00:00
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|