From a29dd89683e0d64b38c73a99a3f1f0d76b09abde Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sat, 22 Nov 2025 22:19:05 -0500 Subject: [PATCH] Make sure Bundle#ends_at is set before validation otherwise we risk creating overlapping bundles and not catching it. --- app/models/notification/bundle.rb | 2 +- test/models/notification/bundle_test.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/models/notification/bundle.rb b/app/models/notification/bundle.rb index e6f63acaa..fa9c9a111 100644 --- a/app/models/notification/bundle.rb +++ b/app/models/notification/bundle.rb @@ -15,7 +15,7 @@ class Notification::Bundle < ApplicationRecord ) end - before_create :set_default_window + before_validation :set_default_window, if: :new_record? validate :validate_no_overlapping diff --git a/test/models/notification/bundle_test.rb b/test/models/notification/bundle_test.rb index bd45d0e1e..dedd12362 100644 --- a/test/models/notification/bundle_test.rb +++ b/test/models/notification/bundle_test.rb @@ -83,6 +83,16 @@ class Notification::BundleTest < ActiveSupport::TestCase assert bundle_5.valid? end + test "overlapping bundles that are created relying on set_default_window are not created" do + @user.notification_bundles.destroy_all + + bundle = @user.notification_bundles.create!(starts_at: Time.current) + + assert_raises ActiveRecord::RecordInvalid do + @user.notification_bundles.create!(starts_at: bundle.starts_at - 1.second) + end + end + test "deliver_all delivers due bundles" do notification = @user.notifications.create!(source: events(:logo_published), creator: @user)