Skip to content

Commit

Permalink
Add User Tier context to Notification Builder (#425)
Browse files Browse the repository at this point in the history
* Add User Tier context to Notificatoin Builder
  • Loading branch information
alexeykazakov authored Aug 22, 2024
1 parent 2184e62 commit 6a52cb3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/notification/notification_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/mail"
"strconv"

"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
}
39 changes: 39 additions & 0 deletions pkg/notification/notification_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "[email protected]")

// 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).
Expand Down
36 changes: 36 additions & 0 deletions pkg/test/tier/usertier.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 6a52cb3

Please sign in to comment.