Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
5.8 KB
Raw Blame
# frozen_string_literal: true

require "test_helper"

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

    @family = families(:dylan_family)
    @redbark_item = redbark_items(:one)
    @redbark_account = redbark_accounts(:savings_account)
  end

  test "create adds a new redbark connection" do
    family = users(:empty).family
    sign_in users(:empty)

    assert_difference "RedbarkItem.count", 1 do
      post redbark_items_url, params: {
        redbark_item: { api_key: "rbk_live_new_key" }
      }
    end

    item = family.redbark_items.order(:created_at).last
    assert_equal "rbk_live_new_key", item.api_key
  end

  test "create with blank api key is rejected" do
    assert_no_difference "RedbarkItem.count" do
      post redbark_items_url, params: {
        redbark_item: { api_key: "" }
      }
    end
  end

  test "invalid create from the page shows the error in the panel" do
    post redbark_items_url,
         params: { redbark_item: { api_key: "" } },
         as: :turbo_stream

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

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

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

  test "update rotates the api key and clears requires_update" do
    @redbark_item.update!(status: :requires_update)

    patch redbark_item_url(@redbark_item), params: {
      redbark_item: { api_key: "rbk_live_rotated" }
    }

    @redbark_item.reload
    assert_equal "rbk_live_rotated", @redbark_item.api_key
    assert @redbark_item.good?
  end

  test "blank api key update is rejected and preserves the stored key" do
    original_key = @redbark_item.api_key

    patch redbark_item_url(@redbark_item), params: {
      redbark_item: { api_key: "" }
    }

    assert_equal original_key, @redbark_item.reload.api_key
  end

  test "sync enqueues a sync" do
    RedbarkItem.any_instance.stubs(:syncing?).returns(false)
    RedbarkItem.any_instance.expects(:sync_later).once

    post sync_redbark_item_url(@redbark_item)
    assert_response :redirect
  end

  test "destroy schedules the connection for deletion" do
    delete redbark_item_url(@redbark_item)

    assert_redirected_to settings_providers_path
    assert @redbark_item.reload.scheduled_for_deletion?
  end

  test "select_accounts drops hostless and unroutable return paths" do
    [ "///evil.example", "/%2fevil.example", "/\\evil.example", "/no-such-route-here" ].each do |return_to|
      get select_accounts_redbark_items_url, params: { return_to: return_to }

      assert_redirected_to setup_accounts_redbark_item_path(@redbark_item, return_to: nil)
    end
  end

  test "select_accounts keeps a routable same-site return path" do
    get select_accounts_redbark_items_url, params: { return_to: "/accounts" }

    assert_redirected_to setup_accounts_redbark_item_path(@redbark_item, return_to: "/accounts")
  end

  test "complete_account_setup creates and links an account" do
    assert_difference "Account.count", 1 do
      post complete_account_setup_redbark_item_url(@redbark_item), params: {
        accounts: { @redbark_account.id => { account_type: "depository" } }
      }
    end

    @redbark_account.reload
    assert @redbark_account.account_provider.present?
    assert_equal "Depository", @redbark_account.account.accountable_type
    assert_not @redbark_account.ignored?
  end

  test "complete_account_setup skip marks the account ignored" do
    assert_no_difference "Account.count" do
      post complete_account_setup_redbark_item_url(@redbark_item), params: {
        accounts: { @redbark_account.id => { account_type: "skip" } }
      }
    end

    assert @redbark_account.reload.ignored?
    assert_not @redbark_item.reload.unlinked_redbark_accounts.exists?(id: @redbark_account.id)
  end

  test "complete_account_setup persists the chosen sync_start_date on the account" do
    start_date = 6.months.ago.to_date

    assert_difference "Account.count", 1 do
      post complete_account_setup_redbark_item_url(@redbark_item), params: {
        accounts: { @redbark_account.id => { account_type: "depository", sync_start_date: start_date.to_s } }
      }
    end

    assert_equal start_date, @redbark_account.reload.sync_start_date
  end

  test "complete_account_setup without a sync_start_date leaves it blank" do
    post complete_account_setup_redbark_item_url(@redbark_item), params: {
      accounts: { @redbark_account.id => { account_type: "depository" } }
    }

    assert_nil @redbark_account.reload.sync_start_date
  end

  test "complete_account_setup rejects an out-of-range sync_start_date" do
    out_of_range_date = 8.years.ago.to_date

    assert_no_difference "Account.count" do
      post complete_account_setup_redbark_item_url(@redbark_item), params: {
        accounts: { @redbark_account.id => { account_type: "depository", sync_start_date: out_of_range_date.to_s } }
      }
    end

    assert_not @redbark_account.reload.account_provider.present?
    assert_nil @redbark_account.sync_start_date
  end

  test "setup_accounts pre-fills the stored sync_start_date instead of resetting it" do
    stored_date = 6.months.ago.to_date
    @redbark_account.update!(sync_start_date: stored_date)

    get setup_accounts_redbark_item_url(@redbark_item)

    assert_select "input#sync_start_date_#{@redbark_account.id}[value='#{stored_date}']"
  end
end