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

137 lines
4.5 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
class BrexItem::SyncerTest < ActiveSupport::TestCase
setup do
@brex_item = brex_items(:one)
@syncer = BrexItem::Syncer.new(@brex_item)
end
test "passes sync window start date to importer" do
window_start_date = Date.new(2026, 2, 1)
sync = mock_sync(window_start_date: window_start_date)
@brex_item.expects(:import_latest_brex_data).with(sync_start_date: window_start_date).once
@syncer.perform_sync(sync)
end
test "records localized setup status text and counts" do
window_start_date = Date.new(2026, 2, 1)
sync = recording_sync(window_start_date: window_start_date)
@brex_item.expects(:import_latest_brex_data).with(sync_start_date: window_start_date).once
@syncer.perform_sync(sync)
assert_equal [
I18n.t("brex_items.syncer.importing_accounts"),
I18n.t("brex_items.syncer.checking_account_configuration"),
I18n.t("brex_items.syncer.accounts_need_setup", count: 1)
], sync.updates.filter_map { |attrs| attrs[:status_text] }
assert_equal 1, sync.sync_stats["total_accounts"]
assert_equal 0, sync.sync_stats["linked_accounts"]
assert_equal 1, sync.sync_stats["unlinked_accounts"]
end
test "records importer failure counts in health stats" do
sync = recording_sync(window_start_date: Date.new(2026, 2, 1))
@brex_item.expects(:import_latest_brex_data).returns(
success: false,
accounts_failed: 2,
transactions_failed: 1
)
@syncer.perform_sync(sync)
assert_equal 2, sync.sync_stats["total_errors"]
assert_equal [
I18n.t("brex_items.syncer.accounts_failed", count: 2),
I18n.t("brex_items.syncer.transactions_failed", count: 1)
], sync.sync_stats["errors"].map { |error| error["message"] }
end
test "records account processing and scheduling failures in health stats" do
account = @brex_item.family.accounts.create!(
name: "Linked Brex Checking",
balance: 0,
currency: "USD",
accountable: Depository.new
)
brex_account = @brex_item.brex_accounts.first
AccountProvider.create!(account: account, provider: brex_account)
sync = recording_sync(window_start_date: Date.new(2026, 2, 1))
@brex_item.expects(:import_latest_brex_data).returns(
success: true,
accounts_failed: 0,
transactions_failed: 0
)
@brex_item.expects(:process_accounts).returns([
{ brex_account_id: brex_account.id, success: false, error: "processing failure" }
])
@brex_item.expects(:schedule_account_syncs).returns([
{ account_id: account.id, success: false, error: "scheduling failure" }
])
@syncer.perform_sync(sync)
assert_equal 2, sync.sync_stats["total_errors"]
assert_equal [
I18n.t("brex_items.syncer.account_processing_failed", count: 1),
I18n.t("brex_items.syncer.account_sync_failed", count: 1)
], sync.sync_stats["errors"].map { |error| error["message"] }
end
test "raises user safe credential error for Brex auth failures" do
sync = mock_sync(window_start_date: Date.new(2026, 2, 1))
@brex_item.expects(:import_latest_brex_data)
.raises(Provider::Brex::BrexError.new("raw upstream auth body", :unauthorized, http_status: 401))
Sentry.expects(:capture_exception)
error = assert_raises(BrexItem::Syncer::SafeSyncError) do
@syncer.perform_sync(sync)
end
assert_equal I18n.t("brex_items.syncer.credentials_invalid"), error.message
end
private
def mock_sync(window_start_date:)
sync = mock("sync")
sync.stubs(:respond_to?).with(:status_text).returns(true)
sync.stubs(:respond_to?).with(:sync_stats).returns(true)
sync.stubs(:sync_stats).returns({})
sync.stubs(:window_start_date).returns(window_start_date)
sync.stubs(:window_end_date).returns(nil)
sync.stubs(:update!)
sync
end
def recording_sync(window_start_date:)
Class.new do
attr_accessor :sync_stats, :status_text
attr_reader :updates
define_method(:initialize) do |start_date|
@window_start_date = start_date
@window_end_date = nil
@created_at = Time.current
@sync_stats = {}
@updates = []
end
attr_reader :window_start_date, :window_end_date, :created_at
def update!(attributes)
@updates << attributes
self.sync_stats = attributes[:sync_stats] if attributes.key?(:sync_stats)
self.status_text = attributes[:status_text] if attributes.key?(:status_text)
end
end.new(window_start_date)
end
end