Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
10.0 KB
Raw Blame
require "test_helper"

class LunchflowItemsControllerTest < ActionDispatch::IntegrationTest
  setup do
    sign_in @user = users(:family_admin)
  end

  test "update from the page re-renders the panel in place instead of leaving for accounts" do
    patch lunchflow_item_url(lunchflow_items(:one)),
          params: { lunchflow_item: { name: "Renamed Lunch Flow" } },
          as: :turbo_stream

    assert_turbo_stream action: "replace", target: "lunchflow-providers-panel"
    assert_includes response.body, %(id="lunchflow-providers-panel")
    assert_equal "Renamed Lunch Flow", lunchflow_items(:one).reload.name
  end

  test "update from the page saves the import start date" do
    patch lunchflow_item_url(lunchflow_items(:one)),
          params: { lunchflow_item: { sync_start_date: "2026-01-15" } },
          as: :turbo_stream

    assert_turbo_stream action: "replace", target: "lunchflow-providers-panel"
    assert_includes response.body, %(value="2026-01-15")
    assert_equal Date.new(2026, 1, 15), lunchflow_items(:one).reload.sync_start_date.to_date
  end

  test "update from the page clears the import start date" do
    lunchflow_items(:one).update!(sync_start_date: Date.new(2026, 1, 15))

    patch lunchflow_item_url(lunchflow_items(:one)),
          params: { lunchflow_item: { sync_start_date: "" } },
          as: :turbo_stream

    assert_turbo_stream action: "replace", target: "lunchflow-providers-panel"
    assert_nil lunchflow_items(:one).reload.sync_start_date
  end

  test "invalid create from the page shows the error in the panel" do
    post lunchflow_items_url,
         params: { lunchflow_item: { name: "Second Lunch Flow", api_key: "" } },
         as: :turbo_stream

    assert_turbo_stream status: :unprocessable_entity, action: "replace", target: "lunchflow-providers-panel"
    assert_includes response.body, ERB::Util.html_escape("Api key can't be blank")
  end

  test "invalid non-Turbo update redirects back instead of rendering a missing template" do
    patch lunchflow_item_url(lunchflow_items(:one)),
          params: { lunchflow_item: { name: "" } },
          headers: { "HTTP_REFERER" => settings_providers_url }

    assert_redirected_to settings_providers_url
    assert_match "Name can't be blank", flash[:alert]
  end

  test "setup accounts renders German subtype options without English fallbacks" do
    ensure_tailwind_build
    @user.update!(locale: "de")

    get setup_accounts_lunchflow_item_url(lunchflow_items(:one))

    assert_response :success
    assert_select "title", text: "Lunch‑Flow-Konten einrichten"
    assert_select "option", text: "Untertyp auswählen"
    assert_select "option", text: "Typ auswählen"

    [
      "Nicht registriertes Anlagekonto",
      "FHSA (Sparkonto für den ersten Immobilienkauf)",
      "RDSP (registrierter Sparplan für Menschen mit Behinderung)",
      "DPSP (aufgeschobener Gewinnbeteiligungsplan)",
      "PRPP (gemeinschaftlicher registrierter Altersvorsorgeplan)",
      "LIF (lebenslanger Einkommensfonds)",
      "LRIF (gebundener Altersvorsorge-Einkommensfonds)",
      "PRIF (vorgeschriebener registrierter Altersvorsorge-Einkommensfonds)",
      "RLIF (beschränkter lebenslanger Einkommensfonds)",
      "Assurance Vie",
      "Eigenheimkredit",
      "Kreditlinie",
      "Unternehmenskredit"
    ].each do |label|
      assert_select "option", text: label
    end

    assert_select "option", text: "UGMA-Treuhandkonto"

    refute_includes response.body, "Select subtype"
    refute_includes response.body, "Select type"
    refute_includes response.body, "First Home Savings Account"
    refute_includes response.body, "UGMA Custodial Account"
  end

  test "setup accounts preserves the English title and placeholders" do
    ensure_tailwind_build
    @user.update!(locale: "en")

    get setup_accounts_lunchflow_item_url(lunchflow_items(:one))

    assert_response :success
    assert_select "title", text: "Set Up Lunch Flow Accounts"
    assert_select "option", text: "Select subtype"
    assert_select "option", text: "Select type"
  end

  test "account setup localizes an unexpected error in German" do
    @user.update!(locale: "de")
    lunchflow_account = lunchflow_accounts(:investment_account)
    Account.stubs(:create_and_sync).raises(StandardError, "Synthetic failure")

    post complete_account_setup_lunchflow_item_url(lunchflow_items(:one)), params: {
      account_types: { lunchflow_account.id => "Investment" },
      account_subtypes: { lunchflow_account.id => "brokerage" }
    }

    assert_redirected_to accounts_path
    assert_equal "Konten konnten nicht angelegt werden: Ein unerwarteter Fehler ist aufgetreten. Versuch es noch einmal.", flash[:alert]
  end

  test "account setup preserves the unexpected error in English" do
    @user.update!(locale: "en")
    lunchflow_account = lunchflow_accounts(:investment_account)
    Account.stubs(:create_and_sync).raises(StandardError, "Synthetic failure")

    post complete_account_setup_lunchflow_item_url(lunchflow_items(:one)), params: {
      account_types: { lunchflow_account.id => "Investment" },
      account_subtypes: { lunchflow_account.id => "brokerage" }
    }

    assert_redirected_to accounts_path
    assert_equal "Failed to create accounts: An unexpected error occurred", flash[:alert]
  end

  test "account selection localizes an unexpected error in German" do
    @user.update!(locale: "de")
    Provider::LunchflowAdapter.stubs(:build_provider).raises(StandardError, "Synthetic failure")

    get select_accounts_lunchflow_items_url

    assert_response :success
    assert_includes response.body, "Ein unerwarteter Fehler ist aufgetreten. Versuch es später noch einmal."
    refute_includes response.body, "An unexpected error occurred. Please try again later."
    assert I18n.exists?("lunchflow_items.api_error.unexpected_error", :de, fallback: false)
    assert_equal "An unexpected error occurred. Please try again later.",
                 I18n.t("lunchflow_items.api_error.unexpected_error", locale: :en, fallback: false)
  end

  test "existing-account selection localizes an unexpected error in German" do
    @user.update!(locale: "de")
    Provider::LunchflowAdapter.stubs(:build_provider).raises(StandardError, "Synthetic failure")

    get select_existing_account_lunchflow_items_url(account_id: accounts(:depository).id)

    assert_response :success
    assert_includes response.body, "Ein unerwarteter Fehler ist aufgetreten. Versuch es später noch einmal."
    refute_includes response.body, "An unexpected error occurred. Please try again later."
  end

  test "account selection preserves provider error details" do
    @user.update!(locale: "de")
    Provider::LunchflowAdapter.stubs(:build_provider).raises(
      Provider::Lunchflow::LunchflowError,
      "Provider-specific failure"
    )

    get select_accounts_lunchflow_items_url

    assert_response :success
    assert_includes response.body, "Provider-specific failure"
    refute_includes response.body, "Ein unerwarteter Fehler ist aufgetreten. Versuch es später noch einmal."
  end

  test "select accounts masks the iban to the last 4 characters" do
    provider = mock("lunchflow_provider")
    provider.stubs(:get_accounts).returns(accounts: [
      { id: "acc_1", name: "Checking", iban: "DE89370400440532013000", institution_name: "Test Bank", currency: "EUR", status: "active" } # pipelock:ignore IBAN
    ])
    Provider::LunchflowAdapter.stubs(:build_provider).returns(provider)

    get select_accounts_lunchflow_items_url

    assert_response :success
    assert_includes response.body, "•3000"
    refute_includes response.body, "DE89370400440532013000" # pipelock:ignore IBAN
  end

  test "select existing account masks the iban to the last 4 characters" do
    provider = mock("lunchflow_provider")
    provider.stubs(:get_accounts).returns(accounts: [
      { id: "acc_1", name: "Checking", iban: "DE89370400440532013000", institution_name: "Test Bank", currency: "EUR", status: "active" } # pipelock:ignore IBAN
    ])
    Provider::LunchflowAdapter.stubs(:build_provider).returns(provider)

    get select_existing_account_lunchflow_items_url(account_id: accounts(:depository).id)

    assert_response :success
    assert_includes response.body, "•3000"
    refute_includes response.body, "DE89370400440532013000" # pipelock:ignore IBAN
  end

  test "invalid non-Turbo create redirects instead of rendering a missing template" do
    assert_no_difference "LunchflowItem.count" do
      post lunchflow_items_url, params: {
        lunchflow_item: {
          name: "Invalid Lunchflow connection",
          api_key: ""
        }
      }
    end

    assert_redirected_to settings_providers_path
    assert_match "Api key can't be blank", flash[:alert]
  end

  test "complete_account_setup's stream lists only the manual accounts the viewer can access" do
    own, shared, private_account = create_manual_accounts_for_scope_test

    post complete_account_setup_lunchflow_item_url(lunchflow_items(:one)),
         params: {},
         headers: { "Turbo-Frame" => "modal" }

    assert_turbo_stream action: "update", target: "manual-accounts"
    assert_includes response.body, own.name
    assert_includes response.body, shared.name
    assert_not_includes response.body, private_account.name
  end

  private
    # The #manual-accounts list rebuilt in this response must match /accounts
    # for the viewer: their own manual accounts and those shared with them,
    # never a member's account that was not shared.
    def create_manual_accounts_for_scope_test
      admin = users(:family_admin)
      member = users(:family_member)
      build = ->(name, owner) {
        Account.create!(family: families(:dylan_family), owner: owner, name: name, currency: "USD", balance: 0,
                        accountable: Depository.create!(subtype: "checking"))
      }
      own = build.call("Admin Own Manual Marker", admin)
      shared = build.call("Member Shared Manual Marker", member)
      shared.account_shares.create!(user: admin, permission: "read_only")
      private_account = build.call("Member Private Manual Marker", member)
      [ own, shared, private_account ]
    end
end