A platform for community discussion. Free, open, simple.
FIX: Do not send notifications for upcoming changes not displayed (#40933)
Martin Brennan74a917394c89
diff --git a/app/jobs/scheduled/check_upcoming_changes.rb b/app/jobs/scheduled/check_upcoming_changes.rbindex 4be752d94a9..1e03b114a27 100644--- a/app/jobs/scheduled/check_upcoming_changes.rb+++ b/app/jobs/scheduled/check_upcoming_changes.rb@@ -75,6 +75,8 @@ module Jobscase status[:error_key]when :does_not_meet_or_exceed_promotion_status:info+ when :should_not_be_displayed+ :debugwhen :already_notified_about_promotion:debugwhen :already_manually_toggleddiff --git a/app/jobs/scheduled/notify_admins_of_available_upcoming_changes.rb b/app/jobs/scheduled/notify_admins_of_available_upcoming_changes.rbindex 4a65887209a..218143f7bab 100644--- a/app/jobs/scheduled/notify_admins_of_available_upcoming_changes.rb+++ b/app/jobs/scheduled/notify_admins_of_available_upcoming_changes.rb@@ -177,10 +177,11 @@ module Jobssince: 1.week.ago,upcoming_changes_meeting_notify_status:SiteSetting.upcoming_change_site_settings.filter do |upcoming_change_name|- UpcomingChanges.meets_or_exceeds_status?(- upcoming_change_name,- UpcomingChanges.previous_status(SiteSetting.promote_upcoming_changes_on_status),- )+ UpcomingChanges::ConditionalDisplay.should_display?(upcoming_change_name) &&+ UpcomingChanges.meets_or_exceeds_status?(+ upcoming_change_name,+ UpcomingChanges.previous_status(SiteSetting.promote_upcoming_changes_on_status),+ )end,)enddiff --git a/app/services/upcoming_changes/notify_promotion.rb b/app/services/upcoming_changes/notify_promotion.rbindex 66d2bc8e858..568ad5c8134 100644--- a/app/services/upcoming_changes/notify_promotion.rb+++ b/app/services/upcoming_changes/notify_promotion.rb@@ -26,6 +26,7 @@ class UpcomingChanges::NotifyPromotionendpolicy :setting_is_available+ policy :change_should_be_displayedpolicy :meets_or_exceeds_statuspolicy :change_has_not_already_been_notified_about_promotionpolicy :admin_has_not_manually_toggled@@ -49,6 +50,10 @@ class UpcomingChanges::NotifyPromotionSiteSetting.respond_to?(params.setting_name)end+ def change_should_be_displayed(params:)+ UpcomingChanges::ConditionalDisplay.should_display?(params.setting_name)+ end+def meets_or_exceeds_status(params:)UpcomingChanges.meets_or_exceeds_status?(params.setting_name,diff --git a/app/services/upcoming_changes/notify_promotions.rb b/app/services/upcoming_changes/notify_promotions.rbindex 58b510588ce..3d6140d6a08 100644--- a/app/services/upcoming_changes/notify_promotions.rb+++ b/app/services/upcoming_changes/notify_promotions.rb@@ -46,6 +46,13 @@ class UpcomingChanges::NotifyPromotionsstatus_hash[:error_key] = :setting_not_availableend+ on_failed_policy(:change_should_be_displayed) do |policy|+ status_hash[+ :error+ ] = "Setting #{setting_name} is not displayed on this site, skipping promotion notification"+ status_hash[:error_key] = :should_not_be_displayed+ end+on_failed_policy(:meets_or_exceeds_status) do |policy|status_hash[:errordiff --git a/spec/jobs/scheduled/check_upcoming_changes_spec.rb b/spec/jobs/scheduled/check_upcoming_changes_spec.rbindex bb73f575a54..bea8bf4b391 100644--- a/spec/jobs/scheduled/check_upcoming_changes_spec.rb+++ b/spec/jobs/scheduled/check_upcoming_changes_spec.rb@@ -229,6 +229,31 @@ RSpec.describe Jobs::CheckUpcomingChanges doendend+ context "when the change should not be displayed on this site" do+ before do+ UpcomingChangeEvent.where(+ upcoming_change_name: :enable_upload_debug_mode,+ event_type: :admins_notified_automatic_promotion,+ ).delete_all+ UpcomingChanges::ConditionalDisplay.stubs(+ :should_display_enable_upload_debug_mode?,+ ).returns(false)+ end++ it "does not notify admins and logs the skip at debug level" do+ track_log_messages do |logger|+ described_class.new.execute({})+ expect(logger.infos.join("\n")).not_to include(+ "Notified site admins about promotion of 'enable_upload_debug_mode'",+ )+ expect(logger.errors.join("\n")).not_to include("enable_upload_debug_mode")+ expect(logger.debugs.join("\n")).to include(+ "Failed to notify about promotion of 'enable_upload_debug_mode': Setting enable_upload_debug_mode is not displayed on this site",+ )+ end+ end+ end+context "when notifying about permanent changes" dobefore doUpcomingChangeEvent.create!(diff --git a/spec/jobs/scheduled/notify_admins_of_available_upcoming_changes_spec.rb b/spec/jobs/scheduled/notify_admins_of_available_upcoming_changes_spec.rbindex 4f3ab7e5364..dfab5a31cc8 100644--- a/spec/jobs/scheduled/notify_admins_of_available_upcoming_changes_spec.rb+++ b/spec/jobs/scheduled/notify_admins_of_available_upcoming_changes_spec.rb@@ -328,6 +328,37 @@ RSpec.describe Jobs::NotifyAdminsOfAvailableUpcomingChanges doendend+ context "when an upcoming change should not be displayed on this site" do+ before do+ UpcomingChanges::ConditionalDisplay.stubs(:should_display_test_upcoming_change?).returns(+ false,+ )+ end++ it "does not create a notification for the hidden change but still notifies for others" do+ result+ data =+ JSON.parse(+ Notification+ .where(notification_type: Notification.types[:upcoming_change_available])+ .last+ .data,+ symbolize_names: true,+ )+ expect(data[:upcoming_change_names]).to eq(%w[test_upcoming_change_b])+ expect(data[:count]).to eq(1)+ end++ it "does not log an admins_notified_available_change event for the hidden change" do+ expect { result }.not_to change {+ UpcomingChangeEvent.where(+ event_type: :admins_notified_available_change,+ upcoming_change_name: :test_upcoming_change,+ ).count+ }+ end+ end+context "when there is an added event for an upcoming change that no longer exists" dobefore doUpcomingChangeEvent.create!(diff --git a/spec/services/upcoming_changes/notify_promotion_spec.rb b/spec/services/upcoming_changes/notify_promotion_spec.rbindex cd48a34497f..5ebf031969c 100644--- a/spec/services/upcoming_changes/notify_promotion_spec.rb+++ b/spec/services/upcoming_changes/notify_promotion_spec.rb@@ -58,6 +58,29 @@ RSpec.describe UpcomingChanges::NotifyPromotion doit { is_expected.to fail_a_policy(:meets_or_exceeds_status) }end+ context "when the change should not be displayed on this site" do+ before do+ UpcomingChanges::ConditionalDisplay.stubs(+ :should_display_enable_upload_debug_mode?,+ ).returns(false)+ end++ it { is_expected.to fail_a_policy(:change_should_be_displayed) }++ it "does not notify admins or create an event" do+ expect { result }.to not_change {+ Notification.where(+ notification_type: Notification.types[:upcoming_change_automatically_promoted],+ ).count+ }.and not_change {+ UpcomingChangeEvent.where(+ event_type: :admins_notified_automatic_promotion,+ upcoming_change_name: :enable_upload_debug_mode,+ ).count+ }+ end+ end+context "when change has already been notified about promotion" dolet(:changes_already_notified_about_promotion) { [:enable_upload_debug_mode] }diff --git a/spec/services/upcoming_changes/notify_promotions_spec.rb b/spec/services/upcoming_changes/notify_promotions_spec.rbindex 8cec5a67c62..d9d7041e006 100644--- a/spec/services/upcoming_changes/notify_promotions_spec.rb+++ b/spec/services/upcoming_changes/notify_promotions_spec.rb@@ -247,6 +247,32 @@ RSpec.describe UpcomingChanges::NotifyPromotions doendend+ context "when a change should not be displayed on this site" do+ before do+ UpcomingChanges::ConditionalDisplay.stubs(+ :should_display_enable_upload_debug_mode?,+ ).returns(false)+ end++ it "does not notify admins for the hidden setting" do+ expect { result }.not_to change {+ Notification+ .where(notification_type: Notification.types[:upcoming_change_automatically_promoted])+ .where("data::text LIKE ?", "%enable_upload_debug_mode%")+ .count+ }+ end++ it "returns the correct error and error key" do+ expect(result[:change_notification_statuses][:enable_upload_debug_mode]).to match(+ success: false,+ error:+ "Setting enable_upload_debug_mode is not displayed on this site, skipping promotion notification",+ error_key: :should_not_be_displayed,+ )+ end+ end+context "when settings are opted out" dobefore { SiteSetting.enable_upload_debug_mode = false }