Skip to content

Commit

Permalink
CR
Browse files Browse the repository at this point in the history
  • Loading branch information
rudream committed Feb 27, 2024
1 parent f08f16a commit 62e7db7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
9 changes: 4 additions & 5 deletions lib/services/local/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ package local

import (
"context"
"fmt"
"time"

"github.com/google/uuid"
"github.com/gravitational/trace"
"google.golang.org/protobuf/types/known/timestamppb"

headerv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/header/v1"
Expand All @@ -32,7 +32,6 @@ import (
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/services/local/generic"
"github.com/gravitational/trace"
)

// NotificationsService manages notification resources in the backend.
Expand Down Expand Up @@ -288,10 +287,10 @@ func CheckAndSetExpiry(notification *notificationsv1.Notification) error {
// If the expiry has already been provided, ensure that it is not more than 90 days from now.
// This is to prevent misuse as we don't want notifications existing for too long and accumulating in the backend.
now := time.Now()
maxExpiry := now.Add(maxExpiry)
timeOfMaxExpiry := now.Add(maxExpiry)

if (*notification.Metadata.Expires).AsTime().After(maxExpiry) {
return trace.BadParameter(fmt.Sprintf("notification expiry cannot be more than %s days from its creation", maxExpiry))
if (*notification.Metadata.Expires).AsTime().After(timeOfMaxExpiry) {
return trace.BadParameter("notification expiry cannot be more than %d days from its creation", int(maxExpiry.Hours()/24))
}

return nil
Expand Down
13 changes: 7 additions & 6 deletions lib/services/local/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ import (
"time"

"github.com/google/go-cmp/cmp"
headerv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/header/v1"
notificationsv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/notifications/v1"
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/backend/memory"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/timestamppb"

headerv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/header/v1"
notificationsv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/notifications/v1"
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/backend/memory"
)

// TestUserNotificationCRUD tests backend operations for user-specific notification resources.
Expand Down Expand Up @@ -272,9 +273,9 @@ func TestUserNotificationStateCRUD(t *testing.T) {
require.Len(t, paginatedOut, 2)
// Verify that notification id's and states are correct, userNotificationState1 should now have the dismissed state.
require.Equal(t, userNotificationState1.Spec.NotificationId, paginatedOut[0].Spec.NotificationId)
require.Equal(t, paginatedOut[0].Status.NotificationState, notificationsv1.NotificationState_NOTIFICATION_STATE_DISMISSED)
require.Equal(t, notificationsv1.NotificationState_NOTIFICATION_STATE_DISMISSED, paginatedOut[0].Status.NotificationState)
require.Equal(t, userNotificationState2.Spec.NotificationId, paginatedOut[1].Spec.NotificationId)
require.Equal(t, paginatedOut[1].Status.NotificationState, notificationsv1.NotificationState_NOTIFICATION_STATE_CLICKED)
require.Equal(t, notificationsv1.NotificationState_NOTIFICATION_STATE_CLICKED, paginatedOut[1].Status.NotificationState)

// Test deleting a notification state.
err = service.DeleteUserNotificationState(ctx, testUsername, notification1Id)
Expand Down
5 changes: 3 additions & 2 deletions lib/services/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ package services
import (
"context"

notificationsv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/notifications/v1"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"

notificationsv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/notifications/v1"
"github.com/gravitational/teleport/lib/utils"
)

// Notifications defines an interface for managing notifications.
Expand Down
7 changes: 4 additions & 3 deletions lib/services/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import (
"time"

"github.com/google/go-cmp/cmp"
headerv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/header/v1"
notificationsv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/notifications/v1"
"github.com/gravitational/teleport/api/types"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/timestamppb"

headerv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/header/v1"
notificationsv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/notifications/v1"
"github.com/gravitational/teleport/api/types"
)

// TestMarshalNotificationRoundTrip tests the marshaling and unmarshaling functions for Notification objects.
Expand Down

0 comments on commit 62e7db7

Please sign in to comment.