From 8f52fd1051e20fdff41321c5cff99fda05af86c1 Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Fri, 12 Apr 2024 10:53:44 -0400 Subject: [PATCH] FIX: Ensure invalid timezone does not block critical emails (#26607) See https://meta.discourse.org/t/invalid-user-timezone-jams-up-their-mail/303306 This ensures that "account silenced" and "account suspended" emails don't fail to send if the user has an invalid timezone. --- app/mailers/user_notifications.rb | 4 +-- spec/mailers/user_notifications_spec.rb | 34 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 555681a361b..13d8b5ffe44 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -167,7 +167,7 @@ class UserNotifications < ActionMailer::Base reason: user_history.details, ) else - silenced_till = user.silenced_till.in_time_zone(user.user_option.timezone) + silenced_till = user.silenced_till.in_time_zone(user.user_option.timezone.presence || "UTC") build_email( user.email, template: "user_notifications.account_silenced", @@ -191,7 +191,7 @@ class UserNotifications < ActionMailer::Base reason: user_history.details, ) else - suspended_till = user.suspended_till.in_time_zone(user.user_option.timezone) + suspended_till = user.suspended_till.in_time_zone(user.user_option.timezone.presence || "UTC") build_email( user.email, template: "user_notifications.account_suspended", diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index a1d33341a54..9593524be26 100644 --- a/spec/mailers/user_notifications_spec.rb +++ b/spec/mailers/user_notifications_spec.rb @@ -1471,6 +1471,23 @@ RSpec.describe UserNotifications do expect(mail.body).to include(date) end end + + context "when user timezone is invalid" do + before { user.user_option.timezone = "" } + + it "doesn't raise error" do + expect { UserNotifications.account_silenced(user) }.not_to raise_error + end + + it "adds the silenced_till date in UTC" do + date = "May 25, 2020, 12:00pm" + user.silenced_till = DateTime.parse(date) + + mail = UserNotifications.account_silenced(user, { user_history: user_history }) + + expect(mail.body).to include(date) + end + end end describe ".account_suspended" do @@ -1501,5 +1518,22 @@ RSpec.describe UserNotifications do expect(mail.body).to include(date) end end + + context "when user timezone is invalid" do + before { user.user_option.timezone = "" } + + it "doesn't raise error" do + expect { UserNotifications.account_suspended(user) }.not_to raise_error + end + + it "adds the suspended_till date in UTC" do + date = "May 25, 2020, 12:00pm" + user.suspended_till = DateTime.parse(date) + + mail = UserNotifications.account_suspended(user, { user_history: user_history }) + + expect(mail.body).to include(date) + end + end end end