mirror of
https://github.com/we-promise/sure.git
synced 2026-05-29 15:34:58 +00:00
* feat(sync): add Brex provider schema Adds Brex item and account tables with per-family credentials, scoped upstream account uniqueness, encrypted token storage, and sanitized provider payload columns. * feat(sync): add Brex provider core Adds Brex item/account models, provider client and adapter support, family connection helpers, and provider enum registration for read-only Brex cash and card data. * feat(sync): add Brex import pipeline Adds Brex account discovery, linked-account sync, cash/card balance processors, transaction import, sanitized metadata handling, and idempotent provider entry processing. * feat(sync): add Brex connection flows Adds Mercury-style Brex connection management, explicit item-scoped account selection and linking, settings provider UI, account index visibility, localized copy, and per-item cache handling. * test(sync): cover Brex provider workflows Adds targeted coverage for Brex provider requests, adapter config, item/account guards, importer behavior, entry processing, and Mercury-style controller flows. * fix(sync): align Brex API edge cases Tightens Brex account fetching against the official card-account response shape, sends transaction start filters as RFC3339 date-times, and keeps provider error bodies out of user-facing messages while expanding provider client guard coverage. * fix(sync): harden Brex provider integration Restrict Brex API base URLs to official hosts, tighten account-selection UI behavior, and add tests for invalid credentials, cache scoping, and provider setup edge cases. * test(sync): avoid Brex secret-shaped fixtures * refactor(sync): extract Brex account flows * fix(sync): address Brex provider review feedback * fix(sync): address Brex review follow-ups Move remaining Brex review cleanup into focused model behavior, tighten link/setup edge cases, localize summaries, and add regression coverage from CodeRabbit feedback. Also records the security-review pass as no-findings after diff-scoped inspection and Brakeman validation. * refactor(sync): split Brex account flow controllers Route Brex account selection and setup actions through small namespaced controllers while keeping existing URLs and helpers stable. Business flow remains in BrexItem::AccountFlow; the main Brex item controller now only handles connection CRUD, provider-panel rendering, destroy, and sync. * fix(sync): address Brex CodeRabbit review * fix(sync): address Brex follow-up review * fix(sync): address Brex review follow-ups * fix(sync): address Brex sync review findings * fix(sync): polish Brex review copy and errors * fix(sync): register Brex provider health * fix(sync): polish Brex bank sync presentation * fix(sync): address Brex review follow-ups * fix(sync): tighten Brex setup params * test(api): stabilize usage rate-limit window * fix(sync): polish Brex setup flow nits * fix(sync): harden Brex setup params * fix(sync): finalize Brex review cleanup --------- Signed-off-by: Juan José Mata <juanjo.mata@gmail.com> Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
332 lines
11 KiB
Ruby
332 lines
11 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "test_helper"
|
|
|
|
class BrexItem::ImporterTest < ActiveSupport::TestCase
|
|
setup do
|
|
@family = families(:dylan_family)
|
|
@brex_item = brex_items(:one)
|
|
@account = @family.accounts.create!(
|
|
name: "Operating Cash",
|
|
balance: 0,
|
|
currency: "USD",
|
|
accountable: Depository.new(subtype: "checking")
|
|
)
|
|
@brex_account = @brex_item.brex_accounts.create!(
|
|
account_id: "cash_1",
|
|
account_kind: "cash",
|
|
name: "Operating Cash",
|
|
currency: "USD",
|
|
current_balance: 0
|
|
)
|
|
AccountProvider.create!(account: @account, provider: @brex_account)
|
|
end
|
|
|
|
test "imports account discovery and fetches transactions only for linked accounts" do
|
|
provider = mock("brex_provider")
|
|
provider.expects(:get_accounts).returns(accounts: [ cash_account_payload, card_account_payload ])
|
|
provider.expects(:get_cash_transactions).with("cash_1", start_date: Date.new(2026, 1, 1)).returns(
|
|
transactions: [
|
|
{
|
|
id: "cash_tx_1",
|
|
amount: { amount: 12_34, currency: "USD" },
|
|
description: "Wire fee",
|
|
posted_at_date: "2026-01-02"
|
|
}
|
|
]
|
|
)
|
|
provider.expects(:get_primary_card_transactions).never
|
|
|
|
result = BrexItem::Importer.new(@brex_item, brex_provider: provider, sync_start_date: Date.new(2026, 1, 1)).import
|
|
|
|
assert result[:success]
|
|
assert_equal 1, result[:accounts_updated]
|
|
assert_equal 1, result[:accounts_created]
|
|
assert_equal [ "cash_tx_1" ], @brex_account.reload.raw_transactions_payload.map { |tx| tx["id"] }
|
|
assert_equal "card", @brex_item.brex_accounts.find_by!(account_id: BrexAccount.card_account_id).account_kind
|
|
end
|
|
|
|
test "counts only newly stored transactions as imported" do
|
|
@brex_account.update!(
|
|
raw_transactions_payload: [
|
|
{
|
|
id: "cash_tx_1",
|
|
amount: { amount: 12_34, currency: "USD" },
|
|
description: "Existing wire fee",
|
|
posted_at_date: "2026-01-02"
|
|
}
|
|
]
|
|
)
|
|
|
|
provider = mock("brex_provider")
|
|
provider.expects(:get_accounts).returns(accounts: [ cash_account_payload ])
|
|
provider.expects(:get_cash_transactions).with("cash_1", start_date: anything).returns(
|
|
transactions: [
|
|
{
|
|
id: "cash_tx_1",
|
|
amount: { amount: 12_34, currency: "USD" },
|
|
description: "Existing wire fee",
|
|
posted_at_date: "2026-01-02"
|
|
},
|
|
{
|
|
id: "cash_tx_2",
|
|
amount: { amount: 56_78, currency: "USD" },
|
|
description: "New wire fee",
|
|
posted_at_date: "2026-01-03"
|
|
}
|
|
]
|
|
)
|
|
|
|
result = BrexItem::Importer.new(@brex_item, brex_provider: provider, sync_start_date: Date.new(2026, 1, 1)).import
|
|
|
|
assert result[:success]
|
|
assert_equal 1, result[:transactions_imported]
|
|
assert_equal [ "cash_tx_1", "cash_tx_2" ], @brex_account.reload.raw_transactions_payload.map { |tx| tx["id"] }
|
|
end
|
|
|
|
test "keeps raw transaction snapshots bounded to the sync window" do
|
|
@brex_account.update!(
|
|
raw_transactions_payload: [
|
|
{
|
|
id: "old_cash_tx",
|
|
amount: { amount: 12_34, currency: "USD" },
|
|
description: "Old wire fee",
|
|
posted_at_date: "2025-12-01"
|
|
},
|
|
{
|
|
id: "recent_cash_tx",
|
|
amount: { amount: 56_78, currency: "USD" },
|
|
description: "Recent wire fee",
|
|
posted_at_date: "2026-01-02"
|
|
}
|
|
]
|
|
)
|
|
|
|
sync_start_date = Date.new(2026, 1, 1)
|
|
provider = mock("brex_provider")
|
|
provider.expects(:get_accounts).returns(accounts: [ cash_account_payload ])
|
|
provider.expects(:get_cash_transactions).with("cash_1", start_date: sync_start_date).returns(
|
|
transactions: [
|
|
{
|
|
id: "ignored_before_window",
|
|
amount: { amount: 1_00, currency: "USD" },
|
|
description: "Ignored old transaction",
|
|
posted_at_date: "2025-12-31"
|
|
},
|
|
{
|
|
id: "new_cash_tx",
|
|
amount: { amount: 2_00, currency: "USD" },
|
|
description: "New transaction",
|
|
posted_at_date: "2026-01-03"
|
|
}
|
|
]
|
|
)
|
|
|
|
result = BrexItem::Importer.new(@brex_item, brex_provider: provider, sync_start_date: sync_start_date).import
|
|
|
|
assert result[:success]
|
|
assert_equal 1, result[:transactions_imported]
|
|
assert_equal [ "recent_cash_tx", "new_cash_tx" ], @brex_account.reload.raw_transactions_payload.map { |tx| tx["id"] }
|
|
end
|
|
|
|
test "uses explicit sync start date for cash and card transaction fetches" do
|
|
card_account = @family.accounts.create!(
|
|
name: "Brex Card",
|
|
balance: 0,
|
|
currency: "USD",
|
|
accountable: CreditCard.new
|
|
)
|
|
brex_card_account = @brex_item.brex_accounts.create!(
|
|
account_id: BrexAccount.card_account_id,
|
|
account_kind: "card",
|
|
name: "Brex Card",
|
|
currency: "USD",
|
|
current_balance: 0
|
|
)
|
|
AccountProvider.create!(account: card_account, provider: brex_card_account)
|
|
|
|
sync_start_date = Date.new(2026, 2, 1)
|
|
provider = mock("brex_provider")
|
|
provider.expects(:get_accounts).returns(accounts: [ cash_account_payload, card_account_payload ])
|
|
provider.expects(:get_cash_transactions).with("cash_1", start_date: sync_start_date).returns(transactions: [])
|
|
provider.expects(:get_primary_card_transactions).with(start_date: sync_start_date).returns(transactions: [])
|
|
|
|
result = BrexItem::Importer.new(
|
|
@brex_item,
|
|
brex_provider: provider,
|
|
sync_start_date: sync_start_date
|
|
).import
|
|
|
|
assert result[:success]
|
|
end
|
|
|
|
test "imports aggregate card transactions only into the selected connection" do
|
|
first_card_account = @family.accounts.create!(
|
|
name: "First Brex Card",
|
|
balance: 0,
|
|
currency: "USD",
|
|
accountable: CreditCard.new
|
|
)
|
|
first_brex_card_account = @brex_item.brex_accounts.create!(
|
|
account_id: BrexAccount.card_account_id,
|
|
account_kind: "card",
|
|
name: "First Brex Card",
|
|
currency: "USD",
|
|
current_balance: 0
|
|
)
|
|
AccountProvider.create!(account: first_card_account, provider: first_brex_card_account)
|
|
|
|
second_item = BrexItem.create!(
|
|
family: @family,
|
|
name: "Second Brex",
|
|
token: "second_brex_token",
|
|
base_url: "https://api.brex.com"
|
|
)
|
|
second_card_account = @family.accounts.create!(
|
|
name: "Second Brex Card",
|
|
balance: 0,
|
|
currency: "USD",
|
|
accountable: CreditCard.new
|
|
)
|
|
second_brex_card_account = second_item.brex_accounts.create!(
|
|
account_id: BrexAccount.card_account_id,
|
|
account_kind: "card",
|
|
name: "Second Brex Card",
|
|
currency: "USD",
|
|
current_balance: 0,
|
|
raw_transactions_payload: [
|
|
{
|
|
id: "second_connection_card_tx",
|
|
amount: { amount: 42_00, currency: "USD" },
|
|
description: "Existing second connection card transaction",
|
|
posted_at_date: "2026-02-01"
|
|
}
|
|
]
|
|
)
|
|
AccountProvider.create!(account: second_card_account, provider: second_brex_card_account)
|
|
|
|
provider = mock("brex_provider")
|
|
provider.expects(:get_accounts).returns(accounts: [ cash_account_payload, card_account_payload ])
|
|
provider.expects(:get_cash_transactions).with("cash_1", start_date: anything).returns(transactions: [])
|
|
provider.expects(:get_primary_card_transactions).with(start_date: anything).returns(
|
|
transactions: [
|
|
{
|
|
id: "first_connection_card_tx",
|
|
amount: { amount: 21_00, currency: "USD" },
|
|
description: "First connection card transaction",
|
|
posted_at_date: "2026-02-02",
|
|
card_id: "card_account_1"
|
|
}
|
|
]
|
|
)
|
|
|
|
result = BrexItem::Importer.new(@brex_item, brex_provider: provider, sync_start_date: Date.new(2026, 2, 1)).import
|
|
|
|
assert result[:success]
|
|
assert_equal [ "first_connection_card_tx" ], first_brex_card_account.reload.raw_transactions_payload.map { |tx| tx["id"] }
|
|
assert_equal [ "second_connection_card_tx" ], second_brex_card_account.reload.raw_transactions_payload.map { |tx| tx["id"] }
|
|
end
|
|
|
|
test "raises and reports snapshot persistence failures" do
|
|
provider = mock("brex_provider")
|
|
provider.expects(:get_accounts).returns(accounts: [ cash_account_payload ])
|
|
@brex_item.expects(:upsert_brex_snapshot!).raises(StandardError.new("snapshot failed"))
|
|
|
|
error = assert_raises StandardError do
|
|
BrexItem::Importer.new(@brex_item, brex_provider: provider).import
|
|
end
|
|
|
|
assert_equal "snapshot failed", error.message
|
|
end
|
|
|
|
test "marks item as requiring update on authorization errors" do
|
|
provider = mock("brex_provider")
|
|
provider.expects(:get_accounts).raises(
|
|
Provider::Brex::BrexError.new("Access forbidden", :access_forbidden, http_status: 403, trace_id: "trace_123")
|
|
)
|
|
|
|
result = BrexItem::Importer.new(@brex_item, brex_provider: provider).import
|
|
|
|
refute result[:success]
|
|
assert @brex_item.reload.requires_update?
|
|
end
|
|
|
|
test "clears requires update after a clean import" do
|
|
@brex_item.update!(status: :requires_update)
|
|
|
|
provider = mock("brex_provider")
|
|
provider.expects(:get_accounts).returns(accounts: [ cash_account_payload ])
|
|
provider.expects(:get_cash_transactions).with("cash_1", start_date: anything).returns(transactions: [])
|
|
|
|
result = BrexItem::Importer.new(@brex_item, brex_provider: provider).import
|
|
|
|
assert result[:success]
|
|
assert @brex_item.reload.good?
|
|
end
|
|
|
|
test "refreshes already discovered unlinked accounts during import" do
|
|
unlinked_account = @brex_item.brex_accounts.create!(
|
|
account_id: "cash_unlinked_1",
|
|
account_kind: "cash",
|
|
name: "Old Unlinked Cash",
|
|
currency: "USD",
|
|
current_balance: 1
|
|
)
|
|
|
|
provider = mock("brex_provider")
|
|
provider.expects(:get_accounts).returns(
|
|
accounts: [
|
|
cash_account_payload,
|
|
cash_account_payload.merge(
|
|
id: "cash_unlinked_1",
|
|
name: "Updated Unlinked Cash",
|
|
current_balance: { amount: 987_65, currency: "USD" }
|
|
)
|
|
]
|
|
)
|
|
provider.expects(:get_cash_transactions).with("cash_1", start_date: anything).returns(transactions: [])
|
|
|
|
result = BrexItem::Importer.new(@brex_item, brex_provider: provider).import
|
|
|
|
assert result[:success]
|
|
assert_equal 2, result[:accounts_updated]
|
|
assert_equal "Updated Unlinked Cash", unlinked_account.reload.name
|
|
assert_equal BigDecimal("987.65"), unlinked_account.current_balance
|
|
end
|
|
|
|
private
|
|
|
|
def cash_account_payload
|
|
{
|
|
id: "cash_1",
|
|
name: "Operating Cash",
|
|
account_kind: "cash",
|
|
status: "ACTIVE",
|
|
current_balance: { amount: 120_000, currency: "USD" },
|
|
available_balance: { amount: 110_000, currency: "USD" },
|
|
account_number: "account-last4-9012",
|
|
routing_number: "routing-last4-0021"
|
|
}
|
|
end
|
|
|
|
def card_account_payload
|
|
{
|
|
id: BrexAccount.card_account_id,
|
|
name: "Brex Card",
|
|
account_kind: "card",
|
|
status: "ACTIVE",
|
|
current_balance: { amount: 1_234, currency: "USD" },
|
|
available_balance: { amount: 100_000, currency: "USD" },
|
|
account_limit: { amount: 150_000, currency: "USD" },
|
|
raw_card_accounts: [
|
|
{
|
|
id: "card_account_1",
|
|
card_metadata: {
|
|
pan: "test-pan-placeholder"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
end
|
|
end
|