The personal finance app for everyone (by everyone)
Ruby
require "test_helper"
class CreditCardsControllerTest < ActionDispatch::IntegrationTest
include AccountableResourceInterfaceTest
setup do
sign_in @user = users(:family_admin)
@account = accounts(:credit_card)
end
test "creates with credit card details" do
assert_difference -> { Account.count } => 1,
-> { CreditCard.count } => 1,
-> { Valuation.count } => 1,
-> { Entry.count } => 1 do
post credit_cards_path, params: {
account: {
name: "New Credit Card",
balance: 1000,
currency: "USD",
institution_name: "Amex",
institution_domain: "americanexpress.com",
notes: "Primary card",
accountable_type: "CreditCard",
accountable_attributes: {
available_credit: 5000,
minimum_payment: 25.51,
apr: 15.99,
expiration_date: 2.years.from_now.to_date,
annual_fee: 99
}
}
}
end
created_account = Account.order(:created_at).last
assert_equal "New Credit Card", created_account.name
assert_equal 1000, created_account.balance
assert_equal "USD", created_account.currency
assert_equal "Amex", created_account[:institution_name]
assert_equal "americanexpress.com", created_account[:institution_domain]
assert_equal "Primary card", created_account[:notes]
assert_equal 5000, created_account.accountable.available_credit
assert_equal 25.51, created_account.accountable.minimum_payment
assert_equal 15.99, created_account.accountable.apr
assert_equal 2.years.from_now.to_date, created_account.accountable.expiration_date
assert_equal 99, created_account.accountable.annual_fee
assert_redirected_to created_account
assert_equal "Credit card account created", flash[:notice]
assert_enqueued_with(job: SyncJob)
end
test "updates with credit card details" do
assert_no_difference [ "Account.count", "CreditCard.count" ] do
patch credit_card_path(@account), params: {
account: {
name: "Updated Credit Card",
balance: 2000,
currency: "USD",
institution_name: "Chase",
institution_domain: "chase.com",
notes: "Updated notes",
accountable_type: "CreditCard",
accountable_attributes: {
id: @account.accountable_id,
available_credit: 6000,
minimum_payment: 50,
apr: 14.99,
expiration_date: 3.years.from_now.to_date,
annual_fee: 0
}
}
}
end
@account.reload
assert_equal "Updated Credit Card", @account.name
assert_equal 2000, @account.balance
assert_equal "Chase", @account[:institution_name]
assert_equal "chase.com", @account[:institution_domain]
assert_equal "Updated notes", @account[:notes]
assert_equal 6000, @account.accountable.available_credit
assert_equal 50, @account.accountable.minimum_payment
assert_equal 14.99, @account.accountable.apr
assert_equal 3.years.from_now.to_date, @account.accountable.expiration_date
assert_equal 0, @account.accountable.annual_fee
assert_redirected_to @account
assert_equal "Credit card account updated", flash[:notice]
assert_enqueued_with(job: SyncJob)
end
test "updates the statement cycle" do
patch credit_card_path(@account), params: {
account: {
accountable_attributes: {
id: @account.accountable_id,
statement_close_day: 25,
due_day: 5
}
}
}
assert_redirected_to @account
assert_equal 25, @account.reload.credit_card.statement_close_day
assert_equal 5, @account.credit_card.due_day
end
test "clearing the statement cycle stores blanks as no cycle" do
@account.credit_card.update!(statement_close_day: 25, due_day: 5)
patch credit_card_path(@account), params: {
account: {
accountable_attributes: {
id: @account.accountable_id,
statement_close_day: "",
due_day: ""
}
}
}
assert_redirected_to @account
assert_nil @account.reload.credit_card.statement_close_day
assert_nil @account.credit_card.due_day
end
test "a statement cycle day outside the month is rejected" do
patch credit_card_path(@account), params: {
account: {
accountable_attributes: {
id: @account.accountable_id,
statement_close_day: 45,
due_day: 5
}
}
}
assert_response :unprocessable_entity
assert_nil @account.reload.credit_card.statement_close_day
end
test "updates enable banking balance interpretation flag when linked" do
enable_banking_account = create_linked_enable_banking_account
get edit_credit_card_path(@account)
assert_response :success
assert_match "treat_balance_as_available_credit", response.body
assert_match I18n.t("credit_cards.form.treat_balance_as_available_credit_label"), response.body
patch credit_card_path(@account), params: {
account: {
name: @account.name,
accountable_type: "CreditCard",
enable_banking: { treat_balance_as_available_credit: "1" }
}
}
assert_redirected_to @account
assert enable_banking_account.reload.treat_balance_as_available_credit?
end
test "an edit saved through Turbo still applies the provider settings" do
enable_banking_account = create_linked_enable_banking_account
patch credit_card_path(@account),
params: { account: { name: @account.name, accountable_type: "CreditCard", enable_banking: { treat_balance_as_available_credit: "1" } } },
headers: { "Accept" => "text/vnd.turbo-stream.html" }, as: :turbo_stream
assert_match %(action="redirect"), response.body
assert enable_banking_account.reload.treat_balance_as_available_credit?
end
test "clears enable banking balance interpretation flag when toggled off" do
enable_banking_account = create_linked_enable_banking_account
enable_banking_account.update!(treat_balance_as_available_credit: true)
patch credit_card_path(@account), params: {
account: {
name: @account.name,
accountable_type: "CreditCard",
enable_banking: { treat_balance_as_available_credit: "0" }
}
}
assert_redirected_to @account
assert_not enable_banking_account.reload.treat_balance_as_available_credit?
end
test "does not persist enable banking flag when the account update fails" do
enable_banking_account = create_linked_enable_banking_account
patch credit_card_path(@account), params: {
account: {
name: "",
accountable_type: "CreditCard",
enable_banking: { treat_balance_as_available_credit: "1" }
}
}
assert_response :unprocessable_entity
assert_not enable_banking_account.reload.treat_balance_as_available_credit?
end
test "handles malformed enable banking params without error" do
create_linked_enable_banking_account
patch credit_card_path(@account), params: {
account: {
name: @account.name,
accountable_type: "CreditCard",
enable_banking: "bogus"
}
}
assert_redirected_to @account
end
test "ignores enable banking params for accounts without an enable banking link" do
patch credit_card_path(@account), params: {
account: {
name: "Still works",
accountable_type: "CreditCard",
enable_banking: { treat_balance_as_available_credit: "1" }
}
}
assert_redirected_to @account
assert_equal "Still works", @account.reload.name
end
test "updates lunch flow balance interpretation flag when linked" do
lunchflow_account = create_linked_lunchflow_account
get edit_credit_card_path(@account)
assert_response :success
assert_select "input[name='account[lunchflow][treat_balance_as_available_credit]']"
assert_enqueued_with(job: SyncJob) do
patch credit_card_path(@account), params: {
account: {
name: @account.name,
accountable_type: "CreditCard",
lunchflow: { treat_balance_as_available_credit: "1" }
}
}
end
assert_redirected_to @account
assert lunchflow_account.reload.treat_balance_as_available_credit?
end
test "clears lunch flow balance interpretation flag when toggled off" do
lunchflow_account = create_linked_lunchflow_account
lunchflow_account.update!(treat_balance_as_available_credit: true)
patch credit_card_path(@account), params: {
account: {
name: @account.name,
accountable_type: "CreditCard",
lunchflow: { treat_balance_as_available_credit: "0" }
}
}
assert_redirected_to @account
assert_not lunchflow_account.reload.treat_balance_as_available_credit?
end
test "resyncs lunch flow when the card limit changes in available credit mode" do
lunchflow_account = create_linked_lunchflow_account
lunchflow_account.update!(treat_balance_as_available_credit: true)
assert_enqueued_jobs 1, only: SyncJob do
patch credit_card_path(@account), params: {
account: { name: @account.name, accountable_type: "CreditCard",
accountable_attributes: { id: @account.accountable.id, available_credit: 7500 } }
}
end
assert_redirected_to @account
assert lunchflow_account.reload.treat_balance_as_available_credit?, "a form without the flag keeps it"
end
test "turning the flag on and setting the limit together syncs once" do
create_linked_lunchflow_account
assert_enqueued_jobs 1, only: SyncJob do
patch credit_card_path(@account), params: {
account: { name: @account.name, accountable_type: "CreditCard",
accountable_attributes: { id: @account.accountable.id, available_credit: 7500 },
lunchflow: { treat_balance_as_available_credit: "1" } }
}
end
end
test "a limit change does not resync lunch flow outside available credit mode" do
create_linked_lunchflow_account
assert_no_enqueued_jobs only: SyncJob do
patch credit_card_path(@account), params: {
account: { name: @account.name, accountable_type: "CreditCard",
accountable_attributes: { id: @account.accountable.id, available_credit: 7500 } }
}
end
end
test "does not persist lunch flow flag when the account update fails" do
lunchflow_account = create_linked_lunchflow_account
patch credit_card_path(@account), params: {
account: {
name: "",
accountable_type: "CreditCard",
lunchflow: { treat_balance_as_available_credit: "1" }
}
}
assert_response :unprocessable_entity
assert_not lunchflow_account.reload.treat_balance_as_available_credit?
end
test "hides the lunch flow toggle for accounts without a lunch flow link" do
get edit_credit_card_path(@account)
assert_response :success
assert_select "input[name='account[lunchflow][treat_balance_as_available_credit]']", count: 0
end
test "updates SimpleFIN balance sign override when linked" do
simplefin_account = create_linked_simplefin_account
get edit_credit_card_path(@account)
assert_response :success
assert_match "balance_sign_override", response.body
assert_match I18n.t("credit_cards.form.simplefin_balance_sign_label"), response.body
assert_enqueued_with(job: SyncJob) do
patch credit_card_path(@account), params: {
account: {
name: @account.name,
accountable_type: "CreditCard",
simplefin: { balance_sign_override: "credit" }
}
}
end
assert_redirected_to @account
assert_equal "credit", simplefin_account.reload.balance_sign_override
end
test "clears SimpleFIN balance sign override when automatic is selected" do
simplefin_account = create_linked_simplefin_account
simplefin_account.update!(balance_sign_override: "credit")
patch credit_card_path(@account), params: {
account: {
name: @account.name,
accountable_type: "CreditCard",
simplefin: { balance_sign_override: "" }
}
}
assert_redirected_to @account
assert_nil simplefin_account.reload.balance_sign_override
end
test "does not persist SimpleFIN override when the account update fails" do
simplefin_account = create_linked_simplefin_account
patch credit_card_path(@account), params: {
account: {
name: "",
accountable_type: "CreditCard",
simplefin: { balance_sign_override: "credit" }
}
}
assert_response :unprocessable_entity
assert_nil simplefin_account.reload.balance_sign_override
end
test "normalizes an unsupported SimpleFIN balance sign to automatic" do
simplefin_account = create_linked_simplefin_account
simplefin_account.update!(balance_sign_override: "credit")
patch credit_card_path(@account), params: {
account: {
name: @account.name,
accountable_type: "CreditCard",
simplefin: { balance_sign_override: "unsupported" }
}
}
assert_redirected_to @account
assert_nil simplefin_account.reload.balance_sign_override
end
test "hides the Sophtron balance sign select for accounts without a Sophtron link" do
get edit_credit_card_path(@account)
assert_response :success
assert_select "select[name='account[sophtron][balance_sign_override]']", count: 0
end
test "updates Sophtron balance sign override when linked" do
sophtron_account = create_linked_sophtron_account
get edit_credit_card_path(@account)
assert_response :success
assert_select "select[name='account[sophtron][balance_sign_override]']"
SophtronAccount::Transactions::Processor.expects(:new).never
assert_enqueued_with(job: SyncJob) do
patch credit_card_path(@account), params: {
account: {
name: @account.name,
accountable_type: "CreditCard",
sophtron: { balance_sign_override: "credit" }
}
}
end
assert_redirected_to @account
assert_equal "credit", sophtron_account.reload.balance_sign_override
assert_equal BigDecimal("-25"), @account.reload.balance
assert_equal BigDecimal("-25"), @account.current_anchor_balance
assert_equal 1, @account.syncs.count
assert_empty sophtron_account.sophtron_item.syncs
end
test "applies a Sophtron sign override when the card requires manual sync" do
sophtron_account = create_linked_sophtron_account
sophtron_account.update!(manual_sync: true)
assert_no_difference -> { sophtron_account.sophtron_item.syncs.count } do
assert_difference -> { @account.syncs.count } => 1 do
patch credit_card_path(@account), params: {
account: {
name: @account.name,
accountable_type: "CreditCard",
sophtron: { balance_sign_override: "debt" }
}
}
end
end
assert_redirected_to @account
assert_equal "debt", sophtron_account.reload.balance_sign_override
assert_equal BigDecimal("25"), @account.reload.balance
assert_equal BigDecimal("25"), @account.current_anchor_balance
end
test "failed Sophtron balance application rolls back and can be retried" do
sophtron_account = create_linked_sophtron_account
original_balance = @account.balance
original_cash_balance = @account.cash_balance
original_anchor = @account.current_anchor_balance
submission = {
account: {
name: @account.name,
accountable_type: "CreditCard",
sophtron: { balance_sign_override: "credit" }
}
}
Account.any_instance.stubs(:set_current_balance).returns(
Account::CurrentBalanceManager::Result.new(success?: false, error: "anchor write failed")
)
assert_no_enqueued_jobs only: SyncJob do
patch credit_card_path(@account), params: submission
end
assert_redirected_to @account
assert_equal I18n.t("credit_cards.update.sophtron_balance_sign_error"), flash[:alert]
assert_nil flash[:notice]
assert_nil sophtron_account.reload.balance_sign_override
assert_equal original_balance, @account.reload.balance
assert_equal original_cash_balance, @account.cash_balance
assert_equal original_anchor, @account.current_anchor_balance
diagnostic = DebugLogEntry.find_by!(source: "CreditCardsController#update_sophtron_settings")
assert_equal @account.family, diagnostic.family
assert_equal sophtron_account.account_provider, diagnostic.account_provider
assert_equal "sophtron", diagnostic.provider_key
Account.any_instance.unstub(:set_current_balance)
assert_enqueued_jobs 1, only: SyncJob do
patch credit_card_path(@account), params: submission
end
assert_redirected_to @account
assert_equal "credit", sophtron_account.reload.balance_sign_override
assert_equal BigDecimal("-25"), @account.reload.balance
assert_equal BigDecimal("-25"), @account.current_anchor_balance
end
test "applies a Sophtron sign override when the parent item requires manual sync" do
sophtron_account = create_linked_sophtron_account
sophtron_account.sophtron_item.update!(manual_sync: true)
patch credit_card_path(@account), params: {
account: {
name: @account.name,
accountable_type: "CreditCard",
sophtron: { balance_sign_override: "credit" }
}
}
assert_redirected_to @account
assert_equal "credit", sophtron_account.reload.balance_sign_override
assert_equal BigDecimal("-25"), @account.reload.balance
assert_empty sophtron_account.sophtron_item.syncs
assert_equal 1, @account.syncs.count
end
test "clears Sophtron balance sign override when automatic is selected" do
sophtron_account = create_linked_sophtron_account
sophtron_account.update!(balance_sign_override: "credit")
@account.update!(balance: -25)
patch credit_card_path(@account), params: {
account: {
name: @account.name,
accountable_type: "CreditCard",
sophtron: { balance_sign_override: "" }
}
}
assert_redirected_to @account
assert_nil sophtron_account.reload.balance_sign_override
assert_equal BigDecimal("25"), @account.reload.balance
assert_equal BigDecimal("25"), @account.current_anchor_balance
end
test "does not persist Sophtron override when the account update fails" do
sophtron_account = create_linked_sophtron_account
patch credit_card_path(@account), params: {
account: {
name: "",
accountable_type: "CreditCard",
sophtron: { balance_sign_override: "credit" }
}
}
assert_response :unprocessable_entity
assert_nil sophtron_account.reload.balance_sign_override
end
test "normalizes an unsupported Sophtron balance sign to automatic" do
sophtron_account = create_linked_sophtron_account
sophtron_account.update!(balance_sign_override: "credit")
patch credit_card_path(@account), params: {
account: {
name: @account.name,
accountable_type: "CreditCard",
sophtron: { balance_sign_override: "unsupported" }
}
}
assert_redirected_to @account
assert_nil sophtron_account.reload.balance_sign_override
end
private
def create_linked_enable_banking_account
enable_banking_item = EnableBankingItem.create!(
family: @account.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: "Linked card",
uid: "hash_cc",
currency: "EUR",
current_balance: 900.00,
credit_limit: 1000.00
)
AccountProvider.create!(account: @account, provider: enable_banking_account)
enable_banking_account
end
def create_linked_lunchflow_account
lunchflow_item = LunchflowItem.new(family: @account.family, name: "Test Lunch Flow", api_key: "test_key")
lunchflow_item.save!(validate: false)
lunchflow_account = lunchflow_item.lunchflow_accounts.create!(
name: "Linked card",
account_id: "lf_linked_card",
currency: "GBP",
current_balance: 4983.83
)
AccountProvider.create!(account: @account, provider: lunchflow_account)
lunchflow_account
end
def create_linked_simplefin_account
simplefin_item = SimplefinItem.create!(
family: @account.family,
name: "Test SimpleFIN",
access_url: "https://example.com/token"
)
simplefin_account = SimplefinAccount.create!(
simplefin_item: simplefin_item,
name: "Linked card",
account_id: "simplefin-linked-card",
currency: "USD",
account_type: "credit",
current_balance: -25
)
AccountProvider.create!(account: @account, provider: simplefin_account)
simplefin_account
end
def create_linked_sophtron_account
sophtron_item = @account.family.sophtron_items.create!(
name: "Test Sophtron",
user_id: "developer-user",
access_key: Base64.strict_encode64("secret-key")
)
sophtron_account = sophtron_item.sophtron_accounts.create!(
name: "Linked card",
account_id: "sophtron-linked-card",
currency: "USD",
balance: -25
)
AccountProvider.create!(account: @account, provider: sophtron_account)
sophtron_account
end
end