Files
sure/test/models/brex_account/transactions/processor_test.rb
ghost 95f6451b39 feat(sync): add Brex provider connections (#1752)
* 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>
2026-05-13 18:13:48 +02:00

93 lines
2.7 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
class BrexAccount::Transactions::ProcessorTest < ActiveSupport::TestCase
setup do
@brex_item = brex_items(:one)
@brex_account = @brex_item.brex_accounts.create!(
account_id: "cash_unlinked",
account_kind: "cash",
name: "Unlinked Cash",
currency: "USD",
raw_transactions_payload: [
{
id: "tx_skipped",
amount: { amount: 1_00, currency: "USD" },
description: "Skipped transaction",
posted_at_date: "2026-01-02"
}
]
)
end
test "counts intentionally skipped transactions separately from failures" do
result = BrexAccount::Transactions::Processor.new(@brex_account).process
assert result[:success]
assert_equal 1, result[:total]
assert_equal 0, result[:imported]
assert_equal 1, result[:skipped]
assert_equal 0, result[:failed]
assert_equal "No linked account", result[:skipped_transactions].first[:reason]
assert_empty result[:errors]
end
test "imports linked transactions successfully" do
link_brex_account!
result = BrexAccount::Transactions::Processor.new(@brex_account).process
assert result[:success]
assert_equal 1, result[:total]
assert_equal 1, result[:imported]
assert_equal 0, result[:skipped]
assert_equal 0, result[:failed]
assert_empty result[:skipped_transactions]
assert_empty result[:errors]
end
test "aggregates partial transaction failures" do
link_brex_account!
@brex_account.update!(
raw_transactions_payload: [
{
id: "tx_success",
amount: { amount: 1_00, currency: "USD" },
description: "Successful transaction",
posted_at_date: "2026-01-02"
},
{
id: "tx_failure",
amount: { amount: 2_00, currency: "USD" },
description: "Failed transaction",
posted_at_date: "not-a-date"
}
]
)
result = BrexAccount::Transactions::Processor.new(@brex_account).process
assert_not result[:success]
assert_equal 2, result[:total]
assert_equal 1, result[:imported]
assert_equal 0, result[:skipped]
assert_equal 1, result[:failed]
assert_empty result[:skipped_transactions]
assert_equal "tx_failure", result[:errors].first[:transaction_id]
assert_match(/Unable to parse transaction date/, result[:errors].first[:error])
end
private
def link_brex_account!
account = @brex_item.family.accounts.create!(
name: "Linked Cash",
balance: 0,
currency: "USD",
accountable: Depository.new
)
AccountProvider.create!(account: account, provider: @brex_account)
end
end