mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 22:34:47 +00:00
* SimpleFin: metadata + merge fixes; holdings (incl. crypto) + Day Change; Sync Summary; ops rakes; lint # Conflicts: # db/schema.rb # Conflicts: # app/controllers/simplefin_items_controller.rb * fix testing * fix linting * xfix linting x2 * Review PR #267 on we-promise/sure (SimpleFin enhancements v2). Address all 15 actionable CodeRabbit comments: Add UUID validations in rakes (e.g., simplefin_unlink), swap Ruby pattern matching/loops for efficient DB queries (e.g., where LOWER(name) LIKE ?), generate docstrings for low-coverage areas (31%), consolidate routes for simplefin_items, move view logic to helpers (e.g., format_transaction_extra), strengthen tests with exact assertions/fixtures for dedup/relink failures. Also, check for overlaps with merged #262 (merchants fix): Ensure merchant creation in simplefin_entry/processor.rb aligns with new payee-based flow and MD5 IDs; add tests for edge cases like empty payees or over-merging pendings. Prioritize security (PII redaction in logs, no hardcoded secrets). * SimpleFin: address CodeRabbit comments (batch 1) - Consolidate simplefin_items routes under a single resources block; keep URLs stable - Replace inline JS with Stimulus auto-relink controller; auto-load relink modal via global modal frame - Improve a11y in relink modal by wrapping rows in labels - Harden unlink rake: default dry_run=true, UUID validation, redact PII in outputs, clearer errors - Backfill rake: default dry_run=true, UUID validation; groundwork for per-SFA counters - Fix-was-merged rake: default dry_run=true, UUID validation; clearer outputs - Idempotent transfer auto-match (find_or_create_by! + RecordNotUnique rescue) - Extract SimpleFin error tooltip assembly into helper and use it in view RuboCop: maintain 2-space indentation, spaces inside array brackets, spaces after commas, and no redundant returns * Linter noise * removed filed commited by mistake. * manual relink flow and tighten composite matching * enforce manual relink UI; fix adapter keywords; guarantee extra.simplefin hash * refactor(simplefin): extract relink service; enforce manual relink UI; tighten composite match; migration 7.2 * add provider date parser; refactor rake; move view queries; partial resilience * run balances-only import in background job. make update flow enqueue balances-only job * persists across all update redirects and initialize used_manual_ids to prevent NameError in relink candidate computation. * SimpleFin: metadata + merge fixes; holdings (incl. crypto) + Day Change; Sync Summary; ops rakes; lint * Fixed failed test after rebase. * scan_ruby fix * Calming the rabbit: Fix AccountProvider linking when accounts change Drop the legacy unique index instead of duplicating it Fix dynamic constant assignment Use fixtures consistently; avoid rescue for control flow. Replace bare rescue with explicit exception class. Move business logic out of the view. Critical: Transaction boundary excludes recompute phase, risking data loss. Inconsistency between documentation and implementation for zero-error case. Refactor to use the compute_unlinked_count helper for consistency. Fix cleanup task default: it deletes by default. Move sync stats computation to controller to avoid N+1 queries. Consolidate duplicate sync query. Clarify the intent of setting flash notice on the error path. Fix Date/Time comparison in should_be_inactive?. Move stats retrieval logic to controller. Remove duplicate Sync summary section. Remove the unnecessary sleep statement; use Capybara's built-in waiting. Add label wrappers for accessibility and consistency. * FIX SimpleFIN new account modal Now new account properly loads as a Modal, instead of new page. Fixes also form showing dashboard instead of settings page. * Remove SimpleFin legacy UI components, migrate schema, and refine linking behavior. # Conflicts: # app/helpers/settings_helper.rb * Extract SimpleFin-related logic to `prepare_show_context` helper and refactor for consistency. Adjust conditional checks and ensure controller variables are properly initialized. * Remove unused SimpleFin maps from prepare_show_context; select IDs to avoid N+1 Replace Tailwind bg-green-500 with semantic bg-success in _simplefin_panel/_provider_form Add f.label :setup_token in simplefin_items/new for a11y Remove duplicate require in AccountsControllerSimplefinCtaTest * Remove unnecessary blank lines * Reduce unnecessary changes This reduces the diff against main * Simplefin Account Setup: Display in modal This fixes an issue with the `X` dismiss button in the top right corner * Removed unnecessary comment. * removed unnecessary function. * fixed broken links * Removed unnecessary file * changed to database query * set to use UTC and gaurd against null * set dry_run=true * Fixed comment * Changed to use a database-level query * matched test name to test behavior. * Eliminate code duplication and Time.zone dependency * make final summary surface failures * lint fix * Revised timezone comment. better handle missing selectors. * sanitized LIKE wildcards * Fixed SimpleFin import to avoid “Currency can’t be blank” validation failures when providers return an empty currency string. * Added helper methods for admin and self-hosted checks * Specify exception types in rescue clauses. * Refined logic to determine transaction dates for credit accounts. * Refined stats calculation for `total_accounts` to track the maximum unique accounts per run instead of accumulating totals. * Moved `unlink_all!` logic to `SimplefinItem::Unlinking` concern and deprecated `SimplefinItem::Unlinker`. Updated related references. * Refined legacy unlinking logic, improved `current_holdings` formatting, and added ENV-based overrides for self-hosted checks. * Enhanced `unlink_all!` with explicit error handling, improved transaction safety, and refined ENV-based self-hosted checks. Adjusted exception types and cleaned up private method handling. * Improved currency assignment logic by adding fallback to `current_account` and `family` currencies. * Enhanced error tracking during SimpleFin account imports by adding categorized error buckets, limiting stored errors to the last 5, and improving `stats` calculations. * typo fix * Didn't realize rabbit was still mad... Refactored SimpleFin error handling and CTA logic: centralized duplicate detection and relink visibility into controller, improved task counters, adjusted redirect notices, and fixed form indexing. * Dang rabbit never stops... Centralized SimpleFin maps logic into `MapsHelper` concern and integrated it into relevant controllers and rake tasks. Optimized queries, reduced redundancy, and improved unlinked counts and manual account checks with batch processing. Adjusted task arguments for clarity. * Persistent rabbit. Optimized SimpleFin maps logic by implementing batch queries for manual account and unlinked count checks, reducing N+1 issues. Improved clarity of rake task argument descriptions and error messages for better usability. * Lost a commit somehow, resolved here. Refactored transaction extra details logic by introducing `build_transaction_extra_details` helper to improve clarity, reusability, and reduce view complexity. Enhanced rake tasks with strict dry-run validation and better error handling. Updated schema to allow nullable `merchant_id` and added conditional unique indexes for recurring transactions. * Refactored sensitive data redaction in `simplefin_unlink` task for recursive handling, optimized SQL sanitization in `simplefin_holdings_backfill`, improved error handling in `transactions_helper`, and streamlined day change calculation logic in `Holding` model. * Lint fix * Removed per PR comments. * Also removing per PR comment. * git commit -m "SimpleFIN polish: preserve #manual-accounts wrapper, unify \"manual\" scope, and correct unlinked counts - Preserve #manual-accounts wrapper: switch non-empty updates to turbo_stream.update and background broadcast_update_to; keep empty-path replace to render <div id=\"manual-accounts\"></div> - Unify definition of manual accounts via Account.visible_manual (visible + legacy-nil + no AccountProvider); reuse in controllers, jobs, and helper - Correct setup/unlinked counts: SimplefinItem::Syncer#finalize_setup_counts and maps now consider AccountProvider links (legacy account AND provider must be absent) Deleted: - app/models/simplefin_item/relink_service.rb - app/controllers/concerns/simplefin_items/relink_helpers.rb - app/javascript/controllers/auto_relink_controller.js - app/views/simplefin_items/_relink_modal.html.erb - app/views/simplefin_items/manual_relink.html.erb - app/views/simplefin_items/relink.html.erb - test/services/simplefin_item/relink_service_test.rb Refs: PR #318 unified link/unlink; PR #267 SimpleFIN; follow-up to fix wrapper ID loss and counting drift." * Extend unlinked account check to include "Investment" type * set SimpleFIN item for `balances`, remove redundant unpacking, and improve holdings task error * SimpleFIN: add `errors` action + modal; do not reintroduce legacy relink actions; removed dead helper * FIX simpleFIN linking * Add delay back, tests benefit from it * Put cache back in * Remove empty `rake` task * Small spelling fixes. --------- Signed-off-by: soky srm <sokysrm@gmail.com> Co-authored-by: Josh Waldrep <joshua.waldrep5+github@gmail.com> Co-authored-by: Juan José Mata <juanjo.mata@gmail.com> Co-authored-by: sokie <sokysrm@gmail.com> Co-authored-by: Dylan Corrales <deathcamel58@gmail.com>
327 lines
13 KiB
Ruby
327 lines
13 KiB
Ruby
require "test_helper"
|
|
|
|
class SimplefinItemsControllerTest < ActionDispatch::IntegrationTest
|
|
fixtures :users, :families
|
|
setup do
|
|
sign_in users(:family_admin)
|
|
@family = families(:dylan_family)
|
|
@simplefin_item = SimplefinItem.create!(
|
|
family: @family,
|
|
name: "Test Connection",
|
|
access_url: "https://example.com/test_access"
|
|
)
|
|
end
|
|
|
|
|
|
test "should destroy simplefin item" do
|
|
assert_difference("SimplefinItem.count", 0) do # doesn't actually delete immediately
|
|
delete simplefin_item_url(@simplefin_item)
|
|
end
|
|
|
|
assert_redirected_to accounts_path
|
|
@simplefin_item.reload
|
|
assert @simplefin_item.scheduled_for_deletion?
|
|
end
|
|
|
|
test "should sync simplefin item" do
|
|
post sync_simplefin_item_url(@simplefin_item)
|
|
assert_redirected_to accounts_path
|
|
end
|
|
|
|
test "should get edit" do
|
|
@simplefin_item.update!(status: :requires_update)
|
|
get edit_simplefin_item_url(@simplefin_item)
|
|
assert_response :success
|
|
end
|
|
|
|
test "should update simplefin item with valid token" do
|
|
@simplefin_item.update!(status: :requires_update)
|
|
|
|
# Mock the SimpleFin provider to prevent real API calls
|
|
mock_provider = mock()
|
|
mock_provider.expects(:claim_access_url).with("valid_token").returns("https://example.com/new_access")
|
|
mock_provider.expects(:get_accounts).returns({ accounts: [] }).at_least_once
|
|
Provider::Simplefin.expects(:new).returns(mock_provider).at_least_once
|
|
|
|
# Let the real create_simplefin_item! method run - don't mock it
|
|
|
|
patch simplefin_item_url(@simplefin_item), params: {
|
|
simplefin_item: { setup_token: "valid_token" }
|
|
}
|
|
|
|
assert_redirected_to accounts_path
|
|
assert_equal "SimpleFin connection updated.", flash[:notice]
|
|
@simplefin_item.reload
|
|
assert @simplefin_item.scheduled_for_deletion?
|
|
end
|
|
|
|
test "should handle update with invalid token" do
|
|
@simplefin_item.update!(status: :requires_update)
|
|
|
|
patch simplefin_item_url(@simplefin_item), params: {
|
|
simplefin_item: { setup_token: "" }
|
|
}
|
|
|
|
assert_response :unprocessable_entity
|
|
assert_includes response.body, I18n.t("simplefin_items.update.errors.blank_token", default: "Please enter a SimpleFin setup token")
|
|
end
|
|
|
|
test "should transfer accounts when updating simplefin item token" do
|
|
@simplefin_item.update!(status: :requires_update)
|
|
|
|
# Create old SimpleFin accounts linked to Maybe accounts
|
|
old_simplefin_account1 = @simplefin_item.simplefin_accounts.create!(
|
|
name: "Test Checking",
|
|
account_id: "sf_account_123",
|
|
currency: "USD",
|
|
current_balance: 1000,
|
|
account_type: "depository"
|
|
)
|
|
old_simplefin_account2 = @simplefin_item.simplefin_accounts.create!(
|
|
name: "Test Savings",
|
|
account_id: "sf_account_456",
|
|
currency: "USD",
|
|
current_balance: 5000,
|
|
account_type: "depository"
|
|
)
|
|
|
|
# Create Maybe accounts linked to the SimpleFin accounts
|
|
maybe_account1 = Account.create!(
|
|
family: @family,
|
|
name: "Checking Account",
|
|
balance: 1000,
|
|
currency: "USD",
|
|
accountable_type: "Depository",
|
|
accountable: Depository.create!(subtype: "checking"),
|
|
simplefin_account_id: old_simplefin_account1.id
|
|
)
|
|
maybe_account2 = Account.create!(
|
|
family: @family,
|
|
name: "Savings Account",
|
|
balance: 5000,
|
|
currency: "USD",
|
|
accountable_type: "Depository",
|
|
accountable: Depository.create!(subtype: "savings"),
|
|
simplefin_account_id: old_simplefin_account2.id
|
|
)
|
|
|
|
# Update old SimpleFin accounts to reference the Maybe accounts
|
|
old_simplefin_account1.update!(account: maybe_account1)
|
|
old_simplefin_account2.update!(account: maybe_account2)
|
|
|
|
# Mock only the external API calls, let business logic run
|
|
mock_provider = mock()
|
|
mock_provider.expects(:claim_access_url).with("valid_token").returns("https://example.com/new_access")
|
|
mock_provider.expects(:get_accounts).returns({
|
|
accounts: [
|
|
{
|
|
id: "sf_account_123",
|
|
name: "Test Checking",
|
|
type: "depository",
|
|
currency: "USD",
|
|
balance: 1000,
|
|
transactions: []
|
|
},
|
|
{
|
|
id: "sf_account_456",
|
|
name: "Test Savings",
|
|
type: "depository",
|
|
currency: "USD",
|
|
balance: 5000,
|
|
transactions: []
|
|
}
|
|
]
|
|
}).at_least_once
|
|
Provider::Simplefin.expects(:new).returns(mock_provider).at_least_once
|
|
|
|
# Perform the update
|
|
patch simplefin_item_url(@simplefin_item), params: {
|
|
simplefin_item: { setup_token: "valid_token" }
|
|
}
|
|
|
|
assert_redirected_to accounts_path
|
|
assert_equal "SimpleFin connection updated.", flash[:notice]
|
|
|
|
# Verify accounts were transferred to new SimpleFin accounts
|
|
assert Account.exists?(maybe_account1.id), "maybe_account1 should still exist"
|
|
assert Account.exists?(maybe_account2.id), "maybe_account2 should still exist"
|
|
|
|
maybe_account1.reload
|
|
maybe_account2.reload
|
|
|
|
# Find the new SimpleFin item that was created
|
|
new_simplefin_item = @family.simplefin_items.where.not(id: @simplefin_item.id).first
|
|
assert_not_nil new_simplefin_item, "New SimpleFin item should have been created"
|
|
|
|
new_sf_account1 = new_simplefin_item.simplefin_accounts.find_by(account_id: "sf_account_123")
|
|
new_sf_account2 = new_simplefin_item.simplefin_accounts.find_by(account_id: "sf_account_456")
|
|
|
|
assert_not_nil new_sf_account1, "New SimpleFin account with ID sf_account_123 should exist"
|
|
assert_not_nil new_sf_account2, "New SimpleFin account with ID sf_account_456 should exist"
|
|
|
|
assert_equal new_sf_account1.id, maybe_account1.simplefin_account_id
|
|
assert_equal new_sf_account2.id, maybe_account2.simplefin_account_id
|
|
|
|
# Verify old SimpleFin accounts no longer reference Maybe accounts
|
|
old_simplefin_account1.reload
|
|
old_simplefin_account2.reload
|
|
assert_nil old_simplefin_account1.current_account
|
|
assert_nil old_simplefin_account2.current_account
|
|
|
|
# Verify old SimpleFin item is scheduled for deletion
|
|
@simplefin_item.reload
|
|
assert @simplefin_item.scheduled_for_deletion?
|
|
end
|
|
|
|
test "should handle partial account matching during token update" do
|
|
@simplefin_item.update!(status: :requires_update)
|
|
|
|
# Create old SimpleFin account
|
|
old_simplefin_account = @simplefin_item.simplefin_accounts.create!(
|
|
name: "Test Checking",
|
|
account_id: "sf_account_123",
|
|
currency: "USD",
|
|
current_balance: 1000,
|
|
account_type: "depository"
|
|
)
|
|
|
|
# Create Maybe account linked to the SimpleFin account
|
|
maybe_account = Account.create!(
|
|
family: @family,
|
|
name: "Checking Account",
|
|
balance: 1000,
|
|
currency: "USD",
|
|
accountable_type: "Depository",
|
|
accountable: Depository.create!(subtype: "checking"),
|
|
simplefin_account_id: old_simplefin_account.id
|
|
)
|
|
old_simplefin_account.update!(account: maybe_account)
|
|
|
|
# Mock only the external API calls, let business logic run
|
|
mock_provider = mock()
|
|
mock_provider.expects(:claim_access_url).with("valid_token").returns("https://example.com/new_access")
|
|
# Return empty accounts list to simulate account was removed from bank
|
|
mock_provider.expects(:get_accounts).returns({ accounts: [] }).at_least_once
|
|
Provider::Simplefin.expects(:new).returns(mock_provider).at_least_once
|
|
|
|
# Perform update
|
|
patch simplefin_item_url(@simplefin_item), params: {
|
|
simplefin_item: { setup_token: "valid_token" }
|
|
}
|
|
|
|
assert_response :redirect
|
|
uri2 = URI(response.redirect_url)
|
|
assert_equal "/accounts", uri2.path
|
|
|
|
# Verify Maybe account still linked to old SimpleFin account (no transfer occurred)
|
|
maybe_account.reload
|
|
old_simplefin_account.reload
|
|
assert_equal old_simplefin_account.id, maybe_account.simplefin_account_id
|
|
assert_equal maybe_account, old_simplefin_account.current_account
|
|
|
|
# Old item still scheduled for deletion
|
|
@simplefin_item.reload
|
|
assert @simplefin_item.scheduled_for_deletion?
|
|
end
|
|
|
|
test "select_existing_account renders empty-state modal when no available simplefin accounts" do
|
|
account = accounts(:depository)
|
|
|
|
get select_existing_account_simplefin_items_url(account_id: account.id)
|
|
assert_response :success
|
|
assert_includes @response.body, "All SimpleFIN accounts appear to be linked already."
|
|
end
|
|
test "destroy should unlink provider links and legacy fk" do
|
|
# Create SFA and linked Account with AccountProvider
|
|
sfa = @simplefin_item.simplefin_accounts.create!(name: "Linked", account_id: "sf_link_1", currency: "USD", current_balance: 1, account_type: "depository")
|
|
acct = Account.create!(family: @family, name: "Manual A", currency: "USD", balance: 0, accountable_type: "Depository", accountable: Depository.create!(subtype: "checking"), simplefin_account_id: sfa.id)
|
|
AccountProvider.create!(account: acct, provider_type: "SimplefinAccount", provider_id: sfa.id)
|
|
|
|
delete simplefin_item_url(@simplefin_item)
|
|
assert_redirected_to accounts_path
|
|
|
|
# Links are removed immediately even though deletion is scheduled
|
|
assert_nil acct.reload.simplefin_account_id
|
|
assert_equal 0, AccountProvider.where(provider_type: "SimplefinAccount", provider_id: sfa.id).count
|
|
end
|
|
|
|
|
|
test "complete_account_setup creates accounts only for truly unlinked SFAs" do
|
|
# Linked SFA (should be ignored by setup)
|
|
linked_sfa = @simplefin_item.simplefin_accounts.create!(name: "Linked", account_id: "sf_l_1", currency: "USD", current_balance: 5, account_type: "depository")
|
|
linked_acct = Account.create!(family: @family, name: "Already Linked", currency: "USD", balance: 0, accountable_type: "Depository", accountable: Depository.create!(subtype: "savings"))
|
|
linked_sfa.update!(account: linked_acct)
|
|
|
|
# Unlinked SFA (should be created via setup)
|
|
unlinked_sfa = @simplefin_item.simplefin_accounts.create!(name: "New CC", account_id: "sf_cc_1", currency: "USD", current_balance: -20, account_type: "credit")
|
|
|
|
post complete_account_setup_simplefin_item_url(@simplefin_item), params: {
|
|
account_types: { unlinked_sfa.id => "CreditCard" },
|
|
account_subtypes: { unlinked_sfa.id => "credit_card" },
|
|
sync_start_date: Date.today.to_s
|
|
}
|
|
|
|
assert_redirected_to accounts_path
|
|
assert_not @simplefin_item.reload.pending_account_setup
|
|
|
|
# Linked one unchanged, unlinked now has an account
|
|
linked_sfa.reload
|
|
unlinked_sfa.reload
|
|
# The previously linked SFA should still point to the same Maybe account via legacy FK or provider link
|
|
assert_equal linked_acct.id, linked_sfa.account&.id
|
|
# The newly created account for the unlinked SFA should now exist
|
|
assert_not_nil unlinked_sfa.account_id
|
|
end
|
|
test "update redirects to accounts after setup without forcing a modal" do
|
|
@simplefin_item.update!(status: :requires_update)
|
|
|
|
# Mock provider to return one account so updated_item creates SFAs
|
|
mock_provider = mock()
|
|
mock_provider.expects(:claim_access_url).with("valid_token").returns("https://example.com/new_access")
|
|
mock_provider.expects(:get_accounts).returns({
|
|
accounts: [
|
|
{ id: "sf_auto_open_1", name: "Auto Open Checking", type: "depository", currency: "USD", balance: 100, transactions: [] }
|
|
]
|
|
}).at_least_once
|
|
Provider::Simplefin.expects(:new).returns(mock_provider).at_least_once
|
|
|
|
patch simplefin_item_url(@simplefin_item), params: { simplefin_item: { setup_token: "valid_token" } }
|
|
|
|
assert_response :redirect
|
|
uri = URI(response.redirect_url)
|
|
assert_equal "/accounts", uri.path
|
|
end
|
|
|
|
test "create does not auto-open when no candidates or unlinked" do
|
|
# Mock provider interactions for item creation (no immediate account import on create)
|
|
mock_provider = mock()
|
|
mock_provider.expects(:claim_access_url).with("valid_token").returns("https://example.com/new_access")
|
|
Provider::Simplefin.expects(:new).returns(mock_provider).at_least_once
|
|
|
|
post simplefin_items_url, params: { simplefin_item: { setup_token: "valid_token" } }
|
|
|
|
assert_response :redirect
|
|
uri = URI(response.redirect_url)
|
|
assert_equal "/accounts", uri.path
|
|
q = Rack::Utils.parse_nested_query(uri.query)
|
|
assert !q.key?("open_relink_for"), "did not expect auto-open when nothing actionable"
|
|
end
|
|
|
|
test "update does not auto-open when no SFAs present" do
|
|
@simplefin_item.update!(status: :requires_update)
|
|
|
|
mock_provider = mock()
|
|
mock_provider.expects(:claim_access_url).with("valid_token").returns("https://example.com/new_access")
|
|
mock_provider.expects(:get_accounts).returns({ accounts: [] }).at_least_once
|
|
Provider::Simplefin.expects(:new).returns(mock_provider).at_least_once
|
|
|
|
patch simplefin_item_url(@simplefin_item), params: { simplefin_item: { setup_token: "valid_token" } }
|
|
|
|
assert_response :redirect
|
|
uri = URI(response.redirect_url)
|
|
assert_equal "/accounts", uri.path
|
|
q = Rack::Utils.parse_nested_query(uri.query)
|
|
assert !q.key?("open_relink_for"), "did not expect auto-open when update produced no SFAs/candidates"
|
|
end
|
|
end
|