Skip to content

Commit

Permalink
increase time_zone robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
ulferts committed Sep 6, 2024
1 parent a5714f7 commit 4c61ce1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def pref
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(*)
Expand Down
33 changes: 33 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,39 @@ def build_user_double_with_expired_password(is_expired)
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]") }
Expand Down

0 comments on commit 4c61ce1

Please sign in to comment.