mirror of
https://github.com/we-promise/sure.git
synced 2026-09-02 21:31:07 +00:00
* fix: gracefully handle invalid family timezone instead of crashing Family#timezone is a free-text IANA zone name with no validation on write. If it becomes stale (e.g. tzdata renames a zone, like the historical Europe/Kiev -> Europe/Kyiv switch) or a migration meant to remap legacy names never ran, Localize#switch_timezone passed the raw string straight to Time.use_zone, which raises ArgumentError for any unrecognized zone. Since switch_timezone runs as an around_action on every request, this crashed the entire app for the affected family, including the login page. Now validates the zone via ActiveSupport::TimeZone[] first and falls back to the app default (logging a DebugLogEntry) instead of raising. The log write is debounced per (family, bad value) via Rails.cache (once per day) so an affected family doesn't write one DebugLogEntry row per page view indefinitely. Fixes #390 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fix: address review feedback on timezone fallback - Make the invalid-timezone debounce lease atomic. Rails.cache.fetch is read-then-write, not atomic, so two concurrent requests could both observe a cache miss and both log before either write landed. Rails.cache.write(unless_exist: true) maps to Redis's atomic SET NX in production, so only one request ever wins the lease. (via CodeRabbit) - Stop using "Europe/Kiev" as the invalid-timezone value in tests. Whether ActiveSupport::TimeZone still resolves that legacy alias depends on the host's installed tzdata version (tzinfo-data is Windows/JRuby-only per Gemfile), so the test's pass/fail behavior wasn't deterministic across machines/CI. Use a deliberately nonexistent name instead. (via Codex) Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fix: validate Family#timezone on write to address root cause of #390 The previous commit made the *crash* graceful, but left the actual defect in place: nothing stopped an unrecognized IANA zone name from being written to Family#timezone in the first place (direct DB/API access, an old dump predating a tzdata rename, or a future rename of a currently-valid zone). Add a Family-level validation using the same ActiveSupport::TimeZone[] lookup Localize#resolved_timezone uses at request time, so "valid at save" and "valid when rendering" can't drift apart. Deliberately not `inclusion: { in: ActiveSupport::TimeZone.all.map(&:name) }`, matching the neighboring locale/date_format validations: verified empirically that the settings form submits `tz.tzinfo.identifier` (e.g. "America/New_York"), not `tz.name` (e.g. "Eastern Time (US & Canada)"), and those differ for all 150 zones Rails ships. An inclusion check against `.name` would have rejected every legitimate value the form submits. The validation only runs when timezone is actually being changed (if: :timezone_changed?). A family with a pre-existing bad value (the exact #390 scenario) must still be able to save unrelated changes -- otherwise this would turn a previously-harmless bad value into a blocker for any other settings update or background job touching that family's record. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
117 lines
3.9 KiB
Ruby
117 lines
3.9 KiB
Ruby
require "test_helper"
|
|
|
|
class LocalizeTest < ActionDispatch::IntegrationTest
|
|
test "uses Accept-Language top locale on login when supported" do
|
|
get new_session_url, headers: { "Accept-Language" => "fr-CA,fr;q=0.9" }
|
|
assert_response :success
|
|
assert_select "button", text: /Se connecter/i
|
|
end
|
|
|
|
test "falls back to English when Accept-Language is unsupported" do
|
|
get new_session_url, headers: { "Accept-Language" => "ru-RU,ru;q=0.9" }
|
|
assert_response :success
|
|
assert_select "button", text: /Войти/i
|
|
end
|
|
|
|
test "uses Accept-Language for onboarding when user locale is not set" do
|
|
sign_in users(:family_admin)
|
|
|
|
get preferences_onboarding_url, headers: { "Accept-Language" => "es-ES,es;q=0.9" }
|
|
assert_response :success
|
|
assert_select "h1", text: /Configura tus preferencias/i
|
|
end
|
|
|
|
test "falls back to family locale when Accept-Language is unsupported" do
|
|
sign_in users(:family_admin)
|
|
|
|
get preferences_onboarding_url, headers: { "Accept-Language" => "ru-RU,ru;q=0.9" }
|
|
assert_response :success
|
|
assert_select "h1", text: /Настройте ваши предпочтения/i
|
|
end
|
|
|
|
test "respects user locale override even when Accept-Language differs" do
|
|
user = users(:family_admin)
|
|
user.update!(locale: "fr")
|
|
sign_in user
|
|
|
|
get preferences_onboarding_url, headers: { "Accept-Language" => "es-ES,es;q=0.9" }
|
|
assert_response :success
|
|
assert_select "h1", text: /Configurez vos préférences/i
|
|
end
|
|
|
|
test "switches locale when locale param is provided" do
|
|
sign_in users(:family_admin)
|
|
|
|
get preferences_onboarding_url(locale: "fr")
|
|
assert_response :success
|
|
assert_select "h1", text: /Configurez vos préférences/i
|
|
end
|
|
|
|
test "ignores invalid locale param and uses family locale" do
|
|
sign_in users(:family_admin)
|
|
|
|
get preferences_onboarding_url(locale: "invalid_locale")
|
|
assert_response :success
|
|
assert_select "h1", text: /Configure your preferences/i
|
|
end
|
|
|
|
test "falls back to default timezone and logs a warning when family timezone is unrecognized" do
|
|
user = users(:family_admin)
|
|
# A deliberately nonexistent zone name, standing in for a stale/renamed IANA
|
|
# value slipping past validation (e.g. the historical "Europe/Kiev" ->
|
|
# "Europe/Kyiv" rename, or a migration that never ran). We can't use a real
|
|
# legacy alias like "Europe/Kiev" here: whether tzinfo still recognizes it
|
|
# depends on the host's installed tzdata version, which would make this
|
|
# test non-deterministic across machines/CI.
|
|
user.family.update_column(:timezone, "Invalid/Timezone")
|
|
sign_in user
|
|
|
|
assert_difference "DebugLogEntry.count", 1 do
|
|
get root_url
|
|
end
|
|
|
|
assert_response :success
|
|
|
|
entry = DebugLogEntry.order(:created_at).last
|
|
assert_equal "warn", entry.level
|
|
assert_includes entry.message, "Invalid/Timezone"
|
|
assert_equal user.family, entry.family
|
|
end
|
|
|
|
test "does not log when family timezone is valid" do
|
|
user = users(:family_admin)
|
|
user.family.update_column(:timezone, "America/New_York")
|
|
sign_in user
|
|
|
|
assert_no_difference "DebugLogEntry.count" do
|
|
get root_url
|
|
end
|
|
|
|
assert_response :success
|
|
end
|
|
|
|
test "does not log again on a second request within the debounce window" do
|
|
user = users(:family_admin)
|
|
user.family.update_column(:timezone, "Invalid/Timezone")
|
|
sign_in user
|
|
|
|
# The test environment's cache store is :null_store (config/environments/test.rb),
|
|
# which never actually caches anything -- every write is a no-op and every
|
|
# key looks nonexistent. Swap in a real store for this test so the
|
|
# debounce lease (Rails.cache.write unless_exist:) is meaningfully
|
|
# exercised instead of trivially passing.
|
|
original_cache = Rails.cache
|
|
Rails.cache = ActiveSupport::Cache::MemoryStore.new
|
|
|
|
assert_difference "DebugLogEntry.count", 1 do
|
|
get root_url
|
|
get root_url
|
|
get root_url
|
|
end
|
|
|
|
assert_response :success
|
|
ensure
|
|
Rails.cache = original_cache
|
|
end
|
|
end
|