From 542651707a5d6bd06e60a66eed7f5f40712d343a Mon Sep 17 00:00:00 2001 From: Livio Amstutz Date: Tue, 25 Jan 2022 11:35:38 +0100 Subject: [PATCH] fix: state on user projection (#3109) * fix: state on user projection * fix: state on user projection * don't change user state on HumanEmailVerifiedEvent --- internal/command/user_human_init_model.go | 3 - internal/query/projection/user.go | 56 +++++++++- internal/query/projection/user_test.go | 128 ++++++++++++++++++++-- 3 files changed, 173 insertions(+), 14 deletions(-) diff --git a/internal/command/user_human_init_model.go b/internal/command/user_human_init_model.go index f513bc326..e75704164 100644 --- a/internal/command/user_human_init_model.go +++ b/internal/command/user_human_init_model.go @@ -47,9 +47,6 @@ func (wm *HumanInitCodeWriteModel) Reduce() error { wm.IsEmailVerified = false case *user.HumanEmailVerifiedEvent: wm.IsEmailVerified = true - if wm.UserState == domain.UserStateInitial { - wm.UserState = domain.UserStateActive - } case *user.HumanInitialCodeAddedEvent: wm.Code = e.Code wm.CodeCreationDate = e.CreationDate() diff --git a/internal/query/projection/user.go b/internal/query/projection/user.go index b442d6b7d..f5873954f 100644 --- a/internal/query/projection/user.go +++ b/internal/query/projection/user.go @@ -94,6 +94,22 @@ func (p *UserProjection) reducers() []handler.AggregateReducer { Event: user.HumanRegisteredType, Reduce: p.reduceHumanRegistered, }, + { + Event: user.HumanInitialCodeAddedType, + Reduce: p.reduceHumanInitCodeAdded, + }, + { + Event: user.UserV1InitialCodeAddedType, + Reduce: p.reduceHumanInitCodeAdded, + }, + { + Event: user.HumanInitializedCheckSucceededType, + Reduce: p.reduceHumanInitCodeSucceeded, + }, + { + Event: user.UserV1InitializedCheckSucceededType, + Reduce: p.reduceHumanInitCodeSucceeded, + }, { Event: user.UserLockedType, Reduce: p.reduceUserLocked, @@ -201,7 +217,7 @@ func (p *UserProjection) reduceHumanAdded(event eventstore.Event) (*handler.Stat handler.NewCol(UserCreationDateCol, e.CreationDate()), handler.NewCol(UserChangeDateCol, e.CreationDate()), handler.NewCol(UserResourceOwnerCol, e.Aggregate().ResourceOwner), - handler.NewCol(UserStateCol, domain.UserStateInitial), + handler.NewCol(UserStateCol, domain.UserStateActive), handler.NewCol(UserSequenceCol, e.Sequence()), handler.NewCol(UserUsernameCol, e.UserName), handler.NewCol(UserTypeCol, domain.UserTypeHuman), @@ -238,7 +254,7 @@ func (p *UserProjection) reduceHumanRegistered(event eventstore.Event) (*handler handler.NewCol(UserCreationDateCol, e.CreationDate()), handler.NewCol(UserChangeDateCol, e.CreationDate()), handler.NewCol(UserResourceOwnerCol, e.Aggregate().ResourceOwner), - handler.NewCol(UserStateCol, domain.UserStateInitial), + handler.NewCol(UserStateCol, domain.UserStateActive), handler.NewCol(UserSequenceCol, e.Sequence()), handler.NewCol(UserUsernameCol, e.UserName), handler.NewCol(UserTypeCol, domain.UserTypeHuman), @@ -261,6 +277,40 @@ func (p *UserProjection) reduceHumanRegistered(event eventstore.Event) (*handler ), nil } +func (p *UserProjection) reduceHumanInitCodeAdded(event eventstore.Event) (*handler.Statement, error) { + e, ok := event.(*user.HumanInitialCodeAddedEvent) + if !ok { + logging.LogWithFields("HANDL-DSfe2", "seq", event.Sequence(), "expectedType", user.HumanInitialCodeAddedType).Error("wrong event type") + return nil, errors.ThrowInvalidArgument(nil, "HANDL-Dvgws", "reduce.wrong.event.type") + } + return crdb.NewUpdateStatement( + e, + []handler.Column{ + handler.NewCol(UserStateCol, domain.UserStateInitial), + }, + []handler.Condition{ + handler.NewCond(UserIDCol, e.Aggregate().ID), + }, + ), nil +} + +func (p *UserProjection) reduceHumanInitCodeSucceeded(event eventstore.Event) (*handler.Statement, error) { + e, ok := event.(*user.HumanInitializedCheckSucceededEvent) + if !ok { + logging.LogWithFields("HANDL-Dgff2", "seq", event.Sequence(), "expectedType", user.HumanInitializedCheckSucceededType).Error("wrong event type") + return nil, errors.ThrowInvalidArgument(nil, "HANDL-Dfvwq", "reduce.wrong.event.type") + } + return crdb.NewUpdateStatement( + e, + []handler.Column{ + handler.NewCol(UserStateCol, domain.UserStateActive), + }, + []handler.Condition{ + handler.NewCond(UserIDCol, e.Aggregate().ID), + }, + ), nil +} + func (p *UserProjection) reduceUserLocked(event eventstore.Event) (*handler.Statement, error) { e, ok := event.(*user.UserLockedEvent) if !ok { @@ -656,7 +706,7 @@ func (p *UserProjection) reduceMachineAdded(event eventstore.Event) (*handler.St handler.NewCol(UserCreationDateCol, e.CreationDate()), handler.NewCol(UserChangeDateCol, e.CreationDate()), handler.NewCol(UserResourceOwnerCol, e.Aggregate().ResourceOwner), - handler.NewCol(UserStateCol, domain.UserStateInitial), + handler.NewCol(UserStateCol, domain.UserStateActive), handler.NewCol(UserSequenceCol, e.Sequence()), handler.NewCol(UserUsernameCol, e.UserName), handler.NewCol(UserTypeCol, domain.UserTypeMachine), diff --git a/internal/query/projection/user_test.go b/internal/query/projection/user_test.go index ba52dbfbc..4c56d38c4 100644 --- a/internal/query/projection/user_test.go +++ b/internal/query/projection/user_test.go @@ -56,7 +56,7 @@ func TestUserProjection_reduces(t *testing.T) { anyArg{}, anyArg{}, "ro-id", - domain.UserStateInitial, + domain.UserStateActive, uint64(15), "user-name", domain.UserTypeHuman, @@ -114,7 +114,7 @@ func TestUserProjection_reduces(t *testing.T) { anyArg{}, anyArg{}, "ro-id", - domain.UserStateInitial, + domain.UserStateActive, uint64(15), "user-name", domain.UserTypeHuman, @@ -167,7 +167,7 @@ func TestUserProjection_reduces(t *testing.T) { anyArg{}, anyArg{}, "ro-id", - domain.UserStateInitial, + domain.UserStateActive, uint64(15), "user-name", domain.UserTypeHuman, @@ -225,7 +225,7 @@ func TestUserProjection_reduces(t *testing.T) { anyArg{}, anyArg{}, "ro-id", - domain.UserStateInitial, + domain.UserStateActive, uint64(15), "user-name", domain.UserTypeHuman, @@ -283,7 +283,7 @@ func TestUserProjection_reduces(t *testing.T) { anyArg{}, anyArg{}, "ro-id", - domain.UserStateInitial, + domain.UserStateActive, uint64(15), "user-name", domain.UserTypeHuman, @@ -336,7 +336,7 @@ func TestUserProjection_reduces(t *testing.T) { anyArg{}, anyArg{}, "ro-id", - domain.UserStateInitial, + domain.UserStateActive, uint64(15), "user-name", domain.UserTypeHuman, @@ -360,6 +360,118 @@ func TestUserProjection_reduces(t *testing.T) { }, }, }, + { + name: "reduceHumanInitCodeAdded", + args: args{ + event: getEvent(testEvent( + repository.EventType(user.HumanInitialCodeAddedType), + user.AggregateType, + []byte(`{}`), + ), user.HumanInitialCodeAddedEventMapper), + }, + reduce: (&UserProjection{}).reduceHumanInitCodeAdded, + want: wantReduce{ + aggregateType: user.AggregateType, + sequence: 15, + previousSequence: 10, + projection: UserTable, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "UPDATE zitadel.projections.users SET (state) = ($1) WHERE (id = $2)", + expectedArgs: []interface{}{ + domain.UserStateInitial, + "agg-id", + }, + }, + }, + }, + }, + }, + { + name: "reduceUserV1InitCodeAdded", + args: args{ + event: getEvent(testEvent( + repository.EventType(user.UserV1InitialCodeAddedType), + user.AggregateType, + []byte(`{}`), + ), user.HumanInitialCodeAddedEventMapper), + }, + reduce: (&UserProjection{}).reduceHumanInitCodeAdded, + want: wantReduce{ + aggregateType: user.AggregateType, + sequence: 15, + previousSequence: 10, + projection: UserTable, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "UPDATE zitadel.projections.users SET (state) = ($1) WHERE (id = $2)", + expectedArgs: []interface{}{ + domain.UserStateInitial, + "agg-id", + }, + }, + }, + }, + }, + }, + { + name: "reduceHumanInitCodeSucceeded", + args: args{ + event: getEvent(testEvent( + repository.EventType(user.HumanInitializedCheckSucceededType), + user.AggregateType, + []byte(`{}`), + ), user.HumanInitializedCheckSucceededEventMapper), + }, + reduce: (&UserProjection{}).reduceHumanInitCodeSucceeded, + want: wantReduce{ + aggregateType: user.AggregateType, + sequence: 15, + previousSequence: 10, + projection: UserTable, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "UPDATE zitadel.projections.users SET (state) = ($1) WHERE (id = $2)", + expectedArgs: []interface{}{ + domain.UserStateActive, + "agg-id", + }, + }, + }, + }, + }, + }, + { + name: "reduceUserV1InitCodeAdded", + args: args{ + event: getEvent(testEvent( + repository.EventType(user.UserV1InitializedCheckSucceededType), + user.AggregateType, + []byte(`{}`), + ), user.HumanInitializedCheckSucceededEventMapper), + }, + reduce: (&UserProjection{}).reduceHumanInitCodeSucceeded, + want: wantReduce{ + aggregateType: user.AggregateType, + sequence: 15, + previousSequence: 10, + projection: UserTable, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "UPDATE zitadel.projections.users SET (state) = ($1) WHERE (id = $2)", + expectedArgs: []interface{}{ + domain.UserStateActive, + "agg-id", + }, + }, + }, + }, + }, + }, { name: "reduceUserLocked", args: args{ @@ -1110,7 +1222,7 @@ func TestUserProjection_reduces(t *testing.T) { anyArg{}, anyArg{}, "ro-id", - domain.UserStateInitial, + domain.UserStateActive, uint64(15), "username", domain.UserTypeMachine, @@ -1156,7 +1268,7 @@ func TestUserProjection_reduces(t *testing.T) { anyArg{}, anyArg{}, "ro-id", - domain.UserStateInitial, + domain.UserStateActive, uint64(15), "username", domain.UserTypeMachine,