Files
sure/test/models/brex_entry/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

132 lines
4.7 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
class BrexEntry::ProcessorTest < ActiveSupport::TestCase
setup do
@family = families(:dylan_family)
@brex_item = brex_items(:one)
@account = @family.accounts.create!(
name: "Brex Card",
balance: 0,
currency: "USD",
accountable: CreditCard.new
)
@brex_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: @account, provider: @brex_account)
end
test "imports card purchase with Brex signed amount preserved" do
entry = BrexEntry::Processor.new(card_transaction(amount: 12_34), brex_account: @brex_account).process
assert_equal BigDecimal("12.34"), entry.amount
assert_equal "USD", entry.currency
assert_equal "brex", entry.source
assert_equal Date.new(2026, 1, 2), entry.date
assert_equal "STAPLES", entry.transaction.merchant.name
assert_equal "card_1", entry.transaction.extra.dig("brex", "card_id")
assert_equal "STAPLES", entry.transaction.extra.dig("brex", "merchant", "raw_descriptor")
refute_includes entry.transaction.extra.dig("brex", "merchant").to_s, "test-pan-placeholder"
refute_includes entry.transaction.extra.dig("brex", "merchant").to_s, "pan"
end
test "imports card payment as negative amount" do
entry = BrexEntry::Processor.new(card_transaction(id: "tx_payment", amount: -50_00, type: "COLLECTION"), brex_account: @brex_account).process
assert_equal BigDecimal("-50.0"), entry.amount
assert_equal "cc_payment", entry.transaction.kind
end
test "is idempotent by external id and source" do
transaction = card_transaction(id: "tx_duplicate", amount: 12_34)
assert_difference -> { @account.entries.where(source: "brex", external_id: "brex_tx_duplicate").count }, 1 do
BrexEntry::Processor.new(transaction, brex_account: @brex_account).process
BrexEntry::Processor.new(transaction, brex_account: @brex_account).process
end
end
test "tolerates nullable Brex fields and unknown types" do
transaction = {
id: "tx_nullable",
amount: nil,
description: "Cash movement",
posted_at_date: "2026-01-03",
initiated_at_date: "2026-01-02",
type: "NEW_BREX_TYPE"
}
entry = BrexEntry::Processor.new(transaction, brex_account: @brex_account).process
assert_equal BigDecimal("0"), entry.amount
assert_equal "Cash movement", entry.name
assert_equal "NEW_BREX_TYPE", entry.transaction.extra.dig("brex", "type")
end
test "uses localized default transaction name" do
transaction = card_transaction(id: "tx_default_name", amount: 12_34)
transaction.delete(:description)
transaction.delete(:merchant)
entry = BrexEntry::Processor.new(transaction, brex_account: @brex_account).process
assert_equal I18n.t("brex_items.entries.default_name"), entry.name
end
test "logs validation failure without re-reading missing external id" do
Rails.logger.expects(:error).with(regexp_matches(/Validation error for transaction brex_unknown/)).once
assert_raises(ArgumentError) do
BrexEntry::Processor.new(card_transaction(id: nil, amount: 12_34), brex_account: @brex_account).process
end
end
test "logs save failure with cached external id" do
Account::ProviderImportAdapter.any_instance
.expects(:import_transaction)
.raises(ActiveRecord::RecordInvalid.new(Entry.new))
Rails.logger.expects(:error).with(regexp_matches(/Failed to save transaction brex_tx_save_failure/)).once
assert_raises(StandardError) do
BrexEntry::Processor.new(card_transaction(id: "tx_save_failure", amount: 12_34), brex_account: @brex_account).process
end
end
test "logs missing transaction currency before using account fallback" do
Rails.logger.expects(:warn).with(regexp_matches(/Invalid Brex currency nil for transaction tx_missing_currency/)).once
entry = BrexEntry::Processor.new(
card_transaction(id: "tx_missing_currency", amount: 12_34).tap { |transaction| transaction[:amount].delete(:currency) },
brex_account: @brex_account
).process
assert_equal "USD", entry.currency
end
private
def card_transaction(id: "tx_1", amount:, type: "CARD_EXPENSE")
{
id: id,
amount: { amount: amount, currency: "USD" },
description: "Office supplies",
posted_at_date: "2026-01-02",
initiated_at_date: "2026-01-01",
type: type,
card_id: "card_1",
merchant: {
raw_descriptor: "STAPLES",
card_metadata: {
pan: "test-pan-placeholder"
}
}
}
end
end