2022-01-13 10:02:39 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
2022-11-30 16:01:17 +00:00
|
|
|
"context"
|
2022-01-13 10:02:39 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-11-30 16:01:17 +00:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
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/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler"
|
2022-11-30 16:01:17 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/crdb"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/repository"
|
2022-10-20 12:36:52 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-11-30 16:01:17 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/usergrant"
|
2022-01-13 10:02:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserGrantProjection_reduces(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
event func(t *testing.T) eventstore.Event
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
reduce func(event eventstore.Event) (*handler.Statement, error)
|
|
|
|
want wantReduce
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "reduceAdded",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(usergrant.UserGrantAddedType),
|
|
|
|
usergrant.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id",
|
|
|
|
"projectId": "project-id",
|
|
|
|
"roleKeys": ["role"]
|
|
|
|
}`),
|
|
|
|
), usergrant.UserGrantAddedEventMapper),
|
|
|
|
},
|
2022-11-30 16:01:17 +00:00
|
|
|
reduce: (&userGrantProjection{
|
|
|
|
StatementHandler: getStatementHandlerWithFilters(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user-id", "org1").Aggregate,
|
|
|
|
"username1",
|
|
|
|
"firstname1",
|
|
|
|
"lastname1",
|
|
|
|
"nickname1",
|
|
|
|
"displayname1",
|
|
|
|
language.German,
|
|
|
|
domain.GenderMale,
|
|
|
|
"email1",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
project.NewProjectAddedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project-id", "org2").Aggregate,
|
|
|
|
"project",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
domain.PrivateLabelingSettingUnspecified,
|
|
|
|
),
|
|
|
|
)(t)}).reduceAdded,
|
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: usergrant.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
|
|
|
expectedStmt: "INSERT INTO projections.user_grants3 (id, resource_owner, instance_id, creation_date, change_date, sequence, user_id, resource_owner_user, project_id, resource_owner_project, grant_id, granted_org, roles, state) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)",
|
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
"ro-id",
|
|
|
|
"instance-id",
|
|
|
|
anyArg{},
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
"user-id",
|
|
|
|
"org1",
|
|
|
|
"project-id",
|
|
|
|
"org2",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
database.StringArray{"role"},
|
|
|
|
domain.UserGrantStateActive,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceAdded with projectgrant",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(usergrant.UserGrantAddedType),
|
|
|
|
usergrant.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"userId": "user-id",
|
|
|
|
"projectId": "project-id",
|
|
|
|
"grantId": "grant-id",
|
|
|
|
"roleKeys": ["role"]
|
|
|
|
}`),
|
|
|
|
), usergrant.UserGrantAddedEventMapper),
|
|
|
|
},
|
|
|
|
reduce: (&userGrantProjection{
|
|
|
|
StatementHandler: getStatementHandlerWithFilters(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user-id", "org1").Aggregate,
|
|
|
|
"username1",
|
|
|
|
"firstname1",
|
|
|
|
"lastname1",
|
|
|
|
"nickname1",
|
|
|
|
"displayname1",
|
|
|
|
language.German,
|
|
|
|
domain.GenderMale,
|
|
|
|
"email1",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
project.NewGrantAddedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project-id", "org2").Aggregate,
|
|
|
|
"grant-id",
|
|
|
|
"org3",
|
|
|
|
[]string{},
|
|
|
|
),
|
|
|
|
)(t)}).reduceAdded,
|
2022-01-13 10:02:39 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: usergrant.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "INSERT INTO projections.user_grants3 (id, resource_owner, instance_id, creation_date, change_date, sequence, user_id, resource_owner_user, project_id, resource_owner_project, grant_id, granted_org, roles, state) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)",
|
2022-01-13 10:02:39 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
"ro-id",
|
2022-03-23 08:02:39 +00:00
|
|
|
"instance-id",
|
2022-01-13 10:02:39 +00:00
|
|
|
anyArg{},
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
"user-id",
|
2022-11-30 16:01:17 +00:00
|
|
|
"org1",
|
2022-01-13 10:02:39 +00:00
|
|
|
"project-id",
|
|
|
|
"",
|
2022-11-30 16:01:17 +00:00
|
|
|
"grant-id",
|
|
|
|
"org3",
|
2022-08-31 07:52:43 +00:00
|
|
|
database.StringArray{"role"},
|
2022-01-13 10:02:39 +00:00
|
|
|
domain.UserGrantStateActive,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceChanged",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(usergrant.UserGrantChangedType),
|
|
|
|
usergrant.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"roleKeys": ["role"]
|
|
|
|
}`),
|
|
|
|
), usergrant.UserGrantChangedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&userGrantProjection{}).reduceChanged,
|
2022-01-13 10:02:39 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: usergrant.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "UPDATE projections.user_grants3 SET (change_date, roles, sequence) = ($1, $2, $3) WHERE (id = $4) AND (instance_id = $5)",
|
2022-01-13 10:02:39 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
2022-08-31 07:52:43 +00:00
|
|
|
database.StringArray{"role"},
|
2022-01-13 10:02:39 +00:00
|
|
|
uint64(15),
|
|
|
|
"agg-id",
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceCascadeChanged",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(usergrant.UserGrantCascadeChangedType),
|
|
|
|
usergrant.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"roleKeys": ["role"]
|
|
|
|
}`),
|
|
|
|
), usergrant.UserGrantCascadeChangedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&userGrantProjection{}).reduceChanged,
|
2022-01-13 10:02:39 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: usergrant.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "UPDATE projections.user_grants3 SET (change_date, roles, sequence) = ($1, $2, $3) WHERE (id = $4) AND (instance_id = $5)",
|
2022-01-13 10:02:39 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
2022-08-31 07:52:43 +00:00
|
|
|
database.StringArray{"role"},
|
2022-01-13 10:02:39 +00:00
|
|
|
uint64(15),
|
|
|
|
"agg-id",
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceRemoved",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(usergrant.UserGrantRemovedType),
|
|
|
|
usergrant.AggregateType,
|
|
|
|
nil,
|
|
|
|
), usergrant.UserGrantRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&userGrantProjection{}).reduceRemoved,
|
2022-01-13 10:02:39 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: usergrant.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.user_grants3 WHERE (id = $1) AND (instance_id = $2)",
|
2022-01-13 10:02:39 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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(UserGrantInstanceID),
|
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.user_grants3 WHERE (instance_id = $1)",
|
2022-10-20 12:36:52 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-13 10:02:39 +00:00
|
|
|
{
|
|
|
|
name: "reduceCascadeRemoved",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(usergrant.UserGrantCascadeRemovedType),
|
|
|
|
usergrant.AggregateType,
|
|
|
|
nil,
|
|
|
|
), usergrant.UserGrantCascadeRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&userGrantProjection{}).reduceRemoved,
|
2022-01-13 10:02:39 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: usergrant.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.user_grants3 WHERE (id = $1) AND (instance_id = $2)",
|
2022-01-13 10:02:39 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceDeactivated",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(usergrant.UserGrantDeactivatedType),
|
|
|
|
usergrant.AggregateType,
|
|
|
|
nil,
|
|
|
|
), usergrant.UserGrantDeactivatedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&userGrantProjection{}).reduceDeactivated,
|
2022-01-13 10:02:39 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: usergrant.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "UPDATE projections.user_grants3 SET (change_date, state, sequence) = ($1, $2, $3) WHERE (id = $4) AND (instance_id = $5)",
|
2022-01-13 10:02:39 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
|
|
|
domain.UserGrantStateInactive,
|
|
|
|
uint64(15),
|
|
|
|
"agg-id",
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceReactivated",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(usergrant.UserGrantReactivatedType),
|
|
|
|
usergrant.AggregateType,
|
|
|
|
nil,
|
|
|
|
), usergrant.UserGrantDeactivatedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&userGrantProjection{}).reduceReactivated,
|
2022-01-13 10:02:39 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: usergrant.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "UPDATE projections.user_grants3 SET (change_date, state, sequence) = ($1, $2, $3) WHERE (id = $4) AND (instance_id = $5)",
|
2022-01-13 10:02:39 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
|
|
|
domain.UserGrantStateActive,
|
|
|
|
uint64(15),
|
|
|
|
"agg-id",
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceUserRemoved",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(user.UserRemovedType),
|
|
|
|
user.AggregateType,
|
|
|
|
nil,
|
|
|
|
), user.UserRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&userGrantProjection{}).reduceUserRemoved,
|
2022-01-13 10:02:39 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: user.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.user_grants3 WHERE (user_id = $1) AND (instance_id = $2)",
|
2022-01-13 10:02:39 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceProjectRemoved",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.ProjectRemovedType),
|
|
|
|
project.AggregateType,
|
|
|
|
nil,
|
|
|
|
), project.ProjectRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&userGrantProjection{}).reduceProjectRemoved,
|
2022-01-13 10:02:39 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.user_grants3 WHERE (project_id = $1) AND (instance_id = $2)",
|
2022-01-13 10:02:39 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceProjectGrantRemoved",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.GrantRemovedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{"grantId": "grantID"}`),
|
|
|
|
), project.GrantRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&userGrantProjection{}).reduceProjectGrantRemoved,
|
2022-01-13 10:02:39 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.user_grants3 WHERE (grant_id = $1) AND (instance_id = $2)",
|
2022-01-13 10:02:39 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"grantID",
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-20 07:33:51 +00:00
|
|
|
{
|
|
|
|
name: "reduceRoleRemoved",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.RoleRemovedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{"key": "key"}`),
|
|
|
|
), project.RoleRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&userGrantProjection{}).reduceRoleRemoved,
|
2022-01-20 07:33:51 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "UPDATE projections.user_grants3 SET roles = array_remove(roles, $1) WHERE (project_id = $2) AND (instance_id = $3)",
|
2022-01-20 07:33:51 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"key",
|
|
|
|
"agg-id",
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2022-01-20 07:33:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceProjectGrantChanged",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.GrantChangedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{"grantId": "grantID", "roleKeys": ["key"]}`),
|
|
|
|
), project.GrantChangedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&userGrantProjection{}).reduceProjectGrantChanged,
|
2022-01-20 07:33:51 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: project.AggregateType,
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "UPDATE projections.user_grants3 SET (roles) = (SELECT ARRAY( SELECT UNNEST(roles) INTERSECT SELECT UNNEST ($1::TEXT[]))) WHERE (grant_id = $2) AND (instance_id = $3)",
|
2022-01-20 07:33:51 +00:00
|
|
|
expectedArgs: []interface{}{
|
2022-08-31 07:52:43 +00:00
|
|
|
database.StringArray{"key"},
|
2022-01-20 07:33:51 +00:00
|
|
|
"grantID",
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2022-01-20 07:33:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-30 16:01:17 +00:00
|
|
|
{
|
|
|
|
name: "org.reduceOwnerRemoved",
|
|
|
|
reduce: (&userGrantProjection{}).reduceOwnerRemoved,
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(org.OrgRemovedEventType),
|
|
|
|
org.AggregateType,
|
|
|
|
nil,
|
|
|
|
), org.OrgRemovedEventMapper),
|
|
|
|
},
|
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: eventstore.AggregateType("org"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
|
|
|
expectedStmt: "UPDATE projections.user_grants3 SET (change_date, sequence, owner_removed) = ($1, $2, $3) WHERE (instance_id = $4) AND (resource_owner = $5)",
|
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
true,
|
|
|
|
"instance-id",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expectedStmt: "UPDATE projections.user_grants3 SET (change_date, sequence, user_owner_removed) = ($1, $2, $3) WHERE (instance_id = $4) AND (resource_owner_user = $5)",
|
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
true,
|
|
|
|
"instance-id",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expectedStmt: "UPDATE projections.user_grants3 SET (change_date, sequence, project_owner_removed) = ($1, $2, $3) WHERE (instance_id = $4) AND (resource_owner_project = $5)",
|
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
true,
|
|
|
|
"instance-id",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expectedStmt: "UPDATE projections.user_grants3 SET (change_date, sequence, granted_org_removed) = ($1, $2, $3) WHERE (instance_id = $4) AND (granted_org = $5)",
|
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
true,
|
|
|
|
"instance-id",
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-13 10:02:39 +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, UserGrantProjectionTable, tt.want)
|
2022-01-13 10:02:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
|
|
|
|
func getStatementHandlerWithFilters(events ...eventstore.Command) func(t *testing.T) crdb.StatementHandler {
|
|
|
|
filters := make([]expect, 0)
|
|
|
|
for _, event := range events {
|
|
|
|
filters = append(filters,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
event,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(t *testing.T) crdb.StatementHandler {
|
|
|
|
return crdb.StatementHandler{
|
|
|
|
ProjectionHandler: &handler.ProjectionHandler{
|
|
|
|
Handler: handler.Handler{
|
|
|
|
Eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
filters...,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|