The personal finance app for everyone (by everyone)
Ruby
require "test_helper"
class PlaidAccount::Liabilities::StudentLoanProcessorTest < ActiveSupport::TestCase
setup do
@plaid_account = plaid_accounts(:one)
@plaid_account.update!(
plaid_type: "loan",
plaid_subtype: "student"
)
# Change the underlying accountable to a Loan so the helper method `loan` is available
@plaid_account.current_account.update!(accountable: Loan.new)
end
test "updates loan details including term months from Plaid data" do
@plaid_account.update!(raw_liabilities_payload: {
student: {
interest_rate_percentage: 5.5,
origination_principal_amount: 20000,
origination_date: Date.new(2020, 1, 1),
expected_payoff_date: Date.new(2022, 1, 1)
}
})
processor = PlaidAccount::Liabilities::StudentLoanProcessor.new(@plaid_account)
processor.process
loan = @plaid_account.current_account.loan
assert_equal "fixed", loan.rate_type
assert_equal 5.5, loan.interest_rate
assert_equal 20000, loan.initial_balance
assert_equal 24, loan.term_months
end
# The payload has carried the drawdown date all along and nothing wrote it
# down, so every imported loan looked originated on import day.
test "records the provider's origination date as the loan's start date" do
@plaid_account.update!(raw_liabilities_payload: {
student: {
interest_rate_percentage: 5.5,
origination_principal_amount: 20000,
origination_date: Date.new(2020, 1, 1),
expected_payoff_date: Date.new(2022, 1, 1)
}
})
PlaidAccount::Liabilities::StudentLoanProcessor.new(@plaid_account).process
assert_equal Date.new(2020, 1, 1), @plaid_account.current_account.loan.start_date
end
# A borrower who corrected the drawdown by hand knows something the provider
# does not.
test "a start date already recorded is not overwritten by the sync" do
@plaid_account.current_account.loan.update!(start_date: Date.new(2019, 3, 4))
@plaid_account.update!(raw_liabilities_payload: {
student: {
interest_rate_percentage: 5.5,
origination_principal_amount: 20000,
origination_date: Date.new(2020, 1, 1),
expected_payoff_date: Date.new(2022, 1, 1)
}
})
PlaidAccount::Liabilities::StudentLoanProcessor.new(@plaid_account).process
assert_equal Date.new(2019, 3, 4), @plaid_account.current_account.loan.start_date
end
# A payoff less than a month after origination is zero months. Stored
# as nil, because a term of no months is not a term -- and the rest of the
# payload must still land rather than being lost with it.
test "a term of under a month is no term, and does not cost the rest of the sync" do
@plaid_account.update!(raw_liabilities_payload: {
student: {
interest_rate_percentage: 6.25,
origination_principal_amount: 900,
origination_date: Date.new(2026, 1, 1),
expected_payoff_date: Date.new(2026, 1, 20)
}
})
PlaidAccount::Liabilities::StudentLoanProcessor.new(@plaid_account).process
loan = @plaid_account.current_account.loan
assert_nil loan.term_months
assert_equal 6.25, loan.interest_rate, "the rest of the payload still lands"
assert_equal 900, loan.initial_balance
end
# Counted in calendar months, not 30-day blocks: thirty years is 10,958 days,
# which divided by 30 made a 360-month loan a 365-month one and put its payoff
# date and current instalment five months out.
test "the term is counted in calendar months" do
@plaid_account.update!(raw_liabilities_payload: {
student: {
interest_rate_percentage: 5.5,
origination_principal_amount: 20000,
origination_date: Date.new(2000, 1, 1),
expected_payoff_date: Date.new(2030, 1, 1)
}
})
PlaidAccount::Liabilities::StudentLoanProcessor.new(@plaid_account).process
assert_equal 360, @plaid_account.current_account.loan.term_months
end
# Both sides of the boundary: a month counts once it has been served in full,
# which is how Loan#months_elapsed counts the same loan's progress.
test "a term month counts once it is served in full" do
[ [ Date.new(2026, 2, 24), nil ], [ Date.new(2026, 2, 25), 1 ], [ Date.new(2027, 1, 24), 11 ] ].each do |payoff, expected|
@plaid_account.update!(raw_liabilities_payload: {
student: {
interest_rate_percentage: 5.5,
origination_principal_amount: 20000,
origination_date: Date.new(2026, 1, 25),
expected_payoff_date: payoff
}
})
PlaidAccount::Liabilities::StudentLoanProcessor.new(@plaid_account).process
if expected.nil?
assert_nil @plaid_account.current_account.loan.reload.term_months, "payoff on #{payoff}"
else
assert_equal expected, @plaid_account.current_account.loan.reload.term_months, "payoff on #{payoff}"
end
end
end
# A loan cannot start in the future (Loan validates it), and a provider date
# that says otherwise must not fail the whole liabilities sync with it. The
# date is left unrecorded and the rest of the payload lands.
test "a future origination date is not recorded and does not fail the sync" do
travel_to Date.new(2026, 1, 10) do
@plaid_account.update!(raw_liabilities_payload: {
student: {
interest_rate_percentage: 5.5,
origination_principal_amount: 20000,
origination_date: Date.new(2026, 3, 1),
expected_payoff_date: Date.new(2036, 3, 1)
}
})
PlaidAccount::Liabilities::StudentLoanProcessor.new(@plaid_account).process
loan = @plaid_account.current_account.loan.reload
assert_nil loan.start_date
assert_equal 5.5, loan.interest_rate, "the rest of the payload still lands"
assert_equal 120, loan.term_months
end
end
test "handles missing payoff dates gracefully" do
@plaid_account.update!(raw_liabilities_payload: {
student: {
interest_rate_percentage: 4.8,
origination_principal_amount: 15000,
origination_date: Date.new(2021, 6, 1)
# expected_payoff_date omitted
}
})
processor = PlaidAccount::Liabilities::StudentLoanProcessor.new(@plaid_account)
processor.process
loan = @plaid_account.current_account.loan
assert_nil loan.term_months
assert_equal 4.8, loan.interest_rate
assert_equal 15000, loan.initial_balance
end
# End to end: what the processor writes has to be what the loan then measures
# itself against. The account carries a balance part way through the term --
# which is all an import ever sees -- and the figures on the overview read the
# origination principal the payload sent, not that balance.
test "an imported loan measures its figures against the principal Plaid sent" do
@plaid_account.update!(raw_liabilities_payload: {
student: {
interest_rate_percentage: 5.0,
origination_principal_amount: 20_000,
origination_date: 5.years.ago.to_date,
expected_payoff_date: 5.years.from_now.to_date
}
})
account = @plaid_account.current_account
account.update!(balance: 10_000)
account.entries.create!(
name: "Starting balance", amount: 10_000, currency: account.currency,
date: 5.years.ago.to_date, entryable: Valuation.new(kind: "opening_anchor")
)
PlaidAccount::Liabilities::StudentLoanProcessor.new(@plaid_account).process
loan = account.reload.loan
assert_equal 20_000, loan.initial_balance
assert_equal 20_000, loan.original_balance.amount, "the overview reads the origination principal"
assert_in_delta 0.5, loan.balance_paid_ratio, 0.0001
end
test "does nothing when loan data absent" do
@plaid_account.update!(raw_liabilities_payload: {})
processor = PlaidAccount::Liabilities::StudentLoanProcessor.new(@plaid_account)
processor.process
loan = @plaid_account.current_account.loan
assert_nil loan.interest_rate
assert_nil loan.initial_balance
assert_nil loan.term_months
end
# This processor sends rate_type "fixed" on every sync, so a loan the user
# marked variable reverted each time.
test "a locked rate type is not forced back to fixed" do
loan = loan_with(rate_type: "variable", interest_rate: 5.0)
loan.lock_attr!(:rate_type)
payload("interest_rate_percentage" => 5.0)
process
assert_equal "variable", loan.reload.rate_type, "Plaid forced a locked rate type back to fixed"
end
# Locks hold per attribute: the unlocked terms in the same payload still land.
test "locked terms hold while the unlocked ones are still written" do
loan = loan_with(initial_balance: 50_000, term_months: 120, interest_rate: 5.0)
loan.lock_attr!(:initial_balance)
loan.lock_attr!(:term_months)
payload(
"interest_rate_percentage" => 6.5,
"origination_principal_amount" => 90_000,
"origination_date" => "2020-01-01",
"expected_payoff_date" => "2040-01-01"
)
process
loan.reload
assert_equal 50_000, loan.initial_balance.to_i, "a locked original balance was overwritten"
assert_equal 120, loan.term_months, "a locked term was overwritten"
assert_equal 6.5, loan.interest_rate.to_f, "the unlocked rate was not written"
end
# term_months is nil without both dates, and nil must not blank a stored term.
test "a payload missing a payoff date leaves the stored term alone" do
loan = loan_with(term_months: 120, interest_rate: 5.0)
payload("interest_rate_percentage" => 5.0, "origination_date" => "2020-01-01")
process
assert_equal 120, loan.reload.term_months, "an incomplete pair of dates blanked the stored term"
end
test "a payload without a rate leaves the stored rate alone" do
loan = loan_with(interest_rate: 4.5)
payload("origination_principal_amount" => 90_000)
process
assert_equal 4.5, loan.reload.interest_rate.to_f, "an omitted rate blanked the stored one"
assert_equal 90_000, loan.initial_balance.to_i
end
# The processor still sends rate_type "fixed", but a user's lock on
# "variable" holds, and the loan's rate change is then dated.
test "a student loan the user locked as variable has its rate change dated" do
loan = loan_with(interest_rate: 4.5, rate_type: "variable", start_date: Date.new(2020, 1, 1))
loan.lock_attr!(:rate_type)
payload("interest_rate_percentage" => 5.2)
PlaidAccount::Liabilities::StudentLoanProcessor.new(@plaid_account.reload, as_of: Date.new(2026, 1, 15)).process
loan.reload
assert_equal "variable", loan.rate_type
assert_equal 4.5, loan.interest_rate.to_f
assert_equal [ "2026-01-15" ], loan.variable_rate_schedule.keys
assert_equal BigDecimal("5.2"), BigDecimal(loan.variable_rate_schedule["2026-01-15"].to_s)
end
private
def loan_with(**attrs)
loan = @plaid_account.current_account.loan
loan.update!(attrs)
loan
end
def payload(data)
@plaid_account.update!(raw_liabilities_payload: { student: data })
end
def process
PlaidAccount::Liabilities::StudentLoanProcessor.new(@plaid_account.reload).process
end
end