diff --git a/lib/services/local/notifications.go b/lib/services/local/notifications.go index 46116a30c1b21..7651909f1122f 100644 --- a/lib/services/local/notifications.go +++ b/lib/services/local/notifications.go @@ -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" @@ -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. @@ -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 diff --git a/lib/services/local/notifications_test.go b/lib/services/local/notifications_test.go index 725dbb541e1e7..c4207b459c25d 100644 --- a/lib/services/local/notifications_test.go +++ b/lib/services/local/notifications_test.go @@ -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. @@ -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) diff --git a/lib/services/notifications.go b/lib/services/notifications.go index 9e021fc60c398..2f17a6b082c40 100644 --- a/lib/services/notifications.go +++ b/lib/services/notifications.go @@ -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. diff --git a/lib/services/notifications_test.go b/lib/services/notifications_test.go index 2be6c8d63df92..45e1e7191e983 100644 --- a/lib/services/notifications_test.go +++ b/lib/services/notifications_test.go @@ -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.