Add tests for SnapTrade error handling and refine unlink behavior (#931)

- Introduced new tests to cover SnapTrade decryption and connection errors in `SnaptradeItemsControllerTest`.
- Updated error messages for improved user clarity.
- Modified `unlink` functionality to preserve `SnaptradeAccount` records while ensuring proper detachment of associated holdings.
This commit is contained in:
LPW
2026-02-07 17:15:18 -05:00
committed by GitHub
parent 36661bdc9b
commit 01c2209492
6 changed files with 105 additions and 34 deletions

View File

@@ -165,6 +165,51 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to account_url(@account)
assert_equal "Account is already linked to a provider", flash[:alert]
end
test "unlink preserves SnaptradeAccount record" do
snaptrade_account = snaptrade_accounts(:fidelity_401k)
investment = accounts(:investment)
AccountProvider.create!(account: investment, provider: snaptrade_account)
investment.reload
assert investment.linked?
delete unlink_account_url(investment)
investment.reload
assert_not investment.linked?
assert_redirected_to accounts_path
# SnaptradeAccount should still exist (not destroyed)
assert SnaptradeAccount.exists?(snaptrade_account.id), "SnaptradeAccount should be preserved after unlink"
# But AccountProvider should be gone
assert_not AccountProvider.exists?(provider_type: "SnaptradeAccount", provider_id: snaptrade_account.id)
end
test "unlink does not enqueue SnapTrade cleanup job" do
snaptrade_account = snaptrade_accounts(:fidelity_401k)
investment = accounts(:investment)
AccountProvider.create!(account: investment, provider: snaptrade_account)
investment.reload
assert_no_enqueued_jobs(only: SnaptradeConnectionCleanupJob) do
delete unlink_account_url(investment)
end
end
test "unlink detaches holdings from SnapTrade provider" do
snaptrade_account = snaptrade_accounts(:fidelity_401k)
investment = accounts(:investment)
ap = AccountProvider.create!(account: investment, provider: snaptrade_account)
# Assign a holding to this provider
holding = holdings(:one)
holding.update!(account_provider: ap)
delete unlink_account_url(investment)
holding.reload
assert_nil holding.account_provider_id, "Holding should be detached from provider after unlink"
end
end
class AccountsControllerSimplefinCtaTest < ActionDispatch::IntegrationTest

View File

@@ -0,0 +1,41 @@
require "test_helper"
class SnaptradeItemsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
@snaptrade_item = snaptrade_items(:configured_item)
end
test "connect handles decryption error gracefully" do
SnaptradeItem.any_instance
.stubs(:user_registered?)
.raises(ActiveRecord::Encryption::Errors::Decryption.new("cannot decrypt"))
get connect_snaptrade_item_url(@snaptrade_item)
assert_redirected_to settings_providers_path
assert_match(/Unable to read SnapTrade credentials/, flash[:alert])
end
test "connect handles general error gracefully" do
SnaptradeItem.any_instance
.stubs(:user_registered?)
.raises(StandardError.new("something broke"))
get connect_snaptrade_item_url(@snaptrade_item)
assert_redirected_to settings_providers_path
assert_match(/Failed to connect/, flash[:alert])
end
test "connect redirects to portal when successful" do
portal_url = "https://app.snaptrade.com/portal/test123"
SnaptradeItem.any_instance.stubs(:user_registered?).returns(true)
SnaptradeItem.any_instance.stubs(:connection_portal_url).returns(portal_url)
get connect_snaptrade_item_url(@snaptrade_item)
assert_redirected_to portal_url
end
end