Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
15.1 KB
Raw Blame
require "test_helper"

class EnableBankingAccount::ProcessorTest < ActiveSupport::TestCase
  setup do
    @family = families(:dylan_family)
    @account = accounts(:depository)
    @enable_banking_item = EnableBankingItem.create!(
      family: @family,
      name: "Test EB",
      country_code: "FR",
      application_id: "app_id",
      client_certificate: "cert"
    )
    @enable_banking_account = EnableBankingAccount.create!(
      enable_banking_item: @enable_banking_item,
      name: "Compte courant",
      uid: "hash_abc",
      currency: "EUR",
      current_balance: 1500.00
    )
    AccountProvider.create!(account: @account, provider: @enable_banking_account)
  end

  test "calls set_current_balance instead of direct account update" do
    EnableBankingAccount::Processor.new(@enable_banking_account).process

    assert_equal 1500.0, @account.reload.cash_balance
  end

  test "promotes the importer's evidence to balance_verified once the anchor is written" do
    @enable_banking_account.update!(balance_evidence_verified: true, balance_verified: false)

    EnableBankingAccount::Processor.new(@enable_banking_account).process

    assert @enable_banking_account.reload.balance_verified?
  end

  test "an unverified balance clears balance_verified when it becomes the anchor" do
    @enable_banking_account.update!(balance_evidence_verified: false, balance_verified: true)

    EnableBankingAccount::Processor.new(@enable_banking_account).process

    assert_not @enable_banking_account.reload.balance_verified?
  end

  test "keeps balance_verified when no balance was fetched and the anchor stays as it is" do
    @enable_banking_account.update_columns(current_balance: nil, balance_evidence_verified: false, balance_verified: true)

    EnableBankingAccount::Processor.new(@enable_banking_account).process

    assert @enable_banking_account.reload.balance_verified?
  end

  test "clears balance_verified when the balance never becomes the anchor" do
    @enable_banking_account.update!(balance_evidence_verified: true, balance_verified: true)
    Account.any_instance.stubs(:set_current_balance).raises(StandardError, "boom")

    assert_raises(StandardError) do
      EnableBankingAccount::Processor.new(@enable_banking_account).process
    end

    assert_not @enable_banking_account.reload.balance_verified?
  end

  test "updates account currency" do
    @enable_banking_account.update!(currency: "USD")

    EnableBankingAccount::Processor.new(@enable_banking_account).process

    assert_equal "USD", @account.reload.currency
  end

  test "does nothing when no linked account" do
    @account.account_providers.destroy_all

    result = EnableBankingAccount::Processor.new(@enable_banking_account).process
    assert_nil result
  end

  test "skips balance update when provider current_balance is nil" do
    @account.update!(cash_balance: 987.65)
    @enable_banking_account.update_columns(current_balance: nil)

    EnableBankingAccount::Transactions::Processor.any_instance.expects(:process).once

    assert_no_changes -> { @account.reload.cash_balance } do
      EnableBankingAccount::Processor.new(@enable_banking_account).process
    end
  end

  test "sets CC balance as absolute debt and tracks available_credit when limit is present" do
    cc_account = accounts(:credit_card)

    @enable_banking_account.update!(
      current_balance: 900.00,
      credit_limit: 1000.00,
      treat_balance_as_available_credit: true
    )
    relink_provider_to(cc_account)

    EnableBankingAccount::Processor.new(@enable_banking_account).process

    assert_equal 100.0, cc_account.reload.cash_balance
    if cc_account.accountable.respond_to?(:available_credit)
      assert_equal 1000.0, cc_account.accountable.reload.available_credit
    end
  end

  test "available credit mode uses manual limit when provider reports a non-positive limit" do
    cc_account = accounts(:credit_card)
    cc_account.accountable.update!(available_credit: 3000.00)
    relink_provider_to(cc_account)

    [ 0, -1 ].each do |provider_credit_limit|
      @enable_banking_account.update!(
        current_balance: 2995.32,
        credit_limit: provider_credit_limit,
        treat_balance_as_available_credit: true
      )

      EnableBankingAccount::Processor.new(@enable_banking_account).process

      assert_equal 4.68, cc_account.reload.cash_balance
      assert_equal 3000.0, cc_account.accountable.reload.available_credit
    end
  end

  test "when treat_balance_as_available_credit is true and card is overpaid, floors debt at zero" do
    cc_account = accounts(:credit_card)

    @enable_banking_account.update!(
      current_balance: 1050.00, # overpaid by 50
      credit_limit: 1000.00,
      treat_balance_as_available_credit: true
    )
    relink_provider_to(cc_account)

    EnableBankingAccount::Processor.new(@enable_banking_account).process

    assert_equal 0.0, cc_account.reload.cash_balance
    if cc_account.accountable.respond_to?(:available_credit)
      assert_equal 1000.0, cc_account.accountable.reload.available_credit
    end
  end

  test "when treat_balance_as_available_credit is true and API limit absent, derives debt from manually set available_credit" do
    cc_account = accounts(:credit_card)

    cc_account.accountable.update!(available_credit: 1000.0)

    @enable_banking_account.update!(
      current_balance: 900.00,
      credit_limit: nil,
      treat_balance_as_available_credit: true
    )

    relink_provider_to(cc_account)

    EnableBankingAccount::Processor.new(@enable_banking_account).process

    # The manually configured available_credit acts as the credit limit and
    # must survive the sync unchanged.
    assert_equal 100.0, cc_account.reload.cash_balance
    assert_equal 1000.0, cc_account.accountable.reload.available_credit
  end

  test "when treat_balance_as_available_credit is true but no limit is known, keeps existing balance" do
    cc_account = accounts(:credit_card)

    cc_account.accountable.update!(available_credit: nil)

    @enable_banking_account.update!(
      current_balance: 900.00,
      credit_limit: nil,
      treat_balance_as_available_credit: true
    )

    relink_provider_to(cc_account)

    balance_before = cc_account.cash_balance

    EnableBankingAccount::Processor.new(@enable_banking_account).process

    # The reported balance is available credit and there's no limit to reverse
    # from, so the debt is unknown. The existing balance must not be overwritten,
    # and available_credit must stay blank so a later sync can't mistake a
    # stale reported balance for a credit limit.
    assert_equal balance_before, cc_account.reload.cash_balance
    assert_nil cc_account.accountable.reload.available_credit
  end

  test "when treat_balance_as_available_credit is false, treats balance as absolute debt natively" do
    cc_account = accounts(:credit_card)

    # API sends current_balance as debt (e.g. 100) and credit limit (e.g. 1000)
    # Debt should remain 100, available credit becomes 900
    @enable_banking_account.update!(
      current_balance: 100.00,
      credit_limit: 1000.00,
      treat_balance_as_available_credit: false
    )
    relink_provider_to(cc_account)

    EnableBankingAccount::Processor.new(@enable_banking_account).process

    assert_equal 100.0, cc_account.reload.cash_balance
    if cc_account.accountable.respond_to?(:available_credit)
      assert_equal 900.0, cc_account.accountable.reload.available_credit
    end
  end

  test "default balance mode preserves remaining credit when provider limit is non-positive" do
    cc_account = accounts(:credit_card)
    cc_account.accountable.update!(available_credit: 3000.00)
    relink_provider_to(cc_account)

    [ 0, -1 ].each do |provider_credit_limit|
      @enable_banking_account.update!(
        current_balance: 100.00,
        credit_limit: provider_credit_limit,
        treat_balance_as_available_credit: false
      )

      EnableBankingAccount::Processor.new(@enable_banking_account).process

      assert_equal 100.0, cc_account.reload.cash_balance
      assert_equal 3000.0, cc_account.accountable.reload.available_credit
    end
  end

  test "sets CC balance to absolute debt when both limit and stored available_credit are absent" do
    cc_account = accounts(:credit_card)
    cc_account.accountable.update!(available_credit: nil)

    @enable_banking_account.update!(current_balance: 300.00, credit_limit: nil)

    relink_provider_to(cc_account)

    EnableBankingAccount::Processor.new(@enable_banking_account).process

    assert_equal 300.0, cc_account.reload.cash_balance
  end

  test "treat_balance_as_available_credit flag is a no-op on Loan accounts" do
    loan_account = accounts(:loan)

    # Even with the flag set to true, loans should only ever process as absolute debt
    @enable_banking_account.update!(
      current_balance: 50000.00,
      credit_limit: 100000.00,
      treat_balance_as_available_credit: true
    )

    relink_provider_to(loan_account)

    EnableBankingAccount::Processor.new(@enable_banking_account).process

    # Balance should match the absolute incoming balance, no credit limit math applied
    assert_equal 50000.0, loan_account.reload.cash_balance
  end

  test "excludes pending entries older than 8 days and marks the exclusion as automatic" do
    stale = create_pending_entry(external_id: "eb_stale_pending", date: 10.days.ago.to_date, amount: 42)
    recent = create_pending_entry(external_id: "eb_recent_pending", date: 3.days.ago.to_date, amount: 43)

    EnableBankingAccount::Processor.new(@enable_banking_account).process

    assert stale.reload.excluded?
    assert stale.auto_excluded_pending?
    assert_not recent.reload.excluded?
  end

  test "an automatically excluded pending is re-activated when the same id is delivered as booked" do
    stale = create_pending_entry(external_id: "eb_hold", date: 10.days.ago.to_date, amount: 80)
    EnableBankingAccount::Processor.new(@enable_banking_account).process
    assert stale.reload.excluded?

    Account::ProviderImportAdapter.new(@account).import_transaction(
      external_id: "eb_hold", amount: 80, currency: @account.currency, date: Date.current,
      name: "Hotel", source: "enable_banking", extra: { "enable_banking" => { "pending" => false } }
    )

    stale.reload
    assert_not stale.excluded?
    assert_not stale.transaction.pending?
    assert_not stale.auto_excluded_pending?
  end

  test "excluding or re-including an auto-excluded pending by hand ends its automatic state" do
    stale = create_pending_entry(external_id: "eb_toggle", date: 10.days.ago.to_date, amount: 82)
    EnableBankingAccount::Processor.new(@enable_banking_account).process
    assert stale.reload.auto_excluded_pending?

    stale.update!(excluded: false)
    stale.update!(excluded: true)

    assert_not stale.reload.auto_excluded_pending?
    Account::ProviderImportAdapter.new(@account).import_transaction(
      external_id: "eb_toggle", amount: 82, currency: @account.currency, date: Date.current,
      name: "Hotel", source: "enable_banking", extra: { "enable_banking" => { "pending" => false } }
    )
    assert stale.reload.excluded?
  end

  test "excluding an already auto-excluded pending by hand ends its automatic state" do
    stale = create_pending_entry(external_id: "eb_stale_page", date: 10.days.ago.to_date, amount: 85)
    EnableBankingAccount::Processor.new(@enable_banking_account).process
    assert stale.reload.auto_excluded_pending?

    # The user excludes it from a page loaded before the sync did.
    stale.update!(excluded: true)

    assert_not stale.reload.auto_excluded_pending?
    Account::ProviderImportAdapter.new(@account).import_transaction(
      external_id: "eb_stale_page", amount: 85, currency: @account.currency, date: Date.current,
      name: "Hotel", source: "enable_banking", extra: { "enable_banking" => { "pending" => false } }
    )
    assert stale.reload.excluded?
  end

  test "excluding by hand clears the mark even when the transaction was loaded before the sync" do
    stale = create_pending_entry(external_id: "eb_preloaded", date: 10.days.ago.to_date, amount: 86)
    loaded = Entry.includes(:entryable).find(stale.id)
    loaded.entryable.extra # loaded before the sync writes the mark

    EnableBankingAccount::Processor.new(@enable_banking_account).process
    assert stale.reload.auto_excluded_pending?

    loaded.update!(excluded: true)

    assert_not stale.reload.auto_excluded_pending?
  end

  test "a pending the user excluded stays excluded when it books" do
    stale = create_pending_entry(external_id: "eb_user_excluded", date: 10.days.ago.to_date, amount: 81)
    stale.update!(excluded: true)

    Account::ProviderImportAdapter.new(@account).import_transaction(
      external_id: "eb_user_excluded", amount: 81, currency: @account.currency, date: Date.current,
      name: "Hotel", source: "enable_banking", extra: { "enable_banking" => { "pending" => false } }
    )

    assert stale.reload.excluded?
  end

  test "an import-locked auto-excluded pending stays excluded when it books" do
    stale = create_pending_entry(external_id: "eb_locked", date: 10.days.ago.to_date, amount: 83)
    EnableBankingAccount::Processor.new(@enable_banking_account).process
    stale.reload.update_columns(import_locked: true)

    Account::ProviderImportAdapter.new(@account).import_transaction(
      external_id: "eb_locked", amount: 83, currency: @account.currency, date: Date.current,
      name: "Hotel", source: "enable_banking", extra: { "enable_banking" => { "pending" => false } }
    )

    assert stale.reload.excluded?
    assert stale.auto_excluded_pending?
  end

  test "excludes a pending entry once its booked twin exists" do
    pending = create_pending_entry(external_id: "eb_pending", date: 3.days.ago.to_date, amount: 25)
    @account.entries.create!(
      name: "Coffee", date: 1.day.ago.to_date, amount: 25, currency: @account.currency,
      external_id: "eb_booked", source: "enable_banking",
      entryable: Transaction.new(extra: { "enable_banking" => { "pending" => false } })
    )

    EnableBankingAccount::Processor.new(@enable_banking_account).process

    assert pending.reload.excluded?
  end

  test "skips the pending cleanup when importing transactions failed" do
    stale = create_pending_entry(external_id: "eb_partial", date: 10.days.ago.to_date, amount: 84)
    EnableBankingAccount::Transactions::Processor.any_instance.stubs(:process).raises(StandardError, "boom")

    EnableBankingAccount::Processor.new(@enable_banking_account).process

    assert_not stale.reload.excluded?
  end

  test "pending cleanup failure does not fail processing" do
    Entry.stubs(:reconcile_pending_duplicates).raises(StandardError, "boom")

    assert_nothing_raised do
      EnableBankingAccount::Processor.new(@enable_banking_account).process
    end
  end

  private
    def relink_provider_to(account)
      AccountProvider.find_by(provider: @enable_banking_account)&.destroy
      AccountProvider.create!(account: account, provider: @enable_banking_account)
    end

    def create_pending_entry(external_id:, date:, amount:)
      @account.entries.create!(
        name: "Pending #{external_id}", date: date, amount: amount, currency: @account.currency,
        external_id: external_id, source: "enable_banking",
        entryable: Transaction.new(extra: { "enable_banking" => { "pending" => true } })
      )
    end
end