Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
43.8 KB
Raw Blame
require "test_helper"
require_relative "../../support/financekit_test_helper"

class Settings::ProvidersControllerTest < ActionDispatch::IntegrationTest
  include ActiveJob::TestHelper
  include FinancekitTestHelper

  setup do
    # Exercise controller jobs independently of the daily login sync.
    families(:dylan_family).update!(auto_sync_on_login: false)
    ensure_tailwind_build
    # Provider credentials are instance-wide, so saving them takes a super
    # admin. Promote the fixture admin rather than switching users so the
    # family-scoped assertions below keep using dylan_family.
    users(:family_admin).update!(role: :super_admin)
    sign_in users(:family_admin)

    # Ensure provider adapters are loaded for all tests
    Provider::Factory.ensure_adapters_loaded
  end

  test "Apple Wallet is available through a disabled App Store button" do
    get settings_providers_url

    assert_response :success
    assert_select "div[data-provider-name='apple wallet'][data-provider-region='us / uk'][data-provider-kind='bank']" do
      assert_select "button[disabled]", text: "App Store"
      assert_select "span[title='Coming soon!']"
      assert_select "a", count: 0
      assert_select "p", text: "US / UK · Bank"
    end
  end

  test "linked Wallet accounts appear with upload and import summaries" do
    financekit_setup
    @item.update!(last_accepted_at: 2.hours.ago, last_imported_at: 1.hour.ago)

    get settings_providers_url

    assert_response :success
    assert_select "[data-provider-name='apple wallet']", count: 0
    assert_select "details#financekit-connection" do
      assert_select "a[href=?]", account_path(@source.account), text: "Test Wallet"
      assert_select "span", text: "Sync active"
      assert_select "dt", text: "Last accepted by Sure"
      assert_select "dt", text: "Last imported into your family"
      assert_select "time[datetime=?]", @item.last_accepted_at.iso8601
      assert_select "time[datetime=?]", @item.last_imported_at.iso8601
      assert_select "form", count: 0
    end
    assert_select "form[action=?]", sync_provider_settings_providers_path(provider_key: "financekit"), count: 0
    assert_includes @controller.view_assigns["connected"].map { |entry| entry[:provider_key] }, "financekit"
  end

  test "Wallet repairs retain linked accounts and missing timestamps show Not yet" do
    financekit_setup
    @item.mark_repair!("test_repair")

    get settings_providers_url

    assert_response :success
    assert_select "details#financekit-connection" do
      assert_select "span", text: "Repair required — open the Sure iOS app"
      assert_select "dd", text: "Not yet", count: 2
      assert_select "a", text: "Test Wallet"
    end
    assert_includes @controller.view_assigns["needs_attention"].map { |entry| entry[:provider_key] }, "financekit"
  end

  test "revoked Wallet connections return to available providers" do
    financekit_setup
    @item.disconnect!

    get settings_providers_url

    assert_response :success
    assert_select "details#financekit-connection", count: 0
    assert_select "[data-provider-name='apple wallet'] button[disabled]", text: "App Store"
  end

  test "Wallet-only connections never offer Sync all including when repair is needed" do
    Provider::PlaidAdapter.configuration.stubs(:configured?).returns(false)
    Provider::PlaidEuAdapter.configuration.stubs(:configured?).returns(false)
    users(:empty).update!(family: Family.create!(name: "Wallet only", currency: "USD"))
    financekit_setup(user: users(:empty))
    sign_in @user

    [ "active", "repair_required" ].each do |status|
      @item.update!(status: status)
      get settings_providers_url

      assert_response :success
      assert_select "details#financekit-connection"
      connections = @controller.view_assigns.values_at("connected", "needs_attention").flatten
      assert_equal [ "financekit" ], connections.map { |entry| entry[:provider_key] }
      assert_select "form[action=?]", sync_all_settings_providers_path, count: 0
    end

    @family.simplefin_items.create!(name: "Test bank", access_url: "https://example.com/access")
    get settings_providers_url
    assert_response :success
    assert_select "form[action=?]", sync_all_settings_providers_path
  end

  test "Wallet settings retain disabled accounts but exclude pending deletion" do
    financekit_setup
    @source.account.update!(status: "disabled")
    get settings_providers_url
    assert_response :success
    assert_select "details#financekit-connection a[href=?]", account_path(@source.account)

    @source.account.update!(status: "pending_deletion")
    get settings_providers_url
    assert_response :success
    assert_select "details#financekit-connection", count: 0
    assert_select "a[href=?]", account_path(@source.account), count: 0
    assert_select "[data-provider-name='apple wallet']"
  end

  test "Wallet summary excludes another family's connections" do
    financekit_setup(user: users(:empty))

    get settings_providers_url

    assert_response :success
    assert_select "details#financekit-connection", count: 0
    assert_select "[data-provider-name='apple wallet']"
  end

  test "Wallet summary excludes accounts the current admin cannot access" do
    financekit_setup
    @source.account.account_shares.destroy_all
    @source.account.update!(owner: users(:family_member))

    get settings_providers_url

    assert_response :success
    assert_select "details#financekit-connection", count: 0
    assert_select "[data-provider-name='apple wallet']"
  end

  test "GET /settings/bank_sync redirects permanently to /settings/providers" do
    get "/settings/bank_sync"
    assert_redirected_to "/settings/providers"
    assert_equal 301, response.status
  end

  test "can access when self hosting is disabled (managed mode)" do
    Rails.configuration.stubs(:app_mode).returns("managed".inquiry)
    get settings_providers_url
    assert_response :success

    patch settings_providers_url, params: { setting: { plaid_client_id: "test123" } }
    assert_redirected_to settings_providers_url
  end

  test "a family admin cannot change instance-wide provider credentials in managed mode" do
    Rails.configuration.stubs(:app_mode).returns("managed".inquiry)
    sign_in users(:empty)

    patch settings_providers_url, params: { setting: { plaid_client_id: "attacker", plaid_secret: "attacker-secret" } }

    assert_redirected_to settings_providers_url
    assert_equal I18n.t("settings.providers.not_authorized"), flash[:alert]
    assert_nil Setting["plaid_client_id"]
    assert_nil Setting["plaid_secret"]
  end

  test "a family admin cannot change provider credentials on a self-hosted instance with several families" do
    with_self_hosting do
      sign_in users(:empty)

      patch settings_providers_url, params: { setting: { plaid_client_id: "attacker" } }

      assert_redirected_to settings_providers_url
      assert_nil Setting["plaid_client_id"]
    end
  end

  test "the admin of the only family on a self-hosted instance can change provider credentials" do
    with_self_hosting do
      Family.stubs(:count).returns(1)
      sign_in users(:empty)

      patch settings_providers_url, params: { setting: { plaid_client_id: "own-client-id" } }

      assert_redirected_to settings_providers_url
      assert_equal "own-client-id", Setting["plaid_client_id"]
    end
  ensure
    Setting["plaid_client_id"] = nil
  end

  test "instance-wide provider forms are only offered to those who may save them" do
    instance_wide_forms = Provider::ConfigurationRegistry.all.map do |config|
      connect_form_settings_providers_path(provider_key: config.provider_key)
    end

    get settings_providers_url
    assert instance_wide_forms.any? { |path| response.body.include?(path) }

    sign_in users(:empty)

    get settings_providers_url
    assert_response :success
    assert instance_wide_forms.none? { |path| response.body.include?(path) }

    get connect_form_settings_providers_path(provider_key: "plaid")
    assert_redirected_to settings_providers_path
    assert_equal I18n.t("settings.providers.not_authorized"), flash[:alert]
  end

  test "should get show when self hosting is enabled" do
    with_self_hosting do
      get settings_providers_url
      assert_response :success
    end
  end

  # A panel missing from family_panel_items reports :ok forever, however badly its
  # connection is doing: compute_provider_sync_health skips any key whose items are
  # absent, so no error badge ever reaches the page.
  test "a failed monobank sync surfaces as an error on the providers page" do
    monobank_items(:one).syncs.create!(status: :failed)

    get settings_providers_url
    assert_response :success

    health = @controller.view_assigns["provider_sync_health"]["monobank"]
    assert health.present?, "monobank is missing from the computed sync health"
    assert health[:error], "monobank sync health should report an error"

    needs_attention = @controller.view_assigns["needs_attention"]
    assert_includes needs_attention.map { |entry| entry[:provider_key] }, "monobank"
  end

  test "shows configured Brex connections in bank sync settings" do
    get settings_providers_url

    assert_response :success
    assert_includes response.body, "Brex"
    assert_includes response.body, "Test Brex Connection"
    assert_includes response.body, "brex-providers-panel"
  end

  test "shows Brex as available when family has no Brex connections" do
    sign_in users(:empty)

    get settings_providers_url

    assert_response :success
    assert_includes response.body, "Brex"
    assert_includes response.body, I18n.t("settings.providers.taglines.brex")
    assert_includes response.body, connect_form_settings_providers_path(provider_key: "brex")
    refute_includes response.body, "Test Brex Connection"
  end

  test "lists active Wise and CoinSpot connections under your connections" do
    get settings_providers_url

    assert_response :success
    %w[wise coinspot].each do |key|
      assert_select "details##{key}-connection"
      assert_select "a[href=?]", connect_form_settings_providers_path(provider_key: key), count: 0
    end
  end

  # Row panels used to sit in a frame of their own. A redirect to a page
  # without that frame showed "Content missing", and a stream to
  # "<key>-providers-panel" replaced the frame when the two shared that id.
  test "connection rows post from the page and leave panel ids unique" do
    get settings_providers_url

    assert_response :success
    assert_operator css_select("details[id$='-connection']").size, :>, 1
    assert_select "details[id$='-connection'] turbo-frame", count: 0

    panel_ids = css_select("[id$='-providers-panel']").map { |element| element["id"] }
    assert_equal panel_ids.uniq, panel_ids
  end

  # Saves and errors from a row or the drawer re-render the panel by replacing
  # "<turbo_id>-providers-panel". SnapTrade never streams to it.
  test "every streamed panel renders the root its streams replace" do
    Settings::ProvidersController::FAMILY_PANELS.reject { |panel| panel[:key] == "snaptrade" }.each do |panel|
      get connect_form_settings_providers_url(provider_key: panel[:key])

      assert_response :success
      assert_select "##{panel[:turbo_id]}-providers-panel", 1, "#{panel[:key]} panel root"
    end
  end

  test "lists Wise and CoinSpot as available when the family has no connections" do
    sign_in users(:empty)

    get settings_providers_url

    assert_response :success
    %w[wise coinspot].each do |key|
      assert_select "details##{key}-connection", count: 0
      assert_select "a[href=?]", connect_form_settings_providers_path(provider_key: key)
    end
  end

  # The provider card reads its tagline with `default: nil`, so a missing key
  # renders a bare name rather than failing. Up shipped that way.
  test "every family panel provider has an English tagline" do
    missing = Settings::ProvidersController::FAMILY_PANEL_KEYS.reject do |key|
      I18n.exists?("settings.providers.taglines.#{key}", :en)
    end

    assert_empty missing
  end

  test "sync all control submits with POST" do
    SimplefinItem.create!(
      family: families(:dylan_family),
      name: "Test SimpleFIN Sync All Control",
      access_url: "https://bridge.simplefin.org/simplefin/access"
    )

    with_self_hosting do
      get settings_providers_url
      assert_response :success
      assert_select "form[action=?][method=?]", sync_all_settings_providers_path, "post"
    end
  end

  test "correctly identifies declared vs dynamic fields" do
    with_self_hosting do
      # plaid_client_id is a dynamic field (not defined in Setting)
      refute Setting.singleton_class.method_defined?(:plaid_client_id=),
        "plaid_client_id= should NOT be defined on Setting's singleton class"

      assert Setting.singleton_class.method_defined?(:plaid_secret=)
      assert Setting.singleton_class.method_defined?(:plaid_eu_secret=)
    end
  end

  test "updates dynamic provider fields using batch update" do
    # plaid_client_id is a dynamic field, stored as an individual entry
    with_self_hosting do
      # Clear any existing plaid settings
      Setting["plaid_client_id"] = nil

      patch settings_providers_url, params: {
        setting: { plaid_client_id: "test_client_id" }
      }

      assert_redirected_to settings_providers_url
      assert_equal "test_client_id", Setting["plaid_client_id"]
    end
  end

  test "batches declared and dynamic fields from same provider atomically" do
    # Test that multiple fields from Plaid are updated as individual entries
    with_self_hosting do
      # Clear existing fields
      Setting["plaid_client_id"] = nil
      Setting["plaid_secret"] = nil
      Setting["plaid_environment"] = nil

      patch settings_providers_url, params: {
        setting: {
          plaid_client_id: "new_client_id",
          plaid_secret: "new_secret",
          plaid_environment: "production"
        }
      }

      assert_redirected_to settings_providers_url

      # All three should be present as individual entries
      assert_equal "new_client_id", Setting["plaid_client_id"]
      assert_equal "new_secret", Setting["plaid_secret"]
      assert_equal "production", Setting["plaid_environment"]
    end
  end

  test "batches provider fields from multiple providers atomically" do
    # Test that fields from different providers are stored as individual entries
    with_self_hosting do
      # Clear existing fields
      Setting["plaid_client_id"] = nil
      Setting["plaid_secret"] = nil
      Setting["plaid_eu_client_id"] = nil
      Setting["plaid_eu_secret"] = nil

      patch settings_providers_url, params: {
        setting: {
          plaid_client_id: "plaid_client",
          plaid_secret: "plaid_secret",
          plaid_eu_client_id: "plaid_eu_client",
          plaid_eu_secret: "plaid_eu_secret"
        }
      }

      assert_redirected_to settings_providers_url

      # All fields should be present
      assert_equal "plaid_client", Setting["plaid_client_id"]
      assert_equal "plaid_secret", Setting["plaid_secret"]
      assert_equal "plaid_eu_client", Setting["plaid_eu_client_id"]
      assert_equal "plaid_eu_secret", Setting["plaid_eu_secret"]
    end
  end

  test "preserves existing dynamic fields when updating new ones" do
    # Test that updating some fields doesn't overwrite other existing fields
    with_self_hosting do
      # Set initial fields
      Setting["existing_field_1"] = "value1"
      Setting["plaid_client_id"] = "old_client_id"

      # Update one field and add a new one
      patch settings_providers_url, params: {
        setting: {
          plaid_client_id: "new_client_id",
          plaid_secret: "new_secret"
        }
      }

      assert_redirected_to settings_providers_url

      # Existing unrelated field should still be there
      assert_equal "value1", Setting["existing_field_1"]

      # Updated field should have new value
      assert_equal "new_client_id", Setting["plaid_client_id"]

      # New field should be added
      assert_equal "new_secret", Setting["plaid_secret"]
    end
  end

  test "skips placeholder values for secret fields" do
    with_self_hosting do
      # Set an initial secret value
      Setting["plaid_secret"] = "real_secret"

      # Try to update with placeholder
      patch settings_providers_url, params: {
        setting: {
          plaid_client_id: "new_client_id",
          plaid_secret: "********"  # Placeholder value
        }
      }

      assert_redirected_to settings_providers_url

      # Client ID should be updated
      assert_equal "new_client_id", Setting["plaid_client_id"]

      # Secret should remain unchanged
      assert_equal "real_secret", Setting["plaid_secret"]
    end
  end

  test "existing provider forms save and mask declared Plaid secrets" do
    %w[plaid plaid_eu].each do |provider|
      key = "#{provider}_secret"
      secret = "synthetic-#{provider}-secret"
      patch settings_providers_url, params: { setting: {
        "#{provider}_client_id" => "synthetic-client", key => secret
      } }

      assert_redirected_to settings_providers_url
      ciphertext = Setting.find_by!(var: key).value
      refute_includes ciphertext, secret
      assert_equal secret, Setting[key]
      assert_not Setting.exists?(var: "dynamic:#{key}")

      get connect_form_settings_providers_path(provider_key: provider)
      assert_response :success
      assert_select "input[name=?][type=password][value=?]", "setting[#{key}]", "********"
      refute_includes response.body, secret
      refute_includes response.body, ciphertext

      patch settings_providers_url, params: { setting: { key => "********" } }
      assert_redirected_to settings_providers_url
      assert_equal ciphertext, Setting.find_by!(var: key).value

      patch settings_providers_url, params: { setting: { key => "" } }
      assert_redirected_to settings_providers_url
      assert_nil Setting[key]
    end
  end

  test "converts blank values to nil and removes from dynamic_fields" do
    with_self_hosting do
      # Set initial values
      Setting["plaid_client_id"] = "old_value"
      assert_equal "old_value", Setting["plaid_client_id"]
      assert Setting.key?("plaid_client_id")

      patch settings_providers_url, params: {
        setting: { plaid_client_id: "  " }  # Blank string with spaces
      }

      assert_redirected_to settings_providers_url
      assert_nil Setting["plaid_client_id"]
      # Entry should be removed, not just set to nil
      refute Setting.key?("plaid_client_id"),
        "nil values should delete the entry"
    end
  end

  test "handles sequential updates to different dynamic fields safely" do
    # This test simulates what would happen if two requests tried to update
    # different dynamic fields sequentially. With individual entries,
    # all changes should be preserved without conflicts.
    with_self_hosting do
      Setting["existing_field"] = "existing_value"

      # Simulate first request updating plaid fields
      patch settings_providers_url, params: {
        setting: {
          plaid_client_id: "client_id_1",
          plaid_secret: "secret_1"
        }
      }

      # Existing field should still be there
      assert_equal "existing_value", Setting["existing_field"]

      # New fields should be added
      assert_equal "client_id_1", Setting["plaid_client_id"]
      assert_equal "secret_1", Setting["plaid_secret"]

      # Simulate second request updating different plaid fields
      patch settings_providers_url, params: {
        setting: {
          plaid_environment: "production"
        }
      }

      # All previously set fields should still be there
      assert_equal "existing_value", Setting["existing_field"]
      assert_equal "client_id_1", Setting["plaid_client_id"]
      assert_equal "secret_1", Setting["plaid_secret"]
      assert_equal "production", Setting["plaid_environment"]
    end
  end

  test "only processes valid configuration fields" do
    with_self_hosting do
      # Try to update a field that doesn't exist in any provider configuration
      patch settings_providers_url, params: {
        setting: {
          plaid_client_id: "valid_field",
          fake_field_that_does_not_exist: "should_be_ignored"
        }
      }

      assert_redirected_to settings_providers_url

      # Valid field should be updated
      assert_equal "valid_field", Setting["plaid_client_id"]

      # Invalid field should not be stored
      assert_nil Setting["fake_field_that_does_not_exist"]
    end
  end

  test "calls reload_configuration on updated providers" do
    with_self_hosting do
      # Mock the adapter class to verify reload_configuration is called
      Provider::PlaidAdapter.expects(:reload_configuration).once

      patch settings_providers_url, params: {
        setting: { plaid_client_id: "new_client_id" }
      }

      assert_redirected_to settings_providers_url
    end
  end

  test "reloads configuration for multiple providers when updated" do
    with_self_hosting do
      # Both Plaid providers (US and EU) should have their configuration reloaded
      Provider::PlaidAdapter.expects(:reload_configuration).once
      Provider::PlaidEuAdapter.expects(:reload_configuration).once

      patch settings_providers_url, params: {
        setting: {
          plaid_client_id: "plaid_client",
          plaid_eu_client_id: "plaid_eu_client"
        }
      }

      assert_redirected_to settings_providers_url
    end
  end

  test "logs errors when update fails" do
    with_self_hosting do
      # Test that errors during update are properly logged and handled gracefully
      # We'll force an error by making the []= method raise
      Setting.expects(:[]=).with("plaid_client_id", "test").raises(StandardError.new("Database error")).once

      # Mock logger to verify error is logged (pin both the exception class
      # name and the message so a regression that drops one still fails).
      Rails.logger.expects(:error).with(regexp_matches(/Failed to update provider settings: StandardError - Database error/)).once

      patch settings_providers_url, params: {
        setting: { plaid_client_id: "test" }
      }

      # Controller should handle the error gracefully with generic message (no internal details)
      assert_response :unprocessable_entity
      assert_equal "Failed to update provider settings. Please try again.", flash[:alert]
    end
  end

  test "shows no changes message when no fields are updated" do
    with_self_hosting do
      # Only send a secret field with placeholder value (which gets skipped)
      Setting["plaid_secret"] = "existing_secret"

      patch settings_providers_url, params: {
        setting: { plaid_secret: "********" }
      }

      assert_redirected_to settings_providers_url
      assert_equal "No changes were made", flash[:notice]
    end
  end

  test "POST sync_all enqueues SyncAllProvidersJob" do
    SimplefinItem.create!(
      family: families(:dylan_family),
      name: "Test SimpleFIN Sync All",
      access_url: "https://bridge.simplefin.org/simplefin/access"
    )
    families(:dylan_family).update_column(:last_sync_all_attempted_at, nil)

    assert_enqueued_with(job: SyncAllProvidersJob) do
      post sync_all_settings_providers_path
    end

    assert_redirected_to settings_providers_path

    follow_redirect!
    assert_response :success
    assert_match(/Syncing all connected providers/i, response.body)
  end

  test "POST sync_all respects recent sync throttle" do
    families(:dylan_family).update_column(:last_sync_all_attempted_at, Time.current)

    assert_no_enqueued_jobs only: SyncAllProvidersJob do
      post sync_all_settings_providers_path
    end

    assert_redirected_to settings_providers_path
    assert_equal I18n.t("settings.providers.sync_all_recently"), flash[:notice]
  end

  test "POST sync for simplefin without an active Simplefin sync enqueues SyncJob" do
    item = SimplefinItem.create!(
      family: families(:dylan_family),
      name: "Test SimpleFIN Per Row Sync",
      access_url: "https://bridge.simplefin.org/simplefin/access"
    )
    Sync.where(syncable_type: "SimplefinItem", syncable_id: item.id).delete_all

    assert_enqueued_jobs 1, only: SyncJob do
      post sync_provider_settings_providers_path(provider_key: "simplefin")
    end

    assert_redirected_to settings_providers_path

    follow_redirect!
    assert_response :success
    assert_match(/Sync started/i, response.body)
  end

  test "POST sync for brex without an active Brex sync enqueues SyncJob" do
    item = brex_items(:one)
    Sync.where(syncable_type: "BrexItem", syncable_id: item.id).delete_all

    assert_enqueued_jobs 1, only: SyncJob do
      post sync_provider_settings_providers_path(provider_key: "brex")
    end

    assert_redirected_to settings_providers_path

    follow_redirect!
    assert_response :success
    assert_match(/Sync started/i, response.body)
  end

  test "GET show includes Interactive Brokers in bank sync providers" do
    get settings_providers_url

    assert_response :success
    assert_match(/Interactive Brokers/i, response.body)
    assert_match(/Flex Query/i, response.body)
  end

  test "GET show warns on configured provider forms when self-hosted encryption keys are not explicitly configured" do
    Setting["plaid_client_id"] = "test-client-id"
    Setting["plaid_secret"] = "test-secret"
    ActiveRecordEncryptionConfig.stubs(:ready?).returns(false)

    with_self_hosting do
      get settings_providers_url

      assert_response :success
      assert_includes response.body, I18n.t("settings.providers.provider_setup_encryption_warning.title")
      assert_select 'a[href=?][target="_blank"][rel="noopener noreferrer"]',
                    "https://github.com/we-promise/sure/blob/main/docs/hosting/docker.md#encrypting-sensitive-data",
                    minimum: 1
      assert_includes response.body, I18n.t("settings.providers.provider_setup_encryption_warning.message")
      assert_includes response.body, I18n.t("settings.providers.drawer_trust_statement_encryption_unconfigured")
    end
  ensure
    Setting["plaid_client_id"] = nil
    Setting["plaid_secret"] = nil
  end

  test "GET show hides provider form encryption warning in managed mode" do
    Setting["plaid_client_id"] = "test-client-id"
    Setting["plaid_secret"] = "test-secret"
    Rails.configuration.stubs(:app_mode).returns("managed".inquiry)
    ActiveRecordEncryptionConfig.stubs(:ready?).returns(false)

    get settings_providers_url

    assert_response :success
    refute_includes response.body, I18n.t("settings.providers.provider_setup_encryption_warning.title")
    assert_select 'a[href=?][target="_blank"][rel="noopener noreferrer"]',
                  "https://github.com/we-promise/sure/blob/main/docs/hosting/docker.md#encrypting-sensitive-data",
                  count: 0
    refute_includes response.body, I18n.t("settings.providers.provider_setup_encryption_warning.message")
    refute_includes response.body, I18n.t("settings.providers.drawer_trust_statement_encryption_unconfigured")
  ensure
    Setting["plaid_client_id"] = nil
    Setting["plaid_secret"] = nil
  end

  test "GET show warns when encryption keys are auto-derived from a known compromised secret" do
    Rails.configuration.stubs(:app_mode).returns("self_hosted".inquiry)
    ActiveRecordEncryptionConfig.stubs(:ready?).returns(true)
    ActiveRecordEncryptionConfig.stubs(:explicitly_configured?).returns(false)
    ActiveRecordEncryptionConfig.stubs(:using_known_compromised_secret_key_base?).returns(true)

    get settings_providers_url

    assert_response :success
    assert_includes response.body, I18n.t("settings.securities.show.compromised_secret_warning.title")
  end

  # Explicit keys are not derived from SECRET_KEY_BASE, so the banner's
  # "encryption keys are not actually secret" does not apply to them.
  test "GET show hides compromised secret warning when encryption keys are set explicitly" do
    Rails.configuration.stubs(:app_mode).returns("self_hosted".inquiry)
    ActiveRecordEncryptionConfig.stubs(:ready?).returns(true)
    ActiveRecordEncryptionConfig.stubs(:explicitly_configured?).returns(true)
    ActiveRecordEncryptionConfig.stubs(:using_known_compromised_secret_key_base?).returns(true)

    get settings_providers_url

    assert_response :success
    refute_includes response.body, I18n.t("settings.securities.show.compromised_secret_warning.title")
  end

  test "GET show hides compromised secret warning when encryption keys are not explicitly configured" do
    # The "not configured at all" warning takes priority; don't show both.
    Rails.configuration.stubs(:app_mode).returns("self_hosted".inquiry)
    ActiveRecordEncryptionConfig.stubs(:ready?).returns(false)
    ActiveRecordEncryptionConfig.stubs(:using_known_compromised_secret_key_base?).returns(true)

    get settings_providers_url

    assert_response :success
    refute_includes response.body, I18n.t("settings.securities.show.compromised_secret_warning.title")
  end

  test "GET show hides compromised secret warning in managed mode" do
    Rails.configuration.stubs(:app_mode).returns("managed".inquiry)
    ActiveRecordEncryptionConfig.stubs(:ready?).returns(true)
    ActiveRecordEncryptionConfig.stubs(:using_known_compromised_secret_key_base?).returns(true)

    get settings_providers_url

    assert_response :success
    refute_includes response.body, I18n.t("settings.securities.show.compromised_secret_warning.title")
  end

  test "GET connect_form renders Interactive Brokers panel" do
    get connect_form_settings_providers_path(provider_key: "ibkr")

    assert_response :success
    assert_match(/Interactive Brokers/i, response.body)
    assert_match(/Query ID/i, response.body)
  end

  test "GET connect_form does not render stored provider credentials" do
    coinstats = families(:dylan_family).coinstats_items.create!(name: "CoinStats", api_key: "cs-stored-key-91b0")
    indexa = indexa_capital_items(:configured_with_token)
    indexa.update!(password: "ix-stored-password-7a2d")

    {
      "coinstats" => [ coinstats_item_path(coinstats), { "coinstats_item[api_key]" => "cs-stored-key-91b0" } ],
      "indexa_capital" => [ indexa_capital_item_path(indexa), {
        "indexa_capital_item[api_token]" => indexa.api_token,
        "indexa_capital_item[password]" => "ix-stored-password-7a2d"
      } ]
    }.each do |provider_key, (form_action, secrets)|
      get connect_form_settings_providers_path(provider_key: provider_key)

      assert_response :success
      secrets.each do |field, stored|
        assert stored.present?, "#{field} needs a stored value for this test to mean anything"
        assert_select "form[action=?] input[name=?]", form_action, field do |inputs|
          assert_nil inputs.first["value"], "#{provider_key} renders #{field}"
        end
        assert_not_includes response.body, stored, "#{provider_key} leaks #{field}"
      end
    end
  end

  test "GET connect_form lists every Interactive Brokers connection" do
    families(:dylan_family).ibkr_items.create!(name: "IBKR Ireland", query_id: "Q1", token: "T1")
    families(:dylan_family).ibkr_items.create!(name: "IBKR US", query_id: "Q2", token: "T2")

    get connect_form_settings_providers_path(provider_key: "ibkr")

    assert_response :success
    assert_includes response.body, "IBKR Ireland"
    assert_includes response.body, "IBKR US"
    assert_includes response.body, I18n.t("settings.providers.ibkr_panel.add_connection")
  end

  test "GET connect_form warns when self-hosted encryption keys are not explicitly configured" do
    with_self_hosting do
      ActiveRecordEncryptionConfig.stubs(:ready?).returns(false)

      get connect_form_settings_providers_path(provider_key: "ibkr")

      assert_response :success
      assert_includes response.body, I18n.t("settings.providers.provider_setup_encryption_warning.title")
      assert_select 'a[href=?][target="_blank"][rel="noopener noreferrer"]',
                    "https://github.com/we-promise/sure/blob/main/docs/hosting/docker.md#encrypting-sensitive-data",
                    minimum: 1
      assert_includes response.body, I18n.t("settings.providers.provider_setup_encryption_warning.message")
      assert_includes response.body, I18n.t("settings.providers.drawer_trust_statement_encryption_unconfigured")
      refute_includes response.body, I18n.t("settings.providers.drawer_trust_statement")
    end
  end

  test "GET connect_form hides encryption warning when self-hosted encryption keys are configured" do
    with_self_hosting do
      ActiveRecordEncryptionConfig.stubs(:ready?).returns(true)

      get connect_form_settings_providers_path(provider_key: "ibkr")

      assert_response :success
      refute_includes response.body, I18n.t("settings.providers.provider_setup_encryption_warning.title")
      assert_select 'a[href=?][target="_blank"][rel="noopener noreferrer"]',
                    "https://github.com/we-promise/sure/blob/main/docs/hosting/docker.md#encrypting-sensitive-data",
                    count: 0
      refute_includes response.body, I18n.t("settings.providers.provider_setup_encryption_warning.message")
      assert_includes response.body, I18n.t("settings.providers.drawer_trust_statement")
      refute_includes response.body, I18n.t("settings.providers.drawer_trust_statement_encryption_unconfigured")
    end
  end

  test "GET connect_form hides encryption warning in managed mode" do
    Rails.configuration.stubs(:app_mode).returns("managed".inquiry)
    ActiveRecordEncryptionConfig.stubs(:ready?).returns(false)

    get connect_form_settings_providers_path(provider_key: "ibkr")

    assert_response :success
    refute_includes response.body, I18n.t("settings.providers.provider_setup_encryption_warning.title")
    assert_select 'a[href=?][target="_blank"][rel="noopener noreferrer"]',
                  "https://github.com/we-promise/sure/blob/main/docs/hosting/docker.md#encrypting-sensitive-data",
                  count: 0
    refute_includes response.body, I18n.t("settings.providers.provider_setup_encryption_warning.message")
    assert_includes response.body, I18n.t("settings.providers.drawer_trust_statement")
    refute_includes response.body, I18n.t("settings.providers.drawer_trust_statement_encryption_unconfigured")
  end

  test "GET connect_form uses shared encryption warning for provider panels" do
    with_self_hosting do
      ActiveRecordEncryptionConfig.stubs(:ready?).returns(false)

      get connect_form_settings_providers_path(provider_key: "wise")

      assert_response :success
      assert_includes response.body, I18n.t("settings.providers.provider_setup_encryption_warning.title")
      assert_select 'a[href=?][target="_blank"][rel="noopener noreferrer"]',
                    "https://github.com/we-promise/sure/blob/main/docs/hosting/docker.md#encrypting-sensitive-data",
                    minimum: 1
      assert_includes response.body, I18n.t("settings.providers.provider_setup_encryption_warning.message")
      refute_includes response.body, I18n.t("wise_items.provider_panel.encryption_warning.title")
      refute_includes response.body, I18n.t("wise_items.provider_panel.encryption_warning.message")
      assert_includes response.body, I18n.t("settings.providers.drawer_trust_statement_encryption_unconfigured")
      refute_includes response.body, I18n.t("settings.providers.drawer_trust_statement")
    end
  end

  test "GET show includes Trade Republic in bank sync providers" do
    get settings_providers_url

    assert_response :success
    assert_match(/Trade Republic/i, response.body)
    assert_match(/Approve the login in your Trade Republic app/i, response.body)
  end

  test "GET connect_form renders Trade Republic panel" do
    get connect_form_settings_providers_path(provider_key: "trade_republic")

    assert_response :success
    assert_match(/Trade Republic/i, response.body)
    assert_match(I18n.t("settings.providers.trade_republic_panel.phone_number_label"), response.body)
  end

  test "GET show hides provider form encryption warning when only runtime-generated keys are available" do
    # Regression test for issue #3142: self-hosted installs relying on the
    # SECRET_KEY_BASE auto-generation fallback (explicitly_configured? false,
    # runtime_configured? true) must NOT see the "encryption keys missing" warning.
    Setting["plaid_client_id"] = "test-client-id"
    Setting["plaid_secret"] = "test-secret"
    Rails.configuration.stubs(:app_mode).returns("self_hosted".inquiry)
    ActiveRecordEncryptionConfig.stubs(:explicitly_configured?).returns(false)
    ActiveRecordEncryptionConfig.stubs(:runtime_configured?).returns(true)

    get settings_providers_url

    assert_response :success
    refute_includes response.body, I18n.t("settings.providers.provider_setup_encryption_warning.title")
    refute_includes response.body, I18n.t("settings.providers.provider_setup_encryption_warning.message")
  ensure
    Setting["plaid_client_id"] = nil
    Setting["plaid_secret"] = nil
  end

  test "GET connect_form for snaptrade shows OAuth setup instructions when instance is not configured" do
    Provider::Snaptrade.stubs(:oauth_configured?).returns(false)

    get connect_form_settings_providers_path(provider_key: "snaptrade")

    assert_response :success
    assert_includes response.body, I18n.t("providers.snaptrade.oauth_setup_step_3")
    refute_includes response.body, I18n.t("providers.snaptrade.oauth_connect_button")
    refute_includes response.body, I18n.t("providers.snaptrade.oauth_status_ready")
  end

  test "GET index lists SnapTrade connections for an OAuth item only while the OAuth app is configured" do
    item = snaptrade_items(:configured_item)

    Provider::Snaptrade.stubs(:oauth_configured?).returns(false)
    get settings_providers_path(manage: 1)
    assert_response :success
    refute_includes response.body, connections_snaptrade_item_path(item)

    Provider::Snaptrade.stubs(:oauth_configured?).returns(true)
    get settings_providers_path(manage: 1)
    assert_response :success
    assert_includes response.body, connections_snaptrade_item_path(item)
  end

  test "GET connect_form for snaptrade offers the Personal API key form when instance is not configured" do
    Provider::Snaptrade.stubs(:oauth_configured?).returns(false)

    get connect_form_settings_providers_path(provider_key: "snaptrade")

    assert_response :success
    assert_select "form[action='#{personal_key_snaptrade_items_path}']" do
      assert_select "input[name='snaptrade_item[client_id]']"
      assert_select "input[name='snaptrade_item[consumer_key]'][type=password]"
    end
  end

  test "GET connect_form for snaptrade shows connect CTA when configured but item is not authorized" do
    sign_in users(:empty)
    Provider::Snaptrade.stubs(:oauth_configured?).returns(true)
    Provider::Snaptrade.stubs(:authorization_code_configured?).returns(true)

    get connect_form_settings_providers_path(provider_key: "snaptrade")

    assert_response :success
    assert_includes response.body, I18n.t("providers.snaptrade.oauth_connect_button")
    assert_includes response.body, I18n.t("providers.snaptrade.oauth_status_ready")
    # Both grants are available here, so the device code is offered alongside.
    assert_includes response.body, I18n.t("providers.snaptrade.oauth_device_button")
    refute_includes response.body, I18n.t("providers.snaptrade.oauth_status_authorized")
    refute_includes response.body, I18n.t("providers.snaptrade.oauth_reauthorize_button")
  end

  test "GET connect_form for snaptrade shows a Personal-key item as syncing when an OAuth app is configured later" do
    sign_in users(:empty)
    Provider::Snaptrade.stubs(:oauth_configured?).returns(true)
    Provider::Snaptrade.stubs(:authorization_code_configured?).returns(false)
    users(:empty).family.snaptrade_items.create!(name: "SnapTrade").apply_personal_key!(client_id: "c", consumer_key: "k")

    get connect_form_settings_providers_path(provider_key: "snaptrade")

    assert_response :success
    assert_includes response.body, I18n.t("providers.snaptrade.oauth_status_personal_key")
    refute_includes response.body, I18n.t("providers.snaptrade.oauth_status_ready_device")
  end

  test "GET connect_form for snaptrade asks to authorize a rejected Personal key once an OAuth app is configured" do
    sign_in users(:empty)
    Provider::Snaptrade.stubs(:oauth_configured?).returns(true)
    Provider::Snaptrade.stubs(:authorization_code_configured?).returns(false)
    item = users(:empty).family.snaptrade_items.create!(name: "SnapTrade")
    item.apply_personal_key!(client_id: "c", consumer_key: "k")
    item.update!(status: :requires_update)

    get connect_form_settings_providers_path(provider_key: "snaptrade")

    assert_response :success
    refute_includes response.body, I18n.t("providers.snaptrade.oauth_status_personal_key")
    assert_includes response.body, I18n.t("providers.snaptrade.oauth_status_ready_device")
  end

  test "GET connect_form for snaptrade offers only the device code without a confidential client" do
    sign_in users(:empty)
    Provider::Snaptrade.stubs(:oauth_configured?).returns(true)
    Provider::Snaptrade.stubs(:authorization_code_configured?).returns(false)

    get connect_form_settings_providers_path(provider_key: "snaptrade")

    assert_response :success
    assert_includes response.body, I18n.t("providers.snaptrade.oauth_device_button")
    assert_includes response.body, I18n.t("providers.snaptrade.oauth_status_ready_device")
    # The browser redirect needs a client secret this deployment does not have.
    refute_includes response.body, I18n.t("providers.snaptrade.oauth_connect_button")
  end

  test "GET connect_form for snaptrade shows authorized status and reauthorize CTA when item is connected" do
    # Default signed-in user (family_admin) belongs to dylan_family, which owns
    # the oauth-authorized `configured_item` fixture.
    Provider::Snaptrade.stubs(:oauth_configured?).returns(true)
    Provider::Snaptrade.stubs(:authorization_code_configured?).returns(true)

    get connect_form_settings_providers_path(provider_key: "snaptrade")

    assert_response :success
    assert_includes response.body, I18n.t("providers.snaptrade.oauth_status_authorized")
    assert_includes response.body, I18n.t("providers.snaptrade.oauth_reauthorize_button")
    assert_includes response.body, I18n.t("providers.snaptrade.manage_connections")
    refute_includes response.body, I18n.t("providers.snaptrade.oauth_connect_button")
  end

  test "POST sync for ibkr without an active Ibkr sync enqueues SyncJob" do
    # Keep the fixture sync on yesterday to cover runs that cross midnight.
    families(:dylan_family).syncs.update_all(created_at: 1.day.ago)
    item = ibkr_items(:configured_item)
    Sync.where(syncable_type: "IbkrItem", syncable_id: item.id).delete_all

    assert_enqueued_jobs 1, only: SyncJob do
      post sync_provider_settings_providers_path(provider_key: "ibkr")
    end

    assert_redirected_to settings_providers_path

    follow_redirect!
    assert_response :success
    assert_match(/Sync started/i, response.body)
  end

  test "non-admin users cannot update providers" do
    with_self_hosting do
      sign_in users(:family_member)

      patch settings_providers_url, params: {
        setting: { plaid_client_id: "test" }
      }

      assert_redirected_to root_path
      assert_equal "Not authorized", flash[:alert]

      # Value should not have changed
      assert_nil Setting["plaid_client_id"]
    end
  end

  test "uses singleton_class method_defined to detect declared fields" do
    with_self_hosting do
      # This test verifies the difference between respond_to? and singleton_class.method_defined?

      # openai_model is a declared field
      assert Setting.singleton_class.method_defined?(:openai_model=),
        "openai_model= should be defined on Setting's singleton class"
      assert Setting.respond_to?(:openai_model=),
        "respond_to? should return true for declared field"

      # plaid_client_id is a dynamic field
      refute Setting.singleton_class.method_defined?(:plaid_client_id=),
        "plaid_client_id= should NOT be defined on Setting's singleton class"
      refute Setting.respond_to?(:plaid_client_id=),
        "respond_to? should return false for dynamic field"

      # Both methods currently return the same result, but singleton_class.method_defined?
      # is more explicit and reliable for checking if a method is actually defined
    end
  end
end