Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
4.6 KB
Raw Blame
# frozen_string_literal: true

require "test_helper"

# Covers the Account factory shared by every crypto-exchange integration:
# `create_from_crypto_exchange_account`, reached through Kraken, Binance and
# CoinSpot.
class Account::CryptoExchangeCreationTest < ActiveSupport::TestCase
  setup do
    @family = families(:dylan_family)
    @family.update!(currency: "USD")
  end

  # `create_and_sync` anchors a new account at the balance it was created with,
  # dated in the past. That is right for an account whose history the user will
  # type in, and wrong for an exchange: the processors import its ledger from
  # inception, so every imported entry lands on top of an opening balance equal
  # to what the account is worth today. On an exchange older than the default
  # anchor date the whole history is inflated by the present value.
  test "a Kraken exchange account opens at zero, not at its current value" do
    account = Account.create_from_kraken_account(kraken_account(current_balance: 1_000))

    assert account.has_opening_anchor?
    assert_equal 0, account.opening_anchor_balance
    assert_equal 1_000, account.balance
  end

  test "a Binance exchange account opens at zero, not at its current value" do
    account = Account.create_from_binance_account(binance_account(current_balance: 2_500))

    assert account.has_opening_anchor?
    assert_equal 0, account.opening_anchor_balance
    assert_equal 2_500, account.balance
  end

  # Kraken reports every balance in USD regardless of what the user trades in,
  # but the processors denominate the account's entries and holdings in the
  # family currency. Inheriting the exchange's currency labelled the account
  # USD and then filled it with EUR figures.
  test "an exchange account is created in the family currency, with the balance converted" do
    @family.update!(currency: "EUR")
    ExchangeRate.create!(from_currency: "USD", to_currency: "EUR", date: Date.current, rate: 0.9)

    account = Account.create_from_kraken_account(kraken_account(current_balance: 1_000, currency: "USD"))

    assert_equal "EUR", account.currency
    assert_equal 900, account.balance
  end

  test "a Binance exchange account is created in the family currency" do
    @family.update!(currency: "EUR")
    ExchangeRate.create!(from_currency: "USD", to_currency: "EUR", date: Date.current, rate: 0.9)

    account = Account.create_from_binance_account(binance_account(current_balance: 2_000, currency: "USD"))

    assert_equal "EUR", account.currency
    assert_equal 1_800, account.balance
  end

  test "a CoinSpot exchange account is created in the family currency" do
    @family.update!(currency: "USD")
    ExchangeRate.create!(from_currency: "AUD", to_currency: "USD", date: Date.current, rate: 0.65)

    account = Account.create_from_coinspot_account(coinspot_account(current_balance: 1_000, currency: "AUD"))

    assert_equal "USD", account.currency
    assert_equal 650, account.balance
  end

  # Relabelling 1,000 USD as 1,000 EUR would be wrong by the whole rate. The
  # first sync writes the converted balance; until then zero is the honest figure.
  test "an exchange account starts at zero when no rate is available" do
    @family.update!(currency: "EUR")
    # Stubbed rather than left absent: with no stored rate the lookup still
    # asks the provider, and a rate coming back would make this pass by luck.
    ExchangeRate.stubs(:find_or_fetch_rate).returns(nil)

    account = Account.create_from_kraken_account(kraken_account(current_balance: 1_000, currency: "USD"))

    assert_equal "EUR", account.currency
    assert_equal 0, account.balance
  end

  private

    def kraken_account(current_balance:, currency: "USD")
      item = KrakenItem.create!(family: @family, name: "Kraken", api_key: "k", api_secret: "s")
      item.kraken_accounts.create!(
        name: "Kraken",
        account_id: "combined",
        account_type: "combined",
        currency: currency,
        current_balance: current_balance
      )
    end

    def coinspot_account(current_balance:, currency: "AUD")
      item = CoinspotItem.create!(family: @family, name: "CoinSpot", api_key: "c", api_secret: "s")
      item.coinspot_accounts.create!(
        name: "CoinSpot",
        account_id: "combined",
        account_type: "combined",
        currency: currency,
        current_balance: current_balance
      )
    end

    def binance_account(current_balance:, currency: "USD")
      item = BinanceItem.create!(family: @family, name: "Binance", api_key: "b", api_secret: "s")
      item.binance_accounts.create!(
        name: "Binance",
        account_type: "combined",
        currency: currency,
        current_balance: current_balance
      )
    end
end