The personal finance app for everyone (by everyone)
Ruby
require "test_helper"
require "ostruct"
class TransactionsHelperTest < ActionView::TestCase
include ApplicationHelper
include EntriesTestHelper
# build_transaction_extra_details reads `extra` off a Transaction (or anything
# that responds to :transaction), so a struct is enough to drive it.
def transaction_with(extra)
OpenStruct.new(extra: extra)
end
test "reconcile filter is hidden unless manual reconciliation is enabled" do
Current.session = Session.create!(user: users(:family_admin))
assert_not_includes transaction_search_filters.map { |filter| filter[:key] }, "reconcile_status_filter"
Current.family.update!(manual_reconciliation: true)
assert_includes transaction_search_filters.map { |filter| filter[:key] }, "reconcile_status_filter"
end
test "can_reconcile_account? mirrors the write access the reconcile action requires" do
Current.session = Session.create!(user: users(:family_member))
assert can_reconcile_account?(accounts(:depository)),
"full_control share is write access"
assert_not can_reconcile_account?(accounts(:credit_card)),
"read_only share cannot reconcile"
assert_not can_reconcile_account?(accounts(:investment)),
"an account with no share at all cannot reconcile"
end
test "can_reconcile_account? lets the owner reconcile" do
Current.session = Session.create!(user: users(:family_admin))
assert can_reconcile_account?(accounts(:investment)), "owner can reconcile"
end
test "can_reconcile_account? does not treat annotate access as write access" do
share = account_shares(:credit_card_shared_with_member)
share.update!(permission: "read_write")
Current.session = Session.create!(user: users(:family_member))
assert_not can_reconcile_account?(accounts(:credit_card)),
"read_write is annotate access, and #reconcile requires write"
end
test "reconcile filter is hidden when the user has no accessible manual account" do
Current.session = Session.create!(user: users(:empty))
Current.family.update!(manual_reconciliation: true)
assert_not_includes transaction_search_filters.map { |filter| filter[:key] }, "reconcile_status_filter"
end
test "bulk_reconcilable_transfer? is false while the setting is off" do
Current.session = Session.create!(user: users(:family_admin))
assert_not bulk_reconcilable_transfer?(entries(:transfer_out))
end
test "bulk_reconcilable_transfer? is true for a writable manual transfer" do
Current.session = Session.create!(user: users(:family_admin))
Current.family.update!(manual_reconciliation: true)
assert bulk_reconcilable_transfer?(entries(:transfer_out))
end
test "bulk_reconcilable_transfer? is false for a read_write share" do
share = account_shares(:credit_card_shared_with_member)
share.update!(permission: "read_write")
Current.session = Session.create!(user: users(:family_member))
Current.family.update!(manual_reconciliation: true)
assert_not bulk_reconcilable_transfer?(entries(:transfer_in))
end
test "bulk_reconcilable_transfer? is false for a synced account" do
Current.session = Session.create!(user: users(:family_admin))
Current.family.update!(manual_reconciliation: true)
transfer = create_transfer(
from_account: accounts(:connected),
to_account: accounts(:depository),
amount: 100
)
assert_not bulk_reconcilable_transfer?(transfer.outflow_transaction.entry)
end
test "bulk_reconcilable_transfer? is false for an unmatched transfer-kind transaction" do
Current.session = Session.create!(user: users(:family_admin))
Current.family.update!(manual_reconciliation: true)
entry = create_transaction(account: accounts(:depository), kind: "funds_movement")
assert_not bulk_reconcilable_transfer?(entry)
end
test "bulk_reconcilable_transfer? is false for a transfer fee" do
Current.session = Session.create!(user: users(:family_admin))
Current.family.update!(manual_reconciliation: true)
transfer = create_transfer(
from_account: accounts(:depository),
to_account: accounts(:credit_card),
amount: 100
)
fee_entry = accounts(:depository).entries.create!(
name: "Transfer fee",
date: Date.current,
amount: 10,
currency: "USD",
entryable: Transaction.new(kind: "standard", transfer: transfer)
)
assert_not bulk_reconcilable_transfer?(fee_entry)
end
test "extracts the named Plaid fields into their own section" do
details = build_transaction_extra_details(transaction_with({
"plaid" => {
"pending" => false,
"original_description" => "AMZN Mktp US*AB12CD SEATTLE WA",
"payment_channel" => "online",
"transaction_code" => "purchase"
}
}))
assert_equal :plaid, details[:kind]
assert_equal "AMZN Mktp US*AB12CD SEATTLE WA", details[:plaid][:original_description]
assert_equal "online", details[:plaid][:payment_channel]
assert_equal "purchase", details[:plaid][:transaction_code]
end
test "flattens payment_meta and counterparties into labeled rows" do
details = build_transaction_extra_details(transaction_with({
"plaid" => {
"payment_meta" => { "reference_number" => "REF-1" },
"counterparties" => [ { "name" => "Amazon", "type" => "merchant" } ]
}
}))
rows = details[:provider_extras].index_by { |row| row[:key] }
assert_equal "REF-1", rows["Payment meta · Reference number"][:value]
assert_equal "Amazon", rows["Counterparty 1 · Name"][:value]
assert_equal "merchant", rows["Counterparty 1 · Type"][:value]
refute rows["Counterparty 1 · Name"][:multiline]
end
# A composed label is part translation, part provider identifier. Running
# `humanize` over the whole thing lowercased the translated half, so the field
# name is translated on its own and the result is left alone.
test "labels a provider field we do not know by humanizing its identifier" do
details = build_transaction_extra_details(transaction_with({
"plaid" => { "payment_meta" => { "some_future_field" => "value" } }
}))
keys = details[:provider_extras].map { |row| row[:key] }
assert_includes keys, "Payment meta · Some future field"
end
# The key becomes part of an i18n lookup, and "" resolves to the parent scope,
# which returned the whole provider_extra_fields hash and rendered it as the label.
test "never renders a translation hash as a label for a blank or dotted key" do
details = build_transaction_extra_details(transaction_with({
"plaid" => { "payment_meta" => { "" => "blank key", "a.b" => "dotted key" } }
}))
details[:provider_extras].each do |row|
assert_kind_of String, row[:key]
refute_match(/by_order_of|confidence_level/, row[:key], "the translation hash leaked into a label")
end
end
# An empty value used to become a row with nothing opposite its label, and made
# the extras non-blank, which opened an Additional details section with no details.
test "skips provider values that are empty" do
details = build_transaction_extra_details(transaction_with({
"simplefin" => { "extra" => { "note" => "", "tags" => [], "blanks" => [ "", nil ], "meta" => {} } }
}))
assert_nil details, "nothing here is worth showing, so the section stays closed"
end
# false and 0 are values a provider meant to report, unlike an empty string.
test "keeps provider values that are false or zero" do
details = build_transaction_extra_details(transaction_with({
"simplefin" => { "extra" => { "recurring" => false, "retries" => 0 } }
}))
values = details[:provider_extras].map { |row| row[:value] }
assert_includes values, false
assert_includes values, 0
end
# The drawer should stay closed for transactions where Plaid told us nothing
# beyond reconciliation flags the pending badge already shows.
test "returns nil when the Plaid extra holds only pending flags" do
details = build_transaction_extra_details(transaction_with({
"plaid" => { "pending" => true, "pending_transaction_id" => "txn_1" }
}))
assert_nil details
end
# SimpleFIN writes a pending flag on every transaction, so this is the common
# case rather than an edge one: without the guard the section opens empty.
test "returns nil when the SimpleFIN extra holds only a pending flag" do
details = build_transaction_extra_details(transaction_with({
"simplefin" => { "pending" => false }
}))
assert_nil details
end
test "still returns SimpleFIN details when the provider sent something to show" do
details = build_transaction_extra_details(transaction_with({
"simplefin" => { "pending" => false, "payee" => "Whole Foods" }
}))
assert_equal :simplefin, details[:kind]
assert_equal "Whole Foods", details[:simplefin][:payee]
end
# Regression: the array branch used to hand provider_extra_row an
# already-encoded string, which then got encoded a second time and rendered as
# an escaped one-liner ("[\n {\"a\": 1}\n]") instead of a formatted block.
test "renders an array of hashes as formatted JSON, encoded once" do
details = build_transaction_extra_details(transaction_with({
"plaid" => {
"payment_meta" => { "parties" => [ { "name" => "Amazon" } ] }
}
}))
row = details[:provider_extras].first
assert row[:multiline]
assert_equal [ { "name" => "Amazon" } ], JSON.parse(row[:value])
assert_includes row[:value], "\n"
refute_includes row[:value], "\\\""
end
# Regression: passing the raw operator straight to t() raised
# I18n::ArgumentError for nil/blank (an empty key) and resolved a whole
# translation subtree for a dotted value like "transactions.search".
test "falls back to the raw operator instead of translating unknown or blank values" do
assert_equal "", transaction_amount_operator_label(nil)
assert_equal "foo", transaction_amount_operator_label("foo")
assert_equal "transactions.search", transaction_amount_operator_label("transactions.search")
end
test "falls back to a raw JSON dump for providers with no structured rendering" do
details = build_transaction_extra_details(transaction_with({ "someprovider" => { "a" => 1 } }))
assert_equal :raw, details[:kind]
assert_empty details[:provider_extras]
assert_equal({ "someprovider" => { "a" => 1 } }, JSON.parse(details[:raw]))
end
test "the convert-to-trade picker looks a shared rate up once for all its securities" do
securities = [ securities(:aapl), securities(:msft) ]
securities.each do |security|
security.stubs(:find_or_fetch_price).returns(Security::Price.new(price: 200, currency: "USD", date: Date.current))
end
ExchangeRate.expects(:find_or_fetch_rate).once.returns(ExchangeRate.new(rate: 0.9))
options = convert_to_trade_security_options(securities, "EUR")
assert_equal [ "18000", "18000" ], options.map { |option| option.last["data-price-cents"].to_s }
end
# Regression: a quote worth less than a cent after conversion (1 JPY is about
# 0.0066 USD) was truncated to 0 cents, so the price check divided by zero
# and warned about a mismatch even when the entered price matched the quote.
test "the convert-to-trade picker keeps fractional cents of a converted quote" do
security = securities(:aapl)
security.stubs(:find_or_fetch_price).returns(Security::Price.new(price: 1, currency: "JPY", date: Date.current))
ExchangeRate.stubs(:find_or_fetch_rate).returns(ExchangeRate.new(rate: 0.0066))
options = convert_to_trade_security_options([ security ], "USD")
assert_in_delta 0.66, options.first.last["data-price-cents"], 0.0001
end
test "the convert-to-trade picker omits the price check for a zero quote" do
security = securities(:aapl)
security.stubs(:find_or_fetch_price).returns(Security::Price.new(price: 0, currency: "USD", date: Date.current))
options = convert_to_trade_security_options([ security ], "USD")
assert_nil options.first.last["data-price-cents"]
end
test "investment_activity_label_name translates the stored label" do
I18n.with_locale(:ru) do
assert_equal "Перевод", investment_activity_label_name("Transfer")
assert_equal "Автоперевод (зачисление)", investment_activity_label_name("Sweep In")
end
assert_equal "Buy", investment_activity_label_name("Buy")
end
test "investment_activity_label_name falls back to the stored label when it has no translation" do
assert_equal "Legacy Label", investment_activity_label_name("Legacy Label")
end
end