The personal finance app for everyone (by everyone)
Ruby
fix(hosting): consistent provider-block visibility + fix Twelve Data toggle bug (#3333)
GFRed6b8b752a8c
diff --git a/app/controllers/settings/hostings_controller.rb b/app/controllers/settings/hostings_controller.rbindex 6d5dddaeec..af71e4d18e 100644--- a/app/controllers/settings/hostings_controller.rb+++ b/app/controllers/settings/hostings_controller.rb@@ -33,10 +33,10 @@ class Settings::HostingsController < ApplicationController@show_tiingo_settings = enabled_securities.include?("tiingo")@show_eodhd_settings = enabled_securities.include?("eodhd")@show_alpha_vantage_settings = enabled_securities.include?("alpha_vantage")- # T-Invest doubles as a brand-logo source consulted regardless of the price- # provider, so its token is useful even when it's not enabled for prices.- # Always surface the token field, decoupled from the securities checklist.- @show_tinkoff_invest_settings = true+ tinkoff_invest_checked = enabled_securities.include?("tinkoff_invest")+ tinkoff_invest_configured = ENV["TINKOFF_INVEST_API_KEY"].present? || Setting.tinkoff_invest_api_key.present?+ @show_tinkoff_invest_settings = tinkoff_invest_checked || enabled_securities.include?("moex_public") || tinkoff_invest_configured+ @tinkoff_invest_moex_only = @show_tinkoff_invest_settings && !tinkoff_invest_checked# Only fetch provider data if we're showing the sectionif @show_twelve_data_settings@@ -95,10 +95,7 @@ class Settings::HostingsController < ApplicationControllerSetting.securities_providers = new_providers.join(",")- # Clear the legacy singular setting so the fallback in- # enabled_securities_providers doesn't re-enable a provider- # the user just unchecked.- Setting.securities_provider = nil if new_providers.empty?+ Setting.securities_provider = "" if new_providers.empty?# Mark securities linked to removed providers as offline so they aren't# silently queried against an incompatible fallback provider (e.g. MFAPIdiff --git a/app/models/setting.rb b/app/models/setting.rbindex 8db7e878d8..7e6217ed0b 100644--- a/app/models/setting.rb+++ b/app/models/setting.rb@@ -142,7 +142,7 @@ class Setting < RailsSettings::Baseplural.to_s.split(",").map(&:strip).reject(&:blank?)else# Backward compat: fall back to singular setting- [ ENV["SECURITIES_PROVIDER"].presence || securities_provider ].compact+ [ ENV["SECURITIES_PROVIDER"].presence || securities_provider.presence ].compactendenddiff --git a/app/views/settings/hostings/_tinkoff_invest_settings.html.erb b/app/views/settings/hostings/_tinkoff_invest_settings.html.erbindex 670c5d1da0..c0ab769e65 100644--- a/app/views/settings/hostings/_tinkoff_invest_settings.html.erb+++ b/app/views/settings/hostings/_tinkoff_invest_settings.html.erb@@ -1,4 +1,7 @@<div class="space-y-4">+ <% if @tinkoff_invest_moex_only %>+ <%= render DS::Alert.new(message: t(".moex_only_notice"), variant: :info) %>+ <% end %><div><h2 class="font-medium mb-1"><%= t(".title") %></h2><% if ENV["TINKOFF_INVEST_API_KEY"].present? %>diff --git a/config/locales/views/settings/hostings/en.yml b/config/locales/views/settings/hostings/en.ymlindex fdd79c9de2..0e90664b96 100644--- a/config/locales/views/settings/hostings/en.yml+++ b/config/locales/views/settings/hostings/en.yml@@ -188,6 +188,7 @@ en:tinkoff_invest_settings:title: T-Invest (T-Bank)description: Enter a read-only T-Invest API token. Used to fetch brand logos for securities (incl. funds and bonds, even when MOEX prices them) and, when enabled above, prices for Russian instruments.+ moex_only_notice: Not enabled for prices above — T-Invest is used to fetch brand logos for all your securities whenever a token is configured, independent of the checkbox above.env_configured_message: Successfully configured through the TINKOFF_INVEST_API_KEY environment variable.label: API Tokenplaceholder: Enter your T-Invest API token herediff --git a/test/controllers/settings/hostings_controller_test.rb b/test/controllers/settings/hostings_controller_test.rbindex e557670fc7..87de343989 100644--- a/test/controllers/settings/hostings_controller_test.rb+++ b/test/controllers/settings/hostings_controller_test.rb@@ -711,4 +711,85 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTestensureSetting.securities_providers = ""end++ test "unchecking every securities provider does not re-enable twelve_data via the legacy fallback" do+ with_self_hosting do+ # Start from the out-of-the-box default (only twelve_data enabled)+ assert_equal [ "twelve_data" ], Setting.enabled_securities_providers++ patch settings_hosting_url, params: { setting: { securities_providers: [] } }++ assert_redirected_to settings_hosting_url+ assert_equal [], Setting.enabled_securities_providers+ end+ ensure+ # Explicitly restore the real default value rather than assigning nil —+ # rails-settings-cached's cache layer doesn't reliably invalidate on+ # delete within a single test process, so a later test can still read+ # back the just-deleted blank override instead of falling through to+ # the field's default.+ Setting.securities_providers = ""+ Setting.securities_provider = "twelve_data"+ end++ # --- T-Invest visibility (issue #3089) ---++ test "hides T-Invest settings when neither tinkoff_invest nor moex_public is enabled" do+ with_self_hosting do+ patch settings_hosting_url, params: { setting: { securities_providers: [ "twelve_data" ] } }++ get settings_hosting_url++ assert_response :success+ # "T-Invest (T-Bank)" also appears as a checkbox label in the always-rendered+ # securities checklist, so assert on the settings block's own field instead.+ assert_select "input[name='setting[tinkoff_invest_api_key]']", false+ end+ ensure+ Setting.securities_providers = ""+ end++ test "shows T-Invest settings when tinkoff_invest is enabled, without the moex-only notice" do+ with_self_hosting do+ patch settings_hosting_url, params: { setting: { securities_providers: [ "tinkoff_invest" ] } }++ get settings_hosting_url++ assert_response :success+ assert_select "input[name='setting[tinkoff_invest_api_key]']"+ assert_not_includes response.body, I18n.t("settings.hostings.tinkoff_invest_settings.moex_only_notice")+ end+ ensure+ Setting.securities_providers = ""+ end++ test "shows T-Invest settings with the moex-only notice when moex_public is enabled, even without tinkoff_invest" do+ with_self_hosting do+ patch settings_hosting_url, params: { setting: { securities_providers: [ "moex_public" ] } }++ get settings_hosting_url++ assert_response :success+ assert_select "input[name='setting[tinkoff_invest_api_key]']"+ assert_includes response.body, I18n.t("settings.hostings.tinkoff_invest_settings.moex_only_notice")+ end+ ensure+ Setting.securities_providers = ""+ end++ test "shows T-Invest settings when a token is already configured, even with neither checkbox enabled" do+ with_self_hosting do+ Setting.tinkoff_invest_api_key = "some-token"+ patch settings_hosting_url, params: { setting: { securities_providers: [ "twelve_data" ] } }++ get settings_hosting_url++ assert_response :success+ assert_select "input[name='setting[tinkoff_invest_api_key]']"+ assert_includes response.body, I18n.t("settings.hostings.tinkoff_invest_settings.moex_only_notice")+ end+ ensure+ Setting.securities_providers = ""+ Setting.tinkoff_invest_api_key = nil+ endenddiff --git a/test/models/setting_test.rb b/test/models/setting_test.rbindex cdadb0458b..ea63d7d032 100644--- a/test/models/setting_test.rb+++ b/test/models/setting_test.rb@@ -131,4 +131,29 @@ class SettingTest < ActiveSupport::TestCaseassert_nil resultassert_equal "test-model", Setting.openai_modelend++ test "enabled_securities_providers falls back to twelve_data when nothing is configured" do+ with_env_overrides("SECURITIES_PROVIDERS" => nil, "SECURITIES_PROVIDER" => nil) do+ assert_equal [ "twelve_data" ], Setting.enabled_securities_providers+ end+ end++ test "enabled_securities_providers returns an empty list when explicitly cleared, not the legacy default" do+ original_providers = Setting.securities_providers+ original_provider = Setting.securities_provider++ Setting.securities_providers = ""+ Setting.securities_provider = ""++ assert_equal [], Setting.enabled_securities_providers+ ensure+ # Restore whatever was there before this test, not a hardcoded value —+ # and restore via assignment (not nil) either way, since+ # rails-settings-cached's cache layer doesn't reliably invalidate on+ # delete within a single test process, so a later test can still read+ # back the just-deleted blank override instead of falling through to+ # the field's default.+ Setting.securities_providers = original_providers.presence || ""+ Setting.securities_provider = original_provider.presence || "twelve_data"+ endend