Files
sure/test/models/redbark_item_test.rb
Oscar c6a240a183 feat(redbark): add australian bank sync (redbark) (#2794)
* add redbark provider integration

- per family api key provider, built like the lunchflow integration
- syncs accounts, balances and transactions from api.redbark.com
- account setup flow, settings panel, locales and routes
- tests and fixtures

* harden redbark integration based on prior provider pr feedback

- use DebugLogEntry.capture for sync/import/unlink failures
- retry 429s and 5xxs with backoff, raise on page cap instead of truncating
- keep raw response bodies out of logs and errors
- not null constraints on account columns, migration base 7.2
- persist ignored flag for skipped accounts so they stop nagging setup
- validate api key on every save, re-arm status on key rotation
- destroy aborts if unlink fails, atomic account create and link
- require_admin on mutating actions, see_other on error redirects
- single grouped query for item account counts
- i18n default connection name, blank password field value
- controller and provider tests

* fix issues found in second review sweep

- add missing syncable scope, without it every family sync raises
- kick off a sync on connection create and on key rotation
- setup dialog fetches accounts inline for fresh connections and shows api errors
- skip balance write when no balance has been fetched yet, never anchor a false zero
- exclude stale and non banking accounts from the batched balances call, per account fallback if the batch is rejected
- detect the server row ceiling and empty pages instead of silently truncating history
- user sync start date only governs the initial backfill, incremental after that
- fetch connections before the per account loop so auth errors propagate once
- drop untemplated index/show/new/edit routes and dead preload/link_accounts actions
- stable dom id on the settings panel so repeat turbo replaces keep working

* skip brokerage connections, found in live testing

- the transactions endpoint 400s for brokerage connections, they belong to /v1/trades
- only import accounts from banking and documents connections
- guard transaction fetches for any legacy linked non banking account

* address review feedback

- treat the truncation header as a pagination signal: split the date window and refetch instead of failing the account
- prune stale pending rows from the snapshot so settled pendings cant come back as duplicates
- block linking a sure account that already has another provider feed
- count setup failures separately from skips and surface an error instead of "all skipped"
- add not nulls on redbark_items name and api key
- enqueue the destroy job after the flag commits, not inside the transaction
- swap bg-gray-400 for bg-surface-inset, drop amounts from info logs, remove i18n default fallbacks
- tests for window splitting, pending pruning and encrypted payload round trip

* fix issues from convention review

- benign skips (unlinked account, blank id, unparseable rows) no longer count as failures, tracked separately so a clean batch reports success
- currency parsing goes through extract_currency so hash shaped payloads resolve instead of falling to the default
- merchant ids use truncated sha256 instead of md5
- debug log entries for import failures and account sync scheduling failures

* bound the raw transactions snapshot to the fetch window

- trim raw_transactions_payload to the current fetch window on merge, same as brex
- keep rows without a parseable date, drop settled pendings as before
- surface skipped rows in the aggregate debug log entry with imported/skipped counts
2026-07-26 07:40:25 +02:00

74 lines
2.4 KiB
Ruby

require "test_helper"
class RedbarkItemTest < ActiveSupport::TestCase
def setup
@redbark_item = redbark_items(:one)
end
test "fixture is valid" do
assert @redbark_item.valid?
end
test "belongs to family" do
assert_equal families(:dylan_family), @redbark_item.family
end
test "credentials_configured returns true when api_key present" do
assert @redbark_item.credentials_configured?
end
test "credentials_configured returns false when api_key blank" do
@redbark_item.api_key = nil
assert_not @redbark_item.credentials_configured?
end
test "redbark_provider returns Provider::Redbark instance" do
provider = @redbark_item.redbark_provider
assert_instance_of Provider::Redbark, provider
assert_equal @redbark_item.api_key, provider.api_key
end
test "redbark_provider returns nil when credentials not configured" do
@redbark_item.api_key = nil
assert_nil @redbark_item.redbark_provider
end
test "syncer returns RedbarkItem::Syncer instance" do
assert_instance_of RedbarkItem::Syncer, @redbark_item.syncer
end
test "has_redbark_credentials reflects configured items" do
assert families(:dylan_family).has_redbark_credentials?
refute families(:empty).has_redbark_credentials?
end
test "ignored accounts are excluded from setup counts" do
account = redbark_accounts(:savings_account)
assert_equal 1, @redbark_item.unlinked_accounts_count
account.update!(ignored: true)
fresh_item = RedbarkItem.find(@redbark_item.id)
assert_equal 0, fresh_item.unlinked_accounts_count
assert_equal 1, fresh_item.total_accounts_count
assert_not RedbarkAccount.needs_setup.exists?(id: account.id)
end
test "encrypted jsonb payloads round-trip nested structures" do
payload = {
"accounts" => [
{ "id" => "acc_1", "name" => "Everyday", "balances" => { "current" => "1024.55", "currency" => "AUD" } },
{ "id" => "acc_2", "name" => "Savings", "meta" => { "tags" => [ "a", "b" ], "nested" => { "deep" => true } } }
],
"fetched_at" => "2026-07-25T00:00:00Z"
}
institution = { "name" => "Test Bank", "logo" => "https://example.com/logo.png" }
@redbark_item.update!(raw_payload: payload, raw_institution_payload: institution)
reloaded = RedbarkItem.find(@redbark_item.id)
assert_equal payload, reloaded.raw_payload
assert_equal institution, reloaded.raw_institution_payload
end
end