A platform for community discussion. Free, open, simple.
FIX: respect user timezone in emails about silencing and suspending (#16918)
Andrei Prigorshnev25e4095c9c51
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rbindex 69299b7a5f7..d3bc5e0bb45 100644--- a/app/mailers/user_notifications.rb+++ b/app/mailers/user_notifications.rb@@ -161,12 +161,13 @@ class UserNotifications < ActionMailer::Basereason: user_history.details)else+ silenced_till = user.silenced_till.in_time_zone(user.user_option.timezone)build_email(user.email,template: "user_notifications.account_silenced",locale: user_locale(user),reason: user_history.details,- silenced_till: I18n.l(user.silenced_till, format: :long)+ silenced_till: I18n.l(silenced_till, format: :long))endend@@ -184,12 +185,13 @@ class UserNotifications < ActionMailer::Basereason: user_history.details)else+ suspended_till = user.suspended_till.in_time_zone(user.user_option.timezone)build_email(user.email,template: "user_notifications.account_suspended",locale: user_locale(user),reason: user_history.details,- suspended_till: I18n.l(user.suspended_till, format: :long)+ suspended_till: I18n.l(suspended_till, format: :long))endenddiff --git a/spec/fabricators/user_history_fabricator.rb b/spec/fabricators/user_history_fabricator.rbnew file mode 100644index 00000000000..4464b03c7cd--- /dev/null+++ b/spec/fabricators/user_history_fabricator.rb@@ -0,0 +1,4 @@+# frozen_string_literal: true++Fabricator(:user_history) do+enddiff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rbindex 1cff65d290d..e8ea2620ceb 100644--- a/spec/mailers/user_notifications_spec.rb+++ b/spec/mailers/user_notifications_spec.rb@@ -1184,4 +1184,68 @@ describe UserNotifications do"[admin](http://test.localhost/u/admin)")endend++ describe ".account_silenced" do+ fab!(:user_history) { Fabricate(:user_history, action: UserHistory.actions[:silence_user]) }++ it "adds the silenced_till date in user's timezone" do+ user.user_option.timezone = "Asia/Tbilisi" # GMT+4+ user.silenced_till = DateTime.parse("May 25, 2020, 12:00pm")++ mail = UserNotifications.account_silenced(user, { user_history: user_history })++ expect(mail.body).to include("May 25, 2020, 4:00pm")+ end++ context "user doesn't have timezone set" do+ before do+ user.user_option.timezone = nil+ end++ 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+ fab!(:user_history) { Fabricate(:user_history, action: UserHistory.actions[:suspend_user]) }++ it "adds the suspended_till date in user's timezone" do+ user.user_option.timezone = "Asia/Tbilisi" # GMT+4+ user.suspended_till = DateTime.parse("May 25, 2020, 12:00pm")++ mail = UserNotifications.account_suspended(user, { user_history: user_history })++ expect(mail.body).to include("May 25, 2020, 4:00pm")+ end++ context "user doesn't have timezone set" do+ before do+ user.user_option.timezone = nil+ end++ 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+ endend