2021-11-02 09:08:47 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-11-02 09:08:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIDPUserLinkProjection_reduces(t *testing.T) {
|
|
|
|
type args struct {
|
2022-01-03 08:19:07 +00:00
|
|
|
event func(t *testing.T) eventstore.Event
|
2021-11-02 09:08:47 +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-11-02 09:08:47 +00:00
|
|
|
want wantReduce
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "reduceAdded",
|
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
user.UserIDPLinkAddedType,
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{
|
2021-11-02 09:08:47 +00:00
|
|
|
"idpConfigId": "idp-config-id",
|
|
|
|
"userId": "external-user-id",
|
|
|
|
"displayName": "gigi@caos.ch"
|
|
|
|
}`),
|
2023-10-19 10:19:10 +00:00
|
|
|
), user.UserIDPLinkAddedEventMapper),
|
2021-11-02 09:08:47 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&idpUserLinkProjection{}).reduceAdded,
|
2021-11-02 09:08:47 +00:00
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: user.AggregateType,
|
|
|
|
sequence: 15,
|
2021-11-02 09:08:47 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "INSERT INTO projections.idp_user_links3 (idp_id, user_id, external_user_id, creation_date, change_date, sequence, resource_owner, instance_id, display_name) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
|
2021-11-02 09:08:47 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"idp-config-id",
|
|
|
|
"agg-id",
|
|
|
|
"external-user-id",
|
|
|
|
anyArg{},
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
"ro-id",
|
2022-03-23 08:02:39 +00:00
|
|
|
"instance-id",
|
2021-11-02 09:08:47 +00:00
|
|
|
"gigi@caos.ch",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceRemoved",
|
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
user.UserIDPLinkRemovedType,
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{
|
2021-11-02 09:08:47 +00:00
|
|
|
"idpConfigId": "idp-config-id",
|
|
|
|
"userId": "external-user-id"
|
|
|
|
}`),
|
2023-10-19 10:19:10 +00:00
|
|
|
), user.UserIDPLinkRemovedEventMapper),
|
2021-11-02 09:08:47 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&idpUserLinkProjection{}).reduceRemoved,
|
2021-11-02 09:08:47 +00:00
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: user.AggregateType,
|
|
|
|
sequence: 15,
|
2021-11-02 09:08:47 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.idp_user_links3 WHERE (idp_id = $1) AND (user_id = $2) AND (external_user_id = $3) AND (instance_id = $4)",
|
2021-11-02 09:08:47 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"idp-config-id",
|
|
|
|
"agg-id",
|
|
|
|
"external-user-id",
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2021-11-02 09:08:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceCascadeRemoved",
|
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
user.UserIDPLinkCascadeRemovedType,
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{
|
2021-11-02 09:08:47 +00:00
|
|
|
"idpConfigId": "idp-config-id",
|
|
|
|
"userId": "external-user-id"
|
|
|
|
}`),
|
2023-10-19 10:19:10 +00:00
|
|
|
), user.UserIDPLinkCascadeRemovedEventMapper),
|
2021-11-02 09:08:47 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&idpUserLinkProjection{}).reduceCascadeRemoved,
|
2021-11-02 09:08:47 +00:00
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: user.AggregateType,
|
|
|
|
sequence: 15,
|
2021-11-02 09:08:47 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.idp_user_links3 WHERE (idp_id = $1) AND (user_id = $2) AND (external_user_id = $3) AND (instance_id = $4)",
|
2021-11-02 09:08:47 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"idp-config-id",
|
|
|
|
"agg-id",
|
|
|
|
"external-user-id",
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2021-11-02 09:08:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-16 13:25:38 +00:00
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
name: "reduceOwnerRemoved",
|
2021-12-16 13:25:38 +00:00
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
org.OrgRemovedEventType,
|
|
|
|
org.AggregateType,
|
|
|
|
[]byte(`{}`),
|
|
|
|
), org.OrgRemovedEventMapper),
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
2022-11-30 16:01:17 +00:00
|
|
|
reduce: (&idpUserLinkProjection{}).reduceOwnerRemoved,
|
2021-12-16 13:25:38 +00:00
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: org.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-16 13:25:38 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2023-09-07 04:54:51 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.idp_user_links3 WHERE (resource_owner = $1) AND (instance_id = $2)",
|
2021-12-16 13:25:38 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2021-12-16 13:25:38 +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{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
instance.InstanceRemovedEventType,
|
|
|
|
instance.AggregateType,
|
|
|
|
nil,
|
|
|
|
), instance.InstanceRemovedEventMapper),
|
2022-10-20 12:36:52 +00:00
|
|
|
},
|
|
|
|
reduce: reduceInstanceRemovedHelper(IDPUserLinkInstanceIDCol),
|
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
|
|
|
sequence: 15,
|
2022-10-20 12:36:52 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.idp_user_links3 WHERE (instance_id = $1)",
|
2022-10-20 12:36:52 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-16 13:25:38 +00:00
|
|
|
{
|
|
|
|
name: "reduceUserRemoved",
|
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
user.UserRemovedType,
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{}`),
|
|
|
|
), user.UserRemovedEventMapper),
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&idpUserLinkProjection{}).reduceUserRemoved,
|
2021-12-16 13:25:38 +00:00
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: user.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-16 13:25:38 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.idp_user_links3 WHERE (user_id = $1) AND (instance_id = $2)",
|
2021-12-16 13:25:38 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2023-08-04 09:35:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceExternalIDMigrated",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.UserIDPExternalIDMigratedType,
|
2023-08-04 09:35:36 +00:00
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"idpConfigId": "idp-config-id",
|
|
|
|
"previousId": "previous-id",
|
|
|
|
"newId": "new-id"
|
|
|
|
}`),
|
|
|
|
), eventstore.GenericEventMapper[user.UserIDPExternalIDMigratedEvent]),
|
|
|
|
},
|
|
|
|
reduce: (&idpUserLinkProjection{}).reduceExternalIDMigrated,
|
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: user.AggregateType,
|
|
|
|
sequence: 15,
|
2023-08-04 09:35:36 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
|
|
|
expectedStmt: "UPDATE projections.idp_user_links3 SET (change_date, sequence, external_user_id) = ($1, $2, $3) WHERE (idp_id = $4) AND (user_id = $5) AND (external_user_id = $6) AND (instance_id = $7)",
|
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
"new-id",
|
|
|
|
"idp-config-id",
|
|
|
|
"agg-id",
|
|
|
|
"previous-id",
|
|
|
|
"instance-id",
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
name: "org IDPConfigRemovedEvent",
|
2021-12-16 13:25:38 +00:00
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
org.IDPConfigRemovedEventType,
|
|
|
|
org.AggregateType,
|
|
|
|
[]byte(`{
|
2021-12-16 13:25:38 +00:00
|
|
|
"idpConfigId": "idp-config-id"
|
|
|
|
}`),
|
2023-10-19 10:19:10 +00:00
|
|
|
), org.IDPConfigRemovedEventMapper),
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&idpUserLinkProjection{}).reduceIDPConfigRemoved,
|
2021-12-16 13:25:38 +00:00
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: org.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-16 13:25:38 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.idp_user_links3 WHERE (idp_id = $1) AND (resource_owner = $2) AND (instance_id = $3)",
|
2021-12-16 13:25:38 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"idp-config-id",
|
|
|
|
"ro-id",
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
name: "iam IDPConfigRemovedEvent",
|
2021-12-16 13:25:38 +00:00
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
instance.IDPConfigRemovedEventType,
|
|
|
|
instance.AggregateType,
|
|
|
|
[]byte(`{
|
2021-12-16 13:25:38 +00:00
|
|
|
"idpConfigId": "idp-config-id"
|
|
|
|
}`),
|
2023-10-19 10:19:10 +00:00
|
|
|
), instance.IDPConfigRemovedEventMapper),
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&idpUserLinkProjection{}).reduceIDPConfigRemoved,
|
2021-12-16 13:25:38 +00:00
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: instance.AggregateType,
|
|
|
|
sequence: 15,
|
2021-12-16 13:25:38 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 16:01:17 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.idp_user_links3 WHERE (idp_id = $1) AND (resource_owner = $2) AND (instance_id = $3)",
|
2021-12-16 13:25:38 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"idp-config-id",
|
|
|
|
"ro-id",
|
2022-11-10 10:59:33 +00:00
|
|
|
"instance-id",
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-11-02 09:08:47 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
event := baseEvent(t)
|
|
|
|
got, err := tt.reduce(event)
|
2023-12-08 14:30:55 +00:00
|
|
|
if ok := zerrors.IsErrorInvalidArgument(err); !ok {
|
2021-11-02 09:08:47 +00:00
|
|
|
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, IDPUserLinkTable, tt.want)
|
2021-11-02 09:08:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|