From 45695d3198ab7030f3452b3ac02d9378b5e23b1a Mon Sep 17 00:00:00 2001 From: Silvan Date: Thu, 2 Dec 2021 19:16:20 +0100 Subject: [PATCH] fix(projection): handle old event in feature (#2773) --- internal/query/projection/feature.go | 10 +- internal/query/projection/feature_test.go | 150 +++++++++++++++++++++- 2 files changed, 156 insertions(+), 4 deletions(-) diff --git a/internal/query/projection/feature.go b/internal/query/projection/feature.go index d544d68bb..42ffee0e1 100644 --- a/internal/query/projection/feature.go +++ b/internal/query/projection/feature.go @@ -142,8 +142,14 @@ func (p *FeatureProjection) reduceFeatureSet(event eventstore.EventReader) (*han if featureEvent.PasswordComplexityPolicy != nil { cols = append(cols, handler.NewCol(FeaturePasswordComplexityPolicyCol, *featureEvent.PasswordComplexityPolicy)) } - if featureEvent.LabelPolicyPrivateLabel != nil { - cols = append(cols, handler.NewCol(FeatureLabelPolicyPrivateLabelCol, *featureEvent.LabelPolicyPrivateLabel)) + if featureEvent.LabelPolicyPrivateLabel != nil || featureEvent.LabelPolicy != nil { + var value bool + if featureEvent.LabelPolicyPrivateLabel != nil { + value = *featureEvent.LabelPolicyPrivateLabel + } else { + value = *featureEvent.LabelPolicy + } + cols = append(cols, handler.NewCol(FeatureLabelPolicyPrivateLabelCol, value)) } if featureEvent.LabelPolicyWatermark != nil { cols = append(cols, handler.NewCol(FeatureLabelPolicyWatermarkCol, *featureEvent.LabelPolicyWatermark)) diff --git a/internal/query/projection/feature_test.go b/internal/query/projection/feature_test.go index 06e209245..0047a6108 100644 --- a/internal/query/projection/feature_test.go +++ b/internal/query/projection/feature_test.go @@ -24,7 +24,7 @@ func TestFeatureProjection_reduces(t *testing.T) { want wantReduce }{ { - name: "org.reduceFeatureSet", + name: "org.reduceFeatureSet new", args: args{ event: getEvent(testEvent( repository.EventType(org.FeaturesSetEventType), @@ -96,6 +96,79 @@ func TestFeatureProjection_reduces(t *testing.T) { }, }, }, + { + name: "org.reduceFeatureSet old", + args: args{ + event: getEvent(testEvent( + repository.EventType(org.FeaturesSetEventType), + org.AggregateType, + []byte(`{ + "tierName": "TierName", + "tierDescription": "TierDescription", + "state": 1, + "stateDescription": "StateDescription", + "auditLogRetention": 1, + "loginPolicyFactors": true, + "loginPolicyIDP": true, + "loginPolicyPasswordless": true, + "loginPolicyRegistration": true, + "loginPolicyUsernameLogin": true, + "loginPolicyPasswordReset": true, + "passwordComplexityPolicy": true, + "labelPolicy": true, + "labelPolicyWatermark": true, + "customDomain": true, + "privacyPolicy": true, + "metadataUser": true, + "customTextMessage": true, + "customTextLogin": true, + "lockoutPolicy": true, + "actions": true + }`), + ), org.FeaturesSetEventMapper), + }, + reduce: (&FeatureProjection{}).reduceFeatureSet, + want: wantReduce{ + aggregateType: eventstore.AggregateType("org"), + sequence: 15, + previousSequence: 10, + projection: FeatureTable, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "UPSERT INTO zitadel.projections.features (aggregate_id, change_date, sequence, is_default, tier_name, tier_description, state, state_description, audit_log_retention, login_policy_factors, login_policy_idp, login_policy_passwordless, login_policy_registration, login_policy_username_login, login_policy_password_reset, password_complexity_policy, label_policy_private_label, label_policy_watermark, custom_domain, privacy_policy, metadata_user, custom_text_message, custom_text_login, lockout_policy, actions) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25)", + expectedArgs: []interface{}{ + "agg-id", + anyArg{}, + uint64(15), + false, + "TierName", + "TierDescription", + domain.FeaturesStateActive, + "StateDescription", + time.Nanosecond, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + }, + }, + }, + }, + }, + }, { name: "org.reduceFeatureSet required values only", args: args{ @@ -154,7 +227,80 @@ func TestFeatureProjection_reduces(t *testing.T) { }, }, { - name: "iam.reduceFeatureSet", + name: "iam.reduceFeatureSet old", + reduce: (&FeatureProjection{}).reduceFeatureSet, + args: args{ + event: getEvent(testEvent( + repository.EventType(iam.FeaturesSetEventType), + iam.AggregateType, + []byte(`{ + "tierName": "TierName", + "tierDescription": "TierDescription", + "state": 1, + "stateDescription": "StateDescription", + "auditLogRetention": 1, + "loginPolicyFactors": true, + "loginPolicyIDP": true, + "loginPolicyPasswordless": true, + "loginPolicyRegistration": true, + "loginPolicyUsernameLogin": true, + "loginPolicyPasswordReset": true, + "passwordComplexityPolicy": true, + "labelPolicy": true, + "labelPolicyWatermark": true, + "customDomain": true, + "privacyPolicy": true, + "metadataUser": true, + "customTextMessage": true, + "customTextLogin": true, + "lockoutPolicy": true, + "actions": true + }`), + ), iam.FeaturesSetEventMapper), + }, + want: wantReduce{ + aggregateType: eventstore.AggregateType("iam"), + sequence: 15, + previousSequence: 10, + projection: FeatureTable, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "UPSERT INTO zitadel.projections.features (aggregate_id, change_date, sequence, is_default, tier_name, tier_description, state, state_description, audit_log_retention, login_policy_factors, login_policy_idp, login_policy_passwordless, login_policy_registration, login_policy_username_login, login_policy_password_reset, password_complexity_policy, label_policy_private_label, label_policy_watermark, custom_domain, privacy_policy, metadata_user, custom_text_message, custom_text_login, lockout_policy, actions) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25)", + expectedArgs: []interface{}{ + "agg-id", + anyArg{}, + uint64(15), + true, + "TierName", + "TierDescription", + domain.FeaturesStateActive, + "StateDescription", + time.Nanosecond, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + }, + }, + }, + }, + }, + }, + { + name: "iam.reduceFeatureSet new", reduce: (&FeatureProjection{}).reduceFeatureSet, args: args{ event: getEvent(testEvent(