From fffda37333db3cad09ff538fe5fef22f91de8e14 Mon Sep 17 00:00:00 2001 From: Alexey Kazakov Date: Wed, 21 Aug 2024 14:23:40 -0700 Subject: [PATCH 1/2] Add User Tier context to Notificatoin Builder --- pkg/notification/notification_builder.go | 14 +++++++ pkg/notification/notification_builder_test.go | 39 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/pkg/notification/notification_builder.go b/pkg/notification/notification_builder.go index 00485b2d..697866f6 100644 --- a/pkg/notification/notification_builder.go +++ b/pkg/notification/notification_builder.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/mail" + "strconv" "github.com/pkg/errors" "k8s.io/apimachinery/pkg/runtime" @@ -25,6 +26,7 @@ type Builder interface { WithControllerReference(owner v1.Object, scheme *runtime.Scheme) Builder WithKeysAndValues(keysAndValues map[string]string) Builder WithUserContext(userSignup *toolchainv1alpha1.UserSignup) Builder + WithUserTierContext(userTier *toolchainv1alpha1.UserTier) Builder Create(ctx context.Context, recipient string) (*toolchainv1alpha1.Notification, error) } @@ -156,3 +158,15 @@ func (b *notificationBuilderImpl) WithUserContext(userSignup *toolchainv1alpha1. }) return b } + +func (b *notificationBuilderImpl) WithUserTierContext(userTier *toolchainv1alpha1.UserTier) Builder { + b.options = append(b.options, func(n *toolchainv1alpha1.Notification) error { + if userTier.Spec.DeactivationTimeoutDays > 0 { + n.Spec.Context["DeactivationTimeoutDays"] = strconv.Itoa(userTier.Spec.DeactivationTimeoutDays) + } else { + n.Spec.Context["DeactivationTimeoutDays"] = "(unlimited)" + } + return nil + }) + return b +} diff --git a/pkg/notification/notification_builder_test.go b/pkg/notification/notification_builder_test.go index 621428cf..16e09ee9 100644 --- a/pkg/notification/notification_builder_test.go +++ b/pkg/notification/notification_builder_test.go @@ -146,6 +146,45 @@ func TestNotificationBuilder(t *testing.T) { assert.Equal(t, "bar", notification.Spec.Context["foo"]) }) + t.Run("success with user tier", func(t *testing.T) { + tests := map[string]struct { + daysInTier int + expectedContext string + }{ + "no deactivation with zero days in tier": { + daysInTier: 0, + expectedContext: "(unlimited)", + }, + "no deactivation with negative days in tier": { + daysInTier: -1, + expectedContext: "(unlimited)", + }, + "deactivation with positive days in tier": { + daysInTier: 15, + expectedContext: "15", + }, + } + for k, tc := range tests { + t.Run(k, func(t *testing.T) { + // given + userTier := &toolchainv1alpha1.UserTier{ + Spec: toolchainv1alpha1.UserTierSpec{ + DeactivationTimeoutDays: tc.daysInTier, + }, + } + + // when + notification, err := NewNotificationBuilder(client, test.HostOperatorNs). + WithUserTierContext(userTier). + Create(context.TODO(), "foo@bar.com") + + // then + require.NoError(t, err) + assert.Equal(t, tc.expectedContext, notification.Spec.Context["DeactivationTimeoutDays"]) + }) + } + }) + t.Run("success with notification type", func(t *testing.T) { // when notification, err := NewNotificationBuilder(client, test.HostOperatorNs). From db0435b8e37b45694aef3429e6676a7203bcd264 Mon Sep 17 00:00:00 2001 From: Alexey Kazakov Date: Wed, 21 Aug 2024 14:41:27 -0700 Subject: [PATCH 2/2] tests --- pkg/test/tier/usertier.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 pkg/test/tier/usertier.go diff --git a/pkg/test/tier/usertier.go b/pkg/test/tier/usertier.go new file mode 100644 index 00000000..8c378828 --- /dev/null +++ b/pkg/test/tier/usertier.go @@ -0,0 +1,36 @@ +package tier + +import ( + toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1" + "github.com/codeready-toolchain/toolchain-common/pkg/test" + "github.com/google/uuid" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func WithDeactivationTimeoutDays(days int) Modifier { + return func(userSignup *toolchainv1alpha1.UserTier) { + userSignup.Spec.DeactivationTimeoutDays = days + } +} + +func WithName(name string) Modifier { + return func(userSignup *toolchainv1alpha1.UserTier) { + userSignup.Name = name + } +} + +type Modifier func(tier *toolchainv1alpha1.UserTier) + +func NewUserTier(modifiers ...Modifier) *toolchainv1alpha1.UserTier { + t := &toolchainv1alpha1.UserTier{ + ObjectMeta: metav1.ObjectMeta{ + Name: uuid.NewString(), + Namespace: test.HostOperatorNs, + CreationTimestamp: metav1.Now(), + }, + } + for _, modify := range modifiers { + modify(t) + } + return t +}