Files
sure/test/models/up_item/importer_test.rb
Jake dc2a565b6a feat(up): add Up Bank (AU) provider integration (#2391)
* feat(up): add Up Bank (AU) provider integration

Adds Up Bank as a per-family, token-based bank sync provider, modelled on
the existing Akahu integration. Up uses a JSON:API REST API with a personal
access token (Bearer), cursor pagination via links.next, and returns both
HELD (pending) and SETTLED transactions from one endpoint.

New:
- Provider::Up client (JSON:API unwrap, links.next pagination, retries,
  typed errors, /util/ping) + Provider::UpAdapter (Factory-registered,
  Depository + Loan).
- UpItem / UpAccount models with Provided, Unlinking, Syncer,
  SyncCompleteEvent, Importer, Processor, Transactions::Processor, and
  UpEntry::Processor (amount sign flip, HELD->pending, foreignAmount FX,
  merchant from description, stale-pending pruning).
- Family::UpConnectable, UpItemsController, routes, settings panel + connect
  flow views, accounts index wiring, initializer, en locale, and model tests.

Core wiring:
- "up" added to Transaction::PENDING_PROVIDERS, the three pending-match SQL
  blocks in Account::ProviderImportAdapter, Provider::Metadata::REGISTRY,
  ProviderMerchant/DataEnrichment source enums, ProviderConnectionStatus,
  settings provider panels, and financial data reset.

Migration create_up_items_and_accounts must be run before use. No external
API endpoints added (no OpenAPI changes).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(up): dump up tables to schema and make since filter TZ-safe

The feature commit added the up_items/up_accounts migration but never
re-dumped db/schema.rb, leaving the schema version and tables stale.
Add the two table definitions and foreign keys and bump the schema
version so a fresh DB load matches the migration.

Also format a bare Date `since` as UTC midnight instead of the server's
local zone, so `filter[since]` is deterministic regardless of where the
app runs (previously shifted by the local UTC offset).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(up): address code review feedback

Behavior/correctness:
- Persist skipped accounts via a new up_accounts.ignored flag and a
  needs_setup scope, so skipped accounts stop resurfacing as "needs
  setup" on every sync. Linking clears the flag.
- destroy now checks unlink_all! per-account results and aborts deletion
  (alert) if any unlink failed, instead of swallowing failures.
- render_provider_panel_error redirect uses :see_other (was an invalid
  4xx redirect status).
- Up provider adapter falls back to item institution name/url when
  institution_metadata is absent (early return previously blocked it).

Resilience/security:
- fetch_all_resources guards against an API repeating the same
  links.next cursor (Set#add?), preventing infinite pagination.
- HTTP client validates absolute URLs (from links.next) against Up's
  HTTPS host before sending the bearer token, preventing credential
  leakage to untrusted hosts.

Diagnostics:
- Route provider sync/import failures through DebugLogEntry.capture
  (controller, UpItem, syncer, unlinking) with family/account context.
  Low-level HTTP client and currency-normalization warnings keep
  Rails.logger to match existing provider conventions.

Data integrity:
- up_accounts.name and currency are NOT NULL (align with model presence
  validations); account_id stays nullable (allow_nil uniqueness).

Forms:
- select_existing_account radio is required; controller guards a blank/
  unknown up_account_id with a friendly alert instead of RecordNotFound.

Tests:
- Add UpAccount needs_setup scope test, pagination loop guard test,
  untrusted-host rejection test; tighten filter[since] assertion to the
  exact UTC timestamp.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(up): address second-round review feedback

- Capture sync/import failures via DebugLogEntry so swallowed errors in
  account/transaction fetching and transaction processing surface in
  /settings/debug instead of only Rails.logger.
- Gate UP_DEBUG_RAW raw payload dump to local envs to avoid leaking PII
  (merchant names, amounts, account IDs) in managed/production logs.
- Collapse linked/unlinked/total account counts into one memoized query
  instead of 3 separate COUNTs per rendered item.
- Rename "Set Up Up Accounts" locale title to "Link Up Accounts".

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* docs(up): add method docstrings and align failed_result keys

Add docstrings to all Up provider source files (controller, models,
providers, concerns) to satisfy the 80% docstring coverage threshold.

Third-round review: failed_result now mirrors import's result shape
(accounts_updated/created/failed, transactions_imported/failed) instead
of the stale accounts_imported key, so failure results stay consistent.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-20 20:33:09 +02:00

157 lines
4.6 KiB
Ruby

require "test_helper"
class UpItem::ImporterTest < ActiveSupport::TestCase
class FakeUpProvider
attr_reader :transaction_calls
def initialize(accounts: nil, transactions: nil)
@transaction_calls = []
@accounts = accounts
@transactions = transactions
end
def get_accounts
@accounts || [
{
id: "acc_123",
displayName: "Up Spending",
accountType: "TRANSACTIONAL",
ownershipType: "INDIVIDUAL",
balance: { currencyCode: "AUD", value: "123.45", valueInBaseUnits: 12345 }
}
]
end
def get_account_transactions(account_id:, since: nil)
@transaction_calls << { account_id: account_id, since: since }
@transactions || [ settled_transaction ]
end
private
def settled_transaction
{
id: "tx_1",
account_id: "acc_123",
status: "SETTLED",
description: "Posted transaction",
amount: { currencyCode: "AUD", value: "-20.00", valueInBaseUnits: -2000 },
settledAt: "2026-01-19T00:00:00+11:00",
createdAt: "2026-01-19T00:00:00+11:00"
}
end
end
setup do
@family = families(:empty)
@up_item = UpItem.create!(
family: @family,
name: "Test Up",
access_token: "up-access-token"
)
@up_account = UpAccount.create!(
up_item: @up_item,
name: "Old name",
account_id: "acc_123",
currency: "AUD"
)
@account = Account.create!(
family: @family,
name: "Spending",
accountable: Depository.new(subtype: "checking"),
balance: 0,
currency: "AUD"
)
AccountProvider.create!(account: @account, provider: @up_account)
end
test "imports account snapshot and stores transactions" do
provider = FakeUpProvider.new
result = UpItem::Importer.new(@up_item, up_provider: provider).import
assert result[:success]
assert_equal 1, result[:accounts_updated]
assert_equal 1, result[:transactions_imported]
@up_account.reload
assert_equal "Up Spending", @up_account.name
assert_equal BigDecimal("123.45"), @up_account.current_balance
assert_equal "TRANSACTIONAL", @up_account.account_type
assert_equal "acc_123", provider.transaction_calls.first[:account_id]
assert_equal [ "tx_1" ], @up_account.raw_transactions_payload.map { |tx| tx["id"] }
end
test "held transaction settling under the same id clears pending without duplicates" do
import_with(transactions: [ held_transaction(id: "tx_h1", amount: "-8.00") ])
process_transactions
assert_equal 1, pending_entries.count
import_with(transactions: [ settled(id: "tx_h1", amount: "-8.00") ])
process_transactions
assert_empty pending_entries
assert_equal 1, @account.entries.where(source: "up").count
entry = @account.entries.find_by(external_id: "up_tx_h1", source: "up")
assert_equal false, entry.entryable.pending?
end
test "removes held transactions that disappear from the latest fetch" do
import_with(transactions: [ held_transaction(id: "tx_h2", amount: "-8.00") ])
process_transactions
assert_equal 1, pending_entries.count
import_with(transactions: [])
process_transactions
@up_account.reload
assert_empty pending_entries
assert_empty @up_account.raw_transactions_payload.select { |tx| UpEntry::Processor.pending?(tx) }
end
private
def import_with(transactions:)
provider = FakeUpProvider.new(transactions: transactions)
UpItem::Importer.new(@up_item, up_provider: provider).import
end
def process_transactions
UpAccount::Transactions::Processor.new(@up_account.reload).process
end
def pending_entries
@account.entries
.joins("INNER JOIN transactions ON transactions.id = entries.entryable_id AND entries.entryable_type = 'Transaction'")
.where(source: "up")
.where("(transactions.extra -> 'up' ->> 'pending')::boolean = true")
end
def held_transaction(id:, amount:, date: "2026-01-15T00:00:00+11:00")
{
id: id,
account_id: "acc_123",
status: "HELD",
description: "Pending card auth",
amount: { currencyCode: "AUD", value: amount, valueInBaseUnits: (amount.to_f * 100).to_i },
settledAt: nil,
createdAt: date
}
end
def settled(id:, amount:, date: "2026-01-16T00:00:00+11:00")
{
id: id,
account_id: "acc_123",
status: "SETTLED",
description: "Posted card auth",
amount: { currencyCode: "AUD", value: amount, valueInBaseUnits: (amount.to_f * 100).to_i },
settledAt: date,
createdAt: date
}
end
end