Add "Link to existing" option in SnapTrade Setup Accounts modal (#935)

* Add account linking functionality for SnapTrade items

- Introduced UI to link existing accounts when setting up SnapTrade items, preventing duplicate account creation.
- Updated controller to fetch linkable accounts.
- Added tests to verify proper filtering of accounts and linking behavior.

* Add `snaptrade_item_id` to account linking flow for SnapTrade items

- Updated controller to allow specifying `snaptrade_item_id` when linking accounts.
- Adjusted form and views to include `snaptrade_item_id` as a hidden field.
- Enhanced tests to validate behavior with the new parameter.
This commit is contained in:
LPW
2026-02-08 04:30:46 -05:00
committed by GitHub
parent 01c2209492
commit 24981ffd52
4 changed files with 137 additions and 29 deletions

View File

@@ -38,4 +38,69 @@ class SnaptradeItemsControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to portal_url
end
test "setup_accounts shows linkable investment and crypto accounts in dropdown" do
get setup_accounts_snaptrade_item_url(@snaptrade_item)
assert_response :success
# Investment and crypto accounts (no provider) should appear in the link dropdown
assert_match accounts(:investment).name, response.body
assert_match accounts(:crypto).name, response.body
# Depository should NOT appear in the link dropdown (wrong type)
# The depository name may appear elsewhere on the page, so check the select options specifically
refute_match(/option.*#{accounts(:depository).name}/, response.body)
end
test "setup_accounts excludes accounts that already have a provider from dropdown" do
# Link the investment account to a snaptrade_account
AccountProvider.create!(
account: accounts(:investment),
provider: snaptrade_accounts(:fidelity_401k)
)
get setup_accounts_snaptrade_item_url(@snaptrade_item)
assert_response :success
# Investment account is now linked → should NOT appear in link dropdown options
refute_match(/option.*#{accounts(:investment).name}/, response.body)
# Crypto still unlinked → should appear
assert_match accounts(:crypto).name, response.body
end
test "link_existing_account links account to snaptrade_account" do
account = accounts(:investment)
snaptrade_account = snaptrade_accounts(:fidelity_401k)
assert_difference "AccountProvider.count", 1 do
post link_existing_account_snaptrade_items_url, params: {
account_id: account.id,
snaptrade_account_id: snaptrade_account.id,
snaptrade_item_id: @snaptrade_item.id
}
end
assert_redirected_to account_path(account)
assert_match(/Successfully linked/, flash[:notice])
snaptrade_account.reload
assert_equal account, snaptrade_account.current_account
end
test "link_existing_account handles missing account gracefully" do
snaptrade_account = snaptrade_accounts(:fidelity_401k)
assert_no_difference "AccountProvider.count" do
post link_existing_account_snaptrade_items_url, params: {
account_id: "nonexistent",
snaptrade_account_id: snaptrade_account.id,
snaptrade_item_id: @snaptrade_item.id
}
end
assert_redirected_to settings_providers_path
assert_match(/not found/i, flash[:alert])
end
end