From 25e4095c9c5169939d3e87974f8faece16c46309 Mon Sep 17 00:00:00 2001 From: Andrei Prigorshnev Date: Fri, 27 May 2022 13:58:54 +0400 Subject: [PATCH] FIX: respect user timezone in emails about silencing and suspending (#16918) --- app/mailers/user_notifications.rb | 6 +- spec/fabricators/user_history_fabricator.rb | 4 ++ spec/mailers/user_notifications_spec.rb | 64 +++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 spec/fabricators/user_history_fabricator.rb diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 69299b7a5f7..d3bc5e0bb45 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -161,12 +161,13 @@ class UserNotifications < ActionMailer::Base reason: 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) ) end end @@ -184,12 +185,13 @@ class UserNotifications < ActionMailer::Base reason: 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) ) end end diff --git a/spec/fabricators/user_history_fabricator.rb b/spec/fabricators/user_history_fabricator.rb new file mode 100644 index 00000000000..4464b03c7cd --- /dev/null +++ b/spec/fabricators/user_history_fabricator.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +Fabricator(:user_history) do +end diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index 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)") end end + + 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 + end end