Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add User Tier context to Notification Builder #425

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea 👍

}
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
}
Loading