Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

increase time_zone robustness

ulferts4c61ce1bfccf
diff --git a/app/models/user.rb b/app/models/user.rb
index c88ad590823..4563f1086b9 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -406,7 +406,7 @@ class User < Principal
end
def time_zone
- @time_zone ||= (pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[pref.time_zone])
+ @time_zone ||= ActiveSupport::TimeZone[pref.time_zone] || ActiveSupport::TimeZone["Etc/UTC"]
end
def reload(*)
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 2cb717f4e12..66d7db19d21 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -823,6 +823,39 @@ RSpec.describe User do
end
end
+ describe "#time_zone" do
+ let(:user) { build(:user, preferences:) }
+
+ context "with an existing time zone in the prefs" do
+ let(:preferences) { { "time_zone" => "Europe/Athens" } }
+
+ it "returns the matching ActiveSupport::TimeZone" do
+ expect(user.time_zone)
+ .to eql ActiveSupport::TimeZone["Europe/Athens"]
+ end
+ end
+
+ context "with an invalid time zone" do
+ # Would need to be Etc/UTC or UTC to be valid
+ let(:preferences) { { "time_zone" => "utc" } }
+
+ it "returns the utc ActiveSupport::TimeZone" do
+ expect(user.time_zone)
+ .to eql ActiveSupport::TimeZone["Etc/UTC"]
+ end
+ end
+
+ context "without a time zone" do
+ # Would need to be Etc/UTC or UTC to be valid
+ let(:preferences) { {} }
+
+ it "returns the utc ActiveSupport::TimeZone" do
+ expect(user.time_zone)
+ .to eql ActiveSupport::TimeZone["Etc/UTC"]
+ end
+ end
+ end
+
describe "#find_by_mail" do
let!(:user1) { create(:user, mail: "[email protected]") }
let!(:user2) { create(:user, mail: "[email protected]") }