Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
12.4 KB
Raw Blame
require "test_helper"

class AkahuItemsControllerTest < ActionDispatch::IntegrationTest
  setup do
    ensure_tailwind_build
    sign_in users(:family_admin)
    SyncJob.stubs(:perform_later)

    @family = families(:dylan_family)
    @akahu_item = AkahuItem.create!(
      family: @family,
      name: "Main Akahu",
      app_token: "akahu-app-credential",
      user_token: "akahu-user-credential"
    )
    @akahu_account = @akahu_item.akahu_accounts.create!(
      name: "Akahu Checking",
      account_id: "acc_123",
      currency: "NZD"
    )
    @account = accounts(:depository)
  end

  # Redirecting back to Bank sync would collapse the open connection row.
  test "sync from the panel re-renders the panel in place" do
    post sync_akahu_item_url(@akahu_item, source: "panel"), as: :turbo_stream

    assert_turbo_stream action: "replace", target: "akahu-providers-panel"
    assert_includes response.body, I18n.t("settings.providers.sync_provider_in_progress")
    assert @akahu_item.reload.syncing?
  end

  # The Accounts page's Sync button posts here too, without the panel's source.
  test "sync from the Accounts page goes back to it" do
    post sync_akahu_item_url(@akahu_item),
         headers: { "Accept" => "text/vnd.turbo-stream.html, text/html, application/xhtml+xml", "Referer" => accounts_url }

    assert_redirected_to accounts_url
  end

  test "setup_accounts preselects mapped account type for each account" do
    AkahuItemsController.any_instance.stubs(:fetch_akahu_accounts_from_api).returns(nil)

    @akahu_account.update!(account_type: "SAVINGS")
    get setup_accounts_akahu_item_url(@akahu_item)
    assert_response :success

    select = css_select("select[name='account_types[#{@akahu_account.id}]']").first
    assert select, "expected scoped account_types[#{@akahu_account.id}] select"
    assert_equal 1, css_select("label[for='#{select['id']}']").size,
                 "label for= should match the generated select id"

    selected_option = css_select("select[name='account_types[#{@akahu_account.id}]'] option[selected='selected']").first
    assert_equal "Depository", selected_option["value"]

    @akahu_account.update!(account_type: "FOREIGN")
    get setup_accounts_akahu_item_url(@akahu_item)
    assert_response :success
    selected_option = css_select("select[name='account_types[#{@akahu_account.id}]'] option[selected='selected']").first
    assert_equal "skip", selected_option["value"]
  end

  test "complete_account_setup uses Akahu account type suggestion subtype for investment accounts" do
    @akahu_account.update!(account_type: "KIWISAVER")

    assert_difference "Account.count", 1 do
      assert_difference "AccountProvider.count", 1 do
        post complete_account_setup_akahu_item_url(@akahu_item), params: {
          account_types: { @akahu_account.id.to_s => "Investment" }
        }
      end
    end

    assert_redirected_to accounts_path
    @akahu_account.reload
    created_account = @akahu_account.current_account
    assert_not_nil created_account
    assert_equal "Investment", created_account.accountable_type
    assert_equal "retirement", created_account.accountable.subtype
  end

  test "select accounts rejects unsafe return paths" do
    AkahuItemsController.any_instance.stubs(:fetch_akahu_accounts_from_api).returns(nil)

    unsafe_return_paths.each do |return_to|
      get select_accounts_akahu_items_url, params: {
        akahu_item_id: @akahu_item.id,
        accountable_type: "Depository",
        return_to: return_to
      }

      assert_response :success
      assert_select %(input[name="return_to"]) do |fields|
        assert fields.first["value"].blank?
      end
    end
  end

  test "select existing account rejects unsafe return paths" do
    AkahuItemsController.any_instance.stubs(:fetch_akahu_accounts_from_api).returns(nil)

    unsafe_return_paths.each do |return_to|
      get select_existing_account_akahu_items_url, params: {
        account_id: @account.id,
        akahu_item_id: @akahu_item.id,
        return_to: return_to
      }

      assert_response :success
      assert_select %(input[name="return_to"]) do |fields|
        assert fields.first["value"].blank?
      end
    end
  end

  test "select existing account preserves safe local return path" do
    return_to = "/accounts?tab=manual"
    AkahuItemsController.any_instance.stubs(:fetch_akahu_accounts_from_api).returns(nil)

    get select_existing_account_akahu_items_url, params: {
      account_id: @account.id,
      akahu_item_id: @akahu_item.id,
      return_to: return_to
    }

    assert_response :success
    assert_select %(input[name="return_to"][value="#{return_to}"])
  end

  test "link accounts rejects unsafe return path on no selection redirect" do
    post link_accounts_akahu_items_url, params: {
      akahu_item_id: @akahu_item.id,
      accountable_type: "Depository",
      return_to: "https://evil.example/accounts"
    }

    assert_redirected_to select_accounts_akahu_items_path(
      akahu_item_id: @akahu_item.id,
      accountable_type: "Depository",
      return_to: nil
    )
  end

  test "link accounts rejects unsafe return path after linking" do
    @akahu_account.update!(current_balance: 10)

    assert_difference "AccountProvider.count", 1 do
      post link_accounts_akahu_items_url, params: {
        akahu_item_id: @akahu_item.id,
        account_ids: [ @akahu_account.id ],
        accountable_type: "Depository",
        return_to: "https://evil.example/accounts"
      }
    end

    assert_redirected_to accounts_path
  end

  test "link existing account rejects unsafe return paths" do
    unsafe_return_paths.each_with_index do |return_to, index|
      account = @family.accounts.create!(
        name: "Manual Checking #{index}",
        balance: 0,
        currency: "NZD",
        accountable: Depository.new
      )
      akahu_account = @akahu_item.akahu_accounts.create!(
        name: "Akahu Checking #{index}",
        account_id: "acc_unsafe_#{index}",
        currency: "NZD"
      )

      assert_difference "AccountProvider.count", 1 do
        post link_existing_account_akahu_items_url, params: {
          account_id: account.id,
          akahu_item_id: @akahu_item.id,
          akahu_account_id: akahu_account.id,
          return_to: return_to
        }
      end

      assert_redirected_to accounts_path
    end
  end

  test "preload accounts uses requested Akahu connection" do
    second_item = AkahuItem.create!(
      family: @family,
      name: "Secondary Akahu",
      app_token: "akahu-app-credential-2",
      user_token: "akahu-user-credential-2"
    )

    AkahuItemsController.any_instance
      .expects(:fetch_akahu_accounts_from_api)
      .with(second_item)
      .returns(nil)

    get preload_accounts_akahu_items_url, params: { akahu_item_id: second_item.id }, as: :json

    assert_response :success
    assert_equal false, JSON.parse(response.body)["has_accounts"]
  end

  test "select accounts uses requested Akahu connection" do
    second_item = AkahuItem.create!(
      family: @family,
      name: "Secondary Akahu",
      app_token: "akahu-app-credential-2",
      user_token: "akahu-user-credential-2"
    )
    second_item.akahu_accounts.create!(
      name: "Secondary Checking",
      account_id: "acc_secondary",
      currency: "NZD"
    )
    AkahuItemsController.any_instance.stubs(:fetch_akahu_accounts_from_api).returns(nil)

    get select_accounts_akahu_items_url, params: { akahu_item_id: second_item.id, accountable_type: "Depository" }

    assert_response :success
    assert_includes response.body, "Secondary Checking"
    refute_includes response.body, "Akahu Checking"
  end

  test "link accounts uses requested Akahu connection" do
    second_item = AkahuItem.create!(
      family: @family,
      name: "Secondary Akahu",
      app_token: "akahu-app-credential-2",
      user_token: "akahu-user-credential-2"
    )
    second_account = second_item.akahu_accounts.create!(
      name: "Secondary Checking",
      account_id: "acc_secondary",
      currency: "NZD",
      current_balance: 42
    )

    assert_difference "Account.count", 1 do
      assert_difference "AccountProvider.count", 1 do
        post link_accounts_akahu_items_url, params: {
          akahu_item_id: second_item.id,
          account_ids: [ second_account.id ],
          accountable_type: "Depository"
        }
      end
    end

    assert_redirected_to accounts_path
    assert_equal second_account.id, AccountProvider.order(:created_at).last.provider_id
    assert_nil @akahu_account.reload.current_account
  end

  test "select existing account uses requested Akahu connection" do
    second_item = AkahuItem.create!(
      family: @family,
      name: "Secondary Akahu",
      app_token: "akahu-app-credential-2",
      user_token: "akahu-user-credential-2"
    )
    second_item.akahu_accounts.create!(
      name: "Secondary Checking",
      account_id: "acc_secondary",
      currency: "NZD"
    )
    AkahuItemsController.any_instance.stubs(:fetch_akahu_accounts_from_api).returns(nil)

    get select_existing_account_akahu_items_url, params: {
      account_id: @account.id,
      akahu_item_id: second_item.id
    }

    assert_response :success
    assert_includes response.body, "Secondary Checking"
    refute_includes response.body, "Akahu Checking"
  end

  # The row and drawer panels post from the page, not a frame. Turbo only
  # follows a 3xx, so a 422 redirect left the form with no feedback.
  test "invalid create outside a frame redirects to the providers page with a 303" do
    assert_no_difference "AkahuItem.count" do
      post akahu_items_url, params: { akahu_item: { app_token: "", user_token: "" } }
    end

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

  # Redirecting back to Bank sync collapses the open connection row.
  test "update from the page re-renders the panel in place" do
    patch akahu_item_url(@akahu_item),
          params: { akahu_item: { name: "Renamed Akahu" } },
          as: :turbo_stream

    assert_turbo_stream action: "replace", target: "akahu-providers-panel"
    assert_includes response.body, %(id="akahu-providers-panel")
    assert_equal "Renamed Akahu", @akahu_item.reload.name
  end

  test "invalid create from the page shows the error in the panel" do
    post akahu_items_url,
         params: { akahu_item: { app_token: "", user_token: "" } },
         as: :turbo_stream

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

  # The new connection belongs in Your connections, so the page reloads.
  test "create from the page still reloads Bank sync" do
    post akahu_items_url,
         params: { akahu_item: { app_token: "new-app-credential", user_token: "new-user-credential" } },
         as: :turbo_stream

    assert_redirected_to settings_providers_path
  end

  test "complete account setup hides raw creation errors from users" do
    raw_message = "raw provider failure with akahu-user-credential"
    AkahuItemsController.any_instance
      .stubs(:create_account_from_akahu)
      .raises(ActiveRecord::RecordNotSaved.new(raw_message))

    post complete_account_setup_akahu_item_url(@akahu_item), params: {
      account_types: { @akahu_account.id.to_s => "Depository" }
    }

    assert_redirected_to accounts_path
    assert_equal I18n.t("akahu_items.complete_account_setup.creation_failed"), flash[:alert]
    refute_includes flash[:alert], raw_message
  end

  test "select accounts leaves the Add Account modal for Settings at the top level" do
    AkahuItem.any_instance.stubs(:credentials_configured?).returns(false)

    get select_accounts_akahu_items_url, params: { akahu_item_id: @akahu_item.id }, headers: { "Turbo-Frame" => "modal" }

    # A redirect would be followed inside the frame and the flash lost.
    assert_response :success
    assert_select "turbo-frame#modal turbo-stream[action=redirect][target=?]", settings_providers_path
    assert_equal I18n.t("akahu_items.select_accounts.no_credentials_configured"), flash[:alert]
  end

  private

    def unsafe_return_paths
      [
        "https://evil.example/accounts",
        "http://evil.example/accounts",
        "//evil.example/accounts",
        "\\evil.example/accounts",
        "/\\evil.example/accounts",
        "/%2fevil.example/accounts",
        "/%2Fevil.example/accounts",
        "/%5cevil.example/accounts",
        "/%5Cevil.example/accounts",
        "/\naccounts",
        "/ accounts",
        "   "
      ]
    end
end