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"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/repository"
|
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/user"
|
2021-12-02 09:31:44 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestOrgMemberProjection_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: "org MemberAddedType",
|
2021-12-02 09:31:44 +01:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(org.MemberAddedEventType),
|
|
|
|
org.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id",
|
|
|
|
"roles": ["role"]
|
|
|
|
}`),
|
|
|
|
), org.MemberAddedEventMapper),
|
|
|
|
},
|
2022-11-30 17:01:17 +01:00
|
|
|
reduce: (&orgMemberProjection{
|
|
|
|
StatementHandler: getStatementHandlerWithFilters(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user-id", "org1").Aggregate,
|
|
|
|
"username1",
|
|
|
|
"firstname1",
|
|
|
|
"lastname1",
|
|
|
|
"nickname1",
|
|
|
|
"displayname1",
|
|
|
|
language.German,
|
|
|
|
domain.GenderMale,
|
|
|
|
"email1",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
)(t)}).reduceAdded,
|
2021-12-02 09:31:44 +01:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: org.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "INSERT INTO projections.org_members3 (user_id, user_resource_owner, user_owner_removed, roles, creation_date, change_date, sequence, resource_owner, instance_id, owner_removed, org_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,
|
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",
|
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: "org MemberChangedType",
|
2021-12-02 09:31:44 +01:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(org.MemberChangedEventType),
|
|
|
|
org.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id",
|
|
|
|
"roles": ["role", "changed"]
|
|
|
|
}`),
|
|
|
|
), org.MemberChangedEventMapper),
|
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&orgMemberProjection{}).reduceChanged,
|
2021-12-02 09:31:44 +01:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: org.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "UPDATE projections.org_members3 SET (roles, change_date, sequence) = ($1, $2, $3) WHERE (instance_id = $4) AND (user_id = $5) AND (org_id = $6)",
|
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),
|
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: "org MemberCascadeRemovedType",
|
2021-12-02 09:31:44 +01:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(org.MemberCascadeRemovedEventType),
|
|
|
|
org.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id"
|
|
|
|
}`),
|
|
|
|
), org.MemberCascadeRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&orgMemberProjection{}).reduceCascadeRemoved,
|
2021-12-02 09:31:44 +01:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: org.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "DELETE FROM projections.org_members3 WHERE (instance_id = $1) AND (user_id = $2) AND (org_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: "org MemberRemovedType",
|
2021-12-02 09:31:44 +01:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(org.MemberRemovedEventType),
|
|
|
|
org.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id"
|
|
|
|
}`),
|
|
|
|
), org.MemberRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&orgMemberProjection{}).reduceRemoved,
|
2021-12-02 09:31:44 +01:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: org.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "DELETE FROM projections.org_members3 WHERE (instance_id = $1) AND (user_id = $2) AND (org_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{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(user.UserRemovedType),
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{}`),
|
|
|
|
), user.UserRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&orgMemberProjection{}).reduceUserRemoved,
|
2021-12-16 14:25:38 +01:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: user.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "DELETE FROM projections.org_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-10 11:59:33 +01:00
|
|
|
name: "org OrgRemovedEventType",
|
2021-12-16 14:25:38 +01:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(org.OrgRemovedEventType),
|
|
|
|
org.AggregateType,
|
|
|
|
[]byte(`{}`),
|
|
|
|
), org.OrgRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&orgMemberProjection{}).reduceOrgRemoved,
|
2021-12-16 14:25:38 +01:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: org.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "UPDATE projections.org_members3 SET (change_date, sequence, owner_removed) = ($1, $2, $3) WHERE (instance_id = $4) AND (resource_owner = $5)",
|
2021-12-16 14:25:38 +01:00
|
|
|
expectedArgs: []interface{}{
|
2022-11-30 17:01:17 +01:00
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
true,
|
|
|
|
"instance-id",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expectedStmt: "UPDATE projections.org_members3 SET (change_date, sequence, user_owner_removed) = ($1, $2, $3) WHERE (instance_id = $4) AND (user_resource_owner = $5)",
|
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
true,
|
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{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(instance.InstanceRemovedEventType),
|
|
|
|
instance.AggregateType,
|
2022-10-26 15:06:48 +02:00
|
|
|
nil,
|
2022-10-20 13:36:52 +01:00
|
|
|
), instance.InstanceRemovedEventMapper),
|
|
|
|
},
|
|
|
|
reduce: reduceInstanceRemovedHelper(MemberInstanceID),
|
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "DELETE FROM projections.org_members3 WHERE (instance_id = $1)",
|
2022-10-20 13:36:52 +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)
|
2022-10-20 13:36:52 +01:00
|
|
|
assertReduce(t, got, err, OrgMemberProjectionTable, tt.want)
|
2021-12-02 09:31:44 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|